CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms
Today Shortest-Path Algorithms – Unweighted Shortest Paths Dijkstra’s Algorithm
Single-Source Shortest Path Problem: Given as input a weighted graph G = (V,E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. Example:
Edges of Negative Costs Edges of negative costs can cause problem. Example: Consider a path from v 5 to v 4. What happens to shortest paths when a negative-cost cycle is present in a graph? In the absence of negative-cost cycles, the shortest path from s to s is zero.
Unweighted Shortest Paths We may say that all edges have weight 1.
Illustration Say initial vertex is v 3 : After finding all vertices whose path length from s is 1:
Illustration After finding all vertices whose path length from s is 2: Final shortest paths:
Breadth-first Search This strategy for searching a graph is known as breadth-first search. It operates by processing the vertices in layers: the vertices closest to the start are evaluated first, and the most distant vertices are evaluated last. What is the equivalent traversal scheme for trees?
Initial Configuration of Table
Pseudocode Running time of this algorithm?
Inefficiency Here is an example: How to remove this efficiency? Use a queue.
Improved Pseudocode
Example
Weighted Shortest Paths Dijkstra’s algorithm Greedy algorithm: solves a problem in stages by doing what appears to be the best choice at each stage.
Dijkstra’s Algorithm Initial configuration:After v 1 is declared known:
Dijkstra’s Algorithm After v 4 is declared known:After v 2 is declared known:
Dijkstra’s Algorithm After v 5 and v 3 are known:After v 7 is declared known:
Dijkstra’s Algorithm After v 6 is declared known and algorithm terminates:
Stages of Dijkstra’s Algorithm
Pseudocode of Dijkstra’s Algorithm Running time?