Daa Unit2 Greedy Method
Daa Unit2 Greedy Method
UNIT-II
The Greedy Method
By
M.Raja Babu
Associate Professor&HOD
Dept of Information Technology
Aditya Engineering College(A)
Surampalem.
Greedy Method
• It is the simplest and straight forward algorithm design strategy.
• Greedy method is used to solve optimization problems.
• Optimization problems have objective functions and a set of
constraints. The solution to this problem is to create a subset of
solutions that satisfies constraints associated with the problem.
• Any subset solution that satisfies these constraints is called a feasible
solution.
• Any feasible solution that maximizes or minimizes the given objective
function is called the optimal solution.
• Every feasible solution need not be an optimal solution. But every
optimal solution is a feasible solution.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Greedy Method
• The greedy method works in stages. At each stage, a decision or choice is
made regarding whether or not a particular input to be considered for
an optimal solution.
• These decisions are locally optimal. Finally the global optimal solution is
obtained by combining locally optimal solutions.
A connected,
undirected graph Four of the spanning trees of the graph
19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
• Here, we consider the spanning tree to consists of both nodes and edges.
24
Minimum cost spanning tree
37
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Algorithm Dijk(Graph[V][V],src )
{
dist[V]; // Array to store minimum distance values from source.
SPTSet[V]; // To represent set of vertices included in SPT
// Initialize all keys as INFINITE and SPTSet as false
for (i = 0; i < V; i++)
{ dist[i] = MAX,
SPTSet[i] = false;
}
/ /Distance of source vertex from itself is always 0
dist[src] = 0;