9_graphs2.v3

9_graphs2.v3 - CS161 Minimum Spanning Trees and Single Source Shortest Paths David Kauchak Single Source Shortest Paths Given a graph G and two

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/25/2011.

Page1 / 8

9_graphs2.v3 - CS161 Minimum Spanning Trees and Single Source Shortest Paths David Kauchak Single Source Shortest Paths Given a graph G and two

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online