{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

9_graphs2.v3

# 9_graphs2.v3 - CS161 Minimum Spanning Trees and Single...

This preview shows pages 1–3. Sign up to view the full content.

CS161 - Minimum Spanning Trees and Single Source Shortest Paths David Kauchak Single Source Shortest Paths Given a graph G and two vertices s,t what is the shortest path from s to t ? For an unweighted graph, BFS gives us a solution to this problem. For weighted graphs, as it turns out, we can calculate the shortest distance from s to all vertices t V in worst case the same amount of time for any particular t , so we’ll look at this problem, which is the single source shortest paths. Shortest path property If the path v 1 ,v 2 ,v 3 ,...,v k where v i V is the shortest path from v 1 to v k then for all 1 i j k , v i ,v i +1 ,...,v j is the shortest path from v i to v j Proof: Consider that a shorter path exists between v i and v j , then we could use this path instead of the path v i ,v i +1 ,...,v j in the path from v 1 to v k , resulting in a shorter path from v 1 to v k , but this is a contradiction. General idea for all the algorithms mark each vertex with an upper bound on the distance from the source to that node. Decrease that value until it is correct. Dijkstra’s algorithm Assume that all of the weights are positive 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Like BFS, exept our frontier that we expand is based on the weights of the edges not the number of edges Dijkstra ( G,s ) 1 for all v V 2 dist [ v ] ←∞ 3 prev [ v ] null 4 dist [ s ] 0 5 Q MakeHeap ( V ) 6 while ! Empty ( Q ) 7 u ExtractMin ( Q ) 8 for all edges ( u,v ) E 9 if dist [ v ] > dist [ u ] + w ( u,v ) 10 dist [ v ] dist [ u ] + w ( u,v ) 11 DecreaseKey ( Q,v,dist [ v ]) 12 prev [ v ] u Example Why doesn’t this hold with negative weights? Consider the graph: A B : 1 ,C : 10 B D : 1 C D : - 10 D E : 5 What is the shortest path from A to E ? Is it correct? Invariant: For every vertex that has been visited/removed from the heap, dist [ v ] is the actual shortest distance from s to v The only time a vertex u gets visited is when the distance from s to that vertex is smaller than any remaining vertex. In addition,
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}