Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This simple solution returns all the paths.</p> <p>It works also with cyclic graphs, printing all cycling paths, hence it can be used also for cycle detection, which is not available in JUNG.</p> <p>It uses an arbitrary class <code>Entity</code> for the nodes, and <code>Edge</code> for the edges. You can create such objects (make sure to implement <code>hashcode()</code> and <code>equals()</code>), or simply substitute them with <code>String</code> objects, depending on your needs.</p> <pre><code>public class AllPaths { private static List&lt;List&lt;Edge&gt;&gt; allPaths; public static List&lt;List&lt;Edge&gt;&gt; getAllPathsBetweenNodes(DirectedGraph&lt;Entity, Edge&gt; graph, Entity startNode, Entity endNode) { allPaths = new ArrayList&lt;List&lt;Edge&gt;&gt;(); List&lt;Edge&gt; currentPath = new ArrayList&lt;Edge&gt;(); findAllPaths(startNode, startNode, endNode, currentPath, graph); return allPaths; } private static void findAllPaths(Entity currentNode, Entity startNode, Entity endNode, List&lt;Edge&gt; currentPath, DirectedGraph&lt;Entity, Edge&gt; graph) { Collection&lt;Edge&gt; outgoingEdges = graph.getOutEdges(currentNode); for (Edge outEdge : outgoingEdges) { Entity outNode = outEdge.getSupertype(); if (outNode.equals(startNode)) { List&lt;Edge&gt; cyclePath = new ArrayList&lt;Edge&gt;(currentPath); cyclePath.add(outEdge); System.out.println("Found cycle provoked by path " + cyclePath); continue; } List&lt;Edge&gt; newPath = new ArrayList&lt;Edge&gt;(currentPath); newPath.add(outEdge); if (outNode.equals(endNode)) { allPaths.add(newPath); continue; } findAllPaths(outNode, startNode, endNode, newPath, graph); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload