0% found this document useful (0 votes)
37 views

Greedy Method

The document discusses various greedy algorithms and problems that can be solved using greedy approaches. It describes the greedy paradigm of making locally optimal choices at each step to arrive at a global optimal solution. Specific problems covered include knapsack problem, job sequencing with deadlines, minimum spanning trees, optimal storage on tapes, and optimal merge patterns. Examples are provided to illustrate greedy solutions for each problem.

Uploaded by

Siddharth Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Greedy Method

The document discusses various greedy algorithms and problems that can be solved using greedy approaches. It describes the greedy paradigm of making locally optimal choices at each step to arrive at a global optimal solution. Specific problems covered include knapsack problem, job sequencing with deadlines, minimum spanning trees, optimal storage on tapes, and optimal merge patterns. Examples are provided to illustrate greedy solutions for each problem.

Uploaded by

Siddharth Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Computer Algorithms

Unit-2

The Greedy method

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

• Make decisions by considering the inputs in some


order

• Each decision is made using an optimization criterion


that can be computed using decisions already made

4
Knapsack Problem

• Given n items and a knapsack (or a bag) of


capacity m.
• Associated with each item (i) is the weight
(wi) and the profit earned.
• If the fraction of item xi is placed in knapsack
• then profit of pixi is earned. 0<=xi<=1

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

•Sort items based on profit in decreasing order


place items till knapsack is full.
•Sort items based on weight in increasing
order place items till knapsack is full.
• Sort items based on profit/weight decreasing,
place items till knapsack is full.

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)

• (w1, w2, w3, w4, w5, w6, w7) = (2,3,5,7,1,4,1)


8
 (0,1,1/2) 20 31.5
Example
• Example 3
• n=4 m=25
• (p1, p2, p3, p4 ) = (2, 5, 8, 1)
• (w1, w2, w3 , w4) = (10, 15, 6, 9)

• Example 4
• n=6 c=20
• (p1, p2, p3, p4, p5, p6) = (12,5,15,7,6,18)

• (w1, w2, w3, w4, w5, w6) = (2,3,5,7,1,5)


9
Example
• Example 5
• n=4 m=30
• (p1, p2, p3, p4 ) = (27, 20, 24, 15)
• (w1, w2, w3 , w4) = (15, 10, 18, 10)

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)

Feasible Solution Processing Sequence Value


• (1,2) 2,1 110
• (1,3) 1,3 or 3,1 115
• (1,4) 4,1 127
• (2,3) 2,3 25
• (3,4) 4,3 42
• (1) 1 100
• (2) 2 10
• (3) 3 15
• (4) 4 27
15
Example
• Example 1 n=5 ,
• (p1, p2, p3, p4 , p5) = (20,15,10,5,1)

• (d1, d2, d3 , d4 , d5) = (2,2,1,3,3)

• Example 2 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (3,5,20,18,1,6,30)

• (d1, d2, d3, d4, d5, d6, d7) = (1,3,4,3,2,1,2)

16
Example
• Example 3 n=5 ,
• (p1, p2, p3, p4 , p5) = (45,15,20,7,65)

• (d1, d2, d3 , d4 , d5) = (1,3,2,1,2)

• Example 4 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (50,15,18,16,8,25,60)

• (d1, d2, d3, d4, d5, d6, d7) = (1,3,4,3,2,1,2)

17
Example
• Example 5 n=5 ,
• (p1, p2, p3, p4 , p5) = (20,16,11,5,25)

• (d1, d2, d3 , d4 , d5) = (2,2,1,2,1)

• Example 6 n=7
• (p1, p2, p3, p4, p5, p6, p7) = (45,5,20,18,6,30,70)

• (d1, d2, d3, d4, d5, d6, d7) = (1,3,4,3,2,1,2)

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.

•Minimum Spanning Tree


• A spanning tree with minimum sum of
weights

 It includes all vertices connected and


sum of weight is minimum
21
Prim’s Algorithm
Steps
1. Select edge with smallest weight, include it in
subset.
2. Select next adjacent vertex with minimum
weight.
3. Include it in the subset if it does not form cycle.
4. Go to step 2 if all vertices are not included in
subset.
22
Prim’s Algorithm

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

• After every step graph we get may not be


connected graph
• Complexity – O(E log E)

29
Application of MST

• Network bandwidth management-


• Minimum bandwidth required to pass
message from one node to another

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

• If all programs are retrieved equally often, then


expected or mean retrieval time is (MRT) is
(1/n) ∑ tj
1≤j≤n
32
Optimal Storage on Tapes
• In the optimal storage on tape problem
• We are required to find a permutation for the n
programs so that
• When they are stored on the tape in this order
• MRT is minimized
• This problem fits into Ordering Paradigm

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

 28,32,12,5,84,53,91,35,3, and 11.

 Find an optimal binary merge pattern for ten


files whose lengths are

 19, 12, 45, 32, 29, 11, 37, 7, 15, 47

38
Optimal Merge Pattern

39
Huffman Code

 Obtain a set of optimal Huffman codes for the


messages (M1,..., M7) with relative
frequencies
 (q1,..., q7) = (4, 5,7,8,10,12, 20).
 Draw the decode tree for this set of codes.

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

You might also like