Skip to content

Commit

Permalink
Merge branch 'master' into security_limits_default_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg committed Sep 26, 2016
2 parents 2fa8498 + 210359e commit 1f1f84e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ public void traverse(int v, Traverser<E> traverser) {
@Override
public List<TIntArrayList> findAllPaths(int from, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled) {
Objects.requireNonNull(pathComplete);
Objects.requireNonNull(pathCanceled);
List<TIntArrayList> paths = new ArrayList<>();
BitSet encountered = new BitSet(vertices.size());
TIntArrayList path = new TIntArrayList(1);
Expand All @@ -366,18 +365,23 @@ public List<TIntArrayList> findAllPaths(int from, Function<V, Boolean> pathCompl
private boolean findAllPaths(int e, int v1or2, Function<V, Boolean> pathComplete, Function<E, Boolean> pathCanceled,
TIntArrayList adjacentEdges, int i, TIntArrayList path, BitSet encountered,
List<TIntArrayList> paths) {
if (encountered.get(v1or2)) {
return false;
}
Vertex<V> obj1or2 = vertices.get(v1or2);
if (pathComplete.apply(obj1or2.getObject())) {
path.add(e);
paths.add(path);
return true;
} else {
path.add(e);
if (i < adjacentEdges.size () - 1){
if (i < adjacentEdges.size () - 1) {
TIntArrayList path2 = new TIntArrayList(path);
BitSet encountered2 = new BitSet(vertices.size());
encountered2.or(encountered);
path2.add(e);
findAllPaths(v1or2, pathComplete, pathCanceled, path2, encountered2, paths);
} else{
} else {
path.add(e);
findAllPaths(v1or2, pathComplete, pathCanceled, path, encountered, paths);
}
return false;
Expand All @@ -393,19 +397,21 @@ private void findAllPaths(int v, Function<V, Boolean> pathComplete, Function<E,
for (int i = 0; i < adjacentEdges.size(); i++) {
int e = adjacentEdges.getQuick(i);
Edge<E> edge = edges.get(e);
if (pathCanceled.apply(edge.getObject())) {
if (pathCanceled != null && pathCanceled.apply(edge.getObject())) {
return;
}
int v1 = edge.getV1();
int v2 = edge.getV2();
if (!encountered.get(v1)) {
if (v == v2) {
if (findAllPaths(e, v1, pathComplete, pathCanceled, adjacentEdges, i, path, encountered, paths)) {
return;
}
} else if (!encountered.get(v2)) {
} else if (v == v1) {
if (findAllPaths(e, v2, pathComplete, pathCanceled, adjacentEdges, i, path, encountered, paths)) {
return;
}
} else {
throw new AssertionError();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
*/
package eu.itesla_project.graph;

import gnu.trove.list.array.TIntArrayList;
import org.junit.After;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -89,4 +94,55 @@ public void testRemoveEdge() {
assertTrue(graph.getEdgeCount() == 0);
}

/**
* 0
* |
* ---------
* | | |
* 1 2 3
* | | |
* ----- |
* | |
* 4 |
* | |
* -------
* |
* 5
*
* edges:
* 0 <-> 1 : 0
* 0 <-> 2 : 1
* 0 <-> 3 : 2
* 1 <-> 4 : 3
* 2 <-> 4 : 4
* 4 <-> 5 : 5
* 3 <-> 5 : 6
*
* all paths (edge numbers) between vertex 0 and 5:
* 0, 3, 5
* 1, 4, 5
* 2, 6
*/
@Test
public void testFindAllPaths() {
graph.addVertex();
graph.addVertex();
graph.addVertex();
graph.addVertex();
graph.addVertex();
graph.addVertex();
graph.setVertexObject(5, new Vertex("end"));
graph.addEdge(0, 1, null); // 0
graph.addEdge(0, 2, null); // 1
graph.addEdge(0, 3, null); // 2
graph.addEdge(1, 4, null); // 3
graph.addEdge(2, 4, null); // 4
graph.addEdge(4, 5, null); // 5
graph.addEdge(3, 5, null); // 6
List<TIntArrayList> paths = graph.findAllPaths(0, vertex -> vertex != null && "end".equals(vertex.name), null);
assertTrue(paths.size() == 3);
assertArrayEquals(paths.get(0).toArray(), new int[] {2, 6});
assertArrayEquals(paths.get(1).toArray(), new int[] {0, 3, 5});
assertArrayEquals(paths.get(2).toArray(), new int[] {1, 4, 5});
}
}

0 comments on commit 1f1f84e

Please sign in to comment.