Professional Documents
Culture Documents
Greedy Algorithms
Paradigm & Examples
Announcements
Assignment#2: DESIGN#1
• Assess: D&C & Greedy design paradigms
• Location: @LMS under “Tests/Quizzes”
• Due: SUN 9 APR 23:55 isA [2 weeks]
• Submission: Upload “[PROBLEM_NAME].cs” file
• Evaluation: 12 (+) Marks
Announcements
ta rt
S E LY
Assignment#2: DESIGN#1 D IAT
M E
IM
• TWO C# Template-based Problems
– GIVEN: reading inputs + checking outputs + sample test cases
– REQUIRED: fill the missing function that solve the problem
Sample Cases UNSEEN Large Cases
Problem Assess (Correctness) (Efficiency) Total
D&C
Problem#1 Taken Problem 2 Marks 6 Marks 8 MARKS
D&C or GREEDY
Problem#2 New Problem 1 Mark 3 Marks 4 MARKS
Announcements
Assignment#2: DESIGN#1
• Problem#1: COMPETITION & BONUSES
Criteria BONUS
1x Faster +3 Marks
Analysis Design
• [EXTRA]
– Huffman Coding
repeatedly improve
current solution Algorithm
convert
Design Transformation problem to
another one
Techniques
Randomization
repeatedly improve
current solution Algorithm
convert
Design Transformation problem to
another one
Techniques
Randomization
• When to use?
– Often in optimization problems, in which
• There can be many possible solutions,
…
Greedy
• Solution Steps:
1. Cast the problem:
• make a choice one sub-problem to solve.
=
+
Optimal Sol. To
Subprob.
Greedy
• Compared with D&C:
1. Easy to propose multiple greedy algorithms for many problems
2. Easy run-time analysis
3. Hard to establish correctness
• Required
– What should he take to maximize his benefit?
20
Ex1: Fractional Knapsack
• Naïve Solution:
1. Find all possible sets of items
2. Chose the max one
– Complexity = O(2N)
21
Ex1: Fractional Knapsack
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with ONE from the N items
2. Sub-problem: Find max benefit to pack the remaining weight from N – 1 items
Max cost
Max-benefit to pack
Min weight remain. W from N-1
22
Ex1: Fractional Knapsack
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with ONE from the N items
2. Sub-problem: Find max benefit to pack the remaining weight from N – 1 items
23
Ex1: Fractional Knapsack
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with MAX cost/unit from the N items
2. Sub-problem: Find max benefit to pack the remaining weight from N – 1 items
Complexity = Θ(Nlog(N))
BONUS: Can we make it O(N)?
HINT: Think of selecting Kth element
25
Ex1: Fractional Knapsack
• CAN greedy choice work with 0-1 knapsack?
• NO
– Unable to fill the knapsack to capacity empty space lowers the effective value per
pound of the load
– We must compare two options:
• the solution to the sub-problem in which the item is included
Dynamic Programming 26
Ex1: Fractional Knapsack
• Ex:
27
Agenda
• Greedy Paradigm
3
Max Set 4 6
2
1 5
• Given:
– A single resource
– A set S of N activities, each with:
si = start time of activity i
fi = finish time of activity i
• Required: 29
Ex2: Activity-Selection
• Compatible Activity
– Activities i and j are compatible if [si, fi) and [sj, fj) don’t overlap
si fj or sj fi
• Naïve Solution:
1. Find all possible sets of compatible activities
2. Chose the max-size one
– Complexity = O(2N)
30
Example
• Here are a set of start and finish times
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 6
2 7 11
10
3 9
5
4 8
• What is the maximum number of activities that can be completed?
• {a3, a9, a11} can be completed
• But so can {a1, a4, a8’ a11} which is a larger set
• But it is not unique, consider {a2, a4, a9’ a11} 31
Ex2: Activity-Selection
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with ONE from the N activity
2. Sub-problem: Find max-size set of compatible activities from N – 1 activity
Select Earliest
WHICH ONE to Chose: Max-size set from N Finish Activ.
Min duration
Earliest start time Max-size set from
N-1
Last start time
Earliest finish time
Last finish time
Has min number of overlaps
33
Ex2: Activity-Selection
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with EARLIEST FINISH or LAST START from the N activity
2. Sub-problem: Find max-size set of compatible activities from N – 1 activity
34
Ex2: Activity-Selection
• High-level pseudo-code:
1. Sort activities by finish time (ascending)
2. Repeatedly select the compatible activities: Sk+1 ≥ Fk
– Required:
• Schedule all the activities using as few lecture halls as possible?
GREEDY or DP?!
Complexity? 37
Ex2: Activity-Selection
• VARIATION#2: MIT 6.046 Lecture1
– Given:
• A set of activities to schedule, each with start si and finish fi
• Single resource
– Required:
• Choose a set of compatible activities such that is maximized?
GREEDY or DP?!
Complexity? 38
Agenda
• Greedy Paradigm
• BRUTE-FORCE • D&C
1. Find all possible sequences Divide: array into 2 halves
2. Chose one with max sum Conquer: each half to find its max seq.
• Complexity? Θ(N3) or Θ(N2) Combine: max of 2 halves & cross sum
• Complexity? Θ(N Log(N))
Can we do BETTER?
Ex3: Max Sequence Sum
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with ONE from the N numbers
2. Sub-problem: Find max sequence sum from remaining numbers
WHICH ONE to Chose: Max Seq. Sum from N Select ONE number
Min overall
Max overall Max Seq. Sum from
remaining
Min +ve
Max +ve
First +ve
First +ve followed by +ve
41
Ex3: Max Sequence Sum
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with ONE from the N numbers
2. Sub-problem: Find max sequence sum from remaining numbers
WHICH ONE to Chose: Max Seq. Sum from N Select ONE number
Min overall
Max overall Max Seq. Sum from
remaining
Min +ve
Max +ve
First +ve
First +ve followed by +ve
42
Ex3: Max Sequence Sum
• Greedy Solution:
1. Cast the problem: (make a choice one sub-problem to solve)
1. Choice: start with 1st +ve number and add it to max(0, current sum)
2. Sub-problem: Find max sequence sum from remaining numbers
• [EXTRA]
– Huffman Coding