0% found this document useful (0 votes)
10 views18 pages

Algorithm CH 4 Lec 1

The document discusses the Greedy Method, a straightforward design technique for solving optimization problems by making locally optimal choices at each step. It includes examples such as the Change-Making Problem and Machine Scheduling, illustrating how the greedy approach can yield feasible solutions, though not always optimal ones. Additionally, it covers the Knapsack Problem, emphasizing the importance of the ratio of profit to weight for achieving optimal solutions using a greedy strategy.

Uploaded by

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

Algorithm CH 4 Lec 1

The document discusses the Greedy Method, a straightforward design technique for solving optimization problems by making locally optimal choices at each step. It includes examples such as the Change-Making Problem and Machine Scheduling, illustrating how the greedy approach can yield feasible solutions, though not always optimal ones. Additionally, it covers the Knapsack Problem, emphasizing the importance of the ratio of profit to weight for achieving optimal solutions using a greedy strategy.

Uploaded by

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

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 d1 because it reduces the remaining amount the most (to 23)
 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

9/26/2024
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.
9/26/2024
The General Method
Algorithm 4.1

9/26/2024
Machine Scheduling(Example 4.2)
Problem definition:
 You are give n tasks and an infinite supply of machines on which
these tasks can be performed. Each task has a start time si and a
finish time fi , si < fi. [si,fi] is the processing interval for task i.
 Two tasks i and j overlap iff their processing intervals overlap.
 For example: the interval [1,4] overlaps with [2,4] but not with [4,7]

Solution
 A feasible task-to-machine assignment is an assignment in which no
machine is assigned two overlapping tasks. Therefore, in a feasible
assignment each machine works on at most one task at any time.
 An optimal assignment is a feasible assignment that utilizes the
fewest number of machines.

9/26/2024
Machine scheduling (Cont.)
Suppose we have
No. of tasks, n= 7
Tasks are labeled - a through g and their start and finish times are as shown in
the following figure.

tasks a b c d e f g
start 0 3 4 9 7 1 6
finish 2 7 7 11 10 5 8
 The following task-to-machine assignment is a feasible assignment that
utilizes seven machines.
Assign task
a to machine M1,
b to machine M2, ….
task g to machine M7.
This assignment is not an optimal assignment because other assignment use fewer machines.
For example: we can assign tasks a, b and d to the same machine, reducing the number of utilized
machines to five.

9/26/2024
Greedy Way to Machine Scheduling
A greedy way to obtain an optimal task assignment is
to assign the tasks in stages, one task per stage and in
nondecreasing order of task start tines.
Call a machine old if at least one task have been
assigned to it. If a machine is not old it is new.
Machine Selection
 Use the greedy criterion: If an old machine becomes
available by the start time of the task to be assigned,
assign the task to this machine; if not , assign it to a new
machine.

9/26/2024
Greedy Way to Machine Scheduling (Cont.)
Example 4.2
 For the given data, the tasks in nondecreasing order of
task start time are as a, f, b, c, g, e, d. The greedy
algorithm assigns tasks to machines in this order.
 The algorithm has n = 7 stages, and in each stage one
task is assigned to a machine.
 The following is an optimal assignment of the tasks that
requires only three machine.
 No other assignment will require less than three
machines.

9/26/2024
Optimal Machine Scheduling

a) Seven tasks

Time
b) Scheduling

9/26/2024
Container Loading Problem

9/26/2024
Greedy way (Container Loading)
 The ship may be loaded in stages ; one container per
stage
 At each stage, we have to select a container to load. For
this decision we may use the greedy criterion:
 From the remaining containers, select the one with least weight.
 This order of selection will keep the total weight of the selected
containers minimum and hence leave maximum capacity for
loading more containers
 Using the greedy algorithm , we first select the container that has
least weight.
 Then the one with the next smallest weight, and so on until either
all containers have been loaded or there isn’t enough capacity for
the next one.

9/26/2024
Container Loading (Example 4:3)

9/26/2024
Algorithm Container Loading

9/26/2024
Knapsack Problem
Problem definition
 Given n objects and a knapsack where object i has a weight wi and
the knapsack has a capacity m
 If a fraction xi of object i placed into knapsack, a profit pixi is earned
 The objective is to obtain a filling of knapsack maximizing the total
profit

 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

9/26/2024
Knapsack Problem (Example)
 Example 4.4: Consider the following instance of the knapsack
problem: n = 3, m=20, (p1,p2,p3) =(25, 24,15), and (w1,w2,w3)
=(18,15,10). Four feasible solutions are:

Of these four feasible solutions, solution 4 yields the maximum profit.


As we shall soon see, this solution is optimal for the given problem
instance.

9/26/2024
9/26/2024
Exercise
Find an optimal solution to the knapsack instance
n =7
m =1 5
(p1, p2, …, p7) = (10, 5, 15, 7, 6, 18, 3)
(w1, w2, …, w7) = (2, 3, 5, 7, 1, 4, 1)

9/26/2024
Knapsack Problem (Complexity)
Time complexity
• Sorting: O(n log n) using fast sorting algorithm like
merge sort
• Greedy Knapsack: O(n)
• So, total time is O(n log n)

• If p1/w1 ≥ p2/w2 ≥ … ≥ pn/wn, then Greedy Knapsack


generates an optimal solution to the given instance of
the knapsack problem.

9/26/2024

You might also like