




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various greedy algorithms for solving problems such as activity selection, fractional knapsack, and coin change making. Theorems proving the optimality of the greedy solutions, as well as examples and algorithms for each problem.
Typology: Slides
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























Activity Selection Problem– Example– Recursive algorithm– Iterative Algorithm
-^
Fractional Knapsack Problem– Problem Analysis– Greedy Approach for Fractional Knapsack
-^
Coin Change Making Problem– Analysis– Greedy Algorithm
Proof (Part B)First we prove second part because it is bit simpler•^
Suppose that S
im
is nonempty
It means there is some activity a
such that:k^
f^ i^
s^ k
< f
≤k^
s
m^
< f
.m
f^ k
< f
.m
Then a
is also in Sk^
and it has an earlier finishij^
time than a
, which contradicts our choice of am
.m
Hence S
im
is empty, proved
im
mj
s^ m
f^ m S a^ m
ij
a^ k
Part A•^
To prove first part, suppose that A
is a maximum-ij^
size subset of mutually compatible activities of S
,ij^
Order A
monotonic increasing order of finish timeij
Let a
be the first activity in Ak^
.ij^
Case 1•^
If a
= ak^
, then we are done, since we have shownm
that a
m^
is used in some maximal subset of
mutually compatible activities of S
.ij^
-^
Making the greedy choice i.e., the activity with the earliestfinish time in S
ij
Only one subproblem left to solve.
Dynamicprogramming
Using the theorem
Number of subproblemsin the optimal solutionNumber of choices toconsider
2 subproblems:S^ ik
, S
kj
j – i – 1 choices
1 choice: the activitywith the earliestfinish time in S
ij
1 subproblem: S
mj
S^ im
=
Recursive-Activity-Selector
( s, f, i, j
)
1
m
←
i^
2
while
m
<
j^
and
s
m^
<^
fi^
Find the first activity in
S
. ij
3
do
m
←
m
4
if^
m
<
j
5
then
return
{ a
} m
Recursive-Activity-Selector (
s, f, m,
j ) 6
else return
Ø
i^ = 0, j^ =
n
←
i^
←
0 + 1 = 1
m
<
j^
(1 < 12) and
s
< 1
f^0
(But 1>0)
if^
m
<
j^
(1 < 12)
return {
a^1
}^
Recursive-Activity-Selector (
s, f,
^1
, 12)
1
2
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^0
a^1
1
i^ = 1, m
i^
m
j^
(2 < 12) and
s
2
f^1
m
m
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^2
a^1
1 m
j^
(4 < 12) and
s
4
f^1
(But 5 > 4)
if^
m
j^
return {
a^4
Recursive-Activity-Selector(
s, f,
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1
1
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1 i^ = 4, m
i^
m
j^
(5 < 12) and
s
5
f^4
m
m
a^5
1
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1 m
j^
(7 < 12) and
s
7
f^4
m
m
a^7
1
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1
a^8
m
j^
(8 < 12) and
s
f^1
(But 8 > 7)
if^
m
j^
return {
a^8
Recursive-Activity-Selector (
s, f, 8,
1
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1
a^8
a^10
m
j^
(10 < 12) and
s
10
f^8
m
m
1
3
4
6
7
8
5
9
10
11
12
13
14
0
time
a^4
a^1
a^8
a^11
m
j^
(11 < 12) and
s 11
f^8
(But 12 > 11)
if^
m
j^
return {
a^11
Recursive-Activity-Selector (
s, f, 11,