Sunteți pe pagina 1din 16

CSE 373

Lecture 8
By
Javed Siddique
Greedy Algorithm
• Similar to Dynamic Programming.
• 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.
Activity Selection Problem
• Suppose we have a set S = {a1, a2, . . . , an} of n
proposed activities that wish to use a resource,
such as a lecture hall, which can be used by only
one activity at a time.
• Each activity ai has a start time si and a finish
time fi, where 0 ≤ si < fi < ∞.
• If selected, activity ai takes place during the half-
open time interval [si , fi ).
• Activities ai and aj are compatible if the intervals
[si , fi ) and [s j , f j ) do not overlap (i.e., ai and aj
are compatible if si ≥ f j or s j ≥ fi ).
Example
Theorem
Proof
Recursive Activity Selector
Runtime Analysis
• Order of n
Huffman Encoding
• Huffman Encoding is a compression technique.
• Very effective technique for compressing data;
savings of 20% to 90% are typical.
• Based on frequency of characters in a set of data.
• Works by finding optimal binary string for each
character.
• Huffman Encoding uses a variable length code.
• Uses prefix code where no code word is also a
prefix of other code word.
• It simplifies decoding.
Algorithm
Runtime Analysis
• If we use a heap to do the extract min
operation, then the runtime is O(nlgn)

S-ar putea să vă placă și