Greedy Algorithms: Activity Selection, Fractional Knapsack, and Coin Change Making, Slides of Design and Analysis of Algorithms

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

2011/2012

Uploaded on 08/06/2012

parnavi
parnavi 🇮🇳

4.2

(15)

119 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No. 25
Greedy Algorithms
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Greedy Algorithms: Activity Selection, Fractional Knapsack, and Coin Change Making and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 25

Greedy Algorithms

•^

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

Today Covered

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

Theorem

S

im

S

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^

Theorem

Why is this Theorem Useful?

-^

Making the greedy choice i.e., the activity with the earliestfinish time in S

ij

  • Reduce the number of subproblems and choices– Solved each subproblem in a top-down fashion -^

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^

  • 1

2

while

m

<

j^

and

s

m^

<^

fi^

Find the first activity in

S

. ij

3

do

m

m

  • 1

4

if^

m

<

j

5

then

return

{ a

} m

Recursive-Activity-Selector (

s, f, m,

j ) 6

else return

Ø

A Recursive Greedy Algorithm

i^ = 0, j^ =

n

  • 1 = 12 m

i^

  • 1

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)

A Recursive Greedy Algorithm

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

A Recursive Greedy Algorithm 2

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,

A Recursive Greedy Algorithm 2

3

4

6

7

8

5

9

10

11

12

13

14

0

time

a^4

a^1

1

A Recursive Greedy Algorithm 2

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

A Recursive Greedy Algorithm 2

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

A Recursive Greedy Algorithm 2

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

A Recursive Greedy Algorithm 2

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

A Recursive Greedy Algorithm 2

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,