420 likes | 431 Views
Graph Algorithms, 3. Binhai Zhu Computer Science Department, Montana State University. Minimum Spanning Trees. Given a weighted, undirected graph G=(V,E) of n vertices, we want to find an acyclic subset T of E which connects all the vertices in V and whose weight
E N D
Graph Algorithms, 3 Binhai Zhu Computer Science Department, Montana State University
Minimum Spanning Trees Given a weighted, undirected graph G=(V,E) of n vertices, we want to find an acyclic subset T of E which connects all the vertices in V and whose weight w(T) = Σ(u,v) inT w(u,v) is minimized. This is also known as the Minimum Spanning Tree problem.
Minimum Spanning Trees Given a weighted, undirected graph G=(V,E) of n vertices, we want to find an acyclic subset T of E which connects all the vertices in V and whose weight w(T) = Σ(u,v) inT w(u,v) Is minimized. This is also known as the Minimum Spanning Tree problem. It has a lot of application in practice, say in computer networks.
MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST property We can’t use a brute-force method, as it will take exponential time. To achieve this, we must have some useful property. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST property We can’t use a brute-force method, as it will take exponential time. To achieve this, we must have some useful property. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2
MST property We can’t use a brute-force method, as it will take exponential time. To achieve this, we must have some useful property. v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2 V-U U
MST property We can’t use a brute-force method, as it will take exponential time. To achieve this, we must have some useful property. MST-Property: The lightest edge e between U and V-U must be in some minimum spanning tree of G. How do we prove this? v4 1.5 2.9 1 1.7 v1 v5 1.6 2 1.3 v3 v2 V-U U
Prim’s algorithm v4 1.5 2.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 1 1.7 v1 v5 1.6 2 1.3 v3 v2 V-U U
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} Running time? 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Prim’s algorithm v4 1.5 0.9 Let G=(V,E)=({1,2,…,n},E) Prim(G,T) //T will be the output • T ← Ø • U ← {1} • While U ≠ V • let (u,v) be the lightest edge with u ε U and v ε V-U • T ← T \union {(u,v)} • U ← U \union {v} Running time? O(|E||V|) using no advanced data structure; with priority queue, O((|V|+|E|)∙log |V|). 3 1.3 v1 v5 1.6 2 1.7 v3 v2
Detailed Prim’s algorithm • Prim(G,w,r) //r is the root of the tree • //key[v] is the minimum weight of an edge connecting v to a vertex in the current tree • For each u in V[G] • do key[u] ← ∞, π[u] ← NIL • key[r] ← 0 • Q ← V[G] //Q is the priority queue, keyed on key[-] • While Q ≠ Ø • do u ← EXTRACT-MIN(Q) • for each v in Adj[u] • if v is in Q and w(u,v) < key[v] • then π[v] ← u • key[v] ← w(u,v) • Running time? O(|E||V|) using no advanced data structure; with priority queue, O((|V|+|E|)∙log |V|).
Kruskal’s algorithm Kruskal(G,w) //which involves Union-Find operations • A ←Ø //A is a forest • For each vertex v in V[G] • do MAKE-SET(v) • Sort edges of E in nondecreasing order by weight w • For each edge (u,v) in E (in nondecreasing order by weight) • do if FIND-SET(u) ≠ FIND-SET(v) • then A ← A U {(u,v)} • UNION(u,v) • Return A Running time? O(|E| x log |V|) --- if UNION, FIND-SET can be done in O(log |V|) time. v4 1.6 0.9 3 1.3 v1 v5 1.5 2 1.7 v3 v2
Kruskal’s algorithm Kruskal(G,w) //which involves Union-Find operations • A ←Ø //A is a forest • For each vertex v in V[G] • do MAKE-SET(v) • Sort edges of E in nondecreasing order by weight w • For each edge (u,v) in E (in nondecreasing order by weight) • do if FIND-SET(u) ≠ FIND-SET(v) • then A ← A U {(u,v)} • UNION(u,v) • Return A Running time? O(|E| x log |V|). v4 1.6 0.9 3 1.3 v1 v5 1.5 2 1.7 v3 v2
Kruskal’s algorithm Kruskal(G,w) //which involves Union-Find operations • A ←Ø //A is a forest • For each vertex v in V[G] • do MAKE-SET(v) • Sort edges of E in nondecreasing order by weight w • For each edge (u,v) in E (in nondecreasing order by weight) • do if FIND-SET(u) ≠ FIND-SET(v) • then A ← A U {(u,v)} • UNION(u,v) • Return A Running time? O(|E| x log |V|). v4 1.6 0.9 3 1.3 v1 v5 1.5 2 1.7 v3 v2
Kruskal’s algorithm Kruskal(G,w) //which involves Union-Find operations • A ←Ø //A is a forest • For each vertex v in V[G] • do MAKE-SET(v) • Sort edges of E in nondecreasing order by weight w • For each edge (u,v) in E (in nondecreasing order by weight) • do if FIND-SET(u) ≠ FIND-SET(v) • then A ← A U {(u,v)} • UNION(u,v) • Return A Running time? O(|E| x log |V|). v4 1.6 0.9 3 1.3 v1 v5 1.5 2 1.7 v3 v2
Kruskal’s algorithm Kruskal(G,w) //which involves Union-Find operations • A ←Ø //A is a forest • For each vertex v in V[G] • do MAKE-SET(v) • Sort edges of E in nondecreasing order by weight w • For each edge (u,v) in E (in nondecreasing order by weight) • do if FIND-SET(u) ≠ FIND-SET(v) • then A ← A U {(u,v)} • UNION(u,v) • Return A Running time? O(|E| x log |V|). v4 1.6 0.9 3 1.3 v1 v5 1.5 2 1.7 v3 v2
Union-Find ADT Motivation: We have disjoint sets of elements. s4 s1 s6 s7 s2 s3 s5
Union-Find ADT Motivation: We have disjoint sets of elements. s4 s1 s6 s7 s2 s3 s5 Problem: Find the set containing a given element. Compute the union of two sets.
Union-Find ADT How do we do that?
Union-Find ADT How do we do that? We will use an array! Example: { {5,9,1,3}, {8,4,11,6,10},{2,7},{12}} 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do that? We will use an array! Example: { {5,9,1,3}, {8,4,11,6,10},{2,7},{12}} i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 0 11 5 6 0 0 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do Find or Find-Set, say Find1(6)? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 0 11 5 6 0 0 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do Find or Find-Set, say Find1(6)? It should return 11 (the root of the tree containing 6)! i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 0 11 5 6 0 0 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 0 11 5 6 0 0 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! But how? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 0 11 5 6 0 0 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! But how? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 11 11 5 6 0 0 12 5 11 7 4 8 9 2 Why? 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! But how? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 11 11 5 6 0 0 12 5 11 7 4 8 9 2 Why? You want to keep the height of the tree as small as possible, as that has sth to do with the cost of Find1. 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 0 8 11 11 5 6 0 0 12 5 11 7 4 8 9 2 But the size of the new tree has changed. How to deal with this? 1 3 6 10
Union-Find ADT How do we do Union, say Union(11,7)? We should union the two trees into one! i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 -4 8 11 11 5 6 -7 -1 12 5 11 7 4 8 9 2 But the size of the new tree has changed. How to deal with this? Root stores `–size’. 1 3 6 10
Union-Find ADT Can we improve Find1 to a new version Find2? Say Find2(10)? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 -4 8 11 11 5 6 -7 -1 12 5 11 7 4 8 9 2 1 3 6 10
Union-Find ADT Can we improve Find1 to a new version Find2? Say Find2(10)? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 -4 8 11 11 5 6 -7 -1 12 5 11 7 4 8 9 2 While returning 11, all the nodes from 10 to the root (11) become the children of the root. 1 3 6 10
Union-Find ADT Can we improve Find1 to a new version Find2? Say Find2(10)? i 1 2 3 4 5 6 7 8 9 10 11 12 parent[i] 9 7 9 11 -411 11 11 5 11-7 -1 12 5 11 7 4 8 9 2 While returning 11, all the nodes from 10 to the root (11) become the children of the root. 1 3 6 10