Skip to content

Commit

Permalink
Merge pull request #51 from HHS/graph_path
Browse files Browse the repository at this point in the history
Removed need for Path and Paths from the Graphs class and thus remove…
  • Loading branch information
shawnhatch authored Nov 5, 2024
2 parents 2fc5107 + 152e64d commit ee8c255
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions src/main/java/gov/hhs/aspr/ms/util/graph/Graphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,55 +57,51 @@ public static <N, E> Graph<N, E> getSourceSinkReducedGraph(Graph<N, E> 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 <N, E> Graph<N, E> getEdgeReducedGraph(Graph<N, E> graph) {
public static <N, E> Graph<N, E> getEdgeReducedGraph(Graph<N, E> graph) {
MutableGraph<N, E> mutableGraph = new MutableGraph<>();
mutableGraph.addAll(graph);
List<E> edges = graph.getEdges();

for (E edge : edges) {
N originNode = graph.getOriginNode(edge);
N destinationNode = graph.getDestinationNode(edge);
Optional<Path<E>> 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<N> nodesToProcess = new ArrayDeque<>();
nodesToProcess.add(destinationNode);
Set<N> 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<Path<E>> optional = Paths.getPath(graph, destinationNode,
// originNode, (e) -> 1, (a, b) -> 0);
if (!pathFound) {
mutableGraph.removeEdge(edge);
}
}
return mutableGraph.asGraph();
}

// private static <N, E> Graph<N, E> getEdgeReducedGraph2(Graph<N, E> graph) {
// MutableGraph<N, E> mutableGraph = new MutableGraph<>();
// mutableGraph.addAll(graph);
// List<E> 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<N> nodesToProcess = new ArrayDeque<>();
// nodesToProcess.add(destinationNode);
//
// while(!nodesToProcess.isEmpty()) {
// N n = nodesToProcess.remove();
// for(E e : mutableGraph.getOutboundEdges(n)) {
// if(!e.equals(edge)) {
//
// }
// }
// }
//
// Optional<Path<E>> 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
*/
Expand Down

0 comments on commit ee8c255

Please sign in to comment.