Chap 4 Greedy Method
Chap 4 Greedy Method
Horwitz, Sahni
The General Greedy Method
Most straightforward design technique
Most problems have n inputs
Greedy solution:
Pick d because it reduces the remaining amount the most (to 23)
1
locally optimal
irrevocable
total profit
maximize pi xi (4.1)
1 i n
subject to wi xi m (4.2)
1 i n
and 0 xi 1, 1 i n (4.3)
m = 15
i a b c
p 10 20 15
w 4 10 5
Algorithm 4.3
m = 15
i 1 2 3
p 15 10 20
w 5 4 10
p/w 3 2.5 2
i U w[i] x[i]
1 15 5 1
2 10 4 1
P = 15 + 10 + 20 x 0.6 = 373 6 10 0.6
Exercise
Find an optimal solution to the knapsack instance
n=7
m=15
(p1, p2, …, p7) = (10, 5, 15, 7, 6, 18, 3)
(w1, w2, …, w7) = (2, 3, 5, 7, 1, 4, 1)
4.2 Knapsack Problem
Time complexity
Sorting: O(n log n) using fast sorting algorithm like
merge sort
GreedyKnapsack: O(n)
So, total time is O(n log n)
Feasible processing
Solution sequence value
1. (1, 2) 2, 1 110
2. (1, 3) 1, 3 or 3, 1 115
3. (1, 4) 4, 1 127
4. (2, 3) 2, 3 25
5. (3, 4) 4, 3 42
6. (1) 1 100
7. (2) 2 10
8. (3) 3 15
9. (4) 4 27
Job Sequencing with Deadlines
Theorem 4.4 The greedy method described above always obtains
an optimal solution to the job sequencing problem.
High level description of job sequencing algorithm
Assuming the jobs are ordered such that p[1]p[2]…p[n]