Unit-5 - Greedy Algorithm
Unit-5 - Greedy Algorithm
While s ≠ n do
x the largest item in C such that s + x <= n
S S ∪ { a coin of value x}
ss+x
}
By: Madhuri Vaghasia(Assistant
11
Professor)
Activity selection problem
• Several competing activities require exclusive use of a common
resource.
• 3. Prove that at any stage of the recursion, one of the optimal choices is
the greedy choice. Thus, it is always safe to make the greedy choice.
• 4. Show that all but one of the sub problems induced by having made
the greedy choice are empty.
– If an optimal solution to sub problem Sij includes an activity ak, then it must also contain
optimal solutions to the sub problems Sik and Skj.
– Given this optimal substructure, we argued that if we knew which activity to use as a k, we
could construct an optimal solution to Sij by selecting ak along with all activities in optimal
solutions to the sub problems Sik and Skj.
– Based on this observation of optimal substructure, we were able to devise the recurrence
that described the value of an optimal solution.
By: Madhuri Vaghasia(Assistant
24
Professor)
Minimum Spanning Trees
• Let G = <V, E> be an undirected, connected graph,
having vertices V and edges E.
• A sub – graph T = <V’, E’> of G is a spanning tree
of G if T is a tree.
• An undirected graph with |V| = n, may have as
many as n(n-1)/2 edges, but a spanning tree T will
have exactly (n-1) edges.
T T ∪ {e}
B B ∪ {v}
return T
}
By: Madhuri Vaghasia(Assistant
33
Professor)
Prim’s algorithm
• Example
{greedy loop}
repeat n – 2 times
• For i = 1, 2, … n, object I has a positive weight wi and a positive value Vi. The
knapsack can carry a weight not exceeding W.
• Our aim is to fill the knapsack in a way that maximizes the value of the included
objects, while respecting the capacity constraint.
• We assume that the objects can be broken into smaller pieces, so we may
decide to carry only a fraction xi of object i, where 0 <= xi <=1.
• In this case, object I contributes xiwi to the total weight in the knapsack, and
xivi to the value of the load.
• In symbols, the problem can be stated as follows:
By: Madhuri Vaghasia(Assistant
38
Professor)
The Knapsack problem
for I = 1 to n do x[i] 0
weight 0
{greedy loop}
while weight < W do
i the best remaining object (will see v/w)
if weight + w[i] <= W then x[i] 1
weight weight + w[i]
else
x[i] (W - weight)/w[i]
weight W
return x
}
By: Madhuri Vaghasia(Assistant
41
Professor)
The Knapsack problem
• There are at least three possible selection functions for
this problem:
W 10 20 30 40 50
V 20 30 66 40 60
Order Time
123 5 + (5 + 10) + (5 + 10 + 3) = 38
132 5 + (5 + 3) + (5 + 3 + 10) = 31
213 10 + (10 + 5) + (10 + 5 + 3) = 43
231 10 + (10 + 3) + (10 + 3 + 5) = 41
312 3 + (3 + 5) + (3 + 5+ 10) = 29
321 3+ (3 + 10) + (3 + 10 + 5) = 34
By: Madhuri Vaghasia(Assistant
48
Professor)
Job Scheduling
• Minimizing time in the system
i = 1 2 3 4
gi = 50 10 15 30
di = 2 1 2 1
By: Madhuri Vaghasia(Assistant
50
Professor)
Job Scheduling
• Scheduling with deadlines
– The schedules to consider and the corresponding
profits are
Order Profit
1 50
2 10
3 15
4 30
1, 3 65
2, 1 60
2, 3 24
3, 1 65
4, 1 80
By: Madhuri Vaghasia(Assistant
51
4, 3 Professor) 45
Job Scheduling
• Scheduling with deadlines