CSC 10400 Discrete Mathematical Structures Lecture 12 Instructor: Pavel Rytir Email: [email protected] The City College of New York Spring 2015 Section 12.1 Tree: Definitions, Properties and Examples Tree Properties on Trees Forest Tree Tree A loop-free undirected graph is a tree if it is connected and contains no cycles. Only G1 is a tree. G2 contains a cycle, and G3 is not connected. FG1 is also a spanning tree of G2 . Theorem If u and v are distinct vertices in a tree T , then there is a unique path that connects u and v . Theorem If u and v are distinct vertices in a tree T , then there is a unique path that connects u and v . Proof. Because T is connected, there is at least one path connecting u and v . If there are more than one path between u and v , then from two such paths some of the edges would form a cycle. Because T has no cycles, there is a unique path connecing u and v . Theorem If G is an undirected graph, then G is connected if and only if G has a spanning tree. Theorem If G is an undirected graph, then G is connected if and only if G has a spanning tree. Proof. If G has a spanning tree T , then for every pair of vertices u and v , there is a path in T connecting them. Thus, G is connected. If G is connected and G is not a tree, we break each cycle by deleting an edge in the cycle. The resulting subgraph is connected and contains all vertices in G . If the resulting subgraph is a tree, we are done. Otherwise, the resulting subgraph has a cycle, and we can continue to break the cycle, until we obtain a tree. Hence, G has a spanning tree. Theorem In every tree T = (V , E ), |V | = |E | + 1. Theorem In every tree T = (V , E ), |V | = |E | + 1. Proof. If |E | = 0, the tree consists of a single isolated vertex, so |V | = 0. Suppose |E | = |V | + 1 for every |E | ≤ k for some k. When |E | = k + 1, we delete an edge from T , and obtain two trees T1 = (V1 , E1 ) and T2 = (V2 , E2 ). Clearly, |V | = |V1 | + |V2 | and |E | = |E1 | + |E2 | + 1. Thus, we have |E | = |E1 | + |E2 | + 1 = (|| V1 − 1) + (|V2 | − 1) + 1 = |V | − 1 By mathematical induction, |V | = |E | + 1. Theorem For any tree T = (V , E ), if |V | ≥ 2, then T has at least two vertices with degree 1. Theorem For any tree T = (V , E ), if |V | ≥ 2, then T has at least two vertices with degree 1. Proof. Let P |V | = n ≥ 2. First, we have |E | = |V | − 1 = n − 1. Second, v ∈V deg (v ) = 2|E | = 2(n − 1). Because T is connected, we have deg (v ) ≥ 1 for every node v . If there are k vertices with degree 1, then we have n − k vertices has degree at least 2. Thus X k + 2(n − k) ≤ deg (v ) = 2(n − 1) v ∈V Solve this inequality, we have k ≥ 2. Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (a) G is a tree. (b) G is connected, but the removal of any edge from G disconnects G into two subgraphs that are trees. (c) G contains no cycles, and |V | = |E | + 1. (d) G is connected and |V | = |E | + 1. (e) G contains no cycles and if u, v ∈ V with (u, v ) ∈ / E , then the graph obtained by adding the edge (u, v ) to G has exactly one cycle. Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (a) G is a tree. (b) G is connected, but the removal of any edge from G disconnects G into two subgraphs that are trees. Proof. (a) ⇒ (b). If G is a tree, then G is connected. Let e = {u, v } be any edge of G . If G − e is connected, there are at least two paths in G from u to v , contradicting that G is a tree. Thus, G − e is disconnected. Removing e from G partitions G into two components: one led by u and the other one led by v . Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (b) G is connected, but the removal of any edge from G disconnects G into two subgraphs that are trees. (c) G contains no cycles, and |V | = |E | + 1. Proof. (b) ⇒ (c). If G has a cycle, then let e = {u, v } be an edge of the cycle. But G − e will be connected, contradicting the hypothesis in part (b). So G has no cycle. Because G is a loop-free undirected graph, by the definition, we know that G is a tree. Consequently, we have |V | = |E | + 1 Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (c) G contains no cycles, and |V | = |E | + 1. (d) G is connected and |V | = |E | + 1. Proof. (c) ⇒ (d). Suppose G has r components: G1 , G2 , . . . , Gr . For 1 ≤ i ≤ r , select a vertex vi ∈ Gi , and add r − 1 edges {v1 , v2 , }, {v2 , v3 }, . . . , {vr −1 , vr } to G to form a new graph G 0 = (V , E 0 ). The graph G 0 still has no cycle, i.e., G 0 is a tree. Since G 0 is a tree, we know that |V | = |E 0 | + 1. But from part (c), |V | = |E | + 1, so |E | = |E 0 | and r − 1 = 0. Because r = 1, it follows that G is connected. Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (d) G is connected and |V | = |E | + 1. (e) G contains no cycles and if u, v ∈ V with (u, v ) ∈ / E , then the graph obtained by adding the edge (u, v ) to G has exactly one cycle. Proof. (d) ⇒ (e). Let C be a cycle in G with r vertices and r edges. Since G is connected, the remaning vertices of G can each be connected to a vertex in C by a path. Each such connection requires at least one new edge. Thus, |E | ≥ |V |, contradicting |V | = |E | + 1. Therefore, G has no cycle and connected, i.e., G is a tree. Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (d) G is connected and |V | = |E | + 1. (e) G contains no cycles and if u, v ∈ V with (u, v ) ∈ / E , then the graph obtained by adding the edge (u, v ) to G has exactly one cycle. Proof. (d) ⇒ (e) (Continue...) When we add a new edge (u, v ) into G , there are two paths between u and v : one is the unique path P between u and v , because G is a tree; the other one is the edge (u, v ). Therefore, P ∪ {(u, v )} is a cycle C1 . Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (d) G is connected and |V | = |E | + 1. (e) G contains no cycles and if u, v ∈ V with (u, v ) ∈ / E , then the graph obtained by adding the edge (u, v ) to G has exactly one cycle. Proof. (d) ⇒ (e) (Continue...) If the new graph has the second cycle C2 , C2 must contain the edge (u, v ); otherwise, G would contain a cycle. If C2 6= C1 , because C2 = P 0 ∪ {u, v }, where P2 is a path from u to v in G , we have P 6= P 0 . Thus, there are two distinct paths from u to v in G , contradicting that G is a tree. Therefore, there has exactly one cycle in the new graph. Theorem The following statements are equivalent for a loop-free undirected graph G = (V , E ) (e) G contains no cycles and if u, v ∈ V with (u, v ) ∈ / E , then the graph obtained by adding the edge (u, v ) to G has exactly one cycle. (a) G is a tree. Proof. (e) ⇒ (a) If G is not connected, let G1 and G2 be the components of G with u ∈ G1 and v ∈ G2 . Adding an new edge (u, v ) will not result in a cycle. Thus, G is connected. Because G has no cycle. G is a tree. Section 12.2 Rooted Trees Rooted trees Tree traversals: preorder, postorder and inorder Graph traversal: BFS and DFS Balanced trees Rooted Tree A tree is a rooted tree if we specify one node as the root of the tree. Figure: A tree with root node r . Ancestors, Descendents, Parents, Children and Siblings Let T = (V , E ) be a tree with root node r . Ancestors For any node x in the tree, if a node y 6= x locates on the path from r to x, then y is an ancestor of x. Ancestors, Descendents, Parents, Children and Siblings Let T = (V , E ) be a tree with root node r . Ancestors For any node x in the tree, if a node y 6= x locates on the path from r to x, then y is an ancestor of x. Descendents If x is an ancestor of y , then y is a descendent of x. Ancestors, Descendents, Parents, Children and Siblings Let T = (V , E ) be a tree with root node r . Ancestors For any node x in the tree, if a node y 6= x locates on the path from r to x, then y is an ancestor of x. Descendents If x is an ancestor of y , then y is a descendent of x. Parents If x is an ancestor of y , and (x, y ) is an edge in the tree, then x is the parent of y . Ancestors, Descendents, Parents, Children and Siblings Let T = (V , E ) be a tree with root node r . Ancestors For any node x in the tree, if a node y 6= x locates on the path from r to x, then y is an ancestor of x. Descendents If x is an ancestor of y , then y is a descendent of x. Parents If x is an ancestor of y , and (x, y ) is an edge in the tree, then x is the parent of y . Children If x is the parent of y , then y is a child of x. Ancestors, Descendents, Parents, Children and Siblings Let T = (V , E ) be a tree with root node r . Ancestors For any node x in the tree, if a node y 6= x locates on the path from r to x, then y is an ancestor of x. Descendents If x is an ancestor of y , then y is a descendent of x. Parents If x is an ancestor of y , and (x, y ) is an edge in the tree, then x is the parent of y . Children If x is the parent of y , then y is a child of x. Siblings If two distinct nodes x and y have the same parent, then x and y are siblings. Examples r ,a and c are ancestors of g . f , g , j, k, p and q are descendants of c. c is the parent of f and g . p and q are children of j. h and i are siblings. Leaves, Internal Nodes, and Levels Let T = (V , E ) be a tree with root r . Leaves A leaf is a node with no child. Internal nodes An internal node is a node with children. Levels For every vertex v , the level of v is the edges contained in the path from r to v . FThe root node has level 0! Examples The nodes p, q, k, d, s, t and u are leaves; and other nodes are internal nodes The node on level 0 is r. The nodes on level 1 is a, b. The nodes on level 2 is c, d, e. The nodes on level 3 are f , g , h, i. Subtree Let T = (V , E ) be a tree. For any node x, the subtree of T at x is the subgraph induced by x and its descendants. (a) A tree T (b) The subtree of T at b Preorder Traversal Let T = (V , E ) be a tree with root r . A preorder traversal on T is defined as follow. If T only contains the root r , then the preorder traversal is the root r itself. Otherwise, let T1 , T2 , . . . , Tk be the subtrees at the children of r , from left to right. The preorder traversal First, visits r . Then, traverses T1 in preorder. Traverses T2 in preorder and so on until traverses Tk in preorder. An Example of Preorder Traversal Let T be a tree with root 1, shown in the following figure. An preorder traversal of this tree is: 1, 2, 5, 11, 12, 13, 14, 3, 6, 7, 4, 8, 9, 10, 15, 16, 17 Postorder Traversal Let T = (V , E ) be a tree with root r . A postorder traversal on T is defined as follow. If T only contains the root r , then the postorder traversal is the root r itself. Otherwise, let T1 , T2 , . . . , Tk be the subtrees at the children of r , from left to right. The postorder First, traverses T1 in postorder. Then, traverses T2 in postorder and so on until traverses Tk in postorder. Finally, visits r . An Example of Postorder Traversal Let T be a tree with root 1, shown in the following figure. An postorder traversal of this tree is: 11, 12, 13, 14, 5, 2, 6, 7, 3, 8, 9, 15, 16, 17, 10, 4, 1 Question How can computer calculate an expression, e.g., ((7 + 3)/5) ↑ ((8 − 5) ∗ 2), where a ↑ b = ab ? Question How can computer calculate an expression, e.g., ((7 + 3)/5) ↑ ((8 − 5) ∗ 2), where a ↑ b = ab ? Use a tree. ((7 + 3)/5) ↑ ((8 − 5) ∗ 2) =((7 + 3)/5) ↑ (3 ∗ 2) =((7 + 3)/5) ↑ 6 =(10/5) ↑ 6 =2 ↑ 6 =64 Polish Notation Question How can computer calculate an expression, e.g., ((7 + 3)/5) ↑ ((8 − 5) ∗ 2), where a ↑ b = ab ? Use the Polish notation, or prefix notation; i.e., presenting a ⊕ b by ⊕ab. Polish Notation Question How can computer calculate an expression, e.g., ((7 + 3)/5) ↑ ((8 − 5) ∗ 2), where a ↑ b = ab ? Use the Polish notation, or prefix notation; i.e., presenting a ⊕ b by ⊕ab. Therefore, the mathematical equation ((7 + 3)/5) ↑ ((8 − 5) ∗ 2) can be represented by Polish notation as ↑ / + 735 ∗ −852 Computation on Polish Notation ↑ / + 735 ∗ −852 = ↑ / + 735∗32 = ↑ /+7356 = ↑ / 10 56 =↑ 26 =64 Preorder Traversal and Polish Notation The tree corresponding to the equation ((7 + 3)/5) ↑ ((8 − 5) ∗ 2) is: Its preorder traversal is: ↑ / + 735 ∗ −852, which is exactly the polish notation of the equation. Binary Tree A tree is a binary tree if every internal node has at most two children. A tree is a complete binary tree if every internal node has exactly two children. (a) A binary tree T (b) A complete binary tree T Inorder Traversal Let T = (V , E ) be a binary tree with root r . A inorder traversal on T is defined as follow. If T only contains the root r , then the inorder traversal is the root r itself. Otherwise, let TL and TR be the subtrees at the left child and the right child R, respectively. The inorder traversal First, traverses TL in inorder, if TL exists. Then, visits r . Finally, traverses TR in inorder, if TR exists. An Example of Postorder Traversal Let T be a tree with root r , shown in the following figure. An postorder traversal of this tree is: p, j, q, f , c, k, g , a, d, r , b, h, s, m, e, i, t, n, u Depth-First Search Algorithm Depth-First search (DFS) algorithm can traverse a loop-free undirected graph. Depth-First Search Algorithm Depth-First search (DFS) algorithm can traverse a loop-free undirected graph. Initial a stack S with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. Depth-First Search Algorithm Depth-First search (DFS) algorithm can traverse a loop-free undirected graph. Initial a stack S with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While S is not empty Depth-First Search Algorithm Depth-First search (DFS) algorithm can traverse a loop-free undirected graph. Initial a stack S with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While S is not empty Pop an element u from the top of the stack S, and mark u as visited. Depth-First Search Algorithm Depth-First search (DFS) algorithm can traverse a loop-free undirected graph. Initial a stack S with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While S is not empty Pop an element u from the top of the stack S, and mark u as visited. Find all unvisited vertices adjacent with u, push these vertices at the top of the stack S, and attach all edges from u to these vertices into the spanning tree T so that u is the parent of these vertices. FThe resulting tree T is exactly a spanning tree of the graph. An Example of DFS Step 1: S = {A}, T = {A}. Step 2: S = {B, C , D}, T = {A, B, C , D}. Step 3: S = {B, C , E , F }, T = {A, B, C , D, E , F }. Step 4: S = {B, C , E }, T = {A, B, C , D, E , F }. Step 5: S = {B, C }, T = {A, B, C , D, E , F }. Step 6: S = {B}, T = {A, B, C , D, E , F }. Step 7: S = ∅, T = {A, B, C , D, E , F }. An Example of DFS The spanning tree generated by DFS is: Breadth-First Search Algorithm Breadth-First search (BFS) algorithm can traverse a loop-free undirected graph. Breadth-First Search Algorithm Breadth-First search (BFS) algorithm can traverse a loop-free undirected graph. Initial a queue Q with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. Breadth-First Search Algorithm Breadth-First search (BFS) algorithm can traverse a loop-free undirected graph. Initial a queue Q with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While Q is not empty Breadth-First Search Algorithm Breadth-First search (BFS) algorithm can traverse a loop-free undirected graph. Initial a queue Q with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While Q is not empty Pop an element u from the front of the queue Q, and mark u as visited. Breadth-First Search Algorithm Breadth-First search (BFS) algorithm can traverse a loop-free undirected graph. Initial a queue Q with only one element v , and initial a spanning tree T which consists of only one vertex v as its root. While Q is not empty Pop an element u from the front of the queue Q, and mark u as visited. Find all unvisited vertices adjacent with u, push these vertices at the end of the queue Q, and attach all edges from u to these vertices into the spanning tree T so that u is the parent of these vertices. FThe resulting tree T is exactly a spanning tree of the graph. An Example of BFS Step 1: Q = {A}, T = {A}. Step 2: Q = {B, C , D}, T = {A, B, C , D}. Step 3: Q = {C , D, E }, T = {A, B, C , D, E }. Step 4: Q = {D, E , F }, T = {A, B, C , D, E , F }. Step 5: Q = {E , F }, T = {A, B, C , D, E , F }. Step 6: Q = {F }, T = {A, B, C , D, E , F }. Step 7: Q = ∅, T = {A, B, C , D, E , F }. An Example of BFS The spanning tree generated by BFS is: Height and Balanced Tree The height of a tree is the largest level number achieved by a leaf of the tree. A rooted tree T of height h is balanced if the level number of every leaf in T is h − 1 or h. (a) height = 3, balanced (b) height = 3, unbalanced Interview Question Suppose you are given 8 coins. Exactly one coin is fake. If we knows that the fake coin is heavier than others, and we have a two pan scale. How many weighings do we need to find the fake one? Interview Question Suppose you are given 8 coins. Exactly one coin is fake. If we knows that the fake coin is heavier than others, and we have a two pan scale. How many weighings do we need to find the fake one? Use the decision tree shown as follows. Thus, two weighings is sufficient and necessary. Interview Question Suppose you are given 12 coins. Exactly one coin is fake. If we do not know whether the fake coin is heavier or lighter than others, and we have a two pan scale. How many weighings do we need to find the fake one? Interview Question Suppose you are given 12 coins. Exactly one coin is fake. If we do not know whether the fake coin is heavier or lighter than others, and we have a two pan scale. How many weighings do we need to find the fake one? This problem is much more difficult than the previous one. The answer is 3. You can google/wiki it. Section 12.4 Weighted Trees and Prefix Codes Weighted trees Prefix code: Huffman code Weighted Tree Let w1 , w2 , . . . , wn be a set of positive numbers called weights. A tree T with n leaves is a weighted tree if we assign w1 , . . . , wn to the n leaves. Prefix Code A set P of binary sequences is called a prefix code if no sequence in P is the prefix of any other sequence in P. Examples 111, 0, 1100, 1101, 10 constitute a prefix code for letters a, b, c, d, e 111, 0, 110, 1101, 10 is not a prefix code because 110 is a prefix of 1101. Use Prefix Code Suppose that 111, 0, 1100, 1101, 10 is the prefix code corresponding to letters a, b, c, d, e respectively. The original message is acdbe. The encoding result is: 11111001101010 Use Prefix Code Suppose that 111, 0, 1100, 1101, 10 is the prefix code corresponding to letters a, b, c, d, e respectively. The original message is acdbe. The encoding result is: 11111001101010 Decoding Method: First, build a tree corresponding to the encoding rule. Scan the codes from the beginning to the end; search the tree until reaching leaves, and translate the substring into a letter. Question How to design a prefix code for a given alphabet (e.g., English letters) so that the expected code length for strings is minimum? Answer Question How to design a prefix code for a given alphabet (e.g., English letters) so that the expected code length for strings is minimum? Answer Use Huffman code. Huffman Code Let w1 , w2 , . . . , wn are weights assigned to n elements. The Huffman code can be constructed by Assign the given weights, one each to a set S of n isolated vertices. Each vertex is a root of complete binary tree of height 0 with a weight assigned to it. While |S| > 1 Choose two trees T and T 0 in S with the smallest two root weights w and w 0 respectively. Create a new tree T ∗ with root weight w ∗ = w + w 0 and having T and T 0 as its left and right subtrees, respectively. Place T ∗ into S and delete T and T 0 from S. When |S| = 1, this complete binary tree is a tree corresponding to Huffman code. An Example of Constructing a Huffman Code Suppose that letters a, b, c, d, e, f have weight 4, 7, 12, 17, 20, 28 respectively. The huffman code of a, b, c, d, e, f is 1000, 1001, 101, 00, 01, 11 respectively.
© Copyright 2024