Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed need for Path and Paths from the Graphs class and thus remove… #51

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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