Lecture5 IO BLG336E 2022
Lecture5 IO BLG336E 2022
Analysis of Algorithms II
Lecture 5:
Greedy Algorithms, Interval Scheduling, Interval
Partitioning, Shortest Paths in a Graph (Dijkstra)
1
Graphs-Last week
• There are a lot of graphs.
4
Proving that the Greedy Solution is Optimal
5
Greedy Analysis Strategies
Greedy algorithm stays ahead. Show that after each step of the
greedy algorithm, its solution is at least as good as any other
algorithm's.
6
Example Problems
Interval Scheduling
Interval Partitioning
7
Interval Scheduling
Interval scheduling.
Job j starts at sj and finishes at fj.
Two jobs compatible if they don't overlap.
Goal: find maximum subset of mutually compatible jobs.
g
h
Time
0 1 2 3 4 5 6 7 8 9 10 11
8
Interval Scheduling Example 1
9
Interval Scheduling Example 2
10
Interval Scheduling Example 3
11
Interval Scheduling Example 4
12
Interval Scheduling: Greedy Algorithms
Greedy template. Consider jobs in some order. Take each job provided
it's compatible with the ones already taken.
13
Interval Scheduling: Greedy Algorithms
Greedy template. Consider jobs in some order. Take each job provided
it's compatible with the ones already taken.
14
Interval Scheduling: Greedy Algorithm
A
for j = 1 to n {
if (job j compatible with A)
A A {j}
}
return A
h
Time
0 1 2 3 4 5 6 7 8 9 10 11
15
Interval Scheduling: Analysis
Greedy: i1 i1 ir ir+1
16
Interval Scheduling: Analysis
Greedy: i1 i1 ir ir+1
17
Interval Partitioning
Interval Partitioning
Interval partitioning.
Aim: Schedule all the requests by using as few resources as possible.
Example: Classroom Scheduling
Lecture j starts at sj and finishes at fj.
Goal: find minimum number of classrooms to schedule all lectures so
that no two occur at the same time in the same room.
e j
c d g
b h
a f i
19
Interval Partitioning
e j
c d g
b h
a f i
c d f j
b g i
a e h
20
Interval Partitioning: Lower Bound on Optimal Solution
Def. The depth of a set of intervals is the maximum number that pass
over any single point on the time-line.
c d f j
b g i
a e h
21
Depth of previous schedule
e j
c d g
b h
a f i
22
Interval Partitioning: Greedy Algorithm
for j = 1 to n {
if (lecture j is compatible with some classroom k)
schedule lecture j in classroom k
else
allocate a new classroom d + 1
schedule lecture j in classroom d + 1
d d + 1
}
b g i
a e h
24
Scheduling to Minimize Lateness
1 2 3 4 5 6
Ex: tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
d3 = 9 d2 = 8 d6 = 15 d1 = 6 d5 = 14 d4 = 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
26
Minimizing Lateness: Greedy Algorithms
27
Minimizing Lateness: Greedy Algorithms
1 2
tj 1 10
counterexample
dj 2 10
28
Minimizing Lateness: Greedy Algorithm
1 2 3 4 5 6
t 0
for j = 1 to n
Assign job j to interval [t, t + tj]
sj t, fj t + tj
t t + tj
output intervals [sj, fj]
max lateness = 1
d1 = 6 d2 = 8 d3 = 9 d4 = 9 d5 = 14 d6 = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
29
Minimizing Lateness: No Idle Time
d=4 d=6 d = 12
0 1 2 3 4 5 6 7 8 9 10 11
This is good since the aggregate execution time can not be smaller. We
must check if it satisfies “minimum lateness.”
30
Minimizing Lateness: Inversions
before swap j i
31
Minimizing Lateness: Inversions
before swap j i
after swap i j
f'j
Pf. Let be the lateness before the swap, and let ' be it afterwards.
'k = k for all k i, j
'i i
¢j = fj¢ - d j (definitio n)
If job j is late: = f -d ( j finishes at time f )
i j i
£ fi - di (i < j )
£ i (definitio n)
32
Minimizing Lateness-Example
Pf. Let be the lateness before the swap, and let ' be it afterwards.
'k = k for all k i, j
¢j = fj¢ - d j (definitio n)
'i i
= fi - d j ( j finishes at time fi )
If job j is late:
£ fi - di (i < j )
£ i (definitio n)
1 2 3 4 5 6
tj 3 2 1 4 3 2
dj 6 8 9 9 14 15
max lateness = 1
d1 = 6 d2 = 8 d3 = 9 d4 = 9 d5 = 14 d6 = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
33
Minimizing Lateness: Analysis of Greedy Algorithm
All schedules with no inversions and no idle time has the same maximum
lateness.
34
Shortest Paths in a Graph
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }
n=8
m = 11
36
Single-Source Shortest Path
37
Shortest Path Problem
2 23 3
9
s
18 Cost of path s-2-3-5-t
14 6
2 = 9 + 23 + 2 + 16
6
30 4 19 = 48.
11
15 5
5
6
20 16
7 t
44
38
Example
A. 0,1,2,3
B. 0,1,4,7
C. 0,1,4,6
D. 0,1,3,6
39
Shortest Path
• What if the graphs are weighted?
• All nonnegative weights: Dijkstra!
• If there are negative weights: Bellman-Ford! (if time
permits)
Caltrain
Hospital
Stadium
Gates
YOU
Packard ARE
HERE
CS161
Union
Dish
Just the graph
How do I get from Gates to the Union?
Caltrain
10 17
Hospital
15
Gates 10
1 Stadium
Packard CS161
1
4
25 Run BFS …
22 Union I should go to the dish and
then back to the union!
10 17
Hospital
15
weighted Gates 10
graph 1 Stadium
10 17
Hospital
15
This is the shortest Gates 10
path from Gates to
the Union. Stadium
1
Packard CS161
It has cost 6. 1
4
25
22 Union
x t
s
Single-source shortest-path problem
• I want to know the shortest path from one vertex
(Gates) to all other vertices.
Hospital
Stadium
Gates
Packard
Back to this example
CS161
Union
Dish
Gates
Dijkstra’s algorithm
1
CS161
• What are the shortest paths
from Gates to everywhere 1
else?
Packard
4
22
Union
25
20
Dish
Dijkstra
intuition
YOINK!
Gates
CS161
Packard Dish Union
Dijkstra
intuition
A vertex is done when it’s not
on the ground anymore.
YOINK!
Gates
CS161
Packard Dish Union
Dijkstra
intuition
YOINK!
Gates
1
Packard
CS161
Dish Union
Dijkstra
intuition
YOINK!
Gates
1
Packard
1
CS161
Dish Union
YOINK!
Dijkstra
intuition
Gates
1
Packard
1
CS161
Union
Dish
YOINK!
Dijkstra
intuition Gates
1
Packard
1
CS161
4 22
Union
Dish
YOINK!
Dijkstra
intuition Gates
1
Packard
This also creates a
tree structure! 1
CS161
Dish
How do we actually implement this?
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
Initialize d[v] = ∞ ∞ 4
for all non-starting vertices
v, and d[Gates] = 0
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
20
Dish
∞
Dijkstra by example Gates 0
How far is a node from Gates? ∞
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
∞ 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
Dish
∞
Dijkstra by example Gates 0
How far is a node from Gates? ∞
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
25
Dijkstra by example Gates 0
How far is a node from Gates? ∞
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 25
Dijkstra by example Gates 0
How far is a node from Gates? ∞
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 25
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 ∞
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra by example Gates 0
How far is a node from Gates? 2
1
CS161
I’m not sure yet
I’m sure 1
x = d[v] is my best over-estimate
x for dist(Gates,v).
Packard
1 4
Current node u
22 6
• Pick the not-sure node u with the Union
smallest estimate d[u]. 25
• Update all u’s neighbors v:
20
• d[v] = min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure. Dish
• Repeat 23
Dijkstra’s algorithm
Dijkstra(G,s):
• Set all vertices to not-sure
• d[v] = ∞ for all v in V
• d[s] = 0
• While there are not-sure nodes:
• Pick the not-sure node u with the smallest estimate d[u].
• For v in u.neighbors:
• d[v] ← min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure.
• Now d(s, v) = d[v]
As usual
• Does it work?
• Yes.
• Is it fast?
• Depends on how you implement it.
Why does this work?
• Theorem:
• Run Dijkstra on G =(V,E), starting from s.
• At the end of the algorithm, the estimate d[v] is the actual
distance d(s,v).
Let’s rename “Gates” to
“s”, our starting vertex.
• Proof outline:
• Claim 1: For all v, d[v] ≥ d(s,v).
• Claim 2: When a vertex v is marked sure, d[v] = d(s,v).
1
But let’s actually prove it. CS161
u Union
Dish
Temporary definition:
v is “good” means that d[v] = d(s,v)
Claim 2
• Want to show that u is good.
s
The vertices in between
u
are beige because they
may or may not be sure.
True shortest path.
Temporary definition:
v is “good” means that d[v] = d(s,v)
Claim 2 means good means not good
“by way of contradiction”
z’
s It may be that z = s.
z It may be that z’ = u.
u
The vertices in between z != u, since u is not good.
are beige because they
may or may not be sure.
True shortest path.
Temporary definition:
v is “good” means that d[v] = d(s,v)
Claim 2 means good means not good
• If 𝑑 𝑧 = 𝑑 𝑢 , then u is good.
We chose u so that d[u] was
• If 𝑑 𝑧 < 𝑑 𝑢 , then z is sure. smallest of the unsure vertices.
So therefore
True shortest path.
r z is sure.
z’
s It may be that z’ = u.
It may be that z = s.
z u
Dijkstra shortest path.
Temporary definition:
v is “good” means that d[v] = d(s,v)
Claim 2 means good means not good
𝒅 𝒛′ ≤ 𝒅 𝒛 + 𝒘 𝒛, 𝒛′ = 𝒅 𝒔, 𝒛′ ≤ 𝒅[𝒛′ ]
So everything is equal!
d(s,z’) = d[z’]
And z’ is good.
r z’ CONTRADICTION!!
s It may be that z = s.
z u
Dijkstra shortest path.
Temporary definition:
v is “good” means that d[v] = d(s,v)
Claim 2 means good means not good
• If 𝑑 𝑧 = 𝑑 𝑢 , then u is good. So u is
• If 𝑑 𝑧 < 𝑑 𝑢 , then z is sure. good!
r z’
aka d[u] = d(s,u)
s It may be that z = s.
z u
Dijkstra shortest path.
Claim 2
When a vertex is marked sure, d[u] = d(s,u)
• For s (the starting vertex):
• The first vertex marked sure has d[s] = d(s,s) = 0.
• For all other vertices:
• Suppose that we are about to add u to the sure list.
• That is, we picked u in the first line here:
• Pick the not-sure node u with the smallest estimate d[u].
• Update all u’s neighbors v:
• d[v] ← min( d[v] , d[u] + edgeWeight(u,v))
• Mark u as sure.
• Repeat
• Proof outline:
• Claim 1: For all v, d[v] ≥ d(s,v).
• Claim 2: When a vertex is marked sure, d[v] = d(s,v).
1
• Dijkstra’s algorithm finds Packard
shortest paths in weighted
graphs with non-negative edge 1
weights. CS161
4 22
• Along the way, it constructs a
nice tree. Union
Dish
As usual
• Does it work?
• Yes.
• Is it fast?
• Depends on how you implement it.
Running time?
Dijkstra(G,s):
• Set all vertices to not-sure
• d[v] = ∞ for all v in V
• d[s] = 0
• While there are not-sure nodes:
• Pick the not-sure node u with the smallest estimate d[u].
• For v in u.neighbors:
• d[v] ← min( d[v] , d[u] + edgeWeight(u,v) )
• Mark u as sure.
• Now dist(s, v) = d[v]
• T(updateKey) 6 5 4 10
*This means that any sequence of d removeMin calls takes time at most O(dlog(n)).
But a few of the d may take longer than O(log(n)) and some may take less time..
In practice
108