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

U-III Greedy Method

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

U-III Greedy Method

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-III
(GREEDY METHOD)
@DR. SHANKAR THAWKAR
Introduction to Greedy method
 It is one of the straight forward design technique, applied to wide verity of
problem.
 In this method we need to obtain a solution that satisfy some constraints.
 Any solution that satisfy some constraint is called feasible solution and a
feasible solution that either maximize or minimize the given solution is called
optimal solution.
 Greedy method suggest that –
 Design an algorithm that works in stages by considering one input at a time
 At each stage a decision is made regarding whether the selection of an input leads to an
optimal solution or not
 Inputs are selected according to some design strategy
Introduction to Greedy method
Greedy Algorithm (A, n)
1. Solution=
2. For i=1 to n
3. X= select (A)
4. If feasible (solution, X) then
5. Solution=union(solution, X)]
6. Return solution
Knapsack Problem
 Suppose we have given ‘n’ objects and a knapsack with capacity ‘M’
 Object ‘i’ has a weight wi
 if the fraction xi ( 0≤ xi ≤ 1) of an object is placed into the knapsack then a profit ‘Pi xi ‘ is
earned.
 The objective is to obtain the filling of knapsack that maximize the profit.
 i.e.

• A feasible solution is a subset (x1, x2, …….. xn) that satisfy equation (2) and (3) and a feasible
solution is an optimal solution that maximize equation (1)
Ex-1: n=3, M=20 , (P1, P2, P3)=(25, 24, 15) & (w1,w2,w3)=(18, 15, 10)

Selection strategy Feasible solution Optimal solution


(x1, x2, x3 )

Select the objects in


decreasing order of
profits

Select the objects in


increasing order of
weight

Select the objects in


decreasing order of
ratio
Ex-2: n=7, M=15 , (P1, ……. P7)=(10, 5, 15, 7, 6, 18, 3) & (w1……..w7)=(2, 3, 5, 7, 1, 4, 1)

Selection strategy Feasible solution Optimal solution


(x1, x2, x3 , x4, x5, x6, x7)

Select the objects in


decreasing order of
profits

Select the objects in


increasing order of weight

Select the objects in


decreasing order of ratio
Job Sequencing with deadlines
 We have given ‘n’ jobs with deadlines and profit

 For a job profit is earned if it is completed by its deadline.

 To complete a job, it has to be processed on a machine for 1-unit of time and only one
machine is available to process all jobs
 A feasible solution is a subset of jobs such that each job is completed by its deadline.
 A optimal solution is a feasible solution with maximum profit value.
Ex-1: n=4, (P1, P2, P3,P4)=(100, 10, 15, 27) & (d1,d2,d3,d4)=(2, 1, 2, 1)

Job selection Order of processing Optimal solution


(Feasible solution) ( profit)
Ex-2: n=5, (P1, …..P5)=(50, 30, 10, 20,15) & (d1,…..d5)=(2, 1, 3, 2, 4)

Job selection Order of processing Optimal solution


(Feasible solution) ( profit)
DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-III
(SINGLE SOURCE SHORTEST PATH ALGORITHMS)
Introduction to Single Source shortest path
Shortest Path-
 Let G=(V, E ) be an directed weighted graph.
 A shortest path from u to v is a path of minimum weight from u to v and it is
defined as-
Introduction to Single Source shortest path
Relaxing an edge-
 The process of relaxing an edge(u, v) consist of testing whether we improve the
shortest path to v found so far by going through u. if so update d[v] and ∏(v)

Algorithm:
Relax(u, v, w)
1. If (d[v]>d[u]+w(u, v) ) then
2. d[v]= d[u]+w(u, v) &
Dijkstra’s Algorithm
 It is used to find solve single source shortest path problem on a weighted directed
graph G=(V,E)
 All the edge weights are non-negative
 It is similar to prim's algo
Algorithm:
Dijkstra(G, w, s) For i=1 to v
1. Initialize single-source(G,s) d[v]=

2. S= end
d[s]=0
3. Q=V(G)
4. While Q = NIL
5. u=extract-min(Q)
6. S=S U {u}
7. for each v adj[u]
8. relax(u, v, w)
Example – find the shortest path for following graph
Example – find the shortest path for following graph

Selected distance d[v] [v]


Vertices s t x y z s t x y z
Example 2 – find the shortest path for following graph (Assume vertex a as source)
Example 2 – find the shortest path for following graph (Assume vertex a as source)

Selected distance d[v] [v]


Vertices a b c d e a b c d e
DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-III
(SINGLE SOURCE SHORTEST PATH ALGORITHMS)
Bellman-ford Algorithm
 It is used to solve single source shortest path problem
 Edge weights may be negative
 Given a weighted directed graph G=(V,E). Bellman-ford algorithm return a
Boolean value indicating whether or not there is a –ve weight cycle which is
reachable from source.
 If the graph contain –ve weight cycle then no solution exist.
Bellman-ford Algorithm
Algorithm:
Bellman-ford(G, w, s)
1. Initialize single-source(G,s)
2. For i=1 to V(G)-1
3. For each edge (u, v) E
4. relax(u, v, w)
5. for each edge (u, v) E
6. if d[v]> d[u]+w(u, v) then
7. return false
8. return True
Complexity = O(V+VE+E)
= O(VE)
Ex-1 distance d[v] [v]
s t x y z s t x y z
DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-III
(SPANNING TREE)
Spanning Tree
Definition-
 Let G=(V, E ) be an undirected graph then a sub-graph T of G is said to be spanning
tree of G if T is a tree.

 A minimum spanning tree (MST) is a spanning tree of minimum weight.


 There are two methods to find MST –
 Kruskal’s Algorithm
 Prim’s Algorithm
Kruskal’s Algorithm
 It is based on Greedy method.
 It select one edge at a time to find MST

1. A← 
2. for each vertex v  V
3. do MAKE-SET(v)
4. sort E into non-decreasing order by w
5. for each (u, v) taken from the sorted list
6. do if FIND-SET(u)  FIND-SET(v)
7. then A ← A  {(u, v)}
8. UNION(u, v)
9. return A
Step-3 : Sort the edges in non-decreasing order

(1,3)=2 ; (4,6)=3 ; (2,5)=4 ; (3,6)=5 ; (1,4)=6 ;

(2,3)=6 ; (3,4)=6 ; (3,5)=7 ; (5,6)=7 ; (1,2)=7

Step-4 : Select edges with min weight


s.t. Set(u) = Set(v)
Step-1 : set A=

Step-2 : create |V| trees using


Make-set()
(1,3)=2 ; (4,6)=3 ; (2,5)=4 ; (3,6)=5 ; (1,4)=6 ; (2,3)=6 ; (3,4)=6 ; (3,5)=7 ; (5,6)=7 ; (1,2)=7

Select Next edges


Q. Find MST for the following Graph using Kruskal’s algorithm
DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-III
(SPANNING TREE)
Prim’s Algorithm
 It is based on Greedy method. It differs with Kruskal’s algorithm in the way the
edges are selected to find MST.
 It is similar to Dijkstra’s algorithm.

How it Works
 Start with arbitrary vertex
 Call this as root r
 Set key of r=0 and others ∞
 Put all the vertices in queue
 Then select edges of min value

For Algorithm refer book


Q. Find MST for the following graph using Kruskal’s and Prim’s Algo
Prim’s method-
Q. Find MST for the following graph using Kruskal’s and Prim’s Algo

You might also like