-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.java
134 lines (113 loc) · 3.09 KB
/
Dijkstra.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Scanner;
public class Dijkstra {
int graph[][];
int numOfVertices;
int shortestPaths[];
Dijkstra(int numOfVertices)
{
this.numOfVertices = numOfVertices;
graph = new int[numOfVertices][numOfVertices];
for(int i = 0; i<numOfVertices; i++)
for(int j = 0; j < numOfVertices; j++)
graph[i][j] = 0;
shortestPaths = new int[this.numOfVertices];
for(int i = 0; i<this.numOfVertices; i++)
shortestPaths[i] = Integer.MAX_VALUE;
}
void display(int src)
{
char source = (char)((char)src + 'A');
for(int i = 0; i < this.numOfVertices; i++)
{
char destination = (char)((char)i + 'A');
String sp = "";
if(shortestPaths[i] == Integer.MAX_VALUE)
sp = "INF";
else
sp = ""+shortestPaths[i];
System.out.println("From Source: "+ source + " to "+destination+" = "+sp);
}
}
int nextClosestVertex(Boolean visited[])
{
int ncv = -1;
int shortPath = Integer.MAX_VALUE;
for(int i = 0; i < this.numOfVertices; i++)
{
if(visited[i] == false && shortestPaths[i] <= shortPath)
{
shortPath = shortestPaths[i];
ncv = i;
}
}
return ncv;
}
void caluculateShortestPaths(int src)
{
Boolean visited[] = new Boolean[this.numOfVertices];
for (int i = 0; i<this.numOfVertices; i++)
visited[i] = false;
shortestPaths[src] = 0;
for(int i = 0; i<this.numOfVertices-1; i++)
{
//find the next shortest distanced node
int ncv = nextClosestVertex(visited);
visited[ncv] = true;
//Next update all distances to neighbor vertices
for(int eachV = 0; eachV < this.numOfVertices; eachV++)
{
if(!visited[eachV] && graph[ncv][eachV] !=0 && shortestPaths[ncv]!= Integer.MAX_VALUE &&
shortestPaths[ncv]+graph[ncv][eachV] < shortestPaths[eachV])
{
shortestPaths[eachV] = shortestPaths[ncv] + graph[ncv][eachV];
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int N;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of vertices: ");
try {
N = sc.nextInt();
Dijkstra dj = new Dijkstra(N);
while(true)
{
System.out.print("Enter source(alphabet) destination(alphabet) and distance OR stop: ");
String next = sc.next();
if(next.equals("stop"))
break;
int src = next.toUpperCase().charAt(0) - 'A';
next = sc.next();
int dst = next.toUpperCase().charAt(0) - 'A';
int distance = sc.nextInt();
if(src >= N || dst >= N)
{
System.out.print("Input error \n");
}
else
{
dj.graph[src][dst] = distance;
}
}
System.out.print("Enter source: ");
String strSource = sc.next();
int source = strSource.toUpperCase().charAt(0) - 'A';
if(source >= N)
System.out.println("Souce is not range of vertices");
else
{
dj.caluculateShortestPaths(source);
dj.display(source);
}
}
catch (Exception e)
{
System.out.print("Input error \n");
}
}
}