Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Cap committed Feb 26, 2015
1 parent bc9f4b9 commit b39ca11
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/main/java/org/jgrapht/util/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static <V, E> DirectedGraph<V, E> build(DirectedGraph<V, E> implicitGraph
}

public static <V, E> DirectedGraph<V, E> build(DirectedGraph<V, E> implicitGraph,
DirectedGraph<V, E> emptyExplicitGraph,
DirectedGraph<V, E> explicitGraph,
Collection<V> init,
int maxVertices) {

Expand All @@ -35,7 +35,7 @@ public static <V, E> DirectedGraph<V, E> build(DirectedGraph<V, E> implicitGraph
whileLoop:
while (!open.isEmpty()) {
V current = open.poll();
emptyExplicitGraph.addVertex(current);
explicitGraph.addVertex(current);

Set<E> outEdges = implicitGraph.outgoingEdgesOf(current);
for (E edge : outEdges) {
Expand All @@ -51,15 +51,37 @@ public static <V, E> DirectedGraph<V, E> build(DirectedGraph<V, E> implicitGraph
V target = implicitGraph.getEdgeTarget(edge);

if (!closed.contains(target)) {
emptyExplicitGraph.addVertex(target);
explicitGraph.addVertex(target);
closed.add(target);
open.offer(target);
}

emptyExplicitGraph.addEdge(current, target, edge);
explicitGraph.addEdge(current, target, edge);
}

Set<E> inEdges = implicitGraph.incomingEdgesOf(current);
for (E edge : inEdges) {

if (counter++ == maxVertices) {
// For some reason, when the condition was counter++ > maxVertices,
// the code was non-deterministically failing.
// throw new RuntimeException("Exceeded maximum number of vertices");
break whileLoop;
}


V source = implicitGraph.getEdgeSource(edge);

if (!closed.contains(source)) {
explicitGraph.addVertex(source);
closed.add(source);
open.offer(source);
}

explicitGraph.addEdge(source, current, edge);
}
}

return emptyExplicitGraph;
return explicitGraph;
}
}

0 comments on commit b39ca11

Please sign in to comment.