-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphShortestPath.java
More file actions
147 lines (126 loc) · 5.44 KB
/
Copy pathGraphShortestPath.java
File metadata and controls
147 lines (126 loc) · 5.44 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
package project;
public class GraphShortestPath {
int numberofvertices;
int[][] adjMatrix;
GraphShortestPath(int numberofvertices) {
this.numberofvertices = numberofvertices;
adjMatrix = new int[numberofvertices][numberofvertices];
for (int i = 0; i < numberofvertices; i++) {
for (int j = 0; j < numberofvertices; j++) {
if (i == j) {
adjMatrix[i][j] = 0;
} else {
adjMatrix[i][j] = Integer.MAX_VALUE;
}
}
}
}
public void addEdge(int u, int v, int w) {
adjMatrix[u][v] = w;
}
public boolean BellmanFord(int source) {
System.out.println("\nBellman-Ford from node " + source + ":");
int[] dist = new int[numberofvertices];
int[] parent = new int[numberofvertices];
//initialize single-source
for (int i = 0; i < numberofvertices; i++) {
dist[i] = Integer.MAX_VALUE;
parent[i]=-1;
}
dist[source] = 0;
for (int k = 0; k < numberofvertices - 1; k++) {
for (int u = 0; u < numberofvertices; u++) { //relax
for (int v = 0; v < numberofvertices; v++) {
if (adjMatrix[u][v] != Integer.MAX_VALUE && dist[u] != Integer.MAX_VALUE && dist[v] > dist[u] + adjMatrix[u][v]) {
dist[v] = dist[u] + adjMatrix[u][v];
parent[v]=u;
}
}
}
}
for (int u = 0; u < numberofvertices; u++) {
for (int v = 0; v < numberofvertices; v++) {
if (adjMatrix[u][v] != Integer.MAX_VALUE && dist[u] != Integer.MAX_VALUE && dist[v] > dist[u] + adjMatrix[u][v]) {
System.out.println(" Negative weight cycle detected!");
return false;
}
}
}
for (int i = 0; i < numberofvertices; i++) {
System.out.println("Node " + i + ": Distance: " + (dist[i] == Integer.MAX_VALUE ? "INF" : dist[i])+ " Parent: "+ (parent[i]==-1 ? "NIL" : parent[i]));
}
return true;
}
public void Dijkstra(int source) {
System.out.println("\n Dijkstra's Algorithm from node " + source + ":");
int[] dist = new int[numberofvertices];
boolean[] visited = new boolean[numberofvertices];
int[] parent = new int[numberofvertices];
for (int i = 0; i < numberofvertices; i++) {
dist[i] = Integer.MAX_VALUE;
visited[i] = false;
parent[i]=-1;
}
dist[source] = 0;
for (int j = 0; j < numberofvertices - 1; j++) {
int u = -1;
int min = Integer.MAX_VALUE;
for (int i = 0; i < numberofvertices; i++) { //find the minimum distanced-vertex which is unvisited
if (!visited[i] && dist[i] < min) {
min = dist[i];
u = i;
}
}
if (u == -1) break;
visited[u] = true;
for (int v = 0; v < numberofvertices; v++) { //go to all the unvisited adjacent vertex of u and relax the edges.
if (!visited[v] && adjMatrix[u][v] != Integer.MAX_VALUE && dist[v] > dist[u] + adjMatrix[u][v] ) {
dist[v] = dist[u] + adjMatrix[u][v];
parent[v]=u;
}
}
}
for (int i = 0; i < numberofvertices; i++) {
System.out.println("Node " + i + ": Distance: " + (dist[i] == Integer.MAX_VALUE ? "INF" : dist[i])+ " Parent: "+ (parent[i]==-1 ? "NIL" : parent[i]));
}
}
public void FloydWarshall() {
System.out.println("\nFloyd-Warshall All-Pairs Shortest Paths:");
int[][] dist = new int[numberofvertices][numberofvertices];
int[][] parent = new int[numberofvertices][numberofvertices];
for (int i = 0; i < numberofvertices; i++) {
for (int j = 0; j < numberofvertices; j++) {
dist[i][j] = adjMatrix[i][j];
if (i!=j && adjMatrix[i][j] != Integer.MAX_VALUE) {
parent[i][j] = i; // direct edge exists
} else {
parent[i][j] = -1; // no path yet
}
}
}
for (int k = 0; k < numberofvertices; k++) {
for (int i = 0; i < numberofvertices; i++) {
for (int j = 0; j < numberofvertices; j++) {
if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE &&dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j]; //update the distance
parent[i][j] = parent[k][j]; // update the parent
}
}
}
}
System.out.println("Distance Matrix:");
for (int i = 0; i < numberofvertices; i++) {
for (int j = 0; j < numberofvertices; j++) {
System.out.print((dist[i][j] == Integer.MAX_VALUE ? "INF" : dist[i][j]) + "\t");
}
System.out.println();
}
System.out.println("\nParent Matrix:");
for (int i = 0; i < numberofvertices; i++) {
for (int j = 0; j < numberofvertices; j++) {
System.out.print((parent[i][j] == -1 ? "NIL" : parent[i][j]) + "\t");
}
System.out.println();
}
}
}