You are on page 1of 15

Discussion #35

Dijkstras Algorithm

Discussion #35

Chapter 7, Section 5.5

1/15

Topics
Shortest path
Greedy algorithms
Dijkstras algorithm

Discussion #35

Chapter 7, Section 5.5

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

Chapter 7, Section 5.5

3/15

Shortest Path in Weighted Graphs


Weights on edges: positive numbers
Algorithms
Simple BFS no longer works (e.g. the
shortest path from a to b is not the
edge that connects them)
Floyds, O(n3); finds all

b
5

2
2

a
1

10

Is there an algorithm that beats


Floyds?
for the single best path?
for all paths?
Discussion #35

Chapter 7, Section 5.5

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

1. Initialize D (distance) table.


2. Until all nodes are settled,
2a. find smallest distance & settle node.
2b. adjust distances in D for unsettled nodes.
Discussion #35

Chapter 7, Section 5.5

5/15

Important Aside: Greedy Algorithms


A greedy algorithm always takes the best
immediate or local solution while finding an
answer.
Greedy algorithms find optimal solutions for some
optimization problems, but may find (far) lessthan-optimal solutions for other optimization
problems.
Dijkstras algorithm is greedy.
There is no greedy algorithm for the traveling salesman
problem (find the shortest path to visit all nodes).

When greedy algorithms work, they are usually


best.
Discussion #35

Chapter 7, Section 5.5

6/15

Trace of Dijkstras Algorithm

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

Circled numbers: shortest path lengths from a.


Discussion #35

Chapter 7, Section 5.5

7/15

Trace with Different Weights


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/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

Circled numbers: shortest path lengths from a.


Discussion #35

Chapter 7, Section 5.5

8/15

Proof that Dijkstras Algorithm Works


Loop Invariant: For each settled node x, the
minimum distance from the start node s to x is
D(x).
Additional observations (which help us in the
proof) are also loop invariants.
(a) The shortest path from s to any settled node v
consists only of settled nodes.
(b) The shortest path from s to any unsettled node
y is at least the largest settled difference so far.

Discussion #35

Chapter 7, Section 5.5

9/15

Proof by Induction
(on the number of iterations)

Basis: 0 iterations: x = s, D(s) = 0 (which is the


minimum since all weights are positive)
For (a): initially s = v, the only settled node
For (b): initially all nodes except s are unsettled, and
the shortest path to each is at least 0, indeed > 0.

Induction: Assume that the main loop invariant


and the loop invariants (a) and (b) hold for k
iterations and show that they hold for k+1
iterations.
Discussion #35

Chapter 7, Section 5.5

10/15

Proof of Induction Part (Sketch)


k settled

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

chooses x, the path from s to x is the shortest using


settled nodes, and the path from s to y is no shorter than
s to x. Since all weights are positive, the path from y to
x is at least 1, so that the assumed shorter path cannot
exist. Further (a) and (b) continue to hold: (a) After
settling x, the path from s to x consists of only settled
nodes. (b) The adjustments are only for paths from x to
any unsettled nodes. Since the distance from s to any
unsettled node y is at least D(x), and D(x) is the largest
settled distance so far.
Chapter 7, Section 5.5

11/15

Big-Oh for Dijkstras Algorithm


Adjacency List:

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)

Unroll the loop:


(b,2)

(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

Over all iterations, the ks add up to 2(m # of edges in the initialization).


Chapter 7, Section 5.5

12/15

Big-Oh for Dijkstras Algorithm


Adjacency List:

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.

Clever using POTs:


1.
2.
O(mlogn)

O(nlogn) POT (Partially Ordered Tree) construction dominates


O(n) each node needs to be settled
2a.
O(logn) swap and bubble down
2b.
O(klogn) for chosen node, go through list of length k

dominates
Discussion #35

Over all iterations, the ks add up to 2(m # of edges in the initialization).


Chapter 7, Section 5.5

13/15

POT Construction/Manipulation
b
5

POT construction (start = a)


2

10

(b,5)

(b,5)
(c,2)

(c,2)
(b,5)

(d,1)

(d,1)

(b,5) (d,1) (b,5) (c,2)


(b,5) (c,2)

1
1

(c,2)

(e,)
e

Find smallest always on top; then swap and bubble down


no
swap
bubble down
adjust
bubbling
(d,1)
(e,)
(c,2)
(c,2)
(c,2)

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

Chapter 7, Section 5.5

14/15

Dijkstras Algorithm vs. Floyds


Dijkstras algorithm is O(mlogn) = O(n2logn) in the
worst case.
Floyds algorithm is O(n3), but computes all shortest
paths.
Dijkstras algorithm can compute all shortest paths
by starting it n times, once for each node. Thus, to
compute all paths, Dijkstras algorithm is O(nmlogn)
= O(n3logn) in the worst case.
Which is better depends on the application.
Dijkstras algorithm is better if only a path from a single
node to another or to all others is needed.
For all paths, Dijkstras algorithm is better for sparse
graphs, and Floyds algorithm is better for dense graphs.
Discussion #35

Chapter 7, Section 5.5

15/15

You might also like