Lec06 Greedy Algorithms (Part 1)
Lec06 Greedy Algorithms (Part 1)
Problem:
A dollar amount to reach and a collection of coin
amounts to use to get there.
Configuration:
A dollar amount yet to return to a customer plus the
coins already returned
Objective function:
Minimize number of coins returned.
Example: Making Change
a $5 bill
a $1 bill, to make $6
no 25¢ coin
three 10¢ coin, to make $6.30
no 1¢ coin
P2 18 11 6
P3 15 14 5
What would be the result if you ran the shortest job first?
Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18,
and 20 minutes
P1 3 10 15
P2 5 11 18
P3 6 14 20
P1 20 14
P2 18 11 5
P3 15 10 6 3
If (weight[n-1] > W)
Return Knapsack(W, weight[], values[], n-1
Else
Return Knapsack(Values[n-1]+Knapsack(W-
weight[n-1], weight[], values[], n-1)
Knapsack(W, weight[], values[],n-1)
19
The Fractional Knapsack Problem
Constraint: x
iS
i W
Example
What item(s) to
choose?
Items:
1 2 3 4 5
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml
Benefit: $12 $32 $40 $30 $50 10 ml
Value per 3 4 20 5 50
ml
Fractional Knapsack Algorithm
Product P1 P2 P3 P4 P5 P6
Weight 8 kg 6 kg 11 kg 7 kg 9 kg 12 kg
Benefit $60 $42 $44 $63 $45 $42
Value per 7.5 7 4 9 5 3.5
kg
SHORTEST PATHS
Outline
Example:
Shortest path between Providence and Honolulu?
849 2 PVD
1843 ORD 1 4
SFO
802
1205
43 LGA
337
17 87 10
2555 1 3
HNL 1233 99
LAX DFW 1120
MIA
Shortest Path Properties
Property 1:
A subpath of a shortest path is itself a shortest path
Property 2:
There is a tree of shortest paths from a start vertex
to all the other vertices
Example:
Tree of shortest paths from Providence
Dijkstra’s Algorithm
To remove cycle
Consider an edge e = (u,z) such that
u is the vertex most recently added to the cloud
z is not in the cloud
The relaxation of edge e updates distance d(z) as
follows:
d(z) min{d(z),d(u) + weight(e)}
Dijkstra’s Algorithm
Q.replaceKey(getLocator(z),r)
Analysis
Graph operations
Method incidentEdges is called once for each vertex
Label operations
Set/get the distance and locator labels of vertex z O(deg(z)) times
Setting/getting a label takes O(1) time
Priority queue operations
Each vertex is inserted once into and removed once from the
priority queue, where each insertion or removal takes O(log n) time
The key of a vertex in the priority queue is modified at most deg(w)
times, where each key change takes O(log n) time
Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is
represented by the adjacency list structure
Recall that ∑vdeg(v) = 2m
The running time can also be expressed as O(m log n) since the
graph is connected
previously discovered path
and weight
relaxed edge
Dijkstra’s Algorithm Example
d()
8 A 4
2
d() d() d()
7 1
B C D
d() 3 9 d()
2 5
E F
0 From source vertex, visit all
8 A 4
neighbours and update the
2
8 2 4 weights d
7 1
B C D
3 9
2 5
E F
0
Choose vertex with
8 A 4 smallest weight to proceed
8
2
2 3
Repeat the process and
7 1
B C D update weights of
3 9 neighbours
5 11
2 5
E F If new path has smaller
total weight, update and
set old path as relaxed
edge
0 0
8 A 4 A 4
8
2 2
8 7 2 1 3 7 2 3
B C D 7 1
B C D
5 3 9 11 3 9
5 8
2 5 2 5
E F E F
0
0
8 A 4
8 A 4
2
8 2 3 2
7 1 7 2 3
B C D 7 1
B C D
5 3 9 8 3 9
2 5 5 8
E F 2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D
3 9
5 8
2 5
E F
Why Dijkstra’s Algorithm Works
5 0 -8 9
2 5
E F