Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7abf247

Browse files
Kruskal’s algorithm in Java
1 parent 08423dc commit 7abf247

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

‎Graph/kruskal/Demo.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package kruskal;
7+
8+
public class Demo
9+
{
10+
public static void main(String [] args)
11+
{
12+
UndirectedWeightedGraph g = new UndirectedWeightedGraph();
13+
14+
g.insertVertex("Zero");
15+
g.insertVertex("One");
16+
g.insertVertex("Two");
17+
g.insertVertex("Three");
18+
g.insertVertex("Four");
19+
g.insertVertex("Five");
20+
g.insertVertex("Six");
21+
g.insertVertex("Seven");
22+
g.insertVertex("Eight");
23+
g.insertVertex("Nine");
24+
25+
g.insertEdge("Zero","One", 19);
26+
g.insertEdge("Zero","Three", 14);
27+
g.insertEdge("Zero","Four", 12);
28+
g.insertEdge("One","Two", 20);
29+
g.insertEdge("One","Four", 18);
30+
g.insertEdge("Two","Four", 17);
31+
g.insertEdge("Two","Five", 15);
32+
g.insertEdge("Two","Nine", 29);
33+
g.insertEdge("Three","Four", 13);
34+
g.insertEdge("Three","Six", 28);
35+
g.insertEdge("Four","Five", 16);
36+
g.insertEdge("Four","Six", 21);
37+
g.insertEdge("Four","Seven", 22);
38+
g.insertEdge("Four","Eight", 24);
39+
g.insertEdge("Five","Eight", 26);
40+
g.insertEdge("Five","Nine", 27);
41+
g.insertEdge("Six","Seven", 23);
42+
g.insertEdge("Seven","Eight", 30);
43+
g.insertEdge("Eight","Nine", 35);
44+
45+
g.kruskals();
46+
}
47+
}

‎Graph/kruskal/Edge.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package kruskal;
7+
8+
public class Edge implements Comparable<Edge>
9+
{
10+
int u;
11+
int v;
12+
int wt;
13+
14+
public Edge(int u, int v, int wt)
15+
{
16+
this.u=u;
17+
this.v=v;
18+
this.wt=wt;
19+
}
20+
21+
public int compareTo(Edge obj)
22+
{
23+
if(wt == obj.wt)
24+
return 0;
25+
else if(wt>obj.wt)
26+
return 1;
27+
else
28+
return -1;
29+
}
30+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package kruskal;
7+
8+
import java.util.PriorityQueue;
9+
10+
public class UndirectedWeightedGraph
11+
{
12+
public final int MAX_VERTICES = 30;
13+
14+
int n;
15+
int e;
16+
int [][] adj;
17+
Vertex [] vertexList;
18+
19+
private static final int NIL= -1;
20+
21+
public UndirectedWeightedGraph()
22+
{
23+
adj = new int[MAX_VERTICES][MAX_VERTICES];
24+
vertexList = new Vertex[MAX_VERTICES];
25+
}
26+
27+
public void kruskals()
28+
{
29+
PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
30+
31+
int u,v;
32+
for(u=0; u<n; u++)
33+
for(v=0; v<u; v++)
34+
{
35+
if(adj[u][v]!=0)
36+
pq.add(new Edge(u,v,adj[u][v]));
37+
}
38+
39+
for(v=0; v<n; v++)
40+
vertexList[v].father = NIL;
41+
42+
int v1,v2,r1=NIL,r2=NIL;
43+
int edgesInTree=0;
44+
int wtTree=0;
45+
46+
while(!pq.isEmpty() && edgesInTree<n-1)
47+
{
48+
Edge edge = pq.remove();
49+
v1=edge.u;
50+
v2=edge.v;
51+
52+
v=v1;
53+
while(vertexList[v].father!=NIL)
54+
v=vertexList[v].father;
55+
r1=v;
56+
57+
v=v2;
58+
while(vertexList[v].father!=NIL)
59+
v=vertexList[v].father;
60+
r2=v;
61+
62+
if(r1!=r2) /*Edge (v1,v2) is included*/
63+
{
64+
edgesInTree++;
65+
System.out.println(vertexList[v1] + "," + vertexList[v2] );
66+
wtTree += edge.wt;
67+
vertexList[r2].father=r1;
68+
}
69+
}
70+
71+
if(edgesInTree < n-1)
72+
throw new RuntimeException("Graph is not connected, no spanning tree possible\n");
73+
74+
System.out.println("Weight of Minimum Spanning Tree is " + wtTree);
75+
}
76+
77+
private int getIndex(String s)
78+
{
79+
for(int i=0; i<n; i++)
80+
if(s.equals(vertexList[i].name))
81+
return i;
82+
throw new RuntimeException("Invalid Vertex");
83+
}
84+
85+
public void insertVertex(String name)
86+
{
87+
vertexList[n++] = new Vertex(name);
88+
}
89+
90+
/*Insert an edge (s1,s2) */
91+
public void insertEdge(String s1, String s2, int wt)
92+
{
93+
int u = getIndex(s1);
94+
int v = getIndex(s2);
95+
if(adj[u][v] !=0 )
96+
System.out.print("Edge already present");
97+
else
98+
{
99+
adj[u][v]=wt;
100+
adj[v][u]=wt;
101+
e++;
102+
}
103+
}
104+
105+
}
106+
107+
108+

‎Graph/kruskal/Vertex.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package kruskal;
7+
8+
public class Vertex
9+
{
10+
String name;
11+
int father;
12+
13+
Vertex(String name)
14+
{
15+
this.name = name;
16+
}
17+
public String toString()
18+
{
19+
return name;
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /