Algo Lecture9 GreedyAlgorithm
Algo Lecture9 GreedyAlgorithm
Tanjina Helaly
DESIGNING AND ANALYZING EFFICIENT
ALGORITHMS
Running time
complexity = O(n)
ACTIVITY SELECTION PROBLEM –
RECURSIVE SOLUTION
s -> the set of start time
f -> the set of finish time.
k -> index of last selected activity
n -> number of activities
Running time
complexity = O(n)
Running time
complexity = O(n)
Running time
complexity = O(n)
Conclusion:
Including item 1 doesn’t give optimal solution. Rather
excluding does.
Greedy algorithm doesn’t give optimal solution
for 0-1 knapsack problem.
FRACTIONAL KNAPSACK PROBLEM
For the fractional knapsack problem, taking the
items in order of greatest value per pound yields
an optimal solution.
FRACTIONAL KNAPSACK ALGORITHM -
ITERATIVE
v → the set of values of items
w → the set of weights of items
c → capacity (weight need to be filled in) of the knapsack.
KS(c, v, w)
sort the item according to v/w in descending order and store in I
i =0, frac=1;
tVal=0;
n = I.length;
Running time complexity including
while (c>0 && i<n)
sorting = O(nlogn)+O(n) = O(nlogn)
if(w[i]<=c) frac = 1;
else frac = c/w[i] Running time complexity without
sorting = O(n)
c = c – frac*w[i]
tVal += fract*v[i]
return tVal;
FRACTIONAL KNAPSACK ALGORITHM -
RECURSIVE
I → sorted items according to v/w in descending order
c → capacity (weight need to be filled in) of the knapsack.
n → number of items
i→ current item
Running time complexity including
sorting = O(nlogn)+O(n) = O(nlogn)
KS(c, I, i, n)
if (c<=0 or i>n) return 0; Running time complexity without
sorting = O(n)
if (c < I[i].weight)
frac = c/I[i].weight
return frac* I[i].value + KS(0, I, i+1, n)
else
return I[i].value + KS(c-I[i].weight, I, i+1, n)
0-1 KNAPSACK ALGORITHM - ITERATIVE
v → the set of values of items
w → the set of weights of items
c → capacity (weight need to be filled in) of the knapsack.
KS(c, v, w)
sort the item according to v/w in descending order and store in I
i =0, frac=1;
tVal=0;
n = I.length;
Running time complexity including
while (c>0 && i<n)
sorting = O(nlogn)+O(n) = O(nlogn)
if(w[i]<=c)
c = c – w[i] Running time complexity without
sorting = O(n)
tVal += v[i]
return tVal;
0-1 KNAPSACK ALGORITHM - RECURSIVE
I → sorted items according to v/w in descending order
c → capacity (weight need to be filled in) of the knapsack.
n → number of items
i→ current item
Running time complexity including
sorting = O(nlogn)+O(n) = O(nlogn)
KS(c, I, i, n)
if (c<=0 or i>n) return 0; Running time complexity without
sorting = O(n)
if (c < I[i].weight)
return KS(c, I, i+1, n)
else
return I[i].value + KS(c-I[i].weight, I, i+1, n)
ANOTHER EXAMPLE
Assume you are a busy person. You have exactly
T time to do some interesting things and you
want to do maximum such things.
Objective:
Maximize the number of interesting things to
complete.
Constraint:
Need to finish the works at T time.
SOLUTION OF EXAMPLE
You are given an array A of integers, where each
element indicates the time a thing takes for
completion. You want to calculate the maximum
number of things that you can do in the limited time
that you have.
This is a simple Greedy-algorithm problem.
In each iteration, you have to greedily select the things
which will take the minimum amount of time to complete.
Steps
Sort the array A in a non-decreasing order.
Select one item at a time
Complete the item if you have enough time (item’s time is less
than your available time.)
Add one to numberOfThingsCompleted.
SO WHEN SHOULD WE USE GREEDY
ALGORITHM?
ELEMENTS OF GREEDY STRATEGY
An greedy algorithm makes a sequence of
choices, each of the choices that seems best at the
moment is chosen
NOT always produce an optimal solution
Problems that has the following 2 properties are
good candidates for greedy algorithm.
Greedy-choice property
Optimal substructure
GREEDY-CHOICE PROPERTY
A globally optimal solution can be arrived at by
making a locally optimal (greedy) choice
Make whatever choice seems best at the moment and
then solve the sub-problem arising after the choice is
made
The choice made by a greedy algorithm may depend on
choices so far, but it cannot depend on any future
choices or on the solutions to sub-problems
Of course, we must prove that a greedy choice at
each step yields a globally optimal solution
OPTIMAL SUBSTRUCTURES
A problem exhibits optimal substructure if an
optimal solution to the problem contains within it
optimal solutions to sub-problems
Example- For Activity Selection Problem
If an optimal solution A to S begins with activity 1, then A’
= A – {1} is optimal to S’={i S: si f1}
APPLICATION
Activity selection problem
Interval partitioning problem