Skip to content

Commit 0ad5788

Browse files
committed
Add Floyd-Warshall activity
1 parent b20bd00 commit 0ad5788

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall;
2+
3+
import java.util.List;
4+
5+
public class FloydWarshall {
6+
int[][] adj;
7+
int[][] path;
8+
9+
public FloydWarshall(int nodes) {
10+
this.adj = new int[nodes][nodes];
11+
this.path = new int[nodes][nodes];
12+
for (int i = 0; i < adj.length; i++) {
13+
for (int j = 0; j < adj[i].length; j++) {
14+
if (i == j) {
15+
this.adj[i][j] = 0;
16+
this.path[i][j] = i;
17+
} else {
18+
this.adj[i][j] = Integer.MAX_VALUE;
19+
this.path[i][j] = -1;
20+
}
21+
}
22+
}
23+
}
24+
25+
public void addEdge(int u, int v, int weight) {
26+
if (weight < adj[u][v]) {
27+
adj[u][v] = weight;
28+
path[u][v] = u;
29+
}
30+
}
31+
32+
public List<Integer> path(int u, int v) { return null; }
33+
34+
public void run() { }
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall.solution;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FloydWarshall {
7+
int[][] adj;
8+
int[][] path;
9+
10+
public FloydWarshall(int nodes) {
11+
this.adj = new int[nodes][nodes];
12+
this.path = new int[nodes][nodes];
13+
for (int i = 0; i < adj.length; i++) {
14+
for (int j = 0; j < adj[i].length; j++) {
15+
if (i == j) {
16+
this.adj[i][j] = 0;
17+
this.path[i][j] = i;
18+
} else {
19+
this.adj[i][j] = Integer.MAX_VALUE;
20+
this.path[i][j] = -1;
21+
}
22+
}
23+
}
24+
}
25+
26+
public void addEdge(int u, int v, int weight) {
27+
if (weight < adj[u][v]) {
28+
adj[u][v] = weight;
29+
path[u][v] = u;
30+
}
31+
}
32+
33+
public List<Integer> path(int u, int v) {
34+
List<Integer> res = new ArrayList<>();
35+
if (path[u][v] == -1)
36+
return res;
37+
int i = v;
38+
while (u != i) {
39+
res.add(0, i);
40+
i = path[u][i];
41+
}
42+
res.add(0, u);
43+
return res;
44+
}
45+
46+
public void run() {
47+
for (int k = 0; k < adj.length; k++) {
48+
for (int i = 0; i < adj.length; i++) {
49+
if (adj[i][k] >= Integer.MAX_VALUE)
50+
continue;
51+
for (int j = 0; j < adj.length; j++) {
52+
if (adj[k][j] >= Integer.MAX_VALUE)
53+
continue;
54+
if (adj[i][k] + adj[k][j] < adj[i][j]) {
55+
adj[i][j] = adj[i][k] + adj[k][j];
56+
path[i][j] = path[k][j];
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.util.Arrays;
6+
7+
public class FloydWarshallTest extends TestCase {
8+
public void test() {
9+
FloydWarshall f = new FloydWarshall(5);
10+
f.addEdge(0, 1, 10);
11+
f.addEdge(0, 3, 5);
12+
f.addEdge(1, 3, 2);
13+
f.addEdge(1, 2, 1);
14+
f.addEdge(2, 4, 4);
15+
f.addEdge(3, 1, 3);
16+
f.addEdge(3, 2, 9);
17+
f.addEdge(3, 4, 2);
18+
f.addEdge(4, 2, 6);
19+
f.run();
20+
21+
assertTrue(f.path(0, 0).equals(Arrays.asList(0)));
22+
assertTrue(f.path(0, 1).equals(Arrays.asList(0, 3, 1)));
23+
assertTrue(f.path(0, 2).equals(Arrays.asList(0, 3, 1, 2)));
24+
assertTrue(f.path(0, 3).equals(Arrays.asList(0, 3)));
25+
assertTrue(f.path(0, 4).equals(Arrays.asList(0, 3, 4)));
26+
assertTrue(f.path(1, 4).equals(Arrays.asList(1, 3, 4)));
27+
}
28+
}

0 commit comments

Comments
 (0)