Professional Documents
Culture Documents
Dijkstras Algorithm
Discussion #35
1/15
Topics
Shortest path
Greedy algorithms
Dijkstras algorithm
Discussion #35
2/15
Shortest Path
Minimum length path from one
node to another (e.g. from a to
e, the shortest path is 2)
Algorithms
Breadth-First-Search, O(n2) in
the worst case
Floyds, O(n3); but finds all
BFS, starting at each node,
O(nm), which beats Floyds for
sparse graphs
Discussion #35
3/15
b
5
2
2
a
1
10
4/15
Dijkstras Algorithm
Find the shortest weighted path
between two given nodes.
(Easier to find the minimum
from one to all.)
Intuitively, special BFS: choose
smallest accumulated path length
Algorithm
b
5
2
2
10
5/15
6/15
1. Initialize D (start=a)
2. Until all nodes are settled:
-find smallest distance & settle node.
-adjust distances in D for unsettled nodes.
iteration
adjust w/d
adjust w/c
adjust w/e
0
1
2
3
4
a
0
b
5
5
54
4
4
c
2
2
2
settled
d
1
1
2
2
2
10
distance
1
1
a
a,d
a,d,c
a,d,c,e
a,d,c,e,b
all nodes settled
7/15
iteration
adjust w/d
adjust w/e
adjust w/c
0
1
2
3
4
b
6
6
6
65
5
c
4
4
43
3
d
1
1
2
2
10
settled
2
4
distance
a
0
1
1
a
a,d
a,d,e
a,d,e,c
a,d,e,c,b
all nodes settled
8/15
Discussion #35
9/15
Proof by Induction
(on the number of iterations)
10/15
Assume Dijkstras
algorithm selects x
to be settled.
s...vx
k+1 settled
s...vx
Assume that the main loop invariant does not hold for the k+1st iteration, then there
is a shorter path from s to x. By the induction hypotheses, since Dijkstras algorithm
s...vx
Discussion #35
11/15
b
5
2
2
10
D(x)
1
1
(b,5)
(c,2)
(a,5)
(c,2)
(a,2)
+ n + k2 +(e,1)
+ n + k(n-1)
(a,1)n + k1 (c,10)
(d,1)
(d,1)
(d,10)
(e,1)
(c,1)
1. Initialize D (start=a)
= (n1)n = O(n2)
2. Until all nodes are settled:
2a. find smallest distance
node. in the
= 2(m&settle
# of edges
2b. adjust distances in D for unsettled
nodes. = O(m)
initialization)
1.
2.
O(n2)
O(n) visit at most all edges for a node cant be more than n
O(n) each node needs to be settled
2a.
O(n) look through D(x) list to find smallest
2b.
O(k) for chosen node, go through list of length k
dominates
Discussion #35
12/15
b
5
2
2
10
D(x)
1
1
(b,5)
(c,2)
(a,5)
(c,2)
(a,2)
(b,2)
(d,10)
(a,1)
(c,10)
(e,1)
(d,1)
(c,1)
(d,1)
(e,1)
1. Initialize D (start=a)
2. Until all nodes are settled:
2a. find smallest distance & settle node.
2b. adjust distances in D for unsettled nodes.
dominates
Discussion #35
13/15
POT Construction/Manipulation
b
5
10
(b,5)
(b,5)
(c,2)
(c,2)
(b,5)
(d,1)
(d,1)
1
1
(c,2)
(e,)
e
needed
(b,5) (c,2)
(b,5) (e,) (b,5) (e,2)
(b,5) (c,2)
(b,5) (e,2)
(e,)
(d,1)
Adjust distances POT doubly linked to adjacency list nodes bubble up and
down, as needed, when going through list of chosen node to adjust values
Discussion #35
14/15
15/15