Lecture 5 Greedy Algorithms Eng
Lecture 5 Greedy Algorithms Eng
• activity-selection problem
• When we are using Greedy Algorithms
activity-selection problem
n is number of activities
f1 f 2 fn .
Algorithm:
1 n lengths;
2 A 1 ;
3 j1;
4 for i 2; i n; i
5 if si f j
6 A A i ;
7 ji;
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
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
Numeric Triangle