Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.

Similar presentations


Presentation on theme: "More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms."— Presentation transcript:

1 More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms

2 Spanning Tree Definition –A spanning tree of a graph G is a tree (acyclic) that connects all the vertices of G once i.e. the tree “spans” every vertex in G –A Minimum Spanning Tree (MST) is a spanning tree on a weighted graph that has the minimum total weight Where might this be useful? Can also be used to approximate some NP-Complete problems

3 Sample MST Which links to make this a MST? Optimal substructure: A subtree of the MST must in turn be a MST of the nodes that it spans. Will use this idea more in dynamic programming.

4 MST Claim Claim: Say that M is a MST –If we remove any edge (u,v) from M then this results in two trees, T1 and T2. –T1 is a MST of its subgraph while T2 is a MST of its subgraph. –Then the MST of the entire graph is T1 + T2 + the smallest edge between T1 and T2 –If some other edge was used, we wouldn’t have the minimum spanning tree overall

5 Greedy Algorithm We can use a greedy algorithm to find the MST. –Skipping some terminology on the cut, the cut respects the edge –Two common algorithms Kruskal Prim

6 Kruskal’s MST Algorithm Idea: Greedily construct the MST –Go through the list of edges and make a forest that is a MST –At each vertex, sort the edges –Edges with smallest weights examined and possibly added to MST before edges with higher weights –Edges added must be “safe edges” that do not ruin the tree property.

7 Kruskal’s Algorithm

8 Kruskal’s Example A={ }, Make each element its own set. {a} {b} {c} {d} {e} {f} {g} {h} Sort edges. Look at smallest edge first: {c} and {f} not in same set, add it to A, union together. Now get {a} {b} {c f} {d} {e} {g} {h}

9 Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e} {g} {h} {e} <> {h}, add edge. Now get {a} {b} {c f} {d} {e h} {g}

10 Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e h} {g} {a} <> {c f}, add edge. Now get {b} {a c f} {d} {e h} {g}

11 Kruskal’s Example Keep going, checking next smallest edge. Had {b} {a c f} {d} {e h} {g} {b} <> {a c f}, add edge. Now get {a b c f} {d} {e h} {g}

12 Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {a b c f}, dont add it!

13 Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {e h}, add it. Now get {a b c f e h} {d}{g}

14 Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f e h} {d}{g} {d} <> {a b c e f h}, add it. Now get {a b c d e f h} {g}

15 Kruskal’s Example Keep going, check next two smallest edges. Had {a b c d e f h} {g} {a b c d e f h} = {a b c d e f h}, don’t add it. 6 5 4 2 9 15 14 10 3 8 a b cd e fg h

16 Kruskal’s Example Do add the last one: Had {a b c d e f h} {g}

17 Runtime of Kruskal’s Algo Runtime depends upon time to union set, find set, make set Idea: number each vertex and use an array –Use an array member[] : member[i] is a number j such that the ith vertex is a member of the jth set. –Example member[1,4,1,2,2] indicates the sets S1={1,3}, S2={4,5} and S4={2}; i.e. position in the array gives the set number. Idea similar to counting sort, up to number of edge members.

18 Set Operations Given the Member array Make-Set(v) member[v] = v Make-Set runs in constant running time for a single set. Find-Set(v) Return member[v] Find-Set runs in constant time. Union(u,v) for i=1 to n do if member[i] = u then member[i]=v Scan through the member array and update old members to be the new set. Running time O(n), length of member array. 12 3 member = [1,2,3]; {1} {2} {3} find-set(2) = 2 Union(2,3) member = [1,3,3] ; {1} {2 3}

19 Overall Runtime O(V) O(ElgE) – using heapsort O(1) O(V) Total runtime: O(V)+O(ElgE)+O(E*(1+V)) = O(E*V) Book describes a version using trees for sets that runs in O(E*lgV) time O(E)

20 Prim’s MST Algorithm Also greedy, like Kruskal’s Will find a MST but may differ from Prim’s if multiple MST’s are possible

21 Prim’s Example

22

23 Prim’s Algorithm

24

25

26 Get spanning tree by connecting nodes with their parents:

27 Runtime for Prim’s Algorithm The inner loop takes O(E lg V) for the heap update inside the O(E) loop. This is over all executions, so it is not multiplied by O(V) for the while loop (this is included in the O(E) runtime through all edges. The Extract-Min requires O(V lg V) time. O(lg V) for the Extract-Min and O(V) for the while loop. Total runtime is then O(V lg V) + O(E lg V) which is O(E lg V) in a connected graph (a connected graph will always have at least V-1 edges). O(V) if using a heap O(V) O(lgV) if using a heap O(E) over entire while(Q<>NIL) loop O(lgV) to update if using a heap!

28 Shortest Path Algorithms

29 Shortest Path? Example: What is the shortest path from g to b?

30 Definitions Formal definition of problem –Input: Weighted directed graph G=(V,E) plus w: E  R, the weight function that maps from edges to weight values (real numbers). –Output: The shortest path between any source S and all other vertices. –The weight of a path is the sum of the weights of the edges on the path. w(u,v) is the weight of arc that connects vertex u and v; it is not necessarily the min. w(p) is the weight of some path p, where p is a list of vertices indicating the path from a source to destination. (u,v) is the weight of the shortest path that connects vertex u and v.

31 Shortest Path One way to address this problem is to shift edge values up to remove negative values

32 Properties of Shortest Paths

33 Relaxation Example

34 Dijkstra’s Algorithm

35 Dijkstra Example (0) 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154

36 Dijkstra Example 1 Extract min, vertex f. S={f}. Update shorter paths. 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 INF,NIL 0,NIL INF,NIL

37 Dijkstra Example 2 Extract min, vertex c. S={fc}. Update shorter paths. 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 15,f INF,NIL 0,NIL 2,f 4,f 15,f INF,NIL

38 Dijkstra Example 3 Extract min, vertex d. S={fcd}. Update shorter paths (None) 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f 6,c

39 Dijkstra Example 4 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f Extract min, vertex a. S={fcda}. Update shorter paths (None) Extract min, vertex b. S={fcdab}. Update shorter paths. 6,c

40 Dijkstra Example 5 Extract min, vertex h. S={fcdabh}. Update shorter paths 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 13,b 0,NIL 2,f 3,c 15,f 6,c

41 Dijkstra Example 6 Extract min, vertex g and h – nothing to update, done! 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

42 Dijkstra Example 7 Can follow parent “pointers” to get the path 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

43 Dijkstra vs. Prim’s MST Very similar to Prim’s MST algorithm, but the two compute different things. Not the shortest path in MST, but picks shortest edge overall to connect all edges –(what case would the MST pick a different edge than Dijkstra?)

44 Dijkstra’s Runtime Using a linear array for the queue, with the index as the vertex number, contents of array as it’s distance value O(V) O(V) to scan through array O(E) over entire while loop O(1) - direct access via array index Total runtime = O(V) + O(V 2 ) + O(E) = O(V 2 )

45 Dijkstra’s Runtime Using a min heap for the queue O(V) O(lgV) O(E) over entire while loop O(lgV) - must update heap key Total runtime = O(V) + O(VlgV) + O(ElgV) = O(ElgV) in a connected graph What if the graph is fully connected? O(V 2 lgV) Actually slower than the simple array-based queue for dense graphs!

46 All Pairs Shortest Path Finding the shortest path between all pairs of vertices One solution is to run Dijkstra’s Algorithm for every vertex. This will require time O(V)(Time-Dijkstra). –If using a linear array for Q, this is time O(V 3 ). –There are other ways to solve this problem using dynamic programming which we will examine next!


Download ppt "More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms."

Similar presentations


Ads by Google