Comparing Prim's With Kruskal's Algo
Comparing Prim's With Kruskal's Algo
KRUSKAL'S ALGO
Luterio, Jenny P.
Villamor, Louise Faye
We have discussed -
Prim’s algorithm is also a Greedy algorithm. It starts with an empty spanning tree. The
idea is to maintain two sets of vertices. The first set contains the vertices already
included in the MST, the other set contains the vertices not yet included. At every step,
it considers all the edges that connect the two sets, and picks the minimum weight edge
from these edges. After picking the edge, it moves the other endpoint of the edge to the
set containing MST.
A group of edges that connects two set of vertices in a graph is called cut in graph
theory. So, at every step of Prim’s algorithm, we find a cut (of two sets, one contains the
vertices already included in MST and other contains rest of the vertices), pick the
minimum weight edge from the cut and include this vertex to MST Set (the set that
contains already included vertices).
Below are the steps for finding MST using Prim’s algorithm
1.Create a set mstSet that keeps track of vertices already included in MST.
2. Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE.
Assign key value as 0 for the first vertex so that it is picked first.
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is
a tree and connects all the vertices together. A single graph can have many different
spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a
weighted, connected and undirected graph is a spanning tree with weight less than or equal
to the weight of every other spanning tree. The weight of a spanning tree is the sum of
weights given to each edge of the spanning tree.
Below are the steps for finding MST using Kruskal’s algorithm
1.Prim’s algorithm works by choosing the adjacent vertices from the selected set of
vertices. In contrast, the Kruskal’s algorithm selects least weight edges instead of using
adjacency list, it organizes the edges by their weights.
3. Prim’s algorithm always generates MST with connected components while this is
not the case in Kruskal’s algorithm where the MST may not have connected
components (i.e. Minimum spanning forest).
4. Kruskal’s algorithm works at a faster pace in the sparse graph. As against, Prim’s
algorithm performs better in the dense graph.
5. The time complexity of Prim’s algorithm is O(V2). Conversely, Kruskal’s algorithm runs
in O(log V) time.
The performance of the two algorithms could differ depending upon the certain
factors such as the number of vertices. Prim’s algorithm runs in O(V2) time and
works well in the massive graphs while Kruskal’s algorithm consumes O(log V) and
perform suitably with small graphs. Although, among both of the algorithm Kruskal’s
algorithm can generate better results.