CSE 21 for Algorithm and System Analysis

Unit 4: Basic Concepts in Section 3: Trees

CSE21: Lecture 14 1 Review : Decision (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 • Let G = ( V, E, φ) be a graph and let e1, e2, ..., en be a trail with sequence a1, a2, . . . , an, a1. (namely returns to its starting point.) • The subgraph G′ of G induced by the set of edges {e1, e2, ..., en} is called a circuit of G. – If the only repeated vertices on the trail are a1 (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 V1,V2,..., Vm 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 G1= (V1, E1, φ1), . . . , Gm = ( Vm, Em, φm) induced by the sets V1,V2,..., Vm 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.

CSE21: Lecture 14 5 Why do we need to learn them? • Tree structure is an important data structure type in . – Decision tree is a special kind of tree. – We can use tree structure to describe hierarchy • Organization chart • Ancestry (family tree) • …

CSE21: Lecture 14 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 e1 ≠ e2 and φ(e1) = φ(e2), then {e1, e 2} induces a cycle.

CSE21: Lecture 14 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.

CSE21: Lecture 14 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 CSE21: Lecture 14 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 T1, . . . , Tc and let the triples for Ti be ( vi, ei, ci). Since a tree is connected, ci = 1. By Theorem 4, ei= vi − 1. Since v=v1+···+vc and e=e1+···+ec, e = ( v1−1)+(v2−1)+···+(vc−1) = (v1+···+ vc)−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.

CSE21: Lecture 14 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. CSE21: Lecture 14 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 P2(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 vi is as large as possible and the others equal 1. We only need to count the edge

number for the vi because edge number for others are 0. – The result for this example is C(13, 2) = (13×12)/(2×1).

CSE21: Lecture 14 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. CSE21: Lecture 14 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 = v0, v1, ..., vk, vk+1 = w, be the list of vertices on the unique path from r to w. We call vk the parent of w and call w a child of vk. • 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.

CSE21: Lecture 14 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. g f e • b has no children, so b is a leaf. • a, c are internal vertices of the tree. c d b

a CSE21: Lecture 14 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 depth- first 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.

CSE21: Lecture 14 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. CSE21: Lecture 14 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 c – The left graph is a spanning c tree of the right graph. d e d e

• A graph is connected if and only if it has a spanning tree.

CSE21: Lecture 14 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 c c – Weight of each edge is shown on the right graph. 1 1 d e – The tree on the left is a d e minimum weight spanning tree for the graph. 3

CSE21: Lecture 14 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′ ={ v0} where v0 is any vertex in V. 2. Possible Edges : Let F ⊆ E be 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.

CSE21: Lecture 14 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: c 1. E′ = ∅, V′ = {c}, F = {{c, d}, {c, e}} 1 2. E′ = {{c, d}}, V′ = {c, d}, F = {{d, e}, {c, e}} 1 3. E′ = {{c, d}, {c , e}}, V′ = {c, d, e}, F = ∅ d e 3 • 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.

CSE21: Lecture 14 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 ⊆ E be 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 ∉ F if 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.

CSE21: Lecture 14 22 Variation of Theorem 5 - Kruskal’s algorithm (2) • For Prim’s algorithm, the first a few changes of E′, V ′ and F are: a 1 2 d 1. E′ = ∅, V′ = {a}, F = {{a, b}, {a, d}} b 2. E′ = {{a, b}}, V′ = {a, b}, F = {{b, c}, {a, d}} 1 2 3. … c • 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. …

CSE21: Lecture 14 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.

CSE21: Lecture 14 24