Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.

Similar presentations


Presentation on theme: "Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23."— Presentation transcript:

1 Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23

2 Minimum Spanning Trees (MST) A common problem in communications networks and circuit design: connecting a set of nodes by a network of minimum total length Represent the problem as an undirected graph where edge weights denote wire lengths 4 8 87 4 14 67 1 9 10 2 11

3 Formally, Given a connected undirected graph G=(V,E), a spanning tree is an acyclic subset of edges T that connects all the vertices together. Assuming each edge (u,v) of G has a numeric weight (or cost) w(u,v), the cost of a spanning tree T is the sum of the weights of the edges in T. A minimum spanning tree (MST) is a spanning tree of minimum weight.

4 Minimum Spanning Trees (MST) Never pays to have cycles Resulting connection graph is connected, undirected and acyclic, hence a free tree Not unique 7 4 8 8 4 14 67 1 9 10 2 11 2 Total weight = 37 7 4 8 8 4 14 67 1 9 10 2 11 2 Total weight = 37

5 Generic MST Algorithm Two main algorithms for computing MSTs –Kruskal’s –Prim’s Both greedy algorithms The greedy strategy captured by a generic aproach: –Grow the MST one edge at a time –which edge to select?

6 Generic MST Algorithms Maintain a set of edges : A Loop invariant: A is a subset of some MST Safe edge (u,v): A U {(u,v)} is also a subset of an MST Generic-MST(G, w) A = empty set while A does not form a spanning tree do find an edge (u,v) that is safe for A A = A U {(u,v)} return A

7 How to recognize safe edges? Definitions first: Cut: Let S be a subset of vertices V. A cut (S, V-S) is a partition of vertices into two disjoint sets S and V-S. 7 4 8 8 4 14 67 1 9 10 2 11 2 S V-S

8 We say that an edge crosses the cut (S, V-S) if one of its endpoints is in S and the other is in (V-S) 7 4 8 8 4 14 67 1 9 10 2 11 2 S V-S

9 We say that a cut respects a set A of edges if no edge in A crosses the cut Total weight = 37 7 4 8 8 4 14 67 1 9 10 2 11 2 S V-S

10 An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. Total weight = 37 7 4 8 8 4 14 67 1 9 10 2 11 2 S V-S

11 How to recognize safe edges? 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 MST 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, edge (u,v) is safe for A.

12 Proof Suppose that no MST contains (u,v). Let T be an MST of G. u v x y 4 8 6 T

13 Add (u,v) to T, creating a cycle. There must be at least one more edge on this cycle that crosses the cut: (x,y) u v x y 4 8 6

14 The edge (x,y) is not in A, since the cut respects A Remove (x,y) and restore the spanning tree T’ w(T’) = w(T) + w(u,v) – w(x,y) < w(T) –Conradicts the assumption that T is an MST. u v x y 4 6 T’

15 Kruskal’s Algorithm Add edges to A in increasing order of weights. –If the edge being considered introduces a cycle, skip it. –Otherwise add to A. Notice that the edges in A make a forest, and they keep getting merged together.

16 Correctness Say (u, v) is the edge is going to be added next, and it does not introduce a cycle in A. Let A’ denote the tree of A that contains vertex u. Consider the cut ( A’, V-A’). Every edge crossing the cut is not in A, so this cut respects A and (u,v) is the light edge crossing it. Thus, (u,v) is safe..

17 How to efficiently detect if adding (u,v) creates a cycle in A Can be done by a data structure called Disjoint Set Union- Find Supports three operations: –CreateSet(u): create a set containing a single item u –FindSet(u): return the set that contains item u –Union(u,v): merge the set containing u with the set containing v Suffices to know: takes O(n log n + m) time to carry out any sequence of m union-find operations on a set of size n.

18 Kruskal(G=(V,E), w) { A = empty set for each (u in V) CreateSet(u) //create a set for each vertex Sort E in increasing order by weight for each ((u,v) from the sorted list E) { if (FindSet(u) != FindSet(v)) { // u and v are in different trees Add(u,v) to A Union(u,v) } return A } Θ(E log E) for sorting the edges O(V log V + E) for a sequence of E union find operations Total : O(E log E) since (E >= V-1)

19 Prim’s Algorithm Edges in A always form a tree (partial MST) Start from a vertex r and grow until the tree spans all vertices Let S denote the set of vertices which are on this partial MST –A is the set of edges connecting the vertices in S At each stage the light edge crossing (S, V-S) is added to A 7 4 8 8 4 14 6 7 1 9 10 2 11 2 S V-S r

20 Prim’s Algorithm Prim(G=(V,E),w,r) { for each (u in V) { key[u] = infinity pred[u] = NIL } key[r] = 0; PQ = {V} //add all vertices to Priority Queue PQ based on keys while (PQ is not empty) do { u = PQ.Extract-Min() for each (v in Adj[u]) if (v is in PQ and w(u,v) < key[v]) then { pred[v] = u key[v]=w(u,v) PQ.DecreaseKey(v, key[v]) //reorganizes the PQ }


Download ppt "Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23."

Similar presentations


Ads by Google