AOA Exp 5 b44
AOA Exp 5 b44
Experiment No.05
A.1 Aim:
Write a program to implement minimum cost spanning trees using Greedy Method
Approach – Kruskal Algorithm and Prim’s Algorithm.
A.2 Prerequisite:
A.3 Outcome:
After successful completion of this experiment, students will be able to apply greedy
method approach to different problems to find optimal solution and analyze the complexity of
Kruskal's algorithm :
Algorithm:
Time Complexity:
Time complexity is large as order of O(V+E) where V is the number of vertices, E is the
number of edges.
Prim’s algorithm:
Prim'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. The
algorithm operates by building this tree one vertex at a time, from an arbitrary starting
vertex, at each step adding the cheapest possible connection from the tree to another
vertex.
Algorithm:
1. Maintain two disjoint sets of vertices. One containing vertices that are in the growing
spanning tree and other that are not in the growing spanning tree.
2. Select the cheapest vertex that is connected to the growing spanning tree and is not
in the growing spanning tree and add it into the growing spanning tree. This can be
done using Priority Queues. Insert the vertices, that are connected to growing
spanning tree, into the Priority Queue.
3. Check for cycles. To do that, mark the nodes which have been already selected and
insert only those nodes in the Priority Queue that are not marked.
Example:
Time Complexity:
The time complexity of the Prim’s Algorithm is O((V+E)logV) because each vertex is
inserted in the priority queue only once and insertion in priority queue take logarithmic
time.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Kruskal’s Algorithm:
// C code to implement Kruskal's algorithm
#include <stdio.h>
#include <stdlib.h>
return parent[component]
= findParent(parent, parent[component]);
}
int parent[n];
int rank[n];
return minCost;
}
// Driver code
int main()
{
int edge[5][3] = { { 0, 1, 10 },
{ 0, 2, 6 },
{ 0, 3, 5 },
{ 1, 3, 15 },
{ 2, 3, 4 } };
printf("%d",kruskalAlgo(5, edge));
return 0;
}
Prims’s Algorithm:
// A C program for Prim's Minimum
// Spanning Tree (MST) algorithm. The program is
// for adjacency matrix representation of the graph
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
// Number of vertices in the graph
#define V 5
return min_index;
}
// Driver's code
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
return 0;
}
B.2 Input and Output:
Kruskal’s Algorithm:
Output:
Prim’s Algorithm:
Output:
B.3 Observations and learning:
Observation:
Kruskal’s Algorithm selects edges in increasing weight order, while Prim’s Algorithm grows the
MST from a starting node. Both efficiently find the Minimum Spanning Tree.
Learning:
We learned how Greedy Methods optimize network connections by minimizing costs while
maintaining full connectivity.
B.4 Conclusion:
Kruskal’s and Prim’s Algorithms effectively construct a Minimum Spanning Tree using the Greedy
approach. Kruskal’s works well with edge lists, while Prim’s is efficient for dense graphs, ensuring
minimal cost connectivity in networks and optimization problems.
Minimum Spanning Tree (MST) has various applications in computer science, networking, and
optimization. Some key applications include:
2. Clustering: Helps in hierarchical clustering by connecting similar data points with minimal
distance.
3. Approximation Algorithms: Used in algorithms like the Traveling Salesman Problem (TSP) to find
near-optimal solutions.
4. Image Segmentation: Helps in partitioning images into meaningful regions by minimizing edge
weights.
5. Routing and Broadcasting: Used in network routing protocols like OSPF (Open Shortest Path
First) to minimize data transfer costs.
6. Circuit Design: Helps in designing efficient VLSI circuits by minimizing wire length.
7. Transportation and Logistics: Used in planning roads, pipelines, and distribution networks to
reduce infrastructure costs.
MST is a fundamental concept in graph theory with numerous real-world applications requiring
cost-effective connectivity.
A Minimum Spanning Tree (MST) of a connected graph with NN vertices always has N−1N-1 edges.
This is because:
A tree with NN vertices always has N−1N-1 edges, ensuring connectivity while minimizing cost.
Q3: Consider an undirected graph G with unique positive weights. Suppose it has
a minimum spanning tree T. If we square all the edge weights and compute
the MST again, do we still get the same tree structure? Explain briefly.
No, the Minimum Spanning Tree (MST) may change if we square all edge
weights.
Explanation:
o MST algorithms like Kruskal’s or Prim’s select edges based on the relative
order of weights (i.e., comparing edge weights).
o If edge weights are unique and positive, the original MST T is determined
based on the order of weights.
o The difference between **33 and 55 was 22, but after squaring, it becomes
1616, which might change the order of MST edge selection.
Conclusion:
Although the order of edge weights is preserved, the differences between them
change, which can lead to a different MST structure when the algorithm
recomputes it. Thus, squaring edge weights can change the MST.
************************