UNIT-4 Daa - 241212 - 070816
UNIT-4 Daa - 241212 - 070816
Greedy method
Most problems in greedy algorithms involve N inputs, and our objective is to
find a feasible (subset) solution that satisfies a given condition and optimizes
our goal. A solution that satisfies the condition given in the problem is called
a feasible solution.
General method
All greedy problems have n inputs. They work in stages by using a selection
procedure to arrange the inputs in a specific order and process them one at a
time. At each stage, a decision is made regarding whether a particular input
should be included in the optimal solution. If adding the next input to the
partially constructed optimal solution results in an infeasible solution, the
input is not added; otherwise, it is included.
Algorithm Greedy (a,n)
{
solution =0; //Initialise the solution.
for i=1 to n do
{
x=select(a);
if(feasible(solution,x))then
solution=union(solution,x);
}
return solution;
}
Feasible is a Boolean-valued function that determines whether X can be
included in the solution vector. The function Union combines X with the
solution and updates the objective function. A greedy algorithm will work
correctly once a particular problem is chosen and the functions Subset,
Feasible, and Union are properly implemented.
Applications-
Job sequencing with deadlines,
knapsack problem,
Minimum cost spanning trees,
Single source shortest path problem.
Knapsack problem
SPANNING TREES:
A spanning tree of a graph G is a subgraph that includes all the vertices of
G with the minimum possible number of edges.
A spanning tree has no cycles and cannot be disconnected.
If a graph has n vertices, then each spanning tree will have n vertices
and n−1edges.
Minimum Spanning Tree : The spanning tree with smallest weight is called
minimum spanning tree.
1. Kruskal’s Algorithm
Kruskal’s Algorithm is a famous greedy algorithm.
It is used to find the Minimum Spanning Tree (MST) of a given graph.
To apply Kruskal’s algorithm, the graph must be weighted, connected,
and undirected.
Kruskal’s Algorithm Implementation
The implementation of Kruskal’s Algorithm is explained in the following steps:
1. Sort all the edges in increasing order of their weights (from low weight
to high weight).
2. Take the edge with the lowest weight and use it to connect the vertices
of the graph. If adding an edge creates a cycle, reject that edge and
move to the next edge with the least weight.
3. Continue adding edges until all the vertices are connected, and a
Minimum Spanning Tree (MST) is obtained.
4. If there are n vertices in the graph, the resulting spanning tree will have
n vertices and n−1 edges.
Solution: This graph contains 7 vertices, so the resulting spanning tree will
have 6 edges.
Step1: Sort all the edges in increasing order of their weights (from low weight
to high weight).
Step 2: The first edge in the sorted list, i.e., (1, 6), which has the least cost,
is included in the spanning tree.
Then, the vertices 1 and 6 are merged into a single set.
Step 3: The next edge in the sorted list, i.e., (3, 4), which has the least cost,
is included in the spanning tree.
Then, the vertices 3 and 4 are merged into a single set.
Step 4: The next edge in the sorted list, i.e., (2, 7), which has the least cost,
is included in the spanning tree.
Then, the vertices 2 and 7 are merged into a single set.
Step 5: The next edge in the sorted list, i.e., (2, 3), which has the least cost,
is included in the spanning tree.
Then, the vertices 2 and 3 are merged into a single set.
Step 6: The next edge in the sorted list, i.e., (4, 7), which has the least cost,
is included in the spanning tree. However, adding this edge creates a cycle,
so it is rejected, and the next least weight edge is considered.
Step 7: The next edge in the sorted list, i.e., (4, 5), which has the least cost,
is included in the spanning tree.
Then, the vertices 4 and 5 are merged into a single set.
Step 8: The next edge in the sorted list, i.e., (5, 7), which has the least cost,
is included in the spanning tree. However, adding this edge creates a cycle,
so it is rejected, and the next least weight edge is considered.
Step 9: The next edge in the sorted list, i.e., (5, 6), which has the least cost,
is included in the spanning tree.
Then, the vertices 5 and 6 are merged into a single set.
2. Prim’s Algorithm
Prim’s Algorithm is a famous greedy algorithm.
It is used to find the Minimum Spanning Tree (MST) of a given graph.
To apply Prim’s Algorithm, the given graph must be weighted,
connected, and undirected.
Prim’s Algorithm Implementation
The implementation of Prim’s Algorithm is explained in the following steps:
1. Randomly choose any vertex. The vertex connecting to the edge with the
least weight is usually selected.
2. Find the smallest edge connecting the tree to a new vertex. If it forms a
cycle, reject it and try the next smallest edge.
3. Keep repeating step 2 until all the vertices are included and the
Minimum Spanning Tree (MST) is obtained.
4. If there are n vertices in the graph, each spanning tree will have n
vertices and n−1 edges.
Step 2: The nodes included in the tree S are 6 and 1. The possible edges from
these nodes that can be added to the minimum spanning tree 'S' are (1, 2)
and (6, 5). The costs of these edges are 28 and 25, respectively. Since the edge
(6, 5) has the lowest cost among the possible edges, it is included in the tree
'S'.
Step 3: The nodes included in tree S are 1, 6, and 5. The possible edges from
these nodes that can be added to the minimum spanning tree 'S' are (1, 2),
(5, 4), and (5, 7). The costs of these edges are 28, 22, and 24, respectively.
Since the edge (5, 4) has the lowest cost among the possible edges, it is
included in the tree 'S'.
Step 4: The nodes included in tree S are 1, 6, 5, and 4. The possible edges
from these nodes that can be added to the minimum spanning tree 'S' are (1,
2), (5, 7), (4, 3), and (4, 7). The costs of these edges are 28, 24, 12, and 18,
respectively. Since the edge (4, 3) has the lowest cost among the possible
edges, it is included in the tree 'S'.
Step 5: The nodes included in tree S are 1, 6, 5, 4, and 3. The possible edges
from these nodes that can be added to the minimum spanning tree 'S' are (1,
2), (5, 7), (4, 7), and (3, 2). The costs of these edges are 28, 24, 18, and 16,
respectively. Since the edge (3, 2) has the lowest cost among the possible
edges, it is included in the tree 'S'.
Applications:
Job scheduling to a processor
Flight landing or takeoff at an airport
Car repair at a workshop
Implementation of DFS
Example 2:
Breadth First Search
Breadth First Search (BFS) is one of the simplest algorithms for searching or
visiting each vertex in a graph. In this method, each node at the same level is
checked before the search proceeds to the next level. BFS uses a queue to
store visited vertices, expanding the path from the earliest visited vertices.
Steps for BFS:
1. Mark all the vertices as unvisited.
2. Choose any vertex, say ‘v’. Mark it as visited and add it to the end of the
queue.
3. For each vertex in the queue, examine all vertices adjacent to ‘v’ in the
same order.
4. Mark all unvisited vertices adjacent to ‘v’ as visited and add them to the
rear of the queue.
5. Remove the vertex at the front of the queue and repeat this procedure.
6. Continue this process until the queue is empty.
Example 2:
Connected components
If G is a connected, undirected graph, then all vertices of the graph can be
visited in the first call to BFS. The subgraph obtained after traversing the
graph using BFS represents the connected component of the graph.
Thus, BFS can be used to determine whether G is connected. All the newly
visited vertices during a call to BFS represent the vertices in the connected
component of graph G. The subgraph formed by these vertices constitutes the
connected component.
Biconnected components
A graph G is biconnected, iff (if and only if) it contains no articulation point
(joint or junction). A vertex v in a connected graph G is an articulation point,
if and only if (iff) the deletion of vertex v together with all edges incident to v
disconnects the graph into two or more none empty components.
Applications:
Network reliability analysis.
Fault-tolerant systems.
Identifying critical infrastructure points.