-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ain/java/com/packt/datastructuresandalg/lesson6/activity/floydwarshall/FloydWarshall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } | ||
} |
62 changes: 62 additions & 0 deletions
62
...com/packt/datastructuresandalg/lesson6/activity/floydwarshall/solution/FloydWarshall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...java/com/packt/datastructuresandalg/lesson6/activity/floydwarshall/FloydWarshallTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |