Skip to content

Commit 08423dc

Browse files
Dijkstra’s Algorithm in Java
1 parent 41229ae commit 08423dc

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

Graph/Dijkstras/Demo.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Dijkstras;
7+
8+
public class Demo
9+
{
10+
public static void main(String [] args)
11+
{
12+
DirectedWeightedGraph g = new DirectedWeightedGraph();
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+
24+
g.insertEdge("Zero","Three", 2);
25+
g.insertEdge("Zero","One", 5);
26+
g.insertEdge("Zero","Four", 8);
27+
g.insertEdge("One","Four", 2);
28+
g.insertEdge("Two","One", 3);
29+
g.insertEdge("Two","Five", 4);
30+
g.insertEdge("Three","Four", 7);
31+
g.insertEdge("Three","Six", 8);
32+
g.insertEdge("Four","Five", 9);
33+
g.insertEdge("Four","Seven", 4);
34+
g.insertEdge("Five","One", 6);
35+
g.insertEdge("Six","Seven", 9);
36+
g.insertEdge("Seven","Three", 5);
37+
g.insertEdge("Seven","Five", 3);
38+
g.insertEdge("Seven","Eight", 5);
39+
g.insertEdge("Eight","Five", 3);
40+
41+
g.findPaths("Zero");
42+
}
43+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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 Dijkstras;
7+
8+
public class DirectedWeightedGraph
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 DirectedWeightedGraph()
23+
{
24+
adj = new int [MAX_VERTICES][MAX_VERTICES];
25+
vertexList = new Vertex[MAX_VERTICES];
26+
}
27+
28+
private void dijkstra(int s)
29+
{
30+
int v,c;
31+
32+
for(v=0; v<n; v++)
33+
{
34+
vertexList[v].status = TEMPORARY;
35+
vertexList[v].pathLength = INFINITY;
36+
vertexList[v].predecessor = NIL;
37+
}
38+
39+
vertexList[s].pathLength = 0;
40+
41+
while(true)
42+
{
43+
c=tempVertexMinPL();
44+
45+
if(c==NIL)
46+
return;
47+
48+
vertexList[c].status = PERMANENT;
49+
50+
for(v=0; v<n; v++)
51+
{
52+
if(isAdjacent(c,v) && vertexList[v].status == TEMPORARY)
53+
if( vertexList[c].pathLength + adj[c][v] < vertexList[v].pathLength )
54+
{
55+
vertexList[v].predecessor = c;
56+
vertexList[v].pathLength = vertexList[c].pathLength + adj[c][v];
57+
}
58+
}
59+
}
60+
}
61+
62+
private int tempVertexMinPL( )
63+
{
64+
int min=INFINITY;
65+
int x=NIL;
66+
for(int v=0; v<n; v++)
67+
{
68+
if(vertexList[v].status == TEMPORARY && vertexList[v].pathLength < min)
69+
{
70+
min=vertexList[v].pathLength;
71+
x=v;
72+
}
73+
}
74+
return x;
75+
}
76+
77+
public void findPaths(String source)
78+
{
79+
int s = getIndex(source);
80+
81+
dijkstra(s);
82+
83+
System.out.println("Source Vertex : " + source + "\n");
84+
for(int v=0; v<n; v++)
85+
{
86+
System.out.println("Destination Vertex : " + vertexList[v]);
87+
if( vertexList[v].pathLength == INFINITY )
88+
System.out.println("There is no path from " + source + " to vertex " + vertexList[v] + "\n");
89+
else
90+
findPath(s,v);
91+
}
92+
}
93+
94+
private void findPath(int s, int v)
95+
{
96+
int i,u;
97+
int [] path = new int[n];
98+
int sd=0;
99+
int count=0;
100+
101+
while(v!=s)
102+
{
103+
count++;
104+
path[count] = v;
105+
u = vertexList[v].predecessor;
106+
sd += adj[u][v];
107+
v=u;
108+
}
109+
count++;
110+
path[count]=s;
111+
112+
System.out.print("Shortest Path is : ");
113+
for(i=count; i>=1; i--)
114+
System.out.print(path[i] + " ");
115+
System.out.println("\n Shortest distance is : " + sd + "\n");
116+
}
117+
118+
119+
private int getIndex(String s)
120+
{
121+
for(int i=0; i<n; i++)
122+
if(s.equals(vertexList[i].name))
123+
return i;
124+
throw new RuntimeException("Invalid Vertex");
125+
}
126+
127+
public void insertVertex(String name)
128+
{
129+
vertexList[n++] = new Vertex(name);
130+
}
131+
132+
133+
private boolean isAdjacent(int u, int v)
134+
{
135+
return (adj[u][v]!=0);
136+
}
137+
138+
/*Insert an edge (s1,s2) */
139+
public void insertEdge(String s1, String s2, int wt)
140+
{
141+
int u = getIndex(s1);
142+
int v = getIndex(s2);
143+
if(u==v)
144+
throw new IllegalArgumentException("Not a valid edge");
145+
146+
if(adj[u][v] !=0 )
147+
System.out.print("Edge already present");
148+
else
149+
{
150+
adj[u][v]=wt;
151+
e++;
152+
}
153+
}
154+
155+
}
156+
157+
158+
159+

Graph/Dijkstras/Vertex.java

Lines changed: 23 additions & 0 deletions
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 Dijkstras;
7+
8+
public class Vertex
9+
{
10+
String name;
11+
int status;
12+
int predecessor;
13+
int pathLength;
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)