Algorithm CH 4 Lec 1
Algorithm CH 4 Lec 1
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
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:
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)
9/26/2024