Professional Documents
Culture Documents
04greedy Mod 1
04greedy Mod 1
4
Greedy
Algorithms
Interval Scheduling
Interval scheduling.
Job j starts at sj and finishes at fj.
a
b
c
d
e
f
g
h
0
10
11
Time
3
A
for j = 1 to n {
if (job j compatible with A)
A A {j}
}
return A
Greedy:
i1
i1
ir
OPT:
j1
j2
jr
ir+1
jr+1
...
Greedy:
i1
i1
ir
ir+1
OPT:
j1
j2
jr
ir+1
...
Interval Partitioning
Interval partitioning.
Lecture j starts at sj and finishes at fj.
e
c
j
g
d
b
a
9
9:30
f
10
10:30
11
11:30
12
12:30
1:30
i
2
2:30
3:30
4:30
Time
10
Interval Partitioning
Interval partitioning.
Lecture j starts at sj and finishes at fj.
d
b
a
9
9:30
i
h
e
10
10:30
11
11:30
12
12:30
1:30
2:30
3:30
4:30
Time
11
d
b
a
9
9:30
i
h
e
10
10:30
11
11:30
12
12:30
1:30
2:30
3:30
4:30
Time
12
{
j is compatible with some classroom k)
lecture j in classroom k
a new classroom d + 1
lecture j in classroom d + 1
1
13
14
Ex:
tj
dj
14
15
lateness = 2
d3 = 9
0
d2 = 8
2
d6 = 15
3
d1 = 6
5
lateness = 0
max lateness = 6
d5 = 14
8
10
d4 = 9
11
12
13
14
15
16
17
tj
10
dj
100
10
counterexample
tj
10
dj
10
counterexample
18
max lateness = 1
d1 = 6
0
d2 = 8
3
d3 = 9
5
d4 = 9
7
d5 = 14
9
10
11
12
d6 = 15
13
14
15
19
d=6
2
d=4
0
d = 12
5
d=6
2
10
11
10
11
d = 12
4
20
21
before swap
after swap
i
i
j
f'j
If job j is late:
22
23
24
requests
cache
26
future queries:
g a b c e d a b b a c d e a f a d e f g h ...
cache miss
27
an unreduced schedule
a reduced schedule
28
S'
t
d
t'
t'
e
S'
Case 1
t'
e
t'
d requested at time t'
Case 2
29
Farthest-In-Future: Analysis
Theorem. FF is optimal eviction algorithm.
Pf. (by induction on number or requests j)
Invariant: There exists an optimal reduced schedule S that makes
the same eviction schedule as SFF through the first j+1 requests.
30
Farthest-In-Future: Analysis
Pf. (continued)
Case 3: (d is not in the cache; SFF evicts e; S evicts f e).
same
same
S
j+1
j
same
S'
e
same
S'
now S' agrees with SFF on first j+1 requests; we show that having
element f in cache is no worse than having element e
31
Farthest-In-Future: Analysis
Let j' be the first time after j+1 that S and S' take a different action,
and let g be item requested at time j'.
must involve e or f (or both)
j'
same
same
S'
Farthest-In-Future: Analysis
Let j' be the first time after j+1 that S and S' take a different action,
and let g be item requested at time j'.
must involve e or f (or both)
j'
same
same
S'
same
same
S'
33
Caching Perspective
Online vs. offline algorithms.
Offline: full sequence of requests is known a priori.
Online (reality): requests are not known in advance.
Caching is among most fundamental online problems in CS.
34
23
9
s
18
14
6
30
15
11
16
20
7
44
19
6
t
36
Dijkstra's Algorithm
Dijkstra's algorithm.
Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
Initialize S = { s }, d(s) = 0.
(v )
min
e (u , v ) : u S
d (u ) e ,
d(u)
u
S
s
37
Dijkstra's Algorithm
Dijkstra's algorithm.
Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
Initialize S = { s }, d(s) = 0.
(v )
min
e (u , v ) : u S
d (u ) e ,
d(u)
u
S
s
38
S
(P) (P') + (x,y) d(x) + (x, y) (y) (v)
nonnegative
weights
inductive
hypothesis
defn of (y)
u
v
Dijkstra chose v
instead of y
39
d(u) l e .
min
e (u,v) : u S
Dijkstra
Array
Binary heap
d-way Heap
Insert
log n
d log d n
ExtractMin
log n
d log d n
log n
ChangeKey
log n
log d n
IsEmpty
n2
m log n
Total
m log
m/n
Fib heap
m + n log n
Extra Slides
Coin Changing
Coin Changing
Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method
to pay amount to customer using fewest number of coins.
Ex: 34.
43
S
while (x 0) {
let k be largest integer such that ck x
if (k = 0)
return "no solution found"
x x - ck
S S {k}
}
return S
ck
P4
N1
10
N+D2
4+5=9
25
Q3
20 + 4 = 24
100
no limit
75 + 24 = 99
45
46
Selecting Breakpoints
Selecting Breakpoints
Selecting breakpoints.
Road trip from Princeton to Palo Alto along fixed route.
Refueling stations at certain points along the way.
Fuel capacity = C.
Goal: makes as few refueling stops as possible.
C
Princeton
C
C
C
1
C
C
Palo Alto
7
48
breakpoints selected
current location
while (x bn)
let p be largest integer such that bp x + C
if (bp = x)
return "no solution"
x bp
S S {p}
return S
49
g1
g2
gr+1
gr
Greedy:
...
OPT:
f0
f1
f2
fr
fr+1
fq
50
g1
g2
gr
gr+1
Greedy:
...
OPT:
f0
f1
f2
fr
fq
51
Edsger W. Dijkstra
The question of whether computers can think is like the
question of whether submarines can swim.
Do only what only you can do.
In their capacity as a tool, computers will be but a ripple
on the surface of our culture. In their capacity as
intellectual challenge, they are without precedent in the
cultural history of mankind.
The use of COBOL cripples the mind; its teaching should,
therefore, be regarded as a criminal offence.
APL is a mistake, carried through to perfection. It is the
language of the future for the programming techniques
of the past: it creates a new generation of coding bums.
52