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

Chap 4 Greedy Method

The document discusses greedy algorithms and their application to optimization problems. It describes the general greedy method, which makes locally optimal choices at each step in the hope of reaching a globally optimal solution. Specific problems discussed include the change-making problem, knapsack problem, and job sequencing with deadlines. For each problem, the greedy approach is presented and it is shown that the greedy solution is optimal for some problems but provides fast approximations for most.

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)
25 views

Chap 4 Greedy Method

The document discusses greedy algorithms and their application to optimization problems. It describes the general greedy method, which makes locally optimal choices at each step in the hope of reaching a globally optimal solution. Specific problems discussed include the change-making problem, knapsack problem, and job sequencing with deadlines. For each problem, the greedy approach is presented and it is shown that the greedy solution is optimal for some problems but provides fast approximations for most.

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/ 13

Ch 4 Greedy Method

Horwitz, Sahni
The General Greedy Method
 Most straightforward design technique
 Most problems have n inputs

 Solution contains a subset of inputs that satisfies a given constraint

 Feasible solution: Any subset that satisfies the constraint

 Need to find a feasible solution that maximizes or minimizes a given

objective function – optimal solution

 Used to determine a feasible solution that may or may not be optimal


 At every point, make a decision that is locally optimal; and hope that

it leads to a globally optimal solution


 Leads to a powerful method for getting a solution that works well for

a wide range of applications


Change-Making Problem
 Given unlimited amounts of coins of denominations d1 > … > dm , give
change for amount n with the least number of coins

 Example: d1 = 25c, d2 =10c, d3 = 5c, d4 = 1c and n = 48c

 Greedy solution:
 Pick d because it reduces the remaining amount the most (to 23)
1

 Pick d2 , remaining amount reduces to 13


 Pick d2, remaining amount reduces to 3
 Pick d4 , remaining amount reduces to 2
 Pick d4, remaining amount reduces to 1
 Pick d4, remaining amount reduces to 0
Greedy Technique
 Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
 Feasible

 locally optimal

 irrevocable

 For some problems, yields an optimal solution for every


instance.
 For most, does not but can be useful for fast
approximations.
The General Method
Algorithm 4.1
4.2 Knapsack Problem
 Problem definition
 Given n objects and a knapsack where object i has a weight w
i
and the knapsack has a capacity m
 If a fraction x of object i placed into knapsack, a profit p x is
i i i
earned
 The objective is to obtain a filling of knapsack maximizing the

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)

 A feasible solution is any set satisfying (4.2) and (4.3)


 An optimal solution is a feasible solution for which (4.1) is
maximized
Knapsack Problem Example 4.4

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)

 If p1/w1 ≥ p2/w2 ≥ … ≥ pn/wn, then GreedyKnapsack


generates an optimal solution to the given instance of
the knapsack problem.
Job Sequencing with Deadlines
i 1 2 3 4 5
pi 5 10 25 15 20
di 2 1 3 3 1
1. We are given a set of n jobs.
2. Associated with job i is an integer deadline di ≧ 0 and a profit pi
≧ 0.
3. For any job i the profit pi is earned iff the job is completed by its
deadline.
4. Each job need one unit of time to be completed and only one
machine is available.
5. A feasible solution is a subset J of jobs such that each job in
this subset can be completed by its deadline and the total profit
is the sum of the jobs’ profits in J.
6. An optimal solution is a feasible solution with maximum profit.
Job Sequencing with Deadlines
 Example
 n=4, (p ,p ,p ,p )=(100,10,15, 27),
1 2 3 4
(d1,d2,d3,d4)=( 2, 1, 2, 1)

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]

GreedyJob(int d[], set J, int n)


// J is a set of jobs that can be
// completed by their deadlines.
{
J = {1};
for (int i=2; i<=n; i++) {
if (all jobs in J ∪{i} can be completed
by their deadlines) J = J ∪{i};
}
}

You might also like