Chapter03 - Greedy Method
Chapter03 - Greedy Method
Chapter 03
Greedy Methods
1
Outline
Greedy method
Applications
Knapsack problem
Minimum cost spanning trees
Single source shortest path problem
Job sequencing with deadlines
2
Greedy Method
Greedy design technique is primarily used in Optimization
problems.
Optimization problems are problems where in we would like to find the
best of all possible solutions.
In other words we need to find the solution which has the optimal
(maximum or minimum) value satisfying the given constraints.
3
Greedy Method
Constructs a solution to an optimization problem step by step
through a sequence of choices that are:
Defined by an
feasible, i.e. satisfying the constraints objective function and
a set of constraints
locally optimal (with respect to some neighborhood definition)
4
Applications of the Greedy Strategy
Optimal solutions:
change making for “normal” coin denominations
Huffman codes
Approximations/heuristics:
traveling salesman problem (TSP)
knapsack problem
5
Change-Making Problem
Given unlimited amounts of coins of denominations d1 > … > dm ,
give change for amount n with the least number of coins
Q: What are the objective function and constraints?
Greedy solution is
optimal for any amount and “normal’’ set of denominations
Ex: Prove the greedy algorithm is optimal for the above denominations.
6
Knapsack Problem
There are n different items in a store
Item xi :
weighs wi pounds
worth $ pi
A thief breaks in
Can carry up to W pounds in his knapsack
What should he take to maximize the value of his haul?
?
7
Knapsack Problem
Each object xi placed into the knapsack(capacity m) subject to
the following constraints to obtain feasible solution.
n
maximize
px
i 1
(maximize the profit)
i i
n
Subject to w x
i 1
i i
≤ w (capacity can’t exceed w)
8
Knapsack Problem Types
0-1 Knapsack Problem:
the items cannot be divided
9
Fractional Knapsack Problem - Example
Consider the following instance of the knapsack problem: n=3, m=20;
(p1, p2, p3) = (25,24,15) & (w1, w2, w3) = (18,15,10)
Method 1: Arrange the items in increasing order of their weights i.e. fill the
knapsack by an object with minimum weight first.
Item Profit Weight Remainin Object
g capacity Selected
x3 15 10 20-10=10 x3=1
x2 24 15 10-10=0 x2=2/3 Rem. Capacity/weight of
x2 = 10/15 = 2/3
x1 25 18 0 x1=0
2
25(0) 24( ) 15(1)
3
16 15
31
10
Example – Contd.
Method 2: Arrange the profits of all items in decreasing order
2
25(1) 24( ) 15(0)
15
48
25
15
28.2
11
Example – Contd.
Method 3: Arrange items in decreasing order of their profit-weight ratio
x1 1.3 25 18 0 x1=0
1
25(0) 24(1) 15( )
2
24 7.5
31.5
12
Minimum Spanning Tree (MST)
Spanning tree of a connected graph G: a connected acyclic
subgraph of G that includes all of G’s vertices
Example:
6 c 6 c c
a a a
1 4 1 1
4
2 2
d d d
b b b
3 3
A Graph G Possible spanning tree minimum spanning tree
13
Contd.
Applications
Useful in constructing networks
Approaches:
Prim’s Algorithm
Kruskal’s Algorithms
14
Prim's algorithm
In computer science, Prim's algorithm (also known as Jarník's
algorithm) is a greedy algorithm that finds a minimum spanning
tree for a weighted undirected graph.
This means it finds a subset of the edges that forms a tree that
includes every vertex, where the total weight of all the edges in
the tree is minimized.
15
Prim’s Algorithm for Finding an MST
Step 1: x V, Let A = {x}, B = V - {x}.
16
An Example for Prim’s Algorithm
17
Example2
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d d d
b 3 b 3 b 3
4 c
4 c
a
a 1
1 6
6
2
2
d
d b 3
b 3
18
Prim’s algorithm - Performance
Needs priority queue for locating closest fringe vertex.
Efficiency
2
O(n ) for weight matrix representation of graph and array
19
Kruskal’s Algorithm
These algorithms find the minimum spanning forest in a possibly
disconnected graph; in contrast, the most basic form of Prim's
algorithm only finds minimum spanning trees in connected
graphs.
However, running Prim's algorithm separately for each connected
component of the graph, it can also be used to find the minimum
spanning forest.
In terms of their asymptotic time complexity, these three
algorithms are equally fast for sparse graphs, but slower than
other more sophisticated algorithms.
However, for graphs that are sufficiently dense, Prim's algorithm
can be made to run in linear time, meeting or improving the time
bounds for other algorithms
20
Kruskal’s MST Algorithm
Sort the edges in nondecreasing order of lengths
On each iteration, add the next edge on the sorted list unless this
would create a cycle. (If it would, skip the edge.)
21
Kruskal’s Algorithm for Finding MST
Input : A weighted, connected and undirected graph
G = (V, E ).
Output : A minimal spanning tree for G.
T:=
While T contains less than n - 1 edges do
Begin
Choose an edge (v, w) from E of the smallest weight
Delete (v, w) from E
If (the adding of (v, w) to T does not create a cycle in T) then
Add (v, w) to T
Else
Discard (v, w)
End
22
Example 1
23
Example2
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d d d
b 3 b 3 b 3
4 c c c
a a a
1 1 1
6 6
2 2 2
d d d
b 3 b 3 b 3
24
Kruskal’s algorithm - Performance
Algorithm looks easier than Prim’s but is harder to implement
(checking for cycles!)
25
Single-Source Shortest Paths (SSSP)
Single Source Shortest Paths Problem: Given a weighted connected (directed)
graph G, find shortest paths from source vertex s to each of the other
vertices
26
4
b c
3 6
Example a 7 2
d
5 4
e
4
d(b,5) c(b,7) e(d,5+4) b c
3 6
2 5
a d e
7 4
4
c(b,7) e(d,9) b c
3 6
2 5
a d e
7 4
e(d,9)
27
Dijkstra’s algorithm - Performance
Applicable to both undirected and directed graphs
Efficiency
2
O(|V| ) for graphs represented by weight matrix and array
28
Job Sequencing
A set of jobs: n
Each job has dead line : d
Each job ‘i’ has profit : p
Objective: maximize profit :∑ pi
i=j
Job that have same deadline with maximum job will be rejected.
Example: n=4,(p1,p2,p3,p4)=(100,10,15,27)
(d1,d2,d3,d4)=(2,1,2,1), Answer: max profit = 127.
2. Consider the following knapsack problem with n=3 and m=105. The
profits are (p1,p2,p3)= (20, 15, 15), then the corresponding weights
are (100,10,10). Obtain feasible solution and find optimal solution.
30
Solution for Question no3 Answer = 74 the maximum profit.
S/no Feasible sol/n Proces sequence ∑ Pi , i=J
1 J={ø} - 0
2 J={7} 2 30
3 J={7,3} 7,3 50
4 J={7,3,4} 7,4,3 68
5 J={7,3,4,6} 6,7,4,3 74
6 J={3} 4 20
7 J={4} 3 18
8 J={6} 1 6
9 J={2} 3 5
10 J={1} 1 3
31