Variation in Terminology: Rosen Bryan
Variation in Terminology: Rosen Bryan
ROSEN path simple path ? ? ? circuit simple circuit Euler circuit Euler path Hamilton circuit Hamilton path
BRYAN walk trail path tour cycle closed walk closed trail Euler tour Euler trail Hamilton cycle Hamilton path
CS3230 Algorithms
The most popular algorithms book uses Euler tour and Hamilton cycle.
Trees
Read Rosen, 10.1, 10.2, 10.4, 10.5
Advantages of using tree models: useful structural properties, efcient algorithms for solving tree problems
Topics
Terminology (Rosen, 10.1) Applications (Rosen, 10.2) Spanning Trees (Rosen, 10.4) Minimum Spanning Trees (Rosen, 10.5)
Terminology
Trees
Denitions. An acyclic graph is one that contains no cycles. A tree is a connected acyclic undirected graph. An acyclic undirected graph is also called a forest.
Trees
Example.
G1 and G2 are trees as both are connected acyclic graphs. G3 is not a tree as it has a cycle ebade. G4 is not a tree as it is not connected.
Forests
Example.
Trees
Recall Theorem 2 of graph theory lecture. There is a path between any two distinct vertices of a connected undirected graph. Theorem 1. There is a unique path between any two distinct vertices of a tree (i.e., connected acyclic undirected graph). Proof by contradiction. 1. Assume 2 distinct paths P1 and P2 from u to v in tree G. 2. There is an edge e = {x, y} of P1 but not of P2 . 3. Since graph (P1 P2 ) e remains connected, it contains a path P from x to y, by the above theorem. 4. But P + e is a cycle in the acyclic graph G.
Trees
Theorem 2. If there is a unique path between any two distinct vertices of a loopless undirected graph G, G is a tree. Proof. Omitted. Left as homework. Theorem 3. There is a unique path between any two distinct vertices of a loopless undirected graph G if and only if G is a tree. Proof. Theorems 1 + 2.
Trees
Theorem 4. If an undirected graph G is a tree, |E(G)| = |V (G)| 1. Proof by induction on |V (G)|. Base case: When |V (G)| = 1, G is a tree with just 1 vertex. Since G has no edges, |E(G)| = 0.
Trees
Theorem 4. If an undirected graph G is a tree, |E(G)| = |V (G)| 1. Inductive case: Suppose theorem is true for all trees with fewer than |V (G)| vertices and |V (G)| 2. 1. Let {u, v} E(G). By Theorem 1, uv is the unique path from u to v in G. So, G {u, v} contains no path from u to v and is disconnected, by Theorem 2 of graph theory lecture. 2. G {u, v} is a forest of two trees G1 and G2 with |V (G1 )| < |V (G)| and |V (G2 )| < |V (G)|. By induction hypothesis, |E(Gi )| = |V (Gi )| 1 for i = 1, 2. 3. |E(G)| = |E(G1 )|+|E(G2 )|+1 = |V (G1 )|+|V (G2 )| 1 = |V (G)| 1 .
Trees
Corollary 1. A tree with more than 1 vertex has at least 2 vertices of degree 1. Proof. 1. Let G be the tree. Then, dG (v) 1 for any v V (G). 2. dG (v) = 2|E(G)| = 2|V (G)| 2, by handshake theorem and Theorem 4.
vV (G)
Trees
Lemma 1. If G is a connected graph containing a cycle C, the resulting graph G by removing any edge of C from G is connected. Proof. 1. Any two distinct vertices u and v of G are distinct vertices of G. Since G is connected, there is a walk from u to v. 2. Let edge e of C be removed. If e is not an edge of the walk, the walk is also a walk in G . 3. If e is an edge of the walk, replace e by the rest of C to form a new walk in G . 4. Since there is a walk between any two distinct vertices of G , G is connected.
Trees
Theorem 5. If an undirected graph G is connected and |E(G)| = |V (G)| 1, G is a tree. Remark. It sufces to show that G is acyclic. Proof by contradiction. 1. Suppose G has a cycle. 2. If an edge is removed from cycle, the resulting graph is connected, by Lemma 1. Repeat this until the resulting graph is acyclic but connected. Call this tree G . 3. |E(G )| = |V (G )| 1, by Theorem 4. 4. Since |E(G )| < |E(G)| and |V (G )| = |V (G)|, this contradicts |E(G)| = |V (G)| 1.
Rooted Trees
Level 0
Level 1
Root a Parent of c: b Children of g: h, i, j Siblings of h: i, j Ancestors of e: c, b, a Descendants of b: c, d, e Internal vertices: a, b, c, g, h, j Leaves: d, e, f, i, k, l, m Subtree rooted at g
Binary Trees
A binary tree is a rooted tree in which every parent has at most two children! !! Left child! !! Right child! !! A full binary tree is a binary tree in which each parent has exactly two children! !! Given any parent v in a binary tree T! !! Left subtree of v: binary tree rooted at left child of v! !! Right subtree of v: binary tree rooted at right child of v!
!!
Binary Trees
Example.
Left child of d is f Right child of d is g Left subtree of c in (b) Right subtree of c in (c)
Binary Trees
Theorem 6. A full binary tree with i internal vertices contains 2i + 1 vertices. Remark. Number of leaves is i + 1. Proof. Since each internal vertex has 2 children, there are 2i vertices in tree other than root. So, tree has 2i + 1 vertices. Theorem 7. A binary tree of height h has at most 2h leaves. Remark. 2h or log2 h.
Proof Sketch. Represent each path from root to leaf as a binary sequence. How many binary sequences are there?
T1 is balanced as all leaves are at levels 3 and 4. T2 is not balanced as the leaves are at levels 2, 3, and 4.
Applications
Decision Trees
Sort a, b, c
Game Trees
Tic-Tac-Toe
Spanning Trees
Example: IP Multicasting
Spanning Trees
Denitions. A graph H is called a subgraph of a graph G if V (H) V (G), E(H) E(G), and e E(H) (fH (e) = fG (e)). A spanning subgraph of graph G is a subgraph H with V (H) = V (G). A spanning tree of graph G is a spanning subgraph of G that is a tree.
Spanning Trees
Example. Find a spanning tree of the simple graph below. Recall Lemma 1 and step 2 of Theorem 5: Keep removing edge from cycle to make graph acyclic but connected.
Spanning Trees
Example. Other spanning trees of the simple graph below.
Spanning Trees
Theorem 8. If a graph G is connected, G has a spanning tree. Proof. 1. If G is not a tree, G has a cycle. 2. If an edge is removed from cycle, the resulting graph is connected, by Lemma 1. Repeat this until the resulting graph is acyclic but connected. Call this tree G . 3. Since V (G ) = V (G), G is a spanning tree.
Spanning Trees
Theorem 9. If a graph G has a spanning tree, G is connected. Proof. 1. The spanning tree G contains every vertex of G. 2. There is a walk between any two vertices of G , by denition. 3. Since G is a subgraph of G, there is a walk between any two vertices of G. So, G is connected. Theorem 10. A graph is connected if and only if it has a spanning tree. Proof. Theorems 8 + 9.
Spanning Trees
Corollary 2. All spanning trees of a graph have the same number of edges. Proof. All spanning trees have same number of vertices. It follows from Theorem 4 that they have same number of edges.
Depth-First Search
Applications. Finding path and cycle Determining connected components Algorithm. procedure DFS(G) T := tree consisting only of vertex v1 visit(v1) procedure visit(v) for each vertex w adjacent to v and not in T add vertex w and edge {v, w} to T visit(w)
Breadth-First Search
Application. Finding path of least length Algorithm. procedure BFS(G) T := tree consisting only of vertex v1 Q := empty queue put v1 in Q while Q is not empty remove rst vertex v from Q for each vertex w adjacent to v if w is not in Q and not in T then add w to end of Q add w and edge {v, w} to T
Problem: choose lease lines minimizing total network cost such that there is a path between any two computer centers. Model: weighted graph Solution: minimum spanning tree
Prims Algorithm
procedure Prim(G) T := a minimum-weight edge for i := 1 to |V(G)| - 2 e := an edge of minimum weight incident to a vertex in T that does not form a cycle in T if added to T T := T with e added
Prims Algorithm
Example. Constructing MST.
Kruskals Algorithm
procedure Kruskal(G) T := empty graph for i := 1 to |V(G)| - 1 e := any edge in G with smallest weight that does not form a cycle in T if added to T T := T with e added