Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309

Similar presentations


Presentation on theme: "Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309"— Presentation transcript:

1 Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University Email : weiqi.luo@yahoo.com Office : A309 weiqi.luo@yahoo.com

2 School of Software  7.1. Trees  7.2. Labeled Trees  7.3. Tree Searching  7.4. Undirected Trees  7.5. Minimal Spanning Trees 2 Chapter seven: Trees

3 School of Software  Tree Let A be a set, and let T be a relation on A. We say that T is a tree if there is a vertex v 0 in A with the property that #1 : there exists a unique path in T from v 0 to every other vertex in A #2 : no path from v 0 to v 0 Usually, v 0 is called the root of the tree, and T is referred to as a rooted tree (denoted by (T, v 0 )) 7.1 Trees 3

4 School of Software  Theorem 1 Let (T,v 0 ) be a rooted tree. Then (a) There are no cycle in T (b) v 0 is the only root of T (c) Each vertex in T, other than v 0, has in-degree one, and v 0 has in-degree zero. Proof: (a) Suppose that there is a cycle q in T, beginning and ending at vertex v. By the definition of a tree, v is not v 0 and there is a path from v 0 to v. The q o p is a different path from v 0 to v, which is a contradiction of a tree. 7.1 Trees 4

5 School of Software  (b) If v 0 ’ is another root of T, there is a path from v 0 to v 0 ’ (since v 0 is a root) and a path q from v 0 ’ to v 0 (since v 0 ’ is a root). Then qop is a cycle from v 0 to v 0, which is a contradiction of a tree.  (c) Let w 1 be a vertex in T other than v 0. There is a unique path v 0, …, v k, w 1, from v 0 to w 1 in T. Thus (v k, w 1 ) in T, so w 1 has in-degree at least one. If the in-degree of w 1 is more than 1, then there must be distinct vertices w 2,w 3 such that (w 2,w 1 ) and (w 3,w 1 ) both in T. case #1: If w 2 & w 3 are not v 0, there are two different paths from v 0 to w 1 (v 0  w 2  w 1, v 0  w 3  w 1, contradiction) case #2: If w 2 or w 3 is v 0, e.g. w 2 =v 0, there also are two different paths from v 0 to w 1 (v 0  w 1, v 0  w 3  w 1 ) 7.1 Trees 5

6 School of Software  Levels, parent-offspring & siblings 7.1 Trees 6 v4v4 v0v0 v1v1 v2v2 v3v3 v5v5 v6v6 v7v7 v8v8 v9v9 Level 1 vertices: the vertices of the edges beginning at v 0 (level 0). Level k vertices: the vertices of the edges beginning at those level k-1 vertices Level 0 Level 1 Level 2 Parent-offspring: for all pairs (r 1, r 2 )in T, r 1 is called the parent of r 2, and r 2 is called the offspring of r 1 Siblings: the vertices that have the same parent Height of a tree: the largest level number of a tree. Leaves: the vertices that have no offsprings

7 School of Software  Theorem 2 Let (T,v 0 ) be a rooted tree on a set A. Then (a) T is irreflexive (b) T is asymmetric (c) If (a,b) in T and (b,c) in T, then (a,c) is not in T, for all a, b and c in A. The proof is left as an exercise. 7.1 Trees 7

8 School of Software  Example 1 Let A be the set consisting of a given woman v 0 and all of her female descendants. We now define the following relation T on A: If v 1 and v 2 are elements of A, then v 1 T v 2 if and only if v 1 is the mother of v 2. The relation T on A is a rooted three with root v 0. 7.1 Trees 8

9 School of Software  Example 2 Let A={v 1,v 2,…v 10 } and let T={(v 2,v 3 ), (v 2,v 1 ), (v 4,v 5 ), (v 4,v 6 ), (v 5,v 8 ), (v 6,v 7 ), (v 4,v 2 ), (v 7,v 9 ), (v 7,v 10 )}. Show that T is a rooted tree and identify the root. 7.1 Trees 9 v4v4 v6v6 v2v2 v5v5 v7v7 v9v9 v 10 v1v1 v3v3 v8v8 Root

10 School of Software  n-tree If n is a positive integer, we say that a tree is an n-tree if every vertex has at most n offspring. In particular, a 2-tree is called a binary tree.  Complete n-tree If all vertices of T, other than the leaves, have exactly n offspring, we say that T is a complete n-tree. A complete 2- tree is called a completed binary tree. 7.1 Trees 10

11 School of Software  Let (T,v 0 ) be a rooted tree on the set A, and let v be a vertex of T. Let B be the set consisting of v and all its descendants, i.e., all vertices of T that can be reached by a path beginning at v. Observe that B ⊆ A. Let T(v) be the restriction of the relation T to B, that is T ∩ (B × B). Delete all vertices that are not descendants of v and all edges that do not begin and end at any such vertex. 7.1 Trees 11 v4v4 v6v6 v2v2 v5v5 v7v7 v9v9 v 10 v1v1 v3v3 v8v8 v5v5 v8v8 T(v 5 ) v2v2 v1v1 v3v3 T(v 2 ) v6v6 v7v7 v9v9 v 10 T(v 6 )

12 School of Software  Theorem 3 If (T,v 0 ) is a rooted tree and v in T, then T(v) is also a rooted tree with root v. We will say that T(v) is the subtree of T beginning at v. Proof: By the definition of T(v), there is a path from v to every other vertex in T(v). If there is a vertex w in T(v) such that there are two distinct paths q and q’ form v to w, and if p is the path in T from v 0 to v, then qop and q’op would be two distinct paths in T form v 0 to w (contradiction!). Thus each path from v to another vertex w in T(v) must be unique. if q is a cycle at v in T(v), then q is also a cycle in T (contradiction!). Thus q cannot exist. Therefore, T(v) is also a rooted tree with root v. 7.1 Trees 12

13 School of Software  Homework Ex. 4, Ex. 10, Ex. 14, Ex. 20, Ex. 26, Ex. 27, Ex. 34 7.1 Trees 13

14 School of Software  Use a tree to denote the following algebraic expression (3 - ( 2 × x ) ) + ( (x – 2 ) - ( 3 + x ) ) 7.2 Labeled Trees 14 + - 3 2xx2 3x x -+ - 1. represent the vertices simply as dots 2. show the label of each vertex next to the dot representing that vertex Here leaves denote the numbers or variables

15 School of Software  Positional n - tree n-tree: every vertex has at most n offspring. label the offspring of a given vertex from left to right with numbers 1,2 …n 7.2 Labeled Trees 15 2 2 3 3 3 1 1 3 23 1 23 1 L L L L L L L L R R R RR R R R R positional 3-tree positional binary tree

16 School of Software  Huffman code trees A variable-length code widely used in lossless data compression. Base Idea: the more frequently used letters are assigned shorter strings. 7.2 Labeled Trees 16 01 1 1 1 0 0 0 R C S A E Decode the string: 0101100 0: E 10: A 110: S 0: E

17 School of Software  Homework Ex. 8, Ex. 14, Ex. 16, Ex. 25, Ex. 27 7.2 Labeled Trees 17

18 School of Software  Visiting Performing appropriate tasks at a vertex will be called visiting the vertex.  Tree search The process of visiting each vertex of a tree in some specific order will be called searching the tree or performing a tree search. 7.3 Tree Searching 18

19 School of Software  Algorithm PREORDER Step 1: visit v Step 2: If v L exists, then apply this algorithm to (T(v L ), v L ). Step 3: If v R exists, then apply this algorithm to (T(v R ), v R ) Recursion: Recursion is the process of repeating items in a self-similar way. Refer to http://en.wikipedia.org/wiki/Recursion 7.3 Tree Searching 19

20 School of Software  Example 1 7.3 Tree Searching 20 A B C DF E GJ I L K H 1 2 3 4 56 7 8 9 10 11 ABCDEFGHIJKL

21 School of Software  Example 2 (a – b) × (c + ( d / e) ) 7.3 Tree Searching 21 × - ab e / + c d 1 2 3 4 5 78 6 ×—a b +c/de

22 School of Software  Prefix or Polish form × - a b + c / d e (a=6, b=4, c=5, d=2, e=2) 1. × - 6 4 + 5 / 2 2 2. × 2 + 5 / 2 2 replacing - 6 4 by 2 since 6-4=2 3. × 2 + 5 1 replacing / 2 2 by 1 since 2/2=1 4. × 2 6 replacing + 5 1 by 6 since 5+1=6 5. 12 replacing×2 6 by12 since 2×6=12 7.3 Tree Searching 22

23 School of Software  Algorithm INORDER Step 1: Search the left subtree (T(v L ), v L ), if it exists; Step 2: Visit the root v; Step 3: Search the right subtree (T(v R ), v R ), if it exists.  Algorithm POSTORDER Step 1: Search the left subtree (T(v L ), v L ), if it exists; Step 2: Search the left subtree (T(v R ), v R ), if it exists; Step 3: Visit the root v. 7.3 Tree Searching 23

24 School of Software  Traveling the tree using INORDER & POSTORDER 7.3 Tree Searching 24 × - ab e / + c d INORDER: a - b × c + d / e (a – b) × (c + ( d / e) ) POSTORDER: a b – c d e / + ×

25 School of Software  Infix notation a - b × c + d / e (a – b) × (c + ( d / e) ) or a – (b × ((c + d) / e) )  Postfix or reverse polish a b – c d e / + × (if a=2,b=1,c=3,d=4,e=2) 1. 2 1 – 3 4 2 / + × 2. 1 3 4 2 / + × replacing 2 1 – with 1 since 2-1=1 3. 1 3 2 + × replacing 4 2 / with 2 since 4/2=2 4. 1 5 × replacing 3 2 + with 5 since 3+2=5 5. 5 replacing 1 5× with 5 since 1×5 =5 7.3 Tree Searching 25

26 School of Software  Let T be any order tree and A be the set of vertices in T Define a binary positional tree B(T) on A: if v in A, the left offspring v L of v in B(T): the first offspring of v in T, if exists. the right offspring v R of v in B(T): the next sibling of v in T, if exists 7.3 Tree Searching 26 1 234 5 67 8 910 11 12 13 1 2 3 4 5 6 7 9 10 11 12 13 8

27 School of Software  Homework Ex. 16, Ex. 17, Ex. 18, Ex. 24, Ex. 26, Ex. 32, Ex. 36 7.3 Tree Searching 27

28 School of Software  Undirected Tree An undirected tree is simply the symmetric closure of a tree, that is, it is the relation that results from a tree when all the edges are made bidirectional. 7.4 Undirected Trees 28 b c e a f d g An undirected diagram T abef c d g An ordinary tree T 2 Note: T is the symmetric closure of T1 and T2. Thus T is a undirected tree. a d g f e b An ordinary tree T 1 c

29 School of Software  Simple Let R be a symmetric relation, and let p: v 1,v 2,…v n be a path in R. we say p is simple if no two edges of p correspond to the same undirected edge. If, in addition, v 1 equals v n, we will call p a simple cycle.  Acyclic A symmetric relation R is acyclic if it contains no simple cycles.  Connected A symmetric relation R is connected if there is a path in R from any vertex to others. 7.4 Undirected Trees 29

30 School of Software  Example 2 Path: a, b, c, e, d Path: f, e, d, c, d, a Path: f, e, a, d, b, a, f & d, a, b, d Path: f, e, d, c, e, f 7.4 Undirected Trees 30 a f b e d c Simple Not simple, since d, c & c, d correspond to the same undirected edge. Simple cycles Not a simple cycle

31 School of Software  Theorem 1 Let R be a symmetric relation on a set A. Then the following statements are equivalent. (a) R is an undirected tree (b) R is connected and acyclic 7.4 Undirected Trees 31

32 School of Software  Theorem 2 Let R be a symmetric relation on a set A. Then R is an undirected tree if and only if either of the following statements is true (a) R is acyclic, and if any undirected edge is added to R, the new relation will not be acyclic; (b) R is connected, and if any undirected edge is removed from R, the new relation will not be connected. 7.4 Undirected Trees 32

33 School of Software  Theorem 3 A tree with n vertices has n-1 edges Proof: Because a tree is connected, there must be at least n-1 edges to connect the n vertices. Support that there are more than n-1 edges. Then either the root has in-degree 1 or some other vertex has in-degree at least 2. But by Theorem 1, Section 7.1, this is impossible. Thus there are exactly n-1 edges. 7.4 Undirected Trees 33 Theorem 1 (c) Each vertex in T, other than v 0, has in-degree one, and v 0 has in-degree zero.

34 School of Software  Spanning Tree If R is a symmetric, connected relation on a set A, we say that a tree T on A is a spanning tree for R if T is a tree with exactly the same vertices as R and which can be obtained from R by deleting some edges of R. For instance, 7.4 Undirected Trees 34 a b c d e f (a) R a b c d e f (b) T’ a b c d e f (c) T’’

35 School of Software  Undirected spanning Tree An undirected spanning tree is the symmetric closure of a spanning tree, and it is useful in some applications. 7.4 Undirected Trees 35 a b c d e f (c) T’’ a b c d e f (c) Undirected Tree

36 School of Software  Example 4 7.4 Undirected Trees 36 a b c d e f (a) a b c d e f (b) a b c d e f (e) a b c d e f (c) a b c d e f (d) a b c d e f (f) Theorem 2(b) & Theorem 3 suggests a simple algorithm for finding an undirected spanning tree for a symmetric relation R.

37 School of Software  Merging the vertices Let R be a relation on a set A, and let a, b in A. Let A 0 =A- {a, b}, and A’=A 0 U {a’}, where a’ is some new element not in A. Define a relation R’ on A’ as follows. Support u, v in A’, u and v are not a’. Let (a’, u) in R’ iff (a,u) in R or (b,u) in R (u, a’) in R’ iff (u,a) in R or (u,b) in R (u, v) in R’ iff (u,v) in R We say that R’ is a result of merging the vertices a and b 7.4 Undirected Trees 37

38 School of Software  Example 5 7.4 Undirected Trees 38 v0v0 v1v1 v2v2 (a) R v6v6 v5v5 v4v4 v3v3 v’ 0 v2v2 (b) Merging v 1 & v 0 into v’ 0 v6v6 v5v5 v4v4 v3v3 v’’ 0 v6v6 (c) Merging v’ 0 & v 2 into v’’ 0 v5v5 v4v4 v3v3

39 School of Software Suppose that vertices a and b of a relation R are merged into a new vertex a’ that replaces a and b to obtain the relation R’. To determine the matrix of R’, we proceed as follows.  Step 1: Let row i represent vertex a and row j represent vertex b. Replace row i by the join of rows i and j. Note: The join of two n-tuples of 0’s and 1’s has 1 in some position exactly when either of those two n-tuples has a 1 in that position.  Step 2: Replace column i by the join of columns i and j  Step 3: Restore the main diagonal to its original values in R  Step 4: Delete row j and column j. 7.4 Undirected Trees 39

40 School of Software  Example 6 7.4 Undirected Trees 40 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v’ 0 v2v2 v3v3 v4v4 v5v5 v6v6 v2v2 v3v3 v4v4 v5v5 v6v6 v ’’0 v3v3 v4v4 v5v5 v6v6 v3v3 v4v4 v5v5 v6v6 (a)(b)(c)

41 School of Software The Prim’s algorithm for finding a spanning tree for a symmetric, connected relation R on A={v 1,v 2,…,v n }  Step 1: Choose v 1 of R, and arrange the matrix of R so that the first row corresponds to v 1  Step 2: Choose a vertex v 2 of R such that (v 1,v 2 ) in R, merge v 1 and v 2 into a new vertex v’ 1, representing {v 1,v 2 }, and replace v 1 by v’ 1. compute the matrix of R’. Call the vertex v’ 1 a merged vertex.  Step 3: Repeat Step 1& 2 on R’ until a relation with a single vertex is obtained. At each stage, keep a record of the set of original vertices that represented by each merged vertex.  Step 4: Construct the spanning tree. At each stage, when merging vertices a & b, select an edge in R from one of the original vertices represented by a to one of the original vertices represented by b. 7.4 Undirected Trees 41

42 School of Software  Example 7 7.4 Undirected Trees 42 ab cd ba c d Matrix Original & merged vertices New vertex to be merged a b cd a b c d a’ b d b da’’ d d c a’  {a, c} b d a’’  {a, c, b} a’’’ a’’’  {a, c, d, b}

43 School of Software  Homework Ex. 6, Ex. 12, Ex. 14, Ex. 16, Ex. 21, Ex. 22 7.4 Undirected Trees 43

44 School of Software  Weighted graph A weighted graph is a graph for which each edges is labeled with a numerical value called its weight. For instance vertices: towns edges: the distance between two connected towns 7.5 Minimal Spanning Trees 44 BD C A F G H E 62 6 2 3 2 3 4 5 5 4 3

45 School of Software  Example 2 7.5 Minimal Spanning Trees 45 B C IA H G J F E D 2.6 4.2 2.1 3.6 2.8 2.4 2.7 3.4 1.7 1.8 3.25.3 2.5 2.1 2.2 3.3 2.9 Vertices: relay stations Edges: the cost for upgrading 4.4

46 School of Software  Distance between vertices The weight of an edge (v i,v j ) is sometimes referred to as the distance between vertices v i and v j.  Nearest neighbor A vertex u is nearest neighbor of vertex v if u and v are adjacent and no other vertex is joined to v by an edge of lesser weight than (u, v) (Note: may have more than one nearest neighbor) 7.5 Minimal Spanning Trees 46

47 School of Software  Example 3 7.5 Minimal Spanning Trees 47 BD C A F G H E 62 6 2 3 2 3 4 5 5 4 The nearest neighbor of A is C The nearest neighbor of F is E & G 3

48 School of Software  The nearest neighbor of a set of vertices A vertex v is a nearest neighbor of a set vertices V={v 1,v 2,…,v k } in a graph if v is adjacent to some member v i of V and no other vertex adjacent to a member of V is joined by an edge of lesser weight than (v,v i ). Note: This vertex v may belong to V 7.5 Minimal Spanning Trees 48 The nearest neighbor of {C,E,J} is D. What is the nearest neighbor of {D,E,F}? Answer: D & F B C IA H G J F E D 2.6 4.2 2.1 3.6 2.8 2.4 2.7 3.4 1.7 1.8 3.25.3 2.5 2.1 2.2 3.3 2.9 4.4

49 School of Software  Minimal spanning tree A n undirected spanning tree in which the total weight of the edges is as small as possible, is called a minimal spanning tree. Prim’s algorithm (Section 7.4, p. 293) can easily be adapted to produce a minimal spanning tree for a weighted graph by using the greedy algorithm. Greedy algorithm makes the locally optimal choice at each stage, and hopes to find the global optimum. http://en.wikipedia.org/wiki/Greedy_algorithm 7.5 Minimal Spanning Trees 49

50 School of Software Algorithm: Prim’s ALGORITHM  Step 1: Choose a vertex v 1 of R. Let V={v 1 } and E={};  Step 2: Choose a nearest neighbor v i of V that is adjacent to v j, v j in V, and for which the edge (v i, v j ) does not form a cycle with members of E. Add v i to V and add (v i, v j ) to E.  Step 3: Repeat Step 2 until |E|=n-1. Then V contains all n vertices of R, and E contains the edges of a minimal spanning tree for R. 7.5 Minimal Spanning Trees 50

51 School of Software  Theorem 1 Prim’s algorithm produces a minimal tree for the relation. Proof … 7.5 Minimal Spanning Trees 51

52 School of Software  Example 5 7.5 Minimal Spanning Trees 52 BD C A F G H E 62 6 2 3 2 3 4 5 5 4 3 BD C A F G H E 62 6 2 3 2 3 4 5 5 4 3 21 Kilometers

53 School of Software  Example 6 7.5 Minimal Spanning Trees 53 B C IA H G J F E D 2.6 4.2 2.1 3.6 2.8 2.4 2.7 3.4 1.7 1.8 3.25.3 2.5 2.1 2.2 3.3 2.9 4.4

54 School of Software  Weight graph & Matrix representation 7.5 Minimal Spanning Trees 54 A B C D 4 3 3 5 2 4 4 3 32 5 5 3 3 2 A BCD A B C D

55 School of Software Algorithm PRIM’S ALGORITHM (Matrix version) Let R be a symmetric, connected relation with n vertices and M be the associated matrix of weights.  Step 1 Choose the smallest entry in M, say m ij. Let a be the vertex that is represented by row I and b the vertex represented by column j.  Step 2 Merge a with b as follows: Replace row i with 7.5 Minimal Spanning Trees 55

56 School of Software Replace column i with Replace the main diagonal with the original entries of M. Delete row j and column j. Call the resulting matrix M’.  Step 3 Repeat Step 1 & 2 on M’ and subsequent matrices until a single vertex is obtained. At each stage, keep a record of the set of original vertices that is represented by each merged vertex. 7.5 Minimal Spanning Trees 56

57 School of Software  Step 4 Construct the minimal spanning tree as follows: At each stage, when merging vertices a and b, select the edge represented by the minimal weight from one of the original vertices represented by a to one of the original vertices represented by b. 7.5 Minimal Spanning Trees 57

58 School of Software  Example 7 7.5 Minimal Spanning Trees 58 Either 2 may be selected as m ij. We choose m 3,4 and merge C and D, This produces A A B C D BCD A A B C’ B With C’  {C,D} and the first edge (C,D), Repeat 1 and 2 On M’ using m 1,3 =3. This gives A’ B B With A’  {A,C,D} and the edge (A,C) A B C D 4 3 3 5 2 A final merge yields the edge (B,D)

59 School of Software Algorithm KRUSKAL’S ALGORITHM Let R be a symmetric, connected relation with n vertices and let S={e 1,e 2,…,e k } be the set of weighted edges of R.  Step 1 Choose an edge e 1 in S of least weight. Let E={e 1 }. Replace S with S-{e 1 }  Step 2 Select an edge e i in S of least weight that will not make a cycle with members of E. Replace E with E U {e i } and S with S-{e i }  Step 3 Repeat Step 2 until |E|=n-1 7.5 Minimal Spanning Trees 59

60 School of Software  Example 8 7.5 Minimal Spanning Trees 60 BD C A F G H E 62 6 2 3 2 3 4 5 5 4 3 21 Kilometers

61 School of Software  Example 9 7.5 Minimal Spanning Trees 61 B A F C E 20 D G 12 14 10 12 15 20 16 10

62 School of Software  Homework Ex. 4, Ex. 8, Ex. 13, Ex. 15, Ex. 20, Ex. 21 7.5 Minimal Spanning Trees 62


Download ppt "Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309"

Similar presentations


Ads by Google