Académique Documents
Professionnel Documents
Culture Documents
unit5 1
General form of Greedy Algorithms
Algorithm greedy (C) //C is an input set having n element
{
S←∅ // S is a solution set
while not solution (S) and C ≠ ∅ do
x ← extract best element from C.
if (x is feasible)
Then C ← C - {x} // extract X from input set C
S ← S ∪ {x} // add X into solution
if solution (S) = ∅
then return “no solutions”
}
Here all input activities are already sorted by increasing finishing time.
z
y
x
w
v
u
t
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
z
y
x
w
v
u
t
s
r
q
0
p Initialization A={p}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p}
z
y
x
w
v
u
t
s
r
Activity ‘p’ and ‘q’ are not
q compatible (overlapped) So
p don't add it in to solution
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p}
z
y
x
w
v
u
t
s
r
Activity ‘p’ and ‘r’ are not
compatible (overlapped) So
q don't add it in to solution
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p}
z
y
x
w
v
u
t
Activity ‘p’ and ‘s’ are
s compatible, So
r add ‘s’ in to solution
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, }
z
y
x
w
v
u
Activity ‘s’ and ‘t’ are
t not compatible (overlapped) So
s don't add it in to solution
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, }
z
y
x
w
v
Activity ‘s’ and ‘u’ are
u not compatible (overlapped) So
t don't add it in to solution
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, }
z
y
x
w
Activity ‘s’ and ‘v’ are
v not compatible (overlapped) So
u don't add it in to solution
t
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, }
z
y
x
Activity ‘s’ and ‘w’ are
w compatible, So add
v ‘w’ in to solution
u
t
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, w, }
z
y
Activity ‘s’ and ‘x’ are
x not compatible (overlapped) So
w don't add it in to solution
v
u
t
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, w, }
z
Activity ‘w’ and ‘y’ are
y not compatible (overlapped), So
x do not add ‘y’ in to solution
w
v
u
t
s
r
q
p
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Activity ={p, s, w, }
Activity ={p, s, w, z }
Activity ={p, s, w, z }
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 19
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 20
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B C
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 21
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B A
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 22
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B E
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 23
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B D
E
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 24
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B E F
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 25
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B E G
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 26
Activity Selection (Interval Scheduling)
B (1)
C (2)
A (3)
E (4)
D (5)
F (6)
G (7)
H (8) Time
0 1 2 3 4 5 6 7 8 9 10 11
B E H
0 1 2 3 4 5 6 7 8 9 10 11
Unit 3 Greedy Algorithms 27
Elements of the Greedy Strategy
․ When to apply greedy algorithms?
Greedy-choice property: A global optimal solution can be
arrived at by making a locally optimal (greedy) choice.
Dynamic programming needs to check the solutions to
subproblems.
Optimal substructure: An optimal solution to the problem
contains within its optimal solutions to subproblems.
E.g., if A is an optimal solution to S, then A' = A - {1} is an optimal
solution to S' = {i ∈ S: si ≥ f1}.
․ Greedy algorithms (heuristics) do not always produce
optimal solutions.
․ Greedy algorithms v.s. dynamic programming (DP)
Common: optimal substructure
Difference: greedy-choice property
DP can be used if greedy solutions are not optimal.
Knapsack Problem
value = benefit /
weight
Item F E A B C D G
benefits 7 14 12 10 8 11 9
Weight 1 3 4 6 5 7 6
Value 7 4.67 3 1.67 1.6 1.57 1.5
Initially
Knapsack W=18
weight = 0
benefits =0
Knapsack F W=18
weight = 1
benefits =7
Knapsack F, E W=18
weight = 1+3 =4
benefits =7+14 =21
Knapsack F, E, A W=18
weight = 1+3 =4+4=8
benefits =7+14 =21+12=33
Knapsack F, E, A, B W=18
weight = 1+3 =4+4=8+6=14
benefits =7+14 =21+12=33+10=43
Remaining items D, G could not put into Knapsack (bag) because bag is full
i.e weight = W
Knapsack = F, E, A, B, C
Weight in bag = W = 18
Benefits = Rs.
49.4
Frequency 45 13 12 16 9 5
(in 1000s)
Fixed-length 000 001 010 011 100 101
codeword
= 300K bits
Fixed-length
codeword
000 001 010 011 100 101
300K bits
Variable-length
codeword
0 101 100 111 1101 1100
224K bits
Savings = 25%
Greedy Choice?
The two smallest nodes are chosen at each step, and this local decision
results in a globally optimal encoding tree.
In general, greedy algorithms use small-grained, or local
minimal/maximal choices to result in a global minimum/maximum
14
From following Lines
z ← Allocate-Node
x ← left[z] ← Extract-Min(Q)
f:5 e:9 c:12 b:13 d:16 a:45 y ← right[z] ← Extract-Min(Q)
f[z] ← f[x]+f[y]
f:5 e:9
f:5 e:9
f:5 e:9
100
z ← Allocate-Node
x ← left[z] ← Extract-Min(Q)
a:45 55
y ← right[z] ← Extract-Min(Q)
f[z] ← f[x]+f[y]
25 30
Insert(Q, z)
f:5 e:9
0 100 1
a:45 55
0 1 Add ‘0’ with left child, and
‘1’with right child of each
node
25 30
0 0 1
1
f:5 e:9
100 1
0 Char Freq Codeword
a:45 55 a 45 0
0 1
b 13 101
25 30 c 12 100
0 1
0 1 d 16 111
e 9 1101
c:12 b:13 14 d:16
f 5 1100
0 1
f:5 e:9
= O(nlog n)
Thus, the total running time of HUFFMAN on a set of n
characters = O(n) + O(nlog n)
= O(n log n)