Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binhai Zhu Computer Science Department, Montana State University

Similar presentations


Presentation on theme: "Binhai Zhu Computer Science Department, Montana State University"— Presentation transcript:

1 Binhai Zhu Computer Science Department, Montana State University
Graph Algorithms, 3 Binhai Zhu Computer Science Department, Montana State University Frequently, presenters must deliver material of a technical nature to an audience unfamiliar with the topic or vocabulary. The material may be complex or heavy with detail. To present technical material effectively, use the following guidelines from Dale Carnegie Training®. Consider the amount of time available and prepare to organize your material. Narrow your topic. Divide your presentation into clear segments. Follow a logical progression. Maintain your focus throughout. Close the presentation with a summary, repetition of the key steps, or a logical conclusion. Keep your audience in mind at all times. For example, be sure data is clear and information is relevant. Keep the level of detail and vocabulary appropriate for the audience. Use visuals to support key points or steps. Keep alert to the needs of your listeners, and you will have a more receptive audience. 7/16/2019

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) in T w(u,v) is minimized. This is also known as the Minimum Spanning Tree problem. 7/16/2019

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) in T 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. 7/16/2019

4 MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

5 MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

6 MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

7 MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

8 MST example We can’t use a brute-force method, as it will take exponential time. v4 1.5 2.9 1 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

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 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

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 v1 1.7 v5 1.6 2 1.3 v3 v2 7/16/2019

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 v1 1.7 v5 1.6 2 1.3 v3 v2 V-U U 7/16/2019

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 v1 1.7 v5 1.6 2 1.3 v3 v2 V-U U 7/16/2019

13 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.7 v5 1.6 2 1.3 v3 v2 V-U U 7/16/2019

14 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

15 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

16 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

17 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

18 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

19 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

20 Prim’s algorithm Let G=(V,E)=({1,2,…,n},E)
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 v1 1.3 v5 1.6 2 1.7 v3 v2 7/16/2019

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|). 7/16/2019

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 v1 1.3 v5 1.5 2 1.7 v3 v2 7/16/2019

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 v1 1.3 v5 1.5 2 1.7 v3 v2 7/16/2019

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 v1 1.3 v5 1.5 2 1.7 v3 v2 7/16/2019

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 v1 1.3 v5 1.5 2 1.7 v3 v2 7/16/2019

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 v1 1.3 v5 1.5 2 1.7 v3 v2 7/16/2019

27 Union-Find ADT Motivation: We have disjoint sets of elements. s4 s1 s6
7/16/2019

28 Union-Find ADT Motivation: We have disjoint sets of elements.
Problem: Find the set containing a given element. Compute the union of two sets. 7/16/2019

29 Union-Find ADT How do we do that? 7/16/2019

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}} 5 11 7 12 9 8 4 2 1 3 6 7/16/2019 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 parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 10

32 Union-Find ADT How do we do Find or Find-Set, say Find1(6)? i
parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 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 parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 10

34 Union-Find ADT How do we do Union, say Union(11,7)?
We should union the two trees into one! i parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 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 parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 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 parent[i] 5 11 7 12 9 8 4 2 Why? 1 3 6 7/16/2019 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 parent[i] 5 11 7 12 9 8 4 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 7/16/2019 10

38 Union-Find ADT How do we do Union, say Union(11,7)?
We should union the two trees into one! i parent[i] 5 11 7 12 8 4 9 2 1 3 6 But the size of the new tree has changed. How to deal with this? 7/16/2019 10

39 Union-Find ADT How do we do Union, say Union(11,7)?
We should union the two trees into one! i parent[i] 5 11 7 12 9 8 4 2 But the size of the new tree has changed. How to deal with this? Root stores `–size’. 1 3 6 7/16/2019 10

40 Union-Find ADT Can we improve Find1 to a new version Find2?
Say Find2(10)? i parent[i] 5 11 7 12 8 4 9 2 1 3 6 7/16/2019 10

41 Union-Find ADT Can we improve Find1 to a new version Find2?
Say Find2(10)? i parent[i] 5 11 7 12 9 8 4 2 While returning 11, all the nodes from 10 to the root (11) become the children of the root. 1 3 6 7/16/2019 10

42 Union-Find ADT Can we improve Find1 to a new version Find2?
Say Find2(10)? i parent[i] 5 11 7 12 9 8 4 2 While returning 11, all the nodes from 10 to the root (11) become the children of the root. 1 3 6 7/16/2019 10


Download ppt "Binhai Zhu Computer Science Department, Montana State University"

Similar presentations


Ads by Google