diff --git a/src/main/java/gov/hhs/aspr/ms/util/graph/Graphs.java b/src/main/java/gov/hhs/aspr/ms/util/graph/Graphs.java index 104da4a..022e891 100644 --- a/src/main/java/gov/hhs/aspr/ms/util/graph/Graphs.java +++ b/src/main/java/gov/hhs/aspr/ms/util/graph/Graphs.java @@ -8,12 +8,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; -import gov.hhs.aspr.ms.util.path.Path; -import gov.hhs.aspr.ms.util.path.Paths; - public final class Graphs { // hidden constructor @@ -61,7 +57,7 @@ public static Graph getSourceSinkReducedGraph(Graph graph) { * edges removed. A non-cyclic edge is one where there is no path through the * graph from the edge's destination node to its source node. */ - public static Graph getEdgeReducedGraph(Graph graph) { + public static Graph getEdgeReducedGraph(Graph graph) { MutableGraph mutableGraph = new MutableGraph<>(); mutableGraph.addAll(graph); List edges = graph.getEdges(); @@ -69,47 +65,43 @@ public static Graph getEdgeReducedGraph(Graph graph) { for (E edge : edges) { N originNode = graph.getOriginNode(edge); N destinationNode = graph.getDestinationNode(edge); - Optional> optional = Paths.getPath(graph, destinationNode, originNode, (e) -> 1, (a, b) -> 0); - if (optional.isEmpty()) { + + /* + * If there is no path from the destination node to the origin node then remove + * the edge + */ + Deque nodesToProcess = new ArrayDeque<>(); + nodesToProcess.add(destinationNode); + Set visited = new LinkedHashSet<>(); + + boolean pathFound = false; + + loopLabel: while (!nodesToProcess.isEmpty()) { + N n = nodesToProcess.remove(); + visited.add(n); + for (E e : mutableGraph.getOutboundEdges(n)) { + + N n2 = mutableGraph.getDestinationNode(e); + if (n2.equals(originNode)) { + pathFound = true; + break loopLabel; + } + if (!visited.contains(n2)) { + nodesToProcess.add(n2); + } + + } + } + + // Optional> optional = Paths.getPath(graph, destinationNode, + // originNode, (e) -> 1, (a, b) -> 0); + if (!pathFound) { mutableGraph.removeEdge(edge); } } return mutableGraph.asGraph(); } -// private static Graph getEdgeReducedGraph2(Graph graph) { -// MutableGraph mutableGraph = new MutableGraph<>(); -// mutableGraph.addAll(graph); -// List edges = graph.getEdges(); -// -// for (E edge : edges) { -// N originNode = graph.getOriginNode(edge); -// N destinationNode = graph.getDestinationNode(edge); -// -// /* -// * If there is no path from the destination node to the origin node that does -// * not use the edge, then remove the edge -// */ -// Deque nodesToProcess = new ArrayDeque<>(); -// nodesToProcess.add(destinationNode); -// -// while(!nodesToProcess.isEmpty()) { -// N n = nodesToProcess.remove(); -// for(E e : mutableGraph.getOutboundEdges(n)) { -// if(!e.equals(edge)) { -// -// } -// } -// } -// -// Optional> optional = Paths.getPath(graph, destinationNode, originNode, (e) -> 1, (a, b) -> 0); -// if (optional.isEmpty()) { -// mutableGraph.removeEdge(edge); -// } -// } -// return mutableGraph.asGraph(); -// } - /** * Returns the GraphCyclisity of the given graph */