Greedy Method
Greedy Method
Unit-2
1
The Greedy method
• Straight forward design technique
• It can be applied to wide variety of problems
• Most of such problems have n inputs and required to
obtain a subset that satisfies some constraints
• Any subset that satisfies these constraints is called a
feasible solution
• Find feasible solution that either maximizes or
minimizes a given objective function.
• The feasible solution that do this, is called optimal
solution
2
Subset Paradigm
• Algorithm works in stages, considering one input at a
time
• At each stage decision is made regarding, whether a
particular input is in an optimal solution
• This is done by considering the inputs in an order
determined by some selection procedure.
• If inclusion of the next input into the partially
constructed optimal solution will result in an infeasible
solution,
• then this input is not added to the partial solution,
Otherwise it is added
• Selection procedure is based on objective function 3
Ordering Paradigm
• Do not call for the selection of an optimal subset
4
Knapsack Problem
5
Knapsack Problem
• Objective is to obtain a filling of the
knapsack that maximizes the total profit.
• Formally the problem can be stated as
• Maximize
∑ pi xi
1≤i≤n
• Subject to
∑ wi xi ≤ m
1≤i≤n
• And 0 ≤ xi ≤ 1 1≤i≤n
6
Greedy Approaches
0/1 Knapsack
7
Example
• Example 1
• n=3 m=20
• (p1, p2, p3) = (25,24,15)
• (w1, w2, w3) = (18, 15, 10)
• Example 2
• n=7 m=15
• (p1, p2, p3, p4, p5, p6, p7) = (10,5,15,7,6,18,3)
• Example 4
• n=6 c=20
• (p1, p2, p3, p4, p5, p6) = (12,5,15,7,6,18)
10
Job Sequencing with Deadlines
• Given a set of n jobs
– Each job is associated with an integer
deadline di ≥ 0 and profit pi ≥0
– For any job profit pi will be earned iff the
job is completed by its deadline
– Each job takes unit time
– Only one machine (server) is available.
11
Job Sequencing with Deadlines
– Feasible solution:
subset J of jobs such that
each job in J is completed within its
deadline
– Optimal solution:
Feasible solution with highest profit
12
Solution
• Greedy Method
– Sorting jobs based on profit ↓
– Sorting jobs based on deadlines ↑
• Implementation
– Using array
13
Example
14
Example
• n=4 , (p1, p2, p3, p4) = (100,10,15,27)
(d1, d2, d3 , d4) = (2,1,2,1)
• Example 2 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (3,5,20,18,1,6,30)
16
Example
• Example 3 n=5 ,
• (p1, p2, p3, p4 , p5) = (45,15,20,7,65)
• Example 4 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (50,15,18,16,8,25,60)
17
Example
• Example 5 n=5 ,
• (p1, p2, p3, p4 , p5) = (20,16,11,5,25)
• Example 6 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (45,5,20,18,6,30,70)
18
Job Sequencing with Deadlines
• Variants
– Processing time is different
– Multiple servers
– Profit factors different if executed on
different server
19
Minimum Cost Spanning Tree
•Spanning Tree-
•Let G=(V,E) be undirected connected
graph.
•A sub-graph t=(V,E’) of G is spanning tree
•iff t is tree.
20
•Weighted graph:
•Weights assigned to edges.
23
24
Prim’s Algorithm
• After every step, graph we get is
connected
• Complexity – O(n2)
25
Kruskal’s Algorithm
Steps
1. Sort the edge list on weight in non decreasing
order.
2. Select next edge and include it in the subset if
it does not form cycle.
3. Go to step 2 if all vertices are not included in
subset.
26
27
28
Kruskal’s Algorithm
29
Application of MST
30
Optimal Storage on Tapes
• There are n programs that are to be stored on
a computer tape of length l
• Associated with each program i is a length li, 1
≤i≤n
• All programs can be store on tape if and only if
the sum of the length of the programs is at
most l
• Whenever a program is to be retrieved from
this tape, the tape is initially positioned at the
front.
31
Optimal Storage on Tapes
• If the programs are stored in order
• i1,i2,i3,…in
• The time tj needed to retrieve the program ij is
proportional to
∑ l ik
1≤k≤j
Minimize d(I) = ∑ ∑ l ik
1≤j≤n 1≤k≤j
O(n log n)
Sort in non-decreasing order 33
Example
• n=3 (l1,l2,l3)=(5,10,3)
• There are n! possible orderings
• Ordering d(I)
• 1,2,3 =38
• 1,3,2 =31
• 2,1,3 =43
• 2,3,1 =41
• 3,1,2 =29
• 3,2,1 =34
34
Optimal Merge Pattern
• When two or more sorted files are to be merged
together,
• the merge can be accomplished by repeatedly
merging sorted files in pairs.
• Approach
– Sort the files based on no of records
– Form pairs
– Merge pairs, Go to above step till complete
– Wrong results. 35
Optimal Merge Pattern
• Revised Approach
– Sort the files based on no of records
– Merge first two files, Go to above step till
complete
– Wrong results.
36
Optimal Merge Pattern
• Re-revised Approach
1 Sort the files based on no of records
2 Select two smallest files,
3 merge them,
4 Place the resultant file at proper position in sorted list
5 repeat step 2 to 4 till all files are merged.
• Example.
1 2 3 4 5
10 5 9 6 12
Find an optimal binary merge pattern for ten
files28,32,12,5,84,53,91,35,3, and 11. whose
37
lengths are
Optimal Merge Pattern
Find an optimal binary merge pattern for ten
files whose lengths are
38
Optimal Merge Pattern
39
Huffman Code
40
SINGLE-SOURCE SHORTEST PATHS
• Graphs can be used to represent the highway
structure of a state or country
• vertices representing cities and edges representing
roads
• The edges can then be assigned weights
• which may be either the distance between two cities
connected by the edge or the average time to drive
41
SINGLE-SOURCE SHORTEST PATHS
• A motorist wishing to drive from city A to B
• would be interested in answers to the following
questions:
• Is there a path from A to B
• If there is more than one path from A to B, which is
the shortest path ?
42
SINGLE-SOURCE SHORTEST PATHS
• The length of a path is defined to be the sum of the
weights of the edges on that path.
• The starting vertex of the path is referred to as the
source, and the last vertex the destination.
• The graphs are digraphs to allow for one-way streets.
• In the problem we consider, we are given a directed
graph G = (V,E),
• a weighting function cost for the edges of G,
• and a source vertex v0.
43
SINGLE-SOURCE SHORTEST PATHS
• The problem is to determine the shortest paths from
v0 to all the remaining vertices of G.
• It is assumed that all the weights are positive.
• The shortest path between v0 and some other node v
is an ordering among a subset of the edges.
• Hence this problem fits the ordering paradigm.
44
45
46
SINGLE-SOURCE SHORTEST PATHS
47
SINGLE-SOURCE SHORTEST PATHS
48
SINGLE-SOURCE SHORTEST PATHS
49
50