0% found this document useful (0 votes)
17 views4 pages

Lecture 5 Greedy Algorithms Eng

The document discusses greedy algorithms and their application to solving activity selection and knapsack problems. It presents an activity selection algorithm that sorts activities by end time and selects the maximal set of compatible activities. It also discusses how greedy algorithms can solve the 0-1 knapsack and fractional knapsack problems by selecting items in order of highest value to weight ratio.

Uploaded by

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

Lecture 5 Greedy Algorithms Eng

The document discusses greedy algorithms and their application to solving activity selection and knapsack problems. It presents an activity selection algorithm that sorts activities by end time and selects the maximal set of compatible activities. It also discusses how greedy algorithms can solve the 0-1 knapsack and fractional knapsack problems by selecting items in order of highest value to weight ratio.

Uploaded by

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

Greedy Algorithms

• activity-selection problem
• When we are using Greedy Algorithms

activity-selection problem

n is number of activities

i -th activity – start time si - end time fi.

i and j are compatible if intervals si , fi and  s j , f j  don't


intersect. In other words fi  s j or f j  si ).

The task is to choose maximal number of compatible activities

We are sorting activities by their end times

f1  f 2   fn .

activities with same end times may sorted in any order.

f -ს and s are arrays of end and start times:

Algorithm:

GREEDY _ ACTIVITY _ SELECTOR (s, f )

1 n  lengths;
2 A  1 ;
3 j1;
4 for i  2; i  n; i  
5 if  si  f j 
6 A  A i ;
7 ji;
8 return A

1
Wher f j  max fk : k  A
e

2
i si fi
1 1 4

2 3 5 2
1

3 0 6 3
1

4 5 7 4
1

5
5 3 8
1 4

6
6 5 9 1 4

7
7 6 10 1 4

8
8 8 11 1 4

9 8 12 9
1 4 8

10 2 13 10
1 4 8

11 12 14 11
1 4 8

1 4 8 11

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Discrete knapsack problem:


N number of things where the price of i-th thing is v i dollars and the weight is wi kg.
The total weight we can carry it M kg.
We have to put in our bag things with maximum total price.

fractional knapsack problem:


N number of things where the price of i-th thing is v i dollars and the weight is wi kg.
The total weight we can carry it W kg.
We have to put in our bag things with maximum total price.
We can divide each thing into several parts.

3
20 $80
$120
30 30
3 +
50 $120
2 + $100
30 $100
20 20
30
1 20 20 + + +
$100
10
10 $60 10 $60 10 $60

$60 $100 $120 Knapcak =$220 =$160 =$180 $240


(ა) (ბ) (გ)

Numeric Triangle

You might also like