Skip to content

Commit 30fb923

Browse files
Prim’s algorithm in Java
1 parent 1b42614 commit 30fb923

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

Graph/prims/Demo.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 prims;
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.Prims();
46+
47+
}
48+
}
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 prims;
7+
8+
public class UndirectedWeightedGraph
9+
{
10+
public final int MAX_VERTICES = 30;
11+
12+
int n;
13+
int e;
14+
int [][] adj;
15+
Vertex [] vertexList;
16+
17+
private static final int TEMPORARY = 1;
18+
private static final int PERMANENT = 2;
19+
private static final int NIL= -1;
20+
private static final int INFINITY = 99999;
21+
22+
public UndirectedWeightedGraph()
23+
{
24+
adj = new int[MAX_VERTICES][MAX_VERTICES];
25+
vertexList = new Vertex[MAX_VERTICES];
26+
}
27+
28+
public void Prims()
29+
{
30+
int c,v;
31+
32+
int edgesInTree=0;
33+
int wtTree=0;
34+
35+
for(v=0; v<n; v++)
36+
{
37+
vertexList[v].status = TEMPORARY;
38+
vertexList[v].length = INFINITY;
39+
vertexList[v].predecessor = NIL;
40+
}
41+
42+
int root=0;
43+
vertexList[root].length = 0;
44+
45+
while(true)
46+
{
47+
c = tempVertexMinL();
48+
49+
if(c == NIL)
50+
{
51+
if(edgesInTree == n-1)
52+
{
53+
System.out.println("Weight of minimum spanning tree is " + wtTree);
54+
return;
55+
}
56+
else
57+
throw new RuntimeException("Graph is not connected, Spanning tree not possible");
58+
}
59+
60+
vertexList[c].status = PERMANENT;
61+
/* Include edge ( vertexList[c].predecessor,c ) in the tree*/
62+
if(c!=root)
63+
{
64+
edgesInTree++;
65+
System.out.println("(" + vertexList[c].predecessor + "," + c + ")");
66+
wtTree = wtTree + adj[vertexList[c].predecessor][c];
67+
}
68+
69+
for(v=0; v<n; v++)
70+
if(isAdjacent(c,v) && vertexList[v].status == TEMPORARY)
71+
if(adj[c][v] < vertexList[v].length)
72+
{
73+
vertexList[v].length = adj[c][v];
74+
vertexList[v].predecessor = c;
75+
}
76+
}/*End of while*/
77+
}
78+
79+
int tempVertexMinL()
80+
{
81+
82+
int min = INFINITY;
83+
int x = NIL;
84+
85+
for(int v=0; v<n; v++)
86+
{
87+
if(vertexList[v].status == TEMPORARY && vertexList[v].length < min)
88+
{
89+
min=vertexList[v].length;
90+
x=v;
91+
}
92+
}
93+
return x;
94+
}
95+
96+
private int getIndex(String s)
97+
{
98+
for(int i=0; i<n; i++)
99+
if(s.equals(vertexList[i].name))
100+
return i;
101+
throw new RuntimeException("Invalid Vertex");
102+
}
103+
104+
public void insertVertex(String name)
105+
{
106+
vertexList[n++] = new Vertex(name);
107+
}
108+
109+
private boolean isAdjacent(int u, int v)
110+
{
111+
return (adj[u][v]!=0);
112+
}
113+
114+
/*Insert an edge (s1,s2) */
115+
public void insertEdge(String s1, String s2, int wt)
116+
{
117+
int u = getIndex(s1);
118+
int v = getIndex(s2);
119+
if(adj[u][v] !=0 )
120+
System.out.print("Edge already present");
121+
else
122+
{
123+
adj[u][v]=wt;
124+
adj[v][u]=wt;
125+
e++;
126+
}
127+
}
128+
}

Graph/prims/Vertex.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 prims;
7+
8+
public class Vertex
9+
{
10+
String name;
11+
int status;
12+
int predecessor;
13+
int length;
14+
15+
Vertex(String name)
16+
{
17+
this.name = name;
18+
}
19+
public String toString()
20+
{
21+
return name;
22+
}
23+
}

0 commit comments

Comments
 (0)