13-3 - Shortest Path Problem For weighted graphs it is...

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

View Full Document Right Arrow Icon
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that has the smallest sum of its edge weights Dijkstra’s algorithm determines the shortest path between a given vertex and all other vertices The algorithm is named after its discoverer, Edgser Dijkstra it is assumed that weight of edges are positive 4 1 3 5 8 2 2 5 1 1 The shortest path between B and G is: B–D–E–F–G and not B–G (or B–A–E–F–G) B A C D E G F
Background image of page 1

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

View Full DocumentRight Arrow Icon
Dijkstra’s Algorithm Finds the shortest path to all nodes from the start node Performs a modified BFS that accounts for cost of vertices The cost of a vertex (to reach a start vertex) is weight of the shortest path from the start node to the vertex using only the vertices which are already visited (except for the last one) Selects the node with the least cost from unvisited vertices In an unweighted graph (weight of each edge is 1) this reduces to a BFS To be able to quickly find an unvisited vertex with the smallest cost, we will use a priority queue (highest priority = smallest cost) When we visit a vertex and remove it from the queue, we might need to update the cost of some vertices (neighbors of the vertex) in queue (decrease it) The shortest path to any node can be found by backtracking in the results array (the results array contains, for each node, the minumum cost and a “parent” node from which one can get to this node achieving the minimum cost)
Background image of page 2
Dijkstra’s Algorithm – Initialization Initialization – insert all vertices in a priority queue ( PQ ) Set the cost of the start vertex to zero Set the costs of all other vertices to infinity and their parent vertices to the start node Note that because the cost to reach the start vertex is zero it will be at the head of the PQ Special requirement on priority queue PQ : we can use min-heap a cost of an item (vertex) can decrease, and in such case we need to bubble-up the item (in time O(log n) ) another complication is that we need to locate the item in the queue
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/26/2010 for the course CMPT 225 taught by Professor Annelavergne during the Spring '07 term at Simon Fraser.

Page1 / 16

13-3 - Shortest Path Problem For weighted graphs it is...

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

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