Unit 2 : Greedy Strategy · Task Scheduling Algorithm Greedy choice: consider tasks by their start...
Transcript of Unit 2 : Greedy Strategy · Task Scheduling Algorithm Greedy choice: consider tasks by their start...
1Reena Pagare MITCOE SPPU
Unit 2 : Greedy Strategy
Reena Pagare
June 2016
2Reena Pagare MITCOE SPPU
Example: Counting money
Suppose you want to count out a certain amount of money, using the fewest possible Rs note and coins
At each step, take the largest possible note or coin that does not overshoot
Example: To make Rs 61 , you can choose:
a Rs 50 note
a Rs 1 coin, to make Rs 51
a Rs 10 note or Rs10 coin , to make Rs 61
3Reena Pagare MITCOE SPPU
Greedy Algorithm Paradigm Characteristics of greedy algorithms:
make a sequence of choices
each choice is the one that seems best so far, only depends on what's been done so far
choice produces a smaller problem to be solved
In order for greedy heuristic to solve the problem, it must be that the optimal solution to the big problem contains optimal solutions to sub problems
4Reena Pagare MITCOE SPPU
Elements of Greedy Strategy
Determine the optimal substructure of the problem
Develop a recursive solution
Show that if we make the greedy choice then only one
subproblem remains
Prove that it is always sake o make the greedy choice
Develop a recursive algorithm that implements the
greedy strategy
Convert the recursive algorithm to an iterative algorithm
5Reena Pagare MITCOE SPPU
Designing a Greedy Algorithm
Cast the problem so that we make a greedy (locally
optimal) choice and are left with one subproblem
Prove there is always a (globally) optimal solution to the
original problem that makes the greedy choice
Show that the choice together with an optimal solution
to the subproblem gives an optimal solution to the
original problem
6Reena Pagare MITCOE SPPU
Optimization problems
An optimization problem is one in which you want
to find, not just a solution, but the best solution
A “greedy algorithm” sometimes works well for
optimization problems
A greedy algorithm works in phases. At each
phase:
You take the best you can get right now, without regard
for future consequences
You hope that by choosing a local optimum at each
step, you will end up at a global optimum
7Reena Pagare MITCOE SPPU
A failure of the greedy algorithm
In some (fictional) monetary system, “coins” come in Rs 1 coin, Rs 7 coin, and Rs 10 coins
Using a greedy algorithm to count out Rs 15 , you would get
A Rs 10 coin
Five Rs 1 coins, for a total of Rs 15
This requires six coins
A better solution would be to use two Rs 7 coin pieces and one Rs 1 coin piece
This only requires three coins
The greedy algorithm results in a solution, but not in an optimal solution
8Reena Pagare MITCOE SPPU
Terms in Greedy Methods
N inputs – {1 ,7, 10 } rs or coins
Subset that satisfies some constraints {10,1} or {7,7,1}
This is feasible solution
Minimizes or Maximizes – Objective function {Total
is Rs 15 ; min no of coins or notes }
A feasible solution that does satisfies the objective
function is called an optimal solution. --- {7,7,1}
Feasible solution need not be optimal.
9Reena Pagare MITCOE SPPU
Greedy Strategy : Control Abstraction
10Reena Pagare MITCOE SPPU
Greedy Strategy : Control Abstraction
SELECT : Selects an input from A , removes it and
assign value to x
FEASIBLE : Boolean – valued function which
determines if can be included into the solution vector.
UNION : combines xth solution and updates the
objective function
Procedure GREEDY : Algorithm
11Reena Pagare MITCOE SPPU
Some Greedy Algorithms
fractional knapsack algorithm
Huffman codes
Kruskal's MST algorithm
Prim's MST algorithm
Dijkstra's SSSP algorithm
Activity Selection
Scheduling Jobs/Tasks
….
12Reena Pagare MITCOE SPPU
Example
Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with weight at most W.
Weight:
Benefit:
1 2 3 4 5
4 ml 8 ml 2 ml 6 ml 1 ml
$12 $32 $40 $30 $50
Items:
Value: 3($ per ml)
4 20 5 50
10 ml
Solution:• 1 ml of 5
• 2 ml of 3
• 6 ml of 4
• 1 ml of 2
“knapsack”
13Reena Pagare MITCOE SPPU
The Fractional Knapsack Problem
Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with weight at most W.
If we are allowed to take fractional amounts, then this is the fractional knapsack problem.
In this case, we let xi denote the amount we take of item i
Objective: maximize
Constraint:
Si
iii wxb )/(
Si
i Wx
14Reena Pagare MITCOE SPPU
Knapsack Problem
15Reena Pagare MITCOE SPPU
Greedy Algorithm
16Reena Pagare MITCOE SPPU
The Fractional Knapsack Algorithm Greedy choice: Keep taking
item with highest value(benefit to weight ratio)
Since
Run time: O(n log n). Why?
Correctness: Suppose there is a better solution
there is an item i with higher value than a chosen item j, but xi<wi, xj>0 and vi<vj
If we substitute some i with j, we get a better solution
How much of i: min{wi-xi, xj}
Thus, there is no better solution than the greedy one
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi
and weight wi; max. weight WOutput: amount xi of each item i
to maximize benefit w/ weight at most W
for each item i in S
xi 0
vi bi / wi {value}
w 0 {total weight}
while w < W
remove item i w/ highest vi
xi min{wi , W - w}
w w + min{wi , W - w}
Si
iii
Si
iii xwbwxb )/()/(
17Reena Pagare MITCOE SPPU
Example - Knapsack
18Reena Pagare MITCOE SPPU
Different Methods
Method 1 : Max Profit (solution 2)
Method 2 : Minimum weight (solution 3)
Method 3 : Min profit/weight ratio (solution 4)
19Reena Pagare MITCOE SPPU
Task Scheduling
Given: a set T of n tasks, each having:
A start time, si
A finish time, fi (where si < fi)
Goal: Perform all the tasks using a minimum number of “machines.”
1 98765432
Machine 1
Machine 3
Machine 2
20Reena Pagare MITCOE SPPU
Task Scheduling Algorithm
Greedy choice: consider tasks by their start time and use as few machines as possible with this order.
Run time: O(n log n). Why?
Correctness: Suppose there is a better schedule.
We can use k-1 machines
The algorithm uses k
Let i be first task scheduled on machine k
Machine i must conflict with k-1 other tasks
But that means there is no non-conflicting schedule using k-1 machines
Algorithm taskSchedule(T)
Input: set T of tasks w/ start time si
and finish time fi
Output: non-conflicting schedule with minimum number of machines
m 0 {no. of machines}
while T is not empty
remove task i w/ smallest si
if there’s a machine j for i then
schedule i on machine j
else
m m + 1
schedule i on machine m
21Reena Pagare MITCOE SPPU
Example
Given: a set T of n tasks, each having:
A start time, si
A finish time, fi (where si < fi)
[1,4], [1,3], [2,5], [3,7], [4,7], [6,9], [7,8] (ordered by start)
Goal: Perform all tasks on min. number of machines
1 98765432
Machine 1
Machine 3
Machine 2
22Reena Pagare MITCOE SPPU
A scheduling problem
You have to run nine jobs, with running times of 3, 5, 6, 10, 11,
14, 15, 18, and 20 minutes
You have three processors on which you can run these jobs
You decide to do the longest-running jobs first, on whatever
processor is available
20
18
15 14
11
10
6
5
3P1
P2
P3
Time to completion: 18 + 11 + 6 = 35 minutes
This solution isn’t bad, but we might be able to do better
23Reena Pagare MITCOE SPPU
Another approach
What would be the result if you ran the shortest job first?
Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20
minutes
That wasn’t such a good idea; time to completion is now
6 + 14 + 20 = 40 minutes
Note, however, that the greedy algorithm itself is fast
All we had to do at each stage was pick the minimum or maximum
20
18
15
14
11
10
6
5
3P1
P2
P3
24Reena Pagare MITCOE SPPU
An optimum solution
This solution is clearly optimal (why?)
Clearly, there are other optimal solutions (why?)
How do we find such a solution?
One way: Try all possible assignments of jobs to processors
Unfortunately, this approach can take exponential time
Better solutions do exist:
20
18
15
14
11
10 6
5
3
P1
P2
P3
25Reena Pagare MITCOE SPPU
Activity-selection Problem
Input: Set S of n activities, a1, a2, …, an.
si = start time of activity i.
fi = finish time of activity i.
Output: Subset A of maximum number of compatible
activities.
Two activities are compatible, if their intervals don’t overlap.
Example: Activities in each line
are compatible.
26Reena Pagare MITCOE SPPU
Optimal Substructure
Suppose an optimal solution includes activity ak.
» This generates two subproblems.
» Selecting from a1, …, ak-1, activities compatible with one
another, and that finish before ak starts (compatible with ak).
Assume activities are sorted by finishing times.
» f1 f2 … fn.
» Selecting from ak+1, …, an, activities compatible with one
another, and that start after ak finishes.
» The solutions to the two subproblems must be optimal.
27Reena Pagare MITCOE SPPU
Recursive Solution
ijjki
ij
Sjkckic
Sjic if}1],[],[max{
if0],[
Recursive
Solution:
Let Sij = subset of activities in S that start after ai
finishes and finish before aj starts.
Subproblems: Selecting maximum number of mutually
compatible activities from Sij.
Let c[i, j] = size of maximum-size subset of mutually
compatible activities in Sij.
28Reena Pagare MITCOE SPPU
Greedy-choice Property
The problem also exhibits the greedy-choice property.
There is an optimal solution to the subproblem Sij, that includes the activity with the smallest finish time in set Sij.
Hence, there is an optimal solution to S that includes a1.
Therefore, make this greedy choice without solving subproblems first and evaluating them.
Solve the subproblem that ensues as a result of making this greedy choice.
Combine the greedy choice and the solution to the subproblem.
29Reena Pagare MITCOE SPPU
Recursive Algorithm
Recursive-Activity-Selector (s, f, i, j)
1. m i+1
2. while m < j and sm < fi
3. do m m+1
4. if m < j
5. then return {am}
Recursive-Activity-Selector(s, f, m, j)
6. else return
Initial Call: Recursive-Activity-Selector (s, f, 0, n+1)
Complexity: (n)
Straightforward to convert the algorithm to an iterative one.
30Reena Pagare MITCOE SPPU
Typical Steps
Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve.
Prove that there’s always an optimal solution that makes the greedy choice, so that the greedy choice is always safe.
Show that greedy choice and optimal solution to subproblem optimal solution to the problem.
Make the greedy choice and solve top-down.
May have to preprocess input to put it into greedy order.
Example: Sorting activities by finish time.
31Reena Pagare MITCOE SPPU
Elements of Greedy Algorithms
Greedy-choice Property.
A globally optimal solution can be arrived at by making a
locally optimal (greedy) choice.
Optimal Substructure.
32Reena Pagare MITCOE SPPU
Example of Activity selection
I 1 2 3 4 5 6 7 8 9 10 11
Si 1 3 0 5 3 5 6 8 8 2 12
Fi 4 5 6 7 9 9 10 11 12 14 16
For this example the subset {a3,a9,a11} consists of mutually
compatible activities. It is not a maximum subset, however
,since the subset {a1,a4,a8,a11} is larger. In fact
{a1,a4,a8,a11} is a largest subset of mutually compatible
activities; another largest subset is {a2,a4,a9,a11}.
33Reena Pagare MITCOE SPPU
Iterative Greedy Activity Selection
34Reena Pagare MITCOE SPPU
Huffman encoding
The Huffman encoding algorithm is a greedy algorithm
You always pick the two smallest numbers to combine
Average bits/char:
0.22*2 + 0.12*3 +
0.24*2 + 0.06*4 +
0.27*2 + 0.09*4
= 2.42
The Huffman
algorithm finds an
optimal solution22 12 24 6 27 9
A B C D E F
15
27
46
54
100
A=00
B=100
C=01
D=1010
E=11
F=1011
35Reena Pagare MITCOE SPPU
Minimum spanning tree
A minimum spanning tree is a least-cost subset of the edges of a graph that connects all the nodes
Start by picking any node and adding it to the tree
Repeatedly: Pick any least-cost edge from a node in the tree to a node not in the tree, and add the edge and new node to the tree
Stop when all nodes have been added to the tree
The result is a least-cost (3+3+2+2+2=12) spanning tree
If you think some other edge should be in the spanning tree:
Try adding that edge
Note that the edge is part of a cycle
To break the cycle, you must remove the edge with the greatest cost
This will be the edge you just added
1
2
3
4
5
6
3 3
3
3
2
2
2
4
4
4
36Reena Pagare MITCOE SPPU
Traveling salesman
A salesman must visit every city (starting from city A), and wants to cover the least possible distance
He can revisit a city (and reuse a road) if necessary
He does this by using a greedy algorithm: He goes to the next nearest city from wherever he is
From A he goes to B
From B he goes to D
This is not going to result in a shortest path!
The best result he can get now will be ABDBCE, at a cost of 16
An actual least-cost path from Ais ADBCE, at a cost of 14
E
A B C
D
2
3 3
4
4 4
37Reena Pagare MITCOE SPPU
Analysis A greedy algorithm typically makes (approximately) n choices
for a problem of size n (The first or last choice may be forced)
Hence the expected running time is:O(n * O(choice(n))), where choice(n) is making a choice among n objects
Counting: Must find largest useable coin from among k sizes of coin (k is a constant), an O(k)=O(1) operation;
Therefore, coin counting is (n)
Huffman: Must sort n values before making n choices
Therefore, Huffman is O(n log n) + O(n) = O(n log n)
Minimum spanning tree: At each new node, must include new edges and keep them sorted, which is O(n log n) overall
Therefore, MST is O(n log n) + O(n) = O(n log n)
38Reena Pagare MITCOE SPPU
Other greedy algorithms Dijkstra’s algorithm for finding the shortest path in a
graph
Always takes the shortest edge connecting a known node to an unknown node
Kruskal’s algorithm for finding a minimum-cost spanning tree
Always tries the lowest-cost remaining edge
Prim’s algorithm for finding a minimum-cost spanning tree
Always takes the lowest-cost edge between nodes in the spanning tree and nodes not yet in the spanning tree
39Reena Pagare MITCOE SPPU
Dijkstra’s shortest-path algorithm Dijkstra’s algorithm finds the shortest paths from a given node to
all other nodes in a graph
Initially,
Mark the given node as known (path length is zero)
For each out-edge, set the distance in each neighboring node equal to the cost
(length) of the out-edge, and set its predecessor to the initially given node
Repeatedly (until all nodes are known),
Find an unknown node containing the smallest distance
Mark the new node as known
For each node adjacent to the new node, examine its neighbors to see whether
their estimated distance can be reduced (distance to known node plus cost of
out-edge)
If so, also reset the predecessor of the new node
40Reena Pagare MITCOE SPPU
Analysis of Dijkstra’s algorithm I
Assume that the average out-degree of a node is some
constant k
Initially,
Mark the given node as known (path length is zero)
This takes O(1) (constant) time
For each out-edge, set the distance in each neighboring node equal to
the cost (length) of the out-edge, and set its predecessor to the initially
given node
If each node refers to a list of k adjacent node/edge pairs, this
takes O(k) = O(1) time, that is, constant time
Notice that this operation takes longer if we have to extract a list
of names from a hash table
41Reena Pagare MITCOE SPPU
Analysis of Dijkstra’s algorithm II
Repeatedly (until all nodes are known), (n times)
Find an unknown node containing the smallest distance
Probably the best way to do this is to put the unknown nodes into a
priority queue; this takes k * O(log n) time each time a new node is
marked “known” (and this happens n times)
Mark the new node as known -- O(1) time
For each node adjacent to the new node, examine its neighbors to
see whether their estimated distance can be reduced (distance to
known node plus cost of out-edge)
If so, also reset the predecessor of the new node
There are k adjacent nodes (on average), operation requires constant
time at each, therefore O(k) (constant) time
Combining all the parts, we get:
O(1) + n*(k*O(log n)+O(k)), that is, O(nk log n) time
42Reena Pagare MITCOE SPPU
Connecting wires
There are n white dots and n black dots, equally spaced, in a line
You want to connect each white dot with some one black dot,
with a minimum total length of “wire”
Example:
Total wire length above is 1 + 1 + 1 + 5 = 8
Do you see a greedy algorithm for doing this?
Does the algorithm guarantee an optimal solution?
Can you prove it?
Can you find a counterexample?
43Reena Pagare MITCOE SPPU
Collecting coins
A checkerboard has a certain number of coins on it
A robot starts in the upper-left corner, and walks to the bottom left-hand corner
The robot can only move in two directions: right and down
The robot collects coins as it goes
You want to collect all the coins using the minimumnumber of robots
Example: Do you see a greedy algorithm for
doing this?
Does the algorithm guarantee an
optimal solution?
Can you prove it?
Can you find a counterexample?
44Reena Pagare MITCOE SPPU
References
Slides adapted from
Len/Devi Fall 2003
Prof. Evdokia Nikolova , Spring 2013
Goodrich Tamassia 2004
Books
Horowitz Sahani, “Fundamentals of Algorithms”
Cormen , “Introduction to Algorithms”