org.jgrapht.alg.CycleDetector Java Examples

The following examples show how to use org.jgrapht.alg.CycleDetector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DagChecker.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean tryAdd(Set<? extends T> roots, Graph<T, Pair> graph, Set<T> knownNodes,
                       CycleDetector<T, Pair> cycleDetector, Deque<T> nodePath)
{
    for (T node : roots) {
        nodePath.addLast(node);
        graph.addVertex(node);
        if (knownNodes.add(node)) {
            Set<? extends T> nodesFrom = nodesFrom(node);
            for (T from : nodesFrom) {
                graph.addVertex(from);
                Pair edge = new Pair(from, node);
                graph.addEdge(from, node, edge);
                nodePath.addLast(from);
                if (cycleDetector.detectCycles())
                    return false;
                nodePath.removeLast();
            }
            if (!tryAdd(nodesFrom, graph, knownNodes, cycleDetector, nodePath))
                return false;
        }
        nodePath.removeLast();
    }
    return true;
}
 
Example #2
Source File: KBestHaplotypeFinder.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructs a new best haplotypes finder.
 *
 * @param graph the seq-graph to search.
 * @param sources source vertices for all haplotypes.
 * @param sinks sink vertices for all haplotypes.
 *
 * @throws IllegalArgumentException if <ul>
 *     <li>any of {@code graph}, {@code sources} or {@code sinks} is {@code null} or</li>
 *     <li>any of {@code sources}' or any {@code sinks}' member is not a vertex in {@code graph}.</li>
 * </ul>
 */
public KBestHaplotypeFinder(final SeqGraph graph, final Set<SeqVertex> sources, final Set<SeqVertex> sinks) {
    Utils.nonNull(graph, "graph cannot be null");
    Utils.nonNull(sources, "sources cannot be null");
    Utils.nonNull(sinks, "sinks cannot be null");
    Utils.validateArg(graph.containsAllVertices(sources), "source does not belong to the graph");
    Utils.validateArg(graph.containsAllVertices(sinks), "sink does not belong to the graph");

    //TODO dealing with cycles here due to a bug in some of the graph transformations that produces cycles.
    //TODO Once that is solve, the if-else below should be substituted by a throw if there is any cycles,
    //TODO just the line commented out below if you want to trade early-bug-fail for speed.
    //this.graph = graph;
    this.graph = new CycleDetector<>(graph).detectCycles() ? removeCycles(graph,sources,sinks) : graph;

    finderByVertex = new HashMap<>(this.graph.vertexSet().size());
    this.sinks = sinks;
    this.sources = sources;
    if (sinks.isEmpty() || sources.isEmpty()) {
        topFinder = DeadEndKBestSubHaplotypeFinder.INSTANCE;
    } else if (sources.size() == 1) {
        topFinder = createVertexFinder(sources.iterator().next());
    } else {
        topFinder = createAggregatedFinder();
    }
}
 
Example #3
Source File: RunnableTaskDagBuilder.java    From workflow with Apache License 2.0 6 votes vote down vote up
private void build(Task task, ImmutableList.Builder<RunnableTaskDag> entriesBuilder, ImmutableMap.Builder<TaskId, Task> tasksBuilder)
{
    DefaultDirectedGraph<TaskId, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
    worker(graph, task, null, tasksBuilder, Sets.newHashSet());

    CycleDetector<TaskId, DefaultEdge> cycleDetector = new CycleDetector<>(graph);
    if ( cycleDetector.detectCycles() )
    {
        throw new RuntimeException("The Task DAG contains cycles: " + task);
    }

    TopologicalOrderIterator<TaskId, DefaultEdge> orderIterator = new TopologicalOrderIterator<>(graph);
    while ( orderIterator.hasNext() )
    {
        TaskId taskId = orderIterator.next();
        Set<DefaultEdge> taskIdEdges = graph.edgesOf(taskId);
        Set<TaskId> processed = taskIdEdges
            .stream()
            .map(graph::getEdgeSource)
            .filter(edge -> !edge.equals(taskId) && !edge.getId().equals(""))
            .collect(Collectors.toSet());
        entriesBuilder.add(new RunnableTaskDag(taskId, processed));
    }
}
 
Example #4
Source File: KBestHaplotypeFinder.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private BaseGraph<V, E> removeCyclesIfNecessary(BaseGraph<V, E> graph, Set<V> sources, Set<V> sinks) {
    if (keepCycles()) {
        return graph;
    } else {
        return new CycleDetector<>(graph).detectCycles() ? removeCyclesAndVerticesThatDontLeadToSinks(graph, sources, sinks) : graph;
    }
}
 
Example #5
Source File: DirectedGraphUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
    CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
    assertTrue(cycleDetector.detectCycles());
    Set<String> cycleVertices = cycleDetector.findCycles();
    assertTrue(cycleVertices.size() > 0);
}
 
Example #6
Source File: InvocationGraphImpl.java    From custom-bytecode-analyzer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean findCycles(MethodGraph vertex) {
  CycleDetector<MethodGraph,DefaultEdge> cycleDetector = new CycleDetector(directedGraph);
  return cycleDetector.detectCyclesContainingVertex(vertex);
}
 
Example #7
Source File: GraphUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean isCyclic(DirectedGraph G) {
	CycleDetector cd = new CycleDetector(G);
	return cd.detectCycles();
}
 
Example #8
Source File: LayoutTest.java    From JQF with BSD 2-Clause "Simplified" License 3 votes vote down vote up
@Fuzz
public void testWithGenerator(@GraphModel(nodes=10, edges=25) DirectedGraph graph) {
    JGraphModelAdapter adapter = new JGraphModelAdapter(graph);

    Assume.assumeFalse(new CycleDetector<>(graph).detectCycles());

    JGraph jgraph = new JGraph(adapter);


    JGraphLayout layout = new JGraphHierarchicalLayout();
    JGraphFacade facade = new JGraphFacade(jgraph);
    layout.run(facade);
}
 
Example #9
Source File: BaseGraph.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Checks for the presence of directed cycles in the graph.
 *
 * @return {@code true} if the graph has cycles, {@code false} otherwise.
 */
public final boolean hasCycles() {
    return new CycleDetector<>(this).detectCycles();
}
 
Example #10
Source File: BaseGraph.java    From gatk with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Checks for the presence of directed cycles in the graph.
 *
 * @return {@code true} if the graph has cycles, {@code false} otherwise.
 */
public final boolean hasCycles() {
    return new CycleDetector<>(this).detectCycles();
}