COMP20007 Design of Algorithms: Greedy Algorithms: Prim and Dijkstra
COMP20007 Design of Algorithms: Greedy Algorithms: Prim and Dijkstra
Lars Kulik
Lecture 8
Semester 1, 2022
1
Greedy Algorithms
This greedy strategy will work for the given denominations, but not
for, say, 25, 10, 1. 2
Greedy Algorithms
3
The Priority Queue
4
Stacks and Queues as Priority Queues
5
Possible Implementations of the Priority Queue
Inject(e) Eject()
6
Spanning Trees
7
Minimum Spanning Trees of Weighted Graphs
4 1 3
5 2 4
b d f
2 4
8
Minimum Spanning Trees
6 5
a c e
4 1 3
5 2 4
b d f
2 4
9
Minimum Spanning Trees: Prim’s Algorithm
function Prim(hV , E i)
VT ← {v0 }
ET ← ∅
for i ← 1 to |V | − 1 do
find a minimum-weight edge (v , u) ∈ VT × (V \ VT )
VT ← VT ∪ {u}
ET ← ET ∪ {(v , u)}
return ET
10
Prim’s Algorithm
Or, we can say that the tree grows to include the node from
outside that has the smallest cost.
A standard way to do this is to organise the nodes that are not yet
included in the spanning tree T as a priority queue organised by
edge cost.
11
Prim’s Algorithm
function Prim(hV , E i)
for each v ∈ V do
cost[v ] ← ∞
prev [v ] ← nil
pick initial node v0
cost[v0 ] ← 0
Q ← InitPriorityQueue(V ) ⊲ priorities are cost values
while Q is non-empty do
u ← EjectMin(Q)
for each (u, w ) ∈ E do
if w ∈ Q and weight(u, w ) < cost[w ] then
cost[w ] ← weight(u, w )
prev [w ] ← u
Update(Q, w , cost[w ]) ⊲ rearranges priority queue
12
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
a, d 2/d 2/d ∞/nil 4/d
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
a, d 2/d 2/d ∞/nil 4/d
a, d, b 1/b ∞/nil 4/d
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
a, d 2/d 2/d ∞/nil 4/d
a, d, b 1/b ∞/nil 4/d
a, d, b, c 5/c 3/c
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
a, d 2/d 2/d ∞/nil 4/d
a, d, b 1/b ∞/nil 4/d
a, d, b, c 5/c 3/c
a, d, b, c, f 4/f
13
Prim’s Algorithm: Example
6 5
a c e
4 1 3
5 2 4
b d f
2 4
Tree T a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a 5/a 6/a 4/a ∞/nil ∞/nil
a, d 2/d 2/d ∞/nil 4/d
a, d, b 1/b ∞/nil 4/d
a, d, b, c 5/c 3/c
a, d, b, c, f 4/f
a, d, b, c, f , e
13
Analysis of Prim’s Algorithm
First, a crude analysis: For each node, we look through the edges
to find those incident to the node, and pick the one with smallest
cost. Thus we get O(|V | · |E |). However, we are using cleverer
data structures.
Using adjacency lists for the graph and a min-heap for the priority
queue, we can do better! We will discuss this later.
14
Dijkstra’s Algorithm
15
Dijkstra’s Algorithm
function Dijkstra(hV , E i, v0 )
for each v ∈ V do
dist[v ] ← ∞
prev [v ] ← nil
dist[v0 ] ← 0
Q ← InitPriorityQueue(V ) ⊲ priorities are distances
while Q is non-empty do
u ← EjectMin(Q)
for each (u, w ) ∈ E do
if w ∈ Q and dist[u] + weight(u, w ) < dist[w ] then
dist[w ] ← dist[u] + weight(u, w )
prev [w ] ← u
Update(Q, w , dist[w ]) ⊲ rearranges priority queue
16
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
a, d 3/d 2/d 5/d 6/d
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
a, d 3/d 2/d 5/d 6/d
a, d, c 3/d 4/c 5/c
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
a, d 3/d 2/d 5/d 6/d
a, d, c 3/d 4/c 5/c
a, d, c, b 4/c 5/c
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
a, d 3/d 2/d 5/d 6/d
a, d, c 3/d 4/c 5/c
a, d, c, b 4/c 5/c
a, d, c, b, e 5/c
17
Dijkstra’s Algorithm: Example
4 2
a c e
3
1 1 1
1
4
b d f
2 5
Covered a b c d e f
− 0/nil ∞/nil ∞/nil ∞/nil ∞/nil ∞/nil
a ∞/nil 4/a 1/a ∞/nil ∞/nil
a, d 3/d 2/d 5/d 6/d
a, d, c 3/d 4/c 5/c
a, d, c, b 4/c 5/c
a, d, c, b, e 5/c
a, d, c, b, e, f
17
Dijkstra’s Algorithm: Tracing Paths
The array prev is not really needed, unless we want to retrace the
shortest paths from node a:
2
a c e
3
1
1
b d f
2
18
Negative Weights
19