Minimum Spanning Trees [for connected & undirected graphs] Course: CS 5130 - Advanced Data Structures and Algorithms Instructor: Dr. Badri Adhikari
Wiring problem in Electronic circuit design Several components are made electrically equivalent by wiring them together. To interconnect a set of n pins, we can use an arrangement of n-1 wires, each connecting two pins. Of course, of the many arrangements possible, the one that uses the least amount of wire is desirable. How many possible configurations are there for a given number of nodes n? Pins in a circuit board We would like to connect all the orange pins
Wiring problem in Electronic circuit design The wiring problem with a connected, undirected graph G = (V, E): ● V is the set of pins, ● E is the set of possible interconnections between pairs of pins, ● For each edge ( u , v ) ∈ E, we have a weight ( u , v ) specifying the cost (amount of wire needed) to connect u and v . We wish to find an acyclic subset T ⊆ E that connects all of the vertices and whose total weight w (T) = ∑ w ( u , v ), where ( u , v ) ∈ T, is minimized. Since T is acyclic and connects all of the vertices, it must form a tree, Pins in a circuit board called spanning tree since it ‘spans’ the graph G. We would like to connect The problem of determining the T is called ‘ minimum-spanning-tree all the orange ones problem’ .
Example All lines (dotted and straight) represent the edges of the graph. a b Dark lines (undotted) represent the edges of MST. e Is this a MST? c d f g
Solving the MST problem Two algorithms for solving the minimum-spanning tree problem: (a) Kruskal’s algorithm, and (b) Prim’s algorithm Both use greedy strategy to obtain the optimal solution. Both rely on a generic method.
Greedily growing a minimum spanning tree We have a connected, undirected graph G = (V, E) with weight function w :E→R. We wish to find a MST for G. Manage a set of edges A, which is a subset of some MST. An edge (u,v) is safe for a set A such that adding (u,v) to A, maintains A as a subset of some MST. We say, edge (u,v) is safe edge for A. At each step, we determine an edge (u,v) that we can add to A so that A ∪ {(u,v)} is also a subset of a minimum spanning tree. How to recognize safe edges?
Greedily growing a minimum spanning tree A cut (S, V-S) of an undirected graph G = (V, E) is a partition of V. An edge (u,v) ∈ E crosses the cut (S, V-S) if one of its endpoints is in S and the other is in V-S. A cut respects a set A of edges if no edge in A crosses the cut. An edge is light edge crossing a cut if its weight is the minimum of any edge crossing the cut. There can be more than one light edge crossing a cut in case of ties.
Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A. A a b 3 7 A 5 - A is a subset of MST. e A - The cut respects A. 1 u v A - An edge is safe for A if it 3 maintains A. S f g V-S
Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A. Proof (approach): Let T be a minimum spanning tree that includes A, and assume that T does not contain the light edge (u, v), since if it does, we are done! (a) Assume that (u, v) is not in the MST but (x, y) is. (b) Construct a new tree T ’ = T - {(x,y)} + {(u,v)}. (c) Show that T ’ is MST as well. (d) Show that (u,v) is safe for A. Light edge
Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A. Proof: Let T be a minimum spanning tree that includes A, and assume that T does not contain the light edge (u, v). The edge (u,v) forms a cycle with the edges on the simple path p from u to v in T. Since u and v are in the opposite sides of the cut (S, V-S), at least one edge in T lies on the simple path p and also crosses the cut. Let (x, y) be such edge. The edge (x,y) is not in A, because the cut respects A. Since (x, y) is on the unique simple path from u to v in T, removing (x,y) breaks T into two components. Adding (u,v) reconnects them to form a new spanning tree T ’ = T - {(x,y)} ∪ {(u,v)}.
Theorem: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G. Let (S, V-S) be any cut of G that respects A, and let (u,v) be a light edge crossing (S,V-S). Then (u,v) is safe for A. Proof (continued): Since (u,v) is a light edge crossing (S, V-S) and (x, y) also crosses the cut, w(u, v) ≤ w(x, y). Therefore, w(T ’ ) = w(T) - w(x,y) + w(u,v) ≤ w(T) But T is a minimum spanning tree, so that w(T) ≤ w(T ’ ); thus T ’ must be a minimum spanning tree. How is (u,v) safe edge for A? We have A ⊆ T ’ and A ∪ {(u, v)} ⊆ T ’ . Since T ’ is a MST (u,v) must be safe for A.
Corollary: Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, and let C = (V C , E C ) be a connected component (tree) in the forest G A = (V, A). If (u,v) is a light edge connecting C to some other component in G A , then (u,v) is safe for A. Proof: a C b 3 The cut (V C , V-V C ) respects A, and 7 5 (u, v) is a light edge for this cut. e Therefore, (u,v) is safe for A. 1 u v 3 V C f g V-V C
The algorithms of Kruskal and Prim In Kruskal’s algorithm, the set A is a forest whose vertices are all those of the given graph. The safe edge added Kruskal’s algorithm Prim’s algorithm to A is always a least-weight edge in the graph that connects two distinct A a a A components. b b 3 3 7 In Prim’s algorithm, the set A forms a 7 A A 5 5 single tree. The safe edge added to A e e is always a least-weight edge 1 A 1 A u v u v connecting the tree to a vertex not in 3 tree. 3 A f f g g
Kruskal’s algorithm Kruskal’s algorithm finds a safe edge to add to the growing forest, i.e. an edge (u, v) of least weight to connect any two trees in the forest. One tree is one connected component. C 1 and C 2 are the two trees that are connected by (u,v). Since (u,v) is a light edge Where will Kruskal’s connecting C 1 to some other tree, the Corollary implies that (u, v) is safe for C 1 . algorithm start? C 1 C 2
https://en.wikipedia.org/wiki/Kruskal%27s_algorithm#/media/File:KruskalDemo.gif
Running time of Kruskal’s algorithm O(V) O(E lg E) O(E) Running time of Kruskal’s = O(E lg E) |E| ≤ |V| 2 ⟹ O (lg E) = O(lg V 2 ) = O(2 lg V) = O(lg V) So, running time of Kruskal’s = O(E lg V)
Prim’s algorithm The Prim’s algorithm has the property that the edges in the set A always form a single tree. A a The tree starts from an arbitrary root vertex r and grows until the A b tree spans all the vertices in V. 3 7 A 5 At each step, a light edge that connects A to an isolated vertex e (one on which an edge of A is incident). 1 A u v 3 By the Corollary, this rule adds only edges that are safe for A; therefore when the algorithm terminates, the edges in A form a f minimum spanning tree. g
Prim’s algorithm All vertices that are not in the tree reside in a min-priority queue Q based on a key attribute. A a For each vertex v, the attribute v. key is the minimum weight of any edge A connecting v to a vertex in the tree; v. key = ∞ if no such edge exists. b 3 7 A The attribute v.π names the parent of v in the tree. 5 e 1 A The algorithm implicitly maintains the set A as: u v A = {(v, v.π) : v ∈ V - {r} - Q} 3 When the algorithm terminates, the min-priority queue A is empty; the f minimum spanning tree A of G is: g A = {(v, v.π) : v ∈ V - {r}}
https://en.wikipedia.org/wiki/Prim%27s_algorithm#/media/File:PrimAlgDemo.gif
Running time of Prim’s algorithm Total time for all calls to Extract-Min operations is O(V * time for Extract-Min()). We scan the adjacency list of each vertex once which takes O(E) time. Let’s assume that the time needed for “v. key = w(u,v)” is x so that O(V) total time is O(x E). Every time a v.key is updated, the heap has to be updated. O(lgV) Queue implementation using binary min-heap: Loop runs Time for Extract-Min = O(lgV) O(E) x = O(lgV) times Total running time = O(V lgV + E lgV ) = O(E lgV) Queue implementation using Fibonacci heap: Time for Extract-Min = O(lgV) x = O(1) Total running time = O(V lgV + E)
Classwork Obtain MST for the graph using: (a) Kruskal’s algorithm (should be easy and quick). (b) Prim’s algorithm
Recommend
More recommend