Download presentation
Presentation is loading. Please wait.
1
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms
2
Today Shortest-Path Algorithms – Unweighted Shortest Paths Dijkstra’s Algorithm
3
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:
4
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.
5
Unweighted Shortest Paths We may say that all edges have weight 1.
6
Illustration Say initial vertex is v 3 : After finding all vertices whose path length from s is 1:
7
Illustration After finding all vertices whose path length from s is 2: Final shortest paths:
8
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?
9
Initial Configuration of Table
10
Pseudocode Running time of this algorithm?
11
Inefficiency Here is an example: How to remove this efficiency? Use a queue.
12
Improved Pseudocode
13
Example
14
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.
15
Dijkstra’s Algorithm Initial configuration:After v 1 is declared known:
16
Dijkstra’s Algorithm After v 4 is declared known:After v 2 is declared known:
17
Dijkstra’s Algorithm After v 5 and v 3 are known:After v 7 is declared known:
18
Dijkstra’s Algorithm After v 6 is declared known and algorithm terminates:
19
Stages of Dijkstra’s Algorithm
20
Pseudocode of Dijkstra’s Algorithm Running time?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.