Lecture 2 - Greedy Techniques
Lecture 2 - Greedy Techniques
Greedy Techniques
Md Mehrab Hossain Opi
Definition 2
The greedy method is a problem-solving strategy where decisions are
made by selecting the best option available at the moment, without
considering future consequences.
Activity Selection
Problem
Problem Statement 4
• Imagine that you are in charge of scheduling the KUET central field.
• Several groups have requested to reserve the field, but you can only
accommodate one group on the field at a time.
• Let be the set of activities that start after activity finishes and
that finish before game starts.
• Let be the maximum set of non-overlapping activities in .
• Suppose activity is in .
• Then we get two subproblems.
• find and .
• Let and .
• Thus, we have .
Optimal Substructure of the Problem 9
• The maximum value of can be computed by choosing the
value of that maximizes the value .
• This problem can be solved using dynamic programming.
• We will see this later in our course.
Making The Greedy Choice 10
RECURSIVE-ACTIVITY-SELECTOR(s,f,k,n)
1) m = k + 1
2) while and s[m] < f[k]
3) m = m+1
4) if
5) return RECURSIVE-ACTIVITY-SELECTOR(s,f,m,n)
6) else return
An Iterative Greedy Algorithm 18
• The recursive procedure can be converted to an iterative
one because the procedure is almost tail recursive.
• It ends with a recursive call to itself followed by a union
operation.
• It is usually a straightforward task to transform a tail-
recursive procedure to an iterative form.
An Iterative Greedy Algorithm 19
GREEDY-ACTIVITY-SELECTOR (s, f, n)
1) A =
2) k = 1
3) for m = 2 to n
4) if
5)
6) k=m
7) return A
Example 20
• Consider the following set of activities sorted by finish time
in monotonically increasing order
1 2 3 4 5 6 7 8 9 10 11
1 3 0 5 3 5 6 7 8 2 12
4 5 6 7 9 9 10 11 12 14 16
Example Solution 21
0 1 2 3 4 5 6 7 8 9 10 11 1 13 14 1 16 time
2 5
The General Method 22
• Most, though not all, of these problems have n inputs and
require us to obtain a subset that satisfies some
constraints.
• Any subset that satisfies these constraints is called a
feasible solution.
• We need to find a feasible solution that either maximizes or
minimizes a given objective function.
• A feasible solution that does this is called an optimal
solution.
The General Method 23
• The greedy method suggests that one can devise an algorithm that works in
stages, considering one input at a time.
• At each stage, a decision is made regarding whether a particular input is in
an optimal solution.
• Inputs are considered in an order determined by some selection procedure.
• If the inclusion of the next input into the partially constructed optimal
solution will result in an infeasible solution, then this input is not added to
the partial solution.
• Otherwise, it is added.
The General Method 24
• The Selection procedure itself is based on some
optimization measure.
• This measure may be the objective function.
• This version of the greedy technique is called the subset
paradigm.
The General Method 25
• Some algorithms do not need selection of an optimal
subset but make decisions by looking at the inputs in some
order.
• Each decision is made by using an optimization criterion
that is computed using the decisions made so far.
• This version of the greedy technique is called the ordering
paradigm.
Elements of the Greedy Strategy 26
Huffman Coding
Huffman Coding 34
• A technique of compressing data to reduce its size without losing any
of the details.
• Data arrives as a sequence of characters.
• Algorithm finds an optimal way of representing each character as a
binary string.
A B C D E F
Frequency 45 13 12 16 9 5
(in thousands)
A B C D E F
Frequency 45 13 12 16 9 5
(in thousands)
Variable-length 0 101 100 111 1101 1100
codeword
14
0 1
F:5 E:9
14
C:12 B:13 0 1 D:16 A:45
F:5 E:9
F:5 E:9
25
C:12 14 B:13 25
D:16 A:45
0 1 0 1
0
F:5 E:9 C:12 B:13
14 25
D:16 A:45
0 1 0 1
30
0 1
D:1
14 30
25 06 1
0 1
0 1 14 D:16 A:45
F:5 E:9 0 1
C:12 B:13
F:5 E:9
25 30
0 1
0 1 14 D:16 A:45
0 1
C:12 B:13
F:5 E:9
55
25 30
0 1
0 1
14 D:16
C:12 B:13 0 1
F:5 E:9
55
25 30 A:45
0 1
0 1
14 D:16
C:12 B:13 0 1
F:5 E:9
1
100
0 1
A:45 55
25 30
0 1
0 1
14 D:16
C:12 B:13 0 1
F:5 E:9
1
25 30
0 1 1 1
B:1
C:12 14 D:16
3
0 1
F:5 E:9
1.
JobID A B C D E
Deadline 2 1 2 1 3
Profit 100 19 27 25 15
Thank You.