Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum Spanning Trees

Similar presentations


Presentation on theme: "Minimum Spanning Trees"— Presentation transcript:

1 Minimum Spanning Trees
Given an undirected graph G=(V,E), find a graph G’=(V,E’) such that E’ is a subset of E |E’| = |V| - 1 G’ is connected is minimal G’ is a minimal spanning tree. Applications: wiring a house, power grids, Internet connections Nov 26, 2001 CSE 373, Autumn 2001

2 Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. G v known Nov 26, 2001 CSE 373, Autumn 2001

3 Example 5 5 v1 v0 5 8 2 2 v3 10 1 v2 v4 10 6 5 3 v5 Nov 26, 2001 CSE 373, Autumn 2001

4 Analysis Running time. Same as Dijkstra’s: O(|E| log |V|) Correctness:
Suppose we have a partially built tree that we know is contained in some minimum spanning tree T. Let (u,v)E, where u is “known” and v is “unknown” and has minimal cost. Then there is a MST T’ that contains the partially built tree and (u,v) that has as low a cost as T. Nov 26, 2001 CSE 373, Autumn 2001

5 Union/Find Algorithms
Equivalence Relations Suppose R is a relation defined on a set S. R is an equivalence relation if: aRa for all aS (reflexive) aRb iff bRa for all a,bS (symmetric) aRb and bRc implies aRc for all a,b,cS (transitive) Examples: Yes No = over the integers  over the integers path from u to v in undirected graphs path from u to v in directed graphs Nov 26, 2001 CSE 373, Autumn 2001

6 Dynamic Equivalence An equivalence relation defines a partition of S.
b a An equivalence relation defines a partition of S. operations: find(a): return the name of the equivalence class of a. union(a,b): merge the equivalence classes of a and b. Dynamic: the classes change because of the union operation. Nov 26, 2001 CSE 373, Autumn 2001

7 Simple Strategy find(i): return A[i]. O(1) union(a,b): Let i = A[a]
1 2 3 N-1 i find(i): return A[i] O(1) union(a,b): Let i = A[a] Let j = A[b] Scan the array and change all the i’s to j’s O(N) Is there a better way? Yes! Nov 26, 2001 CSE 373, Autumn 2001

8 Better Strategy Make unions “easy” but finds “hard”.
Idea: Keep a tree for each equivalence class. find(x) returns the root of the tree x is in. union(x,y): merge the two trees by making one a subtree of the other. 1 2 3 4 5 6 7 after union(1, 2): 1 3 4 5 6 7 2 Nov 26, 2001 CSE 373, Autumn 2001

9 Example, continued after union(3, 4): 1 3 5 6 7 2 4 after union(2, 4):
1 3 5 6 7 2 4 after union(2, 4): 1 5 6 7 3 2 4 implicit (array) implementation: A[i] -1 1 3 i 2 4 5 6 7 Nov 26, 2001 CSE 373, Autumn 2001


Download ppt "Minimum Spanning Trees"

Similar presentations


Ads by Google