MST & Shortest Path -Prim s -Djikstra s
PRIM s - Minimum Spanning Tree - A spanning tree of a graph is a tree that has all the vertices of the graph connected by some edges. - A graph can have one or more number of spanning trees. - If the graph has N vertices then the spanning tree will have N-1 edges.
Pseudocode: MarkedSet = {A}; (0 or starting vertex) UnMarkedSet = {B,C,D}; (1 to N-1 where N is the total number of vertices and 0 is the starting(marked) vertex) while( UnMarkedSet!= empty) { Find edge e = (x,y) such that: 1. x ϵ MarkedSet. y ϵ UnMarkedSet. e has the smallest weight SpanningTree = Spanningtree U {e}; } MarkedSet = MarkedSet U {y}; UnMarkedSet = UnMarkedSet {y};
Here is our Graph 4 B 11 D 4 5 5 8 A 10 C 1
Step 1: Remove all loops 4 B 11 D 4 5 5 8 A 10 C 1
Step 1: Remove all loops B 11 D 4 5 5 A 10 C 1
Step : Remove all parallel edges connecting the same vertices except for the least weighted one. B 11 D 4 5 5 A 10 C 1
Step : Remove all parallel edges connecting the same vertices except for the least weighted one. B 11 D 4 5 5 A 10 C
Step : We are ready to create our MST. Draw a table with rows = columns = number of vertices B 11 D 4 5 5 A 10 C
Step : We are ready to create our MST. Draw a table with rows = columns = number of vertices B 11 D A A B C D 4 5 5 B C A 10 C D
Step 4: Put 0 in cells having the same row and column name B 11 D A 0 A B C D 4 5 5 B 0 C 0 A 10 C D 0
Step 5: Find the edge directly connecting vertex A to other vertices and fill up the table values for cells AB as well as BA B 11 D A B C D A 0 5 4 5 5 B 5 0 C 0 A 10 C D 0
Step 5: Similarly for AC and CA. B 11 D A B C D A 0 5 10 4 5 5 B 5 0 C 10 0 A 10 C D 0
Step 5: The cell AD is put as since no direct path from A to D B 11 D A B C D A 0 5 10 4 5 5 B 5 0 C 10 0 A 10 C D 0
Step 5: Fill other rows similarly B 11 D A B C D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step : Find the smallest value in row A except for 0 and mark it as a minimum edge for both vertices that it connects to. B 11 D A B C D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step : Find the smallest value in row A except for 0 and mark it as a minimum edge for both vertices that it connects to. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: Now looked for the smallest unmarked value in all rows that have a marked edge. In this case, rows A and B that have marked edge 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: Smallest unmarked value out of all values in rows A and B is 4 corresponding to BC and CB. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: Mark BC = CB = 4 A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: Now look for the smallest unmarked value in all three rows A,B,C since all now have marked values in them. In this case, CD = 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: Mark CD = DC = 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0
Step 7: We have our final Minimum Spanning Tree. B 11 D 4 5 5 A 10 C
Weight of MST = Sum of marked edges. AB + BC + CD = 5 + 4 + 5 = 14 B 11 D 4 5 5 A 10 C
In-class practice Compute the Minimum Spanning Tree using Prim s algorithm for the following graph:
Answer or
Time Complexity: Minimum edge weight data structure Time complexity (total) adjacency matrix, searching O( V ) binary heap and adjacency list O(( V + E ) log V ) = O( E log V ) Fibonacci heap and adjacency list O( E + V log V )
Dijkstra s Shortest Path: - Given: weighted graph, G, and source vertex, v - Compute: shortest path to every other vertex in G - Path length is sum of edge weights along path. Shortest path has smallest length among all possible paths
Algorithm: Grow a collection of vertices for which shortest path is known - paths contain only vertices in the set - add as new vertex the one with the smallest distance to the source - shortest path to an outside vertex must contain a current shortest path as a prefix Use a greedy algorithm
Vertex Relaxation: Maintain value D[u] for each vertex Each starts at infinity, and decreases as we find out about a shorter path from v to u (D[v] = 0) Maintain priority queue, Q, of vertices to be relaxed use D[u] as key for each vertex remove min vertex from Q, and relax its neighbors Relaxation for each neighbor of u: If D[u] + w(u,z) < D[z] then, D[z] = D[u] + w(u,z)
Pseudocode : ShortestPath(G, v) init D array entries to infinity D[v]=0 add all vertices to priority queue Q while Q not empty do u = Q.removeMin() for each neighbor, z, of u in Q do if D[u] + w(u,z) < D[z] then D[z] = D[u] + w(u,z) Change key of z in Q to D[z] return D as shortest path lengths
Example: a 5 c e 7 5 b d 9 f 1 g
Step 1: Draw a table with the set of vertices (v) a 5 c e 7 5 b d 9 f 1 g
Step 1: Draw a table with the set of vertices (v) a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a
Step : Mark all vertices starting from a. Subscript denotes the vertex we connect from. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a
Step : The lowest weight is marked red (shortest weight determined) and the next lowest weight in the row is looked for a in this case which is the weight from a to b. So, the next row in the table starts with b and a marked red. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a
Step : The lowest weight is marked red (shortest weight determined) and the next lowest weight in the row is looked for a in this case which is the weight from a to b. So, the next row in the table starts with b and a marked red. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a
Step : The next adjacent vertex from b is looked for and compared with the weight when directly reached from a a. When coming via vertex b, the total weight is a->b( a )+b->d( b ) = 5b. This new weight will replace the older weight in the d column. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a
Step : The next adjacent vertex from b is looked for and compared with the weight when directly reached from a a. When coming via vertex b, the total weight is a->b( a )+b->d( b ) = 5b. This new weight will replace the older weight in the d column. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b
Step : Cannot reach anywhere else from b, so rest of the weights c, e, f, g are copied down from the first row as they were a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b
Step : Cannot reach anywhere else from b, so rest of the weights c, e, f, g are copied down from the first row as they were a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b
Step 4: Next lowest value is selected. So any of 5a or 5b will be valid. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b
Step 4: Next lowest unmarked weight is selected. So any of 5a or 5b will be valid. a b 5 c d 9 7 5 f 1 e g - c->d cost: 5a+c = 7c which is!< 5b, so weight in column d remains 5b. - c->e cost = 5a+c = 11c which is <, so new weight in column e becomes 11c - Similarly for f and g, new weights are 8c and 1c respectively. - Remember: Subscript is where I m coming from! V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c
Step 5: Next lowest 5b. d->f = 5b+9d = 14d!< 11c. f and g are unreachable so their weights remain as they are. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c
Step : Next lowest 8c. f->e = 8c+5f = 1f!< 11c. f->g = 8c+1f = 9f <1c a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f
Step 7: No more scope for improvement so the last row gives me the shortest path from a to any of the other vertices. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f
Shortest Path weight from a to g is = 9f, where f=8c, where c = 5a. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f
So, the shortest path from a to g would be: a 5 c f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f
In-class practice Compute the Shortest Path using Dijkstra s algorithm from node 0 to every node in the following graph:
Answer
Djikstra Analysis: O(nlogn) time to build priority queue O(nlogn) time removing vertices from queue O(mlogn) time relaxing edges Changing key can be done in O(logn) time Total time: O( (n + m)logn ) which can be O(n logn) for dense graph
Prim s vs Dijkstra s Prim builds up MST for each adjacent vertex so, will have to go through all previous vertices to get to the desired vertex Dijkstra s builds up the MST for the shortest possible path to any of the vertices from the source vertex which may or may not include all vertices occurring before the destination vertex. Train that connects to several cities would then use Prim s Algorithm. But a train that could go from one city to another in the shortest possible time, would use Dijkstra s.