CSE 21 Mathematics for Algorithm and System Analysis Unit 4: Basic Concepts in Graph Theory Section 3: Trees 1
Review : Decision Tree (DT-Section 1) Root of the decision tree on the left: 1 Leaves of the decision tree: 4, 5, 8, 9, 10, 11, 7 Child and parent in the decision tree 4 and 5 are children of 2 2 is the parent of 4 and 5 CSE21: Lecture 15 2
Review : Circuit and Cycle Let G = (V, E, φ) be a graph and let e 1, e 2,..., e n be a trail with vertex sequence a 1, a 2,..., a n, a 1. (namely returns to its starting point.) The subgraph G of G induced by the set of edges {e 1, e 2,..., e n } is called a circuit of G. If the only repeated vertices on the trail are a 1 (the start and end), then the circuit is called a simple circuit or cycle. Both circuit and cycle are graphs, not just edge sequences. A graph with loop is a cycle. CSE21: Lecture 13 3
Review : Connected Graph and Components Let G = (V, E, φ) be a graph. If for any two distinct elements u and v of V there is a path P from u to v then G is a connected graph. For a graph G = (V, E, φ), the vertex set is partitioned into subsets V 1,V 2,...,V m such that if u and v are in the same subset then there is a path from u to v and if they are in different subsets there is no such path. The subgraphs G 1 = (V 1, E 1, φ 1 ),..., G m = (V m, E m, φ m ) induced by the sets V 1,V 2,...,V m are called the connected components of G. An example of a graph with two connected components: CSE21: Lecture 13 4
Learning Outcomes By the end of this lesson, you should be able to Get possible relationships between vertex number, edge number and component number of a graph. Use the algorithms to get minimum weight spanning tree for a connected graph. 5
Why do we need to learn them? Tree structure is an important data structure type in computer science. Decision tree is a special kind of tree. We can use tree structure to describe hierarchy Organization chart Ancestry (family tree) 6
Definition 13 : Tree If G is a connected graph without any cycles, then G is called a tree. If V = 1, then G is connected and hence is a tree. Example The graph on the right is connected but is not a tree, because it has many cycles, including G = ({A, B, C}, {a, e, c}). The subgraph of this graph induced by the edges {a, e, g} is a tree. If G is a tree, then φ is an injection. If e 1 e 2 and φ(e 1 ) = φ(e 2 ), then {e 1, e 2 } induces a cycle. 7
Theorem 4 : Alternative definitions of a tree If G is a connected graph, the following are equivalent. (a) G is a tree. (b) G has no cycles. (c) For every pair of vertices u v in G, there is exactly one path from u to v. (d) Removing any edge from G gives a graph which is not connected. (e) The number of vertices of G is one more than the number of edges of G. 8
Definition 14 : Forest A forest is a graph all of whose connected components are trees. In particular, a forest with one component is a tree. Example: The forest has two trees. a b c d h i e f g 9
Example 15 : A relation for forests Suppose a forest has v vertices, e edges and c (connected) components. Question: What values are possible for the triple of numbers (v, e, c)? Let the forest consist of trees T 1,..., T c and let the triples for T i be (v i, e i, c i ). Since a tree is connected, c i = 1. By Theorem 4, e i = v i 1. Since v=v 1 + +v c and e=e 1 + +e c, e = (v 1 1)+(v 2 1)+ +(v c 1) = (v 1 + +v c ) c = v c. Example: Suppose a forest has e = 12 and v = 15. It must be made up of 3 trees because c = v e = 15 12. 10
Example 15 : A relation for forests (2) A graph G = (V, E, φ) has v = 15 and c = 3, what is the minimal edge number it could have? For each component of G, remove edges one by one until we cannot remove any more without breaking the component into two components. At this point, we are left with each component a tree. Now we get a forest of c = 3 trees that still has v = 15 vertices. Because v c = e, this forest has 12 edges. Since we may have removed edges from the original graph to get to this forest, the original graph has at least 12 edges. 11
Example 15 : A relation for forests (3) A simple graph G = (V, E, φ) has v = 15 and c = 3, what is the maximal edge number it could have? If c is not specified, the edges in a simple graph are a subset of P 2 (V) which has C(v, 2) elements, so the a simple graph with v vertices has at most C(v, 2) edges. If c is specified, we need get vertex number for each component. The maximum occurs when one of v i is as large as possible and the others equal 1. We only need to count the edge number for the v i because edge number for others are 0. The result for this example is C(13, 2) = (13 12)/(2 1). 12
Example 15 : A relation for forests (4) Suppose a graph has v vertices, e edges and c (connected) components. There is no graph G = (V, E, φ) with v c > e. If v c = e, the graph is a forest of c trees. If v c < e, there are many graphs, but none of them are forests. If v c < e and the graph is a simple graph, then e C(v+1 c, 2). If v c < e and the graph is a simple graph with loops, then e C(v+1 c, 2) + v. If v c < e and the graph is a graph, then there is no upper bound for e, namely e can be any large number. 13
Definition 15 and 16 : Rooted graph, parent, child, sibling and leaf A pair (G, v), consisting of a graph G = (V, E, φ) and a specified vertex v, is called a rooted graph with root v. Let (T, r) be a rooted tree. If w is any vertex other than r, let r = v 0, v 1,..., v k, v k+1 = w, be the list of vertices on the unique path from r to w. We call v k the parent of w and call w a child of v k. Parents and children are also called fathers and sons. Vertices with the same parent are siblings. A vertex with no children is a leaf. All other vertices are internal vertices of the tree. 14
Examples of rooted graph, parent, child, sibling and leaf The graph on the right is a rooted graph with root a, also a rooted tree with root a. a, c, f is the unique path from root a to vertex f. So c is the parent of f and f is a child of c. Similarly, we know both e and g are children of c. So e, f, g are siblings. b has no children, so b is a leaf. a, c are internal vertices of the tree. g d f c e b a 15
Definition 17 : Rooted plane tree Let (T, r) be a rooted tree. For each vertex, order the children of the vertex. The result is a rooted plane tree, which we abbreviate to RP-tree. RP-trees are also called ordered trees. An RP-tree is also called, in certain contexts, a decision tree. So we can traverse a RP-tree by depthfirst and breadth-first search. Almost all trees in computer science are rooted and plane, so computer scientists usually call a rooted plane tree simply a tree. 16
Example 16 : A rooted plane tree A rooted plane tree T = (V, E, φ) where V = 11 and E = {a,..., j} and φ s definition is in the picture. The vertex 6 is the parent of the vertex 9. The vertices 8, 9, 10, and 11 are the children of 6 and, they are siblings of each other. The leaves of the tree are 4, 5, 7, 8, 9, 10, 11. All other vertices (including the root) are internal vertices of the tree. If we change the order of the children of a vertex, it is still the same graph and tree, but becomes a different RP-tree. 17
Definition 18 : Spanning tree A spanning tree of a (simple) graph G = (V, E) is a subgraph T = (V, E ) which is a tree and has the same set of vertices as G. Example The left graph is a spanning tree of the right graph. c c d e d e A graph is connected if and only if it has a spanning tree. 18
Definition 19 : Weights in a graph Let G = (V, E) be a simple graph and let λ be a function from E to the positive real numbers. We call λ(e) the weight of the edge e. If H = (V, E ) is a subgraph of G, then λ(h), the weight of H, is the sum of λ(e ) over all e E. A minimum weight spanning tree for a connected graph G is a spanning tree such that λ(t) λ(t ) whenever T is another spanning tree. Example Weight of each edge is shown on the right graph. The tree on the left is a minimum weight spanning tree for the graph. d c e d c 1 1 3 e 19
Theorem 5 : Minimum weight spanning tree - Prim s algorithm Let G = (V, E) be a simple graph with edge weights given by λ. If the algorithm stops with V V, G has no spanning tree; otherwise, (V, E ) is a minimum weight spanning tree for G. 1. Start: Let E = and let V ={v 0 } where v 0 is any vertex in V. 2. Possible Edges: Let F Ebe those edges f = {x, y} with one vertex in V and one vertex not in V. If F =, stop. 3. Choose Edge Greedily: Let f = {x, y} be such that λ(f) is a minimum over all f F. Replace V with V {x, y} and E with E {f}. Go to Step 2. 20
Theorem 5 : Minimum weight spanning tree - Prim s algorithm (2) Applying Prim s algorithm for the graph, the changes of E, V and F are: 1. E =, V = {c}, F = {{c, d}, {c, e}} 2. E = {{c, d}}, V = {c, d}, F = {{d, e}, {c, e}} 3. E = {{c, d}, {c, e}}, V = {c, d, e}, F = No matter which vertex to start, this algorithm can guarantee the generated spanning tree has minimum weight. A graph could have multiple minimum weight spanning trees. If λ({d, e}) =1, both G = ({c, d, e}, {{c, d}, {c, e}}) and G = ({c, d, e}, {{c, d}, {d, e}}) have minimum weight. d c 1 1 3 e 21
Variation of Theorem 5 - Kruskal s algorithm In Kruskal s algorithm, step 2 of Prim s algorithm is changed to: 2. Possible Edges: Let F Ebe those edges f = {x, y} where x and y do not belong to the same (connected) component of (V, E ). If F =, stop. The step also means f Fif f forms a cycle with any collection of edges from E. Otherwise, f F. Its difference from Prim s algorithm is that f doesn t have to have a vertex in V (more freedom to choose f). So F in Prim s algorithm is a subset of F in this algorithm. 22
Variation of Theorem 5 - Kruskal s algorithm (2) For Prim s algorithm, the first a few changes of E, V a and F are: 1. E =, V = {a}, F = {{a, b}, {a, d}} 2. E = {{a, b}}, V = {a, b}, F = {{b, c}, {a, d}} 3. For Kruskal s algorithm, the first a few changes of E, V and F are: 1. E =, V = {a}, F = {{a, b}, {a, d}, {b, c}, {c, d}} 2. E = {{b, c}}, V = {a, b, c}, F = {{a, b}, {a, d}, {c, d}} 3. b 1 1 c 2 2 d 23
Homework and Pre-Reading Assignment Homework: Exercise 3.2, 3.3, 3.5, 3.6, 3.7, in page GT-35 to GT- 36. For next class, please finish reading Section 3 and the first a few pages of Section 4 (GT-33 to GT-41). Try to understand the algorithm to get lineal spanning trees from a graph. Try to understand the meanings of Θ and O, and how we can use them to describe time complexities of algorithms. 24