0% found this document useful (0 votes)
43 views

Greedy - Activity - Fractional Knapsack

The document discusses greedy algorithms and provides examples of the activity selection problem and fractional knapsack problem. It explains how greedy algorithms work by making locally optimal choices to try and find a globally optimal solution. It then gives examples of applying greedy algorithms to schedule non-overlapping activities and to optimize value in a fractional knapsack problem.

Uploaded by

Geetika Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Greedy - Activity - Fractional Knapsack

The document discusses greedy algorithms and provides examples of the activity selection problem and fractional knapsack problem. It explains how greedy algorithms work by making locally optimal choices to try and find a globally optimal solution. It then gives examples of applying greedy algorithms to schedule non-overlapping activities and to optimize value in a fractional knapsack problem.

Uploaded by

Geetika Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Greedy Algorithms

• Idea: When we have a choice to make, make the one


that looks best right now
– Make a locally optimal choice in hope of getting a globally
optimal solution

• Greedy algorithms don’t always yield an optimal solution

1
Activity Selection
• Problem
– Schedule the largest possible set of non-overlapping
activities for a given room

Start End Activity

1 8:00am 9:15am Numerical methods class

2 8:30am 10:30am Movie presentation (refreshments served)

3 9:20am 11:00am Data structures class

4 10:00am noon Programming club mtg. (Pizza provided)

5 11:30am 1:00pm Computer graphics class

6 1:05pm 2:15pm Analysis of algorithms class

7 2:30pm 3:00pm Computer security class

8 noon 4:00pm Computer games contest (refreshments served)

9 4:00pm 5:30pm Operating systems class

2
Activity Selection
• Schedule n activities that require exclusive use
of a common resource
S = {a1, . . . , an} – set of activities
• ai needs resource during period [si , fi)
– si = start time and fi = finish time of activity ai
– 0  si < fi < 
• Activities ai and aj are compatible if the
intervals [si , fi) and [sj, fj) do not overlap
fi  s j fj  si
i j j i

3
Activity Selection Problem
Select the largest possible set of nonoverlapping
(mutually compatible) activities.
E.g.:
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 8 9 10 11 12 13 14

• Activities are sorted in increasing order of finish times


• A subset of mutually compatible activities: {a3, a9, a11}
• Maximal set of mutually compatible activities:
{a1, a4, a8, a11} and {a2, a4, a9, a11}

4
Algorithm
Alg.: GREEDY-ACTIVITY-SELECTOR(s, f, n)
1. A ← {a1}
2. i ← 1
3. for m ← 2 to n
4. do if sm ≥ fi ► activity am is compatible with ai
5. then A ← A  {am}
6. i ← m ► ai is most recent addition to A
7. return A

• Assumes that activities are ordered in increasing order of finish time


• Running time: (n) – each activity is examined only once

5
Fractional Knapsack Problem
• Knapsack capacity: W

• There are n items: the i-th item has value vi and


weight wi

• Goal:
– find xi such that for all 0  xi  1, i = 1, 2, .., n

 wixi  W and

 xivi is maximum

6
Fractional Knapsack Problem
Alg.: Fractional-Knapsack (W, v[n], w[n])

1. While w > 0 and as long as there are items remaining

2. pick item with maximum vi/wi

3. xi  min (1, w/wi)

4. remove item i from list

5. w  w – xiwi

• w – the amount of space remaining in the knapsack (w = W)

• Running time: (n) if items already ordered; else (nlgn)


7
Fractional Knapsack - Example
• E.g.:
20
$80
---
Item 3 30 +

Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60

$60 $100 $120 $240

$6/pound $5/pound $4/pound

8
Fractional Knapsack - Problem
Prob: Find an optimal solution for the following
instance of the fractional knapsack problem.
n=3, m=20 (p1,p2,p3)= (25,24,15)
(w1,w2,w3)= (18,15,10)

9
Fractional Knapsack - Problem
Solution: Given knapsack capacity= 20
number of items= 3
v1= p1/w1= 25/18=1.388
v2= p2/w2=24/15=1.6
v3= p3/w3=15/10=1.5
Item weight benefit value
1 18 25 1.388
2 15 24 1.6
3 10 15 1.5
Item taken in decreasing order of value.
Item fraction capacity
X2 1 20-15=5
X3 5/10=1/2 0
Answer: (0,1,1/2) with profit 31.5
10

You might also like