0% found this document useful (0 votes)
69 views11 pages

CS 332: Algorithms: S-S Shortest Path: Dijkstra's Algorithm Disjoint-Set Union Amortized Analysis

This document discusses algorithms for shortest path problems: 1) Dijkstra's algorithm finds the shortest paths from a single source node to all other nodes in a graph with non-negative edge weights. It uses a priority queue to iteratively relax edges and works in O(ElogV) time. 2) Bellman-Ford algorithm can handle graphs with negative edge weights but runs slower in O(VE) time, making multiple passes to relax all edges. 3) Shortest paths in directed acyclic graphs (DAGs) can be found in O(V+E) time by relaxing edges in topological sort order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views11 pages

CS 332: Algorithms: S-S Shortest Path: Dijkstra's Algorithm Disjoint-Set Union Amortized Analysis

This document discusses algorithms for shortest path problems: 1) Dijkstra's algorithm finds the shortest paths from a single source node to all other nodes in a graph with non-negative edge weights. It uses a priority queue to iteratively relax edges and works in O(ElogV) time. 2) Bellman-Ford algorithm can handle graphs with negative edge weights but runs slower in O(VE) time, making multiple passes to relax all edges. 3) Shortest paths in directed acyclic graphs (DAGs) can be found in O(V+E) time by relaxing edges in topological sort order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

CS 332: Algorithms

S-S Shortest Path: Dijkstras Algorithm


Disjoint-Set Union
Amortized Analysis

Review:
Single-Source Shortest Path
Problem: given a weighted directed graph G,

find the minimum-weight path from a given


source vertex s to another vertex v
Shortest-path = minimum weight
Weight of path is sum of edges
E.g., a road map: what is the shortest path from

Chapel Hill to Charlottesville?

Review: Shortest Path Properties


Optimal substructure: the shortest path

consists of shortest subpaths


Let (u,v) be the weight of the shortest path
from u to v. Shortest paths satisfy the triangle
inequality: (u,v) (u,x) + (x,v)
In graphs with negative weight cycles, some
shortest paths will not exist

Review: Relaxation
Key technique: relaxation
Maintain upper bound d[v] on (s,v):
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}
5

Relax
5

Relax
7

Review: Bellman-Ford Algorithm


BellmanFord()
for each v V
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v) E
Relax(u,v, w(u,v));
for each edge (u,v) E
if (d[v] > d[u] + w(u,v))
return no solution;

Initialize d[], which


will converge to
shortest-path value
Relaxation:
Make |V|-1 passes,
relaxing each edge
Test for solution:
have we converged yet?
Ie, negative cycle?

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

Review: Bellman-Ford Algorithm


BellmanFord()
for each v V
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v) E
Relax(u,v, w(u,v));
for each edge (u,v) E
if (d[v] > d[u] + w(u,v))
return no solution;

What will be the


running time?

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

Review: Bellman-Ford
Running time: O(VE)
Not so good for large dense graphs
But a very practical algorithm in many ways
Note that order in which edges are processed affects

how quickly it converges (show example)

DAG Shortest Paths


Problem: finding shortest paths in DAG
Bellman-Ford takes O(VE) time.
How can we do better?
Idea: use topological sort. How does it work again?
If were lucky and processes vertices on each shortest path from left

to right, would be done in one pass


Every path in a dag is subsequence of topologically sorted vertex
order, so processing verts in that order, we will do each path in
forward order (will never relax edges out of vert before doing all
edges into vert).
Thus: just one pass. What will be the running time?

Dijkstras Algorithm
If no negative edge weights, we can beat BF
Similar to breadth-first search
Grow a tree gradually, advancing from vertices

taken from a queue


Also similar to Prims algorithm for MST
Use a priority queue keyed on d[v]

Dijkstras Algorithm
Dijkstra(G)
B
2
10
for each v V
4
3
A
D
d[v] = ;
d[s] = 0; S = ; Q = V;
5
1
C
while (Q )
u = ExtractMin(Q); Ex: run the algorithm
S = S U {u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
Relaxation
Note: this
d[v] = d[u]+w(u,v);
Step
is really a
call to Q->DecreaseKey()

The End

You might also like