1 / 42

Graph Algorithms, 3

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

Download Presentation

Graph Algorithms, 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Algorithms, 3 Binhai Zhu Computer Science Department, Montana State University

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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|).

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. Union-Find ADT Motivation: We have disjoint sets of elements. s4 s1 s6 s7 s2 s3 s5

  28. 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.

  29. Union-Find ADT How do we do that?

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

More Related