0% found this document useful (0 votes)
42 views10 pages

Krushed

The document discusses Kruskal's algorithm for finding the minimum spanning tree of a connected, weighted graph. It provides pseudocode for the algorithm, which works by sorting the edges by weight and building up the minimum spanning tree by adding edges in order of increasing weight while avoiding cycles. The time complexity of Kruskal's algorithm is O(E log E) or O(E log V) where E is the number of edges and V is the number of vertices. C code is also provided as an implementation example.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

Krushed

The document discusses Kruskal's algorithm for finding the minimum spanning tree of a connected, weighted graph. It provides pseudocode for the algorithm, which works by sorting the edges by weight and building up the minimum spanning tree by adding edges in order of increasing weight while avoiding cycles. The time complexity of Kruskal's algorithm is O(E log E) or O(E log V) where E is the number of edges and V is the number of vertices. C code is also provided as an implementation example.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROJECT REPORT

ON
“KRUSKAL ALGORITHIM, ON MINIMUM SPANNING TREES”
Course: Data Structure and Algorithm

SUBMITTED TO

SUBMITTED BY

MANSI SINGH (DC2022MCA0001)

SHIULI SINHA (DC2022MCA0010)

ABHISHEK MANDAL (DC2022MCA0011)

DIPLU KINGKAR DAS (DC2022MCA0012)

ASSAM DON BOSCO UNIVERSITY


Krushkal algorithm

Abstract.

Kruskal's Algorithm is an algorithm used to find the minimum spanning tree in graphical
connectivity that provides the option to continue processing the least-weighted margins. In
the Kruskal algorithm, ordering the weight of the ribs makes it easy to find the shortest
path. This algorithm is independent in nature which will facilitate and improve path
creation.

2.3 The Definition of Kruskal’s Algorithm

Kruskal’s algorithm is one to find minimum spanning tree in a graph connectivity which gives
options of always processing the edge limit with the least weight. This algorithm run by
considering the graph’s biggest edge limit when searching for track in node in a graph which
has been put into a spanning tree. If an edge limit is considered will integrate (with one of
the points not in the spanning tree), or the integration of a point in spanning tree (one of
which is not in the spanning tree), then the edge limit and the final point is included in the
spanning tree. Considering one of the edge limits, algorithm continues by considering the
next larger edge limit weight. In this term, when the edge limit weight values are the same,
then it is unnecessary to determine which weight must be chosen first. Algorithm will stop
when every point have been included in the spanning tree. Note that, when spanning tree
was made, there was a possibility that the edge limit is not connected with the part of the
main tree, although in the end they will integrate with the main three.

Kruskal's Algorithm:
An algorithm to construct a Minimum Spanning Tree for a connected weighted
graph. It is a Greedy Algorithm. The Greedy Choice is to put the smallest weight edge
that does not because a cycle in the MST constructed so far.
If the graph is not linked, then it finds a Minimum Spanning Tree.

Steps for finding MST using Kruskal's Algorithm

1. Arrange the edge of G in order of increasing weight.


2. Starting only with the vertices of G and proceeding sequentially add each
edge which does not result in a cycle, until (n - 1) edges are used.
3. EXIT.

MST- KRUSKAL (G, w)


1. A ← ∅
2. for each vertex v ∈ V [G]
3. do MAKE - SET (v)
4. sort the edges of E into non decreasing order by weight w
5. for each edge (u, v) ∈ E, taken in non decreasing order by weight
6. do if FIND-SET (μ) ≠ if FIND-SET (v)
7. then A ← A ∪ {(u, v)}
8. UNION (u, v)
9. return A

Analysis: Where E is the number of edges in the graph and V is the number of


vertices, Kruskal's Algorithm can be shown to run in O (E log E) time, or simply, O (E
log V) time, all with simple data structures. These running times are equivalent
because:

o E is at most V2 and log V2= 2 x log V is O (log V).


o If we ignore isolated vertices, which will each their components of the
minimum spanning tree, V ≤ 2 E, so log V is O (log E).

Thus the total time is

1. O (E log E) = O (E log V).  

For Example: Find the Minimum Spanning Tree of the following graph using
Kruskal's algorithm.
Solution: First we initialize the set A to the empty set and create |v| trees, one
containing each vertex with MAKE-SET procedure. Then sort the edges in E into order
by non-decreasing weight.

There are 9 vertices and 12 edges. So MST formed (9-1) = 8 edges

Now, check for each edge (u, v) whether the endpoints u and v belong to the same
tree. If they do then the edge (u, v) cannot be supplementary. Otherwise, the two
vertices belong to different trees, and the edge (u, v) is added to A, and the vertices
in two trees are merged in by union procedure.

Step1: So, first take (h, g) edge


Step 2: then (g, f) edge.

Step 3: then (a, b) and (i, g) edges are considered, and the forest becomes
Step 4: Now, edge (h, i). Both h and i vertices are in the same set. Thus it creates a
cycle. So this edge is discarded.

        Then edge (c, d), (b, c), (a, h), (d, e), (e, f) are considered, and the forest becomes.

Step 5: In (e, f) edge both endpoints e and f exist in the same tree so discarded this
edge. Then (b, h) edge, it also creates a cycle.

Step 6: After that edge (d, f) and the final spanning tree is shown as in dark lines.

Step 7: This step will be required Minimum Spanning Tree because it contains all the
9 vertices and (9 - 1) = 8 edges
1. e → f,  b → h,  d → f [cycle will be formed]  

Minimum Cost MST

Code of the Kruskal algorithm in C

// Kruskal's algorithm in C

#include <stdio.h>

#define MAX 30

typedef struct edge {


int u, v, w;
} edge;

typedef struct edge_list {


edge data[MAX];
int n;
} edge_list;

edge_list elist;

int Graph[MAX][MAX], n;
edge_list spanlist;

void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();

// Applying Krushkal Algo


void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;

for (i = 1; i < n; i++)


for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}

sort();

for (i = 0; i < n; i++)


belongs[i] = i;

spanlist.n = 0;

for (i = 0; i < elist.n; i++) {


cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);

if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}

int find(int belongs[], int vertexno) {


return (belongs[vertexno]);
}

void applyUnion(int belongs[], int c1, int c2) {


int i;

for (i = 0; i < n; i++)


if (belongs[i] == c2)
belongs[i] = c1;
}

// Sorting algo
void sort() {
int i, j;
edge temp;

for (i = 1; i < elist.n; i++)


for (j = 0; j < elist.n - 1; j++)
if (elist.data[j].w > elist.data[j + 1].w) {
temp = elist.data[j];
elist.data[j] = elist.data[j + 1];
elist.data[j + 1] = temp;
}
}

// Printing the result


void print() {
int i, cost = 0;

for (i = 0; i < spanlist.n; i++) {


printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}

printf("\nSpanning tree cost: %d", cost);


}

int main() {
int i, j, total_cost;

n = 6;

Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;

Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;

Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;

Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;

Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;

Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;

kruskalAlgo();
print();
}

You might also like