งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Trees Hierarchical Structures. Outlines Definition of trees Modeling by trees Properties of trees Applications – Binary search trees – Prefix codes –

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Trees Hierarchical Structures. Outlines Definition of trees Modeling by trees Properties of trees Applications – Binary search trees – Prefix codes –"— ใบสำเนางานนำเสนอ:

1 Trees Hierarchical Structures

2 Outlines Definition of trees Modeling by trees Properties of trees Applications – Binary search trees – Prefix codes – Game trees Tree traversal Spanning trees 23012332TREES

3 DEFINITIONS 2301233TREES3

4 Definition A tree is a connected undirected graph with no simple circuits. 2301233TREES4 Trees Not trees

5 THEOREM An undirected graph is a tree iff there is a unique simple path between any two of its vertices. Proof: There are two parts of the proof. (I)If there is a unique simple path between any two of its vertices in a graph, then the graph is a tree. (II)If an undirected graph G is a tree then there is a unique simple path between any two of its vertices in G. (Part I) Let G be a graph, and there is a unique simple path between any two vertices. Because there is a path between any two vertices, G is a connected graph. Because there is a unique path between any two vertices, there is no circuits in G. Thus, G is a tree. 2301233TREES5

6 THEOREM (Part II) If an undirected graph G is a tree then there is a unique simple path between any two of its vertices in G. Let T be a tree. Then, there is no simple circuits in T. Let x and y be two vertices in T. T is a connected graph; there is a simple path P from x to y. Assume there is another simple path Q between x and y. Then, there is a cycle in T created from path P and path Q. But there is no circuit in a tree, which leads to a contradiction. Then, our assumption is not true. It means there is no other path between x and y. That is, there is only one unique path between x and y. 2301233TREES6

7 Rooted Trees A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. 2301233TREES7

8 T Terminologies If v is a vertex other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v. If u is a parent of v, then v is a child of u. If u and v have the same parent, then u and v are siblings. If v is a vertex other than the root, the ancestor of v is a vertex u on the path from the root to v. If u is a ancestor of v, then v is a descendant of u. A vertex in a tree is called a leaf if it has no children. A vertex is a tree is called an internal vertex if it has at least one child. 2301233TREES8 r u v v u u v

9 m-ary Trees A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m=2 is called a binary tree. 2301233TREES9 … … … … … … … …

10 MODELING BY TREES

11 Organization Chart Chulalongkorn University ScienceBiologyBotonyZoologyChemistry Material Science CeramicsPolymersMathematics Computer Science PhysicsEngineering Industrial Engineering Computer Engineering Electrical Engineering Mechanical Engineering 2301233TREES11

12 File System rootusersFacultyNitiThitiPitiStudentVanaVipaVivatsystemOSDriversMonitorKeyboardMouse 2301233TREES12

13 PROPERTIES OF TREES 2301233TREES13

14 THEOREM A tree with n vertices has n -1 edges. Proof: We will prove this theorem by induction on the number of vertices in a tree. Let T be a tree with n vertices. Basis: ( n =1) T is a tree with only one vertex. Assume that there is at least one edge in this tree. Then, that edge must connect the only vertex, and create a circuit. Thus, it is not a tree, which is a contradiction. Therefore, there is no edge in T. Induction hypothesis: A tree with n vertices has n -1 edges. 2301233TREES14

15 THEOREM Induction step: We can construct a tree T’ with n +1 vertices from a tree T with n vertices by adding another vertex v in T. In T’, we can add an edge e from: (1)v to v, (2)one vertex x to another vertex y in T, and (3) v to any vertex in T. 2301233TREES15 v T x y

16 THEOREM In case (1), it creates a cycle at v, and T’ cannot be a tree. Thus, this type of edge cannot be added in T’. In case (2), there is a path between x and y (since T is a tree) and an edge between x and y. Thus, a circuit is created and this type of edge cannot be added in T ’. In case (3), if an edge is added between v and another vertex y in T, there is a path from v to y, which has a path to each of the other vertices in T. Thus, there are paths from v to any vertex in T. Thus, T’ is a tree. If another edge is added in T’, it created a cycle and it is no longer a tree. Therefore, only one edge can be added to created a tree. Then, there are n edges in T’. 2301233TREES16 v T x y

17 THEOREM A full m -ary tree with i internal vertices contains n = mi +1 vertices. Proof: For a full m -ary tree, each vertex has m children. If there are i internal vertices, then there are mi children in this tree. But we also need to consider the root which is not a child of any vertex. Thus, the total number of vertices is mi +1. 2301233TREES17

18 THEOREM A full m -ary tree with n vertices has i = ( n -1)/ m internal vertices and l = [( m -1) n +1]/ m leaves, Proof: Let n be the total number of vertices, i be the number of internal vertices, and l be the number of leaves, and n = i + l. From the previous theorem, n = mi + 1. Thus, i = ( n - 1)/ m. Also, n = mi + 1 = i + l. Then, l = i(m -1) + 1 = = = =. 2301233TREES18

19 THEOREM A full m -ary tree with i internal vertices has n = mi +1 vertices and l = ( m -1) i + 1 leaves, Proof: Let n be the total number of vertices, i be the number of internal vertices, and l be the number of leaves, and n = mi + 1. Since n = i + l, mi + 1 = i + l. Then, l = mi – i + 1 = i(m -1) + 1. 2301233TREES19

20 THEOREM A full m -ary tree with l leaves has n = ( ml -1)/( m -1) vertices and i =( l -1)/( m -1) internal vertices. Proof: Let n be the total number of vertices, i be the number of internal vertices, and l be the number of leaves. From the previous theorem, n = mi + 1. (I) Since n = i + l, i = n - l. (II) Substitute (II) in (I), n = m(n-l) + 1. Then, n = mn - ml + 1 and mn - n = ml - 1 That is, n(m – 1) = ml - 1, and n = ( ml -1)/( m -1). (III) Substitute (III) in (II), i = ( ml -1)/( m -1) - l. Then, i = ( ml -1- ml + l )/( m -1), and i = ( l -1)/( m -1). 2301233TREES20

21 THEOREM ต้นไม้ m-ary ที่มีความสูง h มีใบไม่เกิน m h ใบ Proof: Basis: h =1. ต้นไม้ m-ary ที่มีความสูง 1 มีใบไม่เกิน m ใบ ( ดังแสดงใน รูป ) Induction hypothesis: ต้นไม้ m-ary ที่มีความสูง h มีใบไม่ เกิน m h ใบ Induction step: จะแสดงว่าต้นไม้ m-ary ที่มีความสูง h+1 มีใบไม่เกิน m h+1 ใบ สร้างต้นไม้สูง h+1 จากต้นไม้ที่สูง h โดยให้ใบของต้นไม้ ที่สูง h ทุกใบมีลูก จาก induction hypothesis, ใบของต้นไม้ที่สูง h มีไม่เกิน m h ใบ เนื่องจากต้นไม้นี้เป็นต้นไม้ m-ary แต่ละโหนดมีลูกไม่เกิน m ดังนั้นต้นไม้ที่สร้างขึ้นนี้มีใบ ไม่เกิน m  m h = m h+1 ใบ 2301233TREES21 …

22 COROLLARY ถ้าต้นไม้ m-ary สูง h มีใบ l ใบแล้ว h   log m l  Proof: จากทฤษฎีที่แล้ว l  m h. ดังนั้น h  log m l เนื่องจาก h เป็นจำนวนเต็ม h   log m l  2301233TREES22

23 COROLLARY ถ้าต้นไม้ full m-ary สูง h มีใบ l ใบและเป็นต้นไม้ที่ balanced แล้ว h =  log m l  Proof: ให้ T เป็นต้นไม้ full m-ary ที่ balanced และสูง h ใบของ T อยู่ที่ระดับ h หรือ h-1 และต้องมีใบที่ระดับ h ดังนั้นต้องมีใบมากกว่า m h-1 ใบ นั่นคือ m h-1 < l  m h และ h-1 < log m l  h ดังนั้น h =  log m l  2301233TREES23

24 APPLICATIONS 2301233TREES24

25 Binary Search Tree: Dictionary melodyhierarchydecisionabilityjeopardyidentitylingerkingdommakerparticleobligeneglecttangiblevehiclewheel 2301233TREES25

26 Prefix Codes 2301233TREES26 e a s t n r c i mth d 01 01 01 0 1 01 01 01 01 0 1 01 e a s t n i t d r 01 01 01 0 1 01 01 0 1 01 esta 0 1 0 1 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 dea

27 Game Trees 2301233TREES27 x xx xoxox o x o x o x o x x o x xx o xx o

28 TREE TRAVERSAL 2301233TREES28

29 Traversal Algorithms Inorder traversalPreorder traversalPostorder traversal 2301233TREES29

30 Inorder Traversal Let T be an ordered rooted tree with root r. If T consists of r only, then r is the inorder traversal of T. Otherwise, the inorder traversal of T is composed of the inorder traversal of T 1, r, the inorder traversal of T 2, …, and the inorder traversal of T n, when T 1, T 2, …, and T n are subtrees at r from left to right. 2301233TREES30 a c fed b acfedb

31 Inorder Traversal Algorithm procedure inorder(T: tree) r := root of T if r is a leaf then list r else begin lchild := first child of r inorder(subtree with lchild as its root) list r for each child c of r from left to right, except lchild inorder(subtree with c as its root) end 2301233TREES31

32 Preorder Traversal Let T be an ordered rooted tree with root r. If T consists of r only, then r is the preorder traversal of T. Otherwise, the preorder traversal of T is composed of r, the preorder traversal of T 1, the preorder traversal of T 2, …, and the preorder traversal of T n, when T 1, T 2, …, and T n are subtrees at r from left to right. 2301233TREES32 a c fed b acfedb

33 Preorder Traversal Algorithm procedure preorder(T: tree) r := root of T list r for each child c of r from left to right begin preorder(subtree with c as its root) end 2301233TREES33

34 Postorder Traversal Let T be an ordered rooted tree with root r. If T consists of r only, then r is the postorder traversal of T. Otherwise, the preorder traversal of T is composed of the postorder traversal of T 1, the postorder traversal of T 2, …, the postrorder traversal of T n, and r, when T 1, T 2, …, and T n are subtrees at r from left to right. 2301233TREES34 a c fed b acfedb

35 Postorder Traversal Algorithm procedure postorder(T: tree) r := root of T if r is a leaf then list r else begin for each child c of r from left to right postorder(subtree with c as its root) list r end 2301233TREES35

36 a Depth-first Search 2301233TREES36 a b cd klm rts efg opq hij procedure DFS(T: tree) r := root of T if r has at least one child then for each child c of node r begin Q := subtree with root c DFS(Q) end cbodfpgqerisjthlmk stack Bottom of stack backtracking

37 Breadth-first Search 2301233TREES37 a b cd klm rts efg opq hij procedure BFS(T: tree) r := root of T Q := enpty queue Add r to Q while Q is not empty begin Remove v from Q for each child c of v Add c to Q end bcdeafghijklopqrmst Queue

38 SPANNING TREES 2301233TREES38

39 Spanning Trees Definition Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. 2301233TREES39 A E B I C FHG J D A E B I C FHG J D A E B I C FHG J D

40 Finding a Spanning Tree Eliminating cycles 2301233TREES40 A E B I C FHG J D

41 THOEREM A simple graph is connected iff it has a spanning tree. Proof: Part 1: A simple graph is connected if it has a spanning tree. Part 2: A simple graph has a spanning tree if it is connected. 2301233TREES41

42 Part 1 A simple graph is connected if it has a spanning tree. Proof: กำหนดให้ G เป็น simple graph ที่มี spanning tree เนื่องจาก G มี spanning tree ดังนั้นต้องมีวิถีระหว่าง โหนดทุกคู่ นั่นคือ G เป็น connected graph 2301233TREES42

43 Part 2 A simple graph has a spanning tree if it is connected. Proof: กำหนดให้ G เป็น simple graph ที่ connected ถ้า G เป็นต้นไม้ G มี spanning tree แล้ว ถ้า G เป็นต้นไม้แล้วต้องมี circuit ใน G ดังนั้นให้ C เป็น cycle ใน G ถ้าลบเส้นเชื่อมหนึ่งเส้นใน C ออกแล้วยังมีวิถีระหว่าง โหนดทุกคู่ใน cycle C ดังนั้นยังมีเส้นเชื่อมระหว่างทุกโหนดใน G ถ้าเราทำเช่นนี้จนไม่มี cycle เหลือแล้ว จะยังคงมีวิถี ระหว่างโหนดทุกคู่ใน G Graph ที่เหลือนี้เป็น spanning tree นั่นคือ G มี spanning tree 2301233TREES43

44 Finding a Spanning Tree Using Depth-first Traversal 2301233TREES44 A E B I C FHG J D procedure DFS(G: graph, r: vertex) T := a tree with root r visit(r, T) procedure visit(v: vertex, T: tree) for each child c of node v begin add vertex c and edge {v,c} to T visit(c) end

45 Finding a Spanning Tree Using Breadth-first Traversal 2301233TREES45 A E B I C FHG J D procedure BFS(G: graph) T := tree containing a vertex r Q := enpty queue Add r to Q while Q is not empty begin Remove v from Q for each neighbor c of v if c is not in Q nor T then begin add c to Q add c and {v, c} to T end Queue AEBFCIGHJD

46 Minimum Spanning Trees Definition A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges. 2301233TREES46

47 Example 2301233TREES47 A E B I C FHG J D 1 1 2 3 1 1 1 2 7 11 55 1 A E B I C FHG J D 1 3 1 1 17 1 5 1 A E B I C FHG J D 1 2 1 17 1 55 1 A E B I C FHG J D 1 11 1 1 2 11 1 A spanning tree: weight=21A spanning tree: weight=24 graphA minimum spanning tree: weight=10

48 Prim’s Algorithm Choose the lightest edge first. Then, choose the lightest edge connecting to one of the already-chosen edges, as long as no circuit is created. 2301233TREES48 A E B I C FHG J D 1 1 2 3 1 1 1 2 7 1 1 55 1

49 Prim’s Algorithm Choose the lightest edge first. Then, choose the lightest edge connecting to one of the already-chosen edges, as long as no circuit is created. procedure Prim(G: weighted, undirected graph) T := {e | e is lightest edge} for i:=1 to number_of_node (G)-2 begin e := a lightest edge incident to a vertex in T, and not creating a cycle in T. Add e to T. end 2301233TREES49

50 THEOREM Prim’s algorithm creates a minimum spanning tree. Proof: กำหนดให้ G เป็นกราฟเชื่อมต่อแบบมีน้ำหนักที่มี n vertex กำหนดให้ T เป็นกราฟย่อยที่สร้างจาก Prim’s algorithm ขั้นแรก เราจะแสดงว่า T เป็น spanning tree. เนื่องจาก Prim’s algorithm เลือก edge ที่ไม่ทำให้เกิด cycle เรารู้ว่า T เป็นกราฟที่ไม่มีวงจร ด้วย induction proof เราบอกได้ว่ามีวิถีระหว่างโหนด ทุกคู่ เนื่องจาก T เป็นกราฟเชื่อมต่อที่ไม่มีวงจร ดังนั้น T เป็น ต้นไม้ เมื่อ Prim’s algorithm ทำงานจบ T มีโหนดทั้งหมด n โหนด ดังนั้น T เป็น spanning tree 2301233TREES50

51 THEOREM ขั้นต่อมา เราจะแสดงว่า T เป็น spanning tree ที่มีน้ำหนักน้อย สุด สมมุติว่า T ไม่เป็น spanning tree ที่มีน้ำหนักน้อยสุด และ w(T) เป็นน้ำหนักรวมของ edge ใน T ดังนั้นให้ H เป็น spanning tree ที่มีน้ำหนักน้อยสุด เและมี edge ร่วมกับ T มากที่สุด กำหนดให้ e 1, e 2,…, e n-1 เป็นเส้นเชื่อมที่เลือกโดยขั้นตอนวิธี Prim’s เรียงตามลำดับ สมมุติว่า e i เป็น edge แรกใน T ที่ไม่อยู่ใน H ถ้าเพิ่ม e i ใน H จะทำให้เกิดวงจรและ H จะไม่เป็นต้นไม้ ดังนั้นเราจะลบ e j ที่อยู่ในวงจรแต่ไม่อยู่ใน T ออกจาก H เพื่อให้ H ยังเป็นต้นไม้ ( ต้นไม้นี้คือ H-{e j }  {e i }) แต่ w(e j ) > w(e i ) เพราะ Prim’s algorithm เลือก e i ก่อน e j ดังนั้น H ไม่เป็น spanning tree ที่มีน้ำหนักน้อยสุด ซึ่งทำให้เกิด ข้อขัดแย้ง สรุปได้ว่า T ไม่เป็น spanning tree ที่มีน้ำหนักน้อยสุด 2301233TREES51

52 Kruskal’s Algorithm Choose the lightest edge, as long as no circuit is created. procedure Kruskal(G: weighted, undirected graph) T := empty graph for i:=1 to number_of_node(G)-1 begin e := any lightest edge which is not in T and does not create a circuit in T. Add e to T. end 2301233TREES52

53 Kruskal’s Algorithm Choose the lightest edge, as long as no circuit is created. procedure Kruskal(G: weighted, undirected graph) 2301233TREES53 A E B I C FHG J D 1 1 2 3 1 1 1 2 7 1 1 55 1

54 THEOREM Kruskal’s algorithm correctly finds a minimum spanning tree. Proof Let G be a graph and T be a subgraph selected from Kruskal algorithm In the first step, T is a spanning tree with minimum weight in the forest we have constructed so far (initially, F consists of n trees of 1 node each, and at each step two trees get merged until finally F is just a single tree at the end). 2301233TREES54

55 THEOREM Assume by induction that there exists an MST M of G that is consistent with F, i.e., all edges in F are also in M ; this is clearly true at the start when F has no edges. Let e be the next edge added by the algorithm. Our goal is to show that there exists an MST M′ of G consistent with F ∪ { e }. 2301233TREES55

56 THEOREM If e ∈ M then we are done ( M′ = M ). Else add e into M, creating a cycle. Since the two endpoints of e were in different trees of F, if you follow around the cycle you must eventually traverse some edge e′ = e whose endpoints are also in two different trees of F (because you eventually have to get back to the node you started from). Now, both e and e′ were eligible to be added into F, which by definition of our algorithm means that len ( e ) ≤ len ( e′ ). So, adding e and removing e′ from M creates a tree M′ that is also a MST and contains F ∪ { e }, as desired. 2301233TREES56


ดาวน์โหลด ppt Trees Hierarchical Structures. Outlines Definition of trees Modeling by trees Properties of trees Applications – Binary search trees – Prefix codes –

งานนำเสนอที่คล้ายกัน


Ads by Google