Skip to content

Commit

Permalink
Add Floyd-Warshall activity
Browse files Browse the repository at this point in the history
  • Loading branch information
jcazevedo committed Apr 18, 2018
1 parent b20bd00 commit 0ad5788
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall;

import java.util.List;

public class FloydWarshall {
int[][] adj;
int[][] path;

public FloydWarshall(int nodes) {
this.adj = new int[nodes][nodes];
this.path = new int[nodes][nodes];
for (int i = 0; i < adj.length; i++) {
for (int j = 0; j < adj[i].length; j++) {
if (i == j) {
this.adj[i][j] = 0;
this.path[i][j] = i;
} else {
this.adj[i][j] = Integer.MAX_VALUE;
this.path[i][j] = -1;
}
}
}
}

public void addEdge(int u, int v, int weight) {
if (weight < adj[u][v]) {
adj[u][v] = weight;
path[u][v] = u;
}
}

public List<Integer> path(int u, int v) { return null; }

public void run() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall.solution;

import java.util.ArrayList;
import java.util.List;

public class FloydWarshall {
int[][] adj;
int[][] path;

public FloydWarshall(int nodes) {
this.adj = new int[nodes][nodes];
this.path = new int[nodes][nodes];
for (int i = 0; i < adj.length; i++) {
for (int j = 0; j < adj[i].length; j++) {
if (i == j) {
this.adj[i][j] = 0;
this.path[i][j] = i;
} else {
this.adj[i][j] = Integer.MAX_VALUE;
this.path[i][j] = -1;
}
}
}
}

public void addEdge(int u, int v, int weight) {
if (weight < adj[u][v]) {
adj[u][v] = weight;
path[u][v] = u;
}
}

public List<Integer> path(int u, int v) {
List<Integer> res = new ArrayList<>();
if (path[u][v] == -1)
return res;
int i = v;
while (u != i) {
res.add(0, i);
i = path[u][i];
}
res.add(0, u);
return res;
}

public void run() {
for (int k = 0; k < adj.length; k++) {
for (int i = 0; i < adj.length; i++) {
if (adj[i][k] >= Integer.MAX_VALUE)
continue;
for (int j = 0; j < adj.length; j++) {
if (adj[k][j] >= Integer.MAX_VALUE)
continue;
if (adj[i][k] + adj[k][j] < adj[i][j]) {
adj[i][j] = adj[i][k] + adj[k][j];
path[i][j] = path[k][j];
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.packt.datastructuresandalg.lesson6.activity.floydwarshall;

import junit.framework.TestCase;

import java.util.Arrays;

public class FloydWarshallTest extends TestCase {
public void test() {
FloydWarshall f = new FloydWarshall(5);
f.addEdge(0, 1, 10);
f.addEdge(0, 3, 5);
f.addEdge(1, 3, 2);
f.addEdge(1, 2, 1);
f.addEdge(2, 4, 4);
f.addEdge(3, 1, 3);
f.addEdge(3, 2, 9);
f.addEdge(3, 4, 2);
f.addEdge(4, 2, 6);
f.run();

assertTrue(f.path(0, 0).equals(Arrays.asList(0)));
assertTrue(f.path(0, 1).equals(Arrays.asList(0, 3, 1)));
assertTrue(f.path(0, 2).equals(Arrays.asList(0, 3, 1, 2)));
assertTrue(f.path(0, 3).equals(Arrays.asList(0, 3)));
assertTrue(f.path(0, 4).equals(Arrays.asList(0, 3, 4)));
assertTrue(f.path(1, 4).equals(Arrays.asList(1, 3, 4)));
}
}

0 comments on commit 0ad5788

Please sign in to comment.