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

Greedy Algorithms

Greedy algorithms work by selecting the locally optimal choice at each step in the hope of finding a globally optimal solution. They require the greedy-choice property and optimal substructure. Examples where greedy algorithms can be applied include activity selection problems, fractional knapsack problems, Huffman coding, minimum spanning trees, shortest paths problems, and task scheduling. Huffman coding uses a greedy approach to build an optimal prefix code by assigning shorter codes to more frequent characters.

Uploaded by

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

Greedy Algorithms

Greedy algorithms work by selecting the locally optimal choice at each step in the hope of finding a globally optimal solution. They require the greedy-choice property and optimal substructure. Examples where greedy algorithms can be applied include activity selection problems, fractional knapsack problems, Huffman coding, minimum spanning trees, shortest paths problems, and task scheduling. Huffman coding uses a greedy approach to build an optimal prefix code by assigning shorter codes to more frequent characters.

Uploaded by

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

Greedy Algorithms

A greedy algorithm always makes the


choice that looks best at the moment.
That is, it makes a locally optimal choice
in the hope that this choice will lead to a
globally optimal solution.
Greedy Algorithms
Must have
– Greedy-choice property: a globally optimal
solution can be arrived at by making a locally
optimal choice
– Optimal substructure: an optimal solution to a
problem contains optimal solutions to its
subproblems
Problem based on Greedy
Algorithms
• An activity-selection problem
• Fractional knapsack problem
• Huffman code
• Minimum spanning tree Algorithm
• Dijkstra's single source shortest paths
algorithm
• Task scheduling problem etc.
An activity-selection problem
The activity-selection problem is to select
maximum-size subset of mutually
compatible activities. For example, consider
the following set S of activities, which we
have sorted in monotonically increasing
order of finish time:
• 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 1112 13 14
An activity-selection problem

• GREEDY-ACTIVITY-SELECTOR(s, f)
• 1 n ← length[s]
• 2 A ← {1}
• 3j←1
• 4 for i ← 2 to n
• 5 do if si ≥ fj
• 6 then A ← A U{i}
• 7 j ←i
• 8 return A
0–1 knapsack problem
• The 0–1 knapsack problem is posed as
follows. A thief robbing a store finds n items; the
ith item is worth vi dollars and weighs wi pounds,
where vi and wi are integers.
• He wants to take as valuable a load as possible,
but he can carry at most W pounds in his
knapsack for some integer W.
• Which items should he take? (This is called the
0–1 knapsack problem because each item must
either be taken or left behind; the thief cannot
take a fractional amount of an item or take an
item more than once.)
fractional knapsack problem

• In the fractional knapsack problem, the


setup is the same, but the thief can take
fractions of items, rather than having to
make a binary (0–1) choice for each item.
You can think of an item in the 0–1
knapsack problem as being like a gold
ingot, while an item in the fractional
knapsack problem is more like gold dust.
Huffman codes
• Huffman codes are a widely used and very
effective technique for compressing data;
savings of 20% to 90% are typical, depending on
the characteristics of the data being compressed
• Huffman's greedy algorithm uses a table of the
frequencies of occurrence of the characters to
build up an optimal way of representing each
character as a binary string.
Huffman codes
• Suppose we have a 100,000-character data file that
we wish to store compactly. We observe that the
characters in the file occur with the frequencies given
by Figure 16.3. That is, only six different characters
appear, and the character a occurs 45,000 times.
a b c d e f
• Frequency (in thousands) 45 13 12 16 9 5
• Fixed-length codeword 000 001 010 011 100 101
• Variable-length codeword 0 101 100 111 1101 1100
Huffman codes
• A data file of 100,000 characters contains only
the characters a–f, with the frequencies
indicated. If each character is assigned a 3-bit
codeword,the file can be encoded in 300,000
bits.
• Using the variable-length code shown, the file
can be encoded in 224,000 bits.
• If we use a fixed-length code, we need 3 bits to
represent six characters: a = 000, b = 001, ..., f =
101. This method requires 300,000 bits to code
the entire file. Can we do better?
Huffman codes
• A variable-length code can do considerably
better than a fixed-length code, by giving
frequent characters short codewords and
infrequent characters long codewords. Figure
16.3 shows such a code; here the 1-bit string 0
represents a, and the 4-bit string 1100
represents f.
• This code requires(45 · 1 + 13 · 3 + 12 · 3 + 16 ·
3 + 9 · 4 + 5 · 4) · 1,000 = 224,000 bits to
represent the file, a savings of approximately
25%. In fact, this is an optimal character codefor
this file.
Huffman code.
• Huffman invented a greedy algorithm that
constructs an optimal prefix code called a
Huffman code.
Constructing a Huffman code
• HUFFMAN(C)
• 1 n ← |C|
• 2 Q←C
• 3 for i= 1 to n - 1
• 4 do allocate a new node z
• 5 left[z]←x← EXTRACT-MIN (Q)
• 6 right[z]←y← EXTRACT-MIN (Q)
• 7 f [z]←f [x] +f [y]
• 8 INSERT(Q, z)
• 9 return EXTRACT-MIN(Q) ▹Return the root of the tree.

• the total running time of HUFFMAN on a set of n characters is O (n


lg n).
A task-scheduling problem
• A unit-time task is a job, such as a program to be run on a computer, that
requires
• exactly one unit of time to complete. Given a finite set S of unit-time tasks, a
schedule for S is a permutation of S specifying the order in which these
tasks are to be performed. The first task in the schedule begins at time 0
and finishes at time 1, the second task begins at time 1 and finishes at time
2, and so on.
• The problem of scheduling unit-time tasks with deadlines and penalties
for a
• single processor has the following inputs:
•  a set S = {a1, a2, ..., an} of n unit-time tasks;
•  a set of n integer deadlines d1, d2, ..., dn, such that each di satisfies 1 ≤
di≤ n and task ai is supposed to finish by time di; and
•  a set of n nonnegative weights or penalties w1, w2, ..., wn, such that we
incur a penalty of wi if task ai is not finished by time di and we incur no
penalty if a task finishes by its deadline.
• We are asked to find a schedule for S that minimizes the total penalty
incurred for missed deadlines.

You might also like