U-III Greedy Method
U-III Greedy Method
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)
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)
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
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.
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
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