0% found this document useful (0 votes)
12 views14 pages

Dijkstra

The document discusses the concept of shortest paths in weighted directed graphs, including definitions, types of shortest-path problems, and relevant theorems such as optimal substructure and triangle inequality. It introduces Dijkstra's algorithm for finding the shortest path from a source vertex to all other vertices, detailing its correctness and running time based on different data structures. Applications of shortest-path algorithms include network routing, robot motion planning, and traffic map generation.

Uploaded by

vermakirti022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Dijkstra

The document discusses the concept of shortest paths in weighted directed graphs, including definitions, types of shortest-path problems, and relevant theorems such as optimal substructure and triangle inequality. It introduces Dijkstra's algorithm for finding the shortest path from a source vertex to all other vertices, detailing its correctness and running time based on different data structures. Applications of shortest-path algorithms include network routing, robot motion planning, and traffic map generation.

Uploaded by

vermakirti022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Shortest Path

• Generalize distance to weighted setting


• Digraph G = (V,E) with weight function W: E → R
(assigning real values to edges)
• Weight of path p = v1 → v2 → … → vk is
k −1
w( p) =  w(vi , vi +1 )
i =1
• Shortest path = a path of the minimum weight
• Applications
– static/dynamic network routing
– robot motion planning
– map/route generation in traffic

February 8, 2003 1
Shortest-Path Problems
• Shortest-Path problems
– Single-source (single-destination). Find a shortest path
from a given source (vertex s) to each of the vertices. The
topic of this lecture.
– Single-pair. Given two vertices, find a shortest path
between them. Solution to single-source problem solves
this problem efficiently, too.
– All-pairs. Find shortest-paths for every pair of vertices.
Dynamic programming algorithm.
– Unweighted shortest-paths – BFS.

February 8, 2003 2
Optimal Substructure
• Theorem: subpaths of shortest paths are
shortest paths
• Proof (cut and paste)
– if some subpath were not the shortest path, one
could substitute the shorter subpath and create a
shorter total path

February 8, 2003 3
Triangle Inequality
• Definition
– (u,v)  weight of a shortest path from u to v
• Theorem
– (u,v)  (u,x) + (x,v) for any x
• Proof
– shortest path u v is no longer than any other path u v –
in particular, the path concatenating the shortest path u x
with the shortest path x v

February 8, 2003 4
Negative Weights and Cycles?
• Negative edges are OK, as long as there are no
negative weight cycles (otherwise paths with
arbitrary small “lengths” would be possible)
• Shortest-paths can have no cycles (otherwise
we could improve them by removing cycles)
– Any shortest-path in graph G can be no longer
than n – 1 edges, where n is the number of
vertices

February 8, 2003 5
Relaxation
• For each vertex in the graph, we maintain d[v], the
estimate of the shortest path from s, initialized to 
at start
• Relaxing an edge (u,v) means testing whether we can
improve the shortest path to v found so far by going
through u
Relax (u,v,w)
u v u v
2 2 if d[v] > d[u]+w(u,v)then
    d[v]  d[u]+w(u,v)
[v]  u
Relax(u,v) Relax(u,v)

 2   2 
u v u v
February 8, 2003 6
Dijkstra's Algorithm
• Non-negative edge weights
• Greedy, similar to Prim's algorithm for MST
• Like breadth-first search (if all weights = 1, one can
simply use BFS)
• Use Q, priority queue keyed by d[v] (BFS used FIFO
queue, here we use a PQ, which is re-organized
whenever some d decreases)
• Basic idea
– maintain a set S of solved vertices
– at each step select "closest" vertex u, add it to S, and relax
all edges from u

February 8, 2003 7
Dijkstra’s Pseudo Code
• Graph G, weight function w, root s

relaxing
edges

February 8, 2003 8
Dijkstra’s Example
u v u v
1 1
10   10  
9 9
2 3 2 3
s  4 6 s  4 6
7 7
5 5
 2   2 
x y x y
u v u v
1 1
10   10  
9 9
2 3
s  2 3
4 6
s  4 6
7 7
5 5
   2 
2
x y x y

February 8, 2003 9
Dijkstra’s Example (2)
u v u v
1 1
10   10  
9 9
2 3 2 3
 4 6  4 6
7 7
5 5
 2   2 
x y x y

• Observe
– relaxation step (lines 10-11)
– setting d[v] updates Q (needs Decrease-Key)
– similar to Prim's MST algorithm

February 8, 2003 10
Dijkstra’s Correctness
• We will prove that whenever u is added to S, d[u] =
(s,u), i.e., that d is minimum, and that equality is
maintained thereafter
• Proof
– Note that v, d[v]  (s,v)
– Let u be the first vertex picked such that there is a shorter
path than d[u], i.e., that  d[u]  (s,u)
– We will show that this assumption leads to a contradiction

February 8, 2003 11
Dijkstra Correctness (2)
• Let y be the first vertex V – S on the actual shortest
path from s to u, then it must be that d[y] = (s,y)
because
– d[x] is set correctly for y's predecessor x S on the shortest
path (by choice of u as the first vertex for which d is set
incorrectly)
– when the algorithm inserted x into S, it relaxed the edge
(x,y), assigning d[y] the correct value

February 8, 2003 12
Dijkstra Correctness (3)

d [u ]  ( s , u ) (initial assumption)
= ( s , y ) + ( y , u ) (optimal substructure)
= d [ y ] + ( y , u ) (correctness of d [ y ])
 d[ y] (no negative weights)

• But d[u] > d[y]  algorithm would have chosen y


(from the PQ) to process next, not u  Contradiction
• Thus d[u] = (s,u) at time of insertion of u into S, and
Dijkstra's algorithm is correct

February 8, 2003 13
Dijkstra’s Running Time
• Extract-Min executed |V| time
• Decrease-Key executed |E| time
• Time = |V| TExtract-Min + |E| TDecrease-Key
• T depends on different Q implementations

Q T(Extract T(Decrease- Total


-Min) Key)
array ( V) (1) (V 2 )
binary heap (lg V) (lg V) (E lg V)
Fibonacci heap (lg V) (1) (amort.) (V lgV + E)

February 8, 2003 14

You might also like