Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It works! In an <a href="https://stackoverflow.com/questions/2910186">earlier iteration</a> of the <a href="https://en.wikipedia.org/wiki/Johnson%27s_algorithm" rel="nofollow noreferrer">Johnson algorithm</a>, I had supposed that <code>A</code> was an <a href="http://en.wikipedia.org/wiki/Adjacency_matrix" rel="nofollow noreferrer">adjacency matrix</a>. Instead, it appears to represent an <a href="http://en.wikipedia.org/wiki/Adjacency_list" rel="nofollow noreferrer">adjacency list</a>. In that example, implemented below, the vertices <a href="http://en.wikipedia.org/wiki/Adjacency_list" rel="nofollow noreferrer">{a, b, c}</a> are numbered {0, 1, 2}, yielding the following circuits.</p> <p>Addendum: As noted in this proposed <a href="https://stackoverflow.com/review/suggested-edits/1478397">edit</a> and helpful <a href="https://stackoverflow.com/a/14824734/230513">answer</a>, the algorithm specifies that <code>unblock()</code> should remove the element having the <em>value</em> <code>w</code>, not the element having the <em>index</em> <code>w</code>.</p> <pre><code>list.remove(Integer.valueOf(w)); </code></pre> <p>Sample output:</p> <pre> 0 1 0 0 1 2 0 0 2 0 0 2 1 0 1 0 1 1 0 2 1 1 2 0 1 1 2 1 2 0 1 2 2 0 2 2 1 0 2 2 1 2 </pre> <p>By default, the program starts with <code>s = 0</code>; implementing <code>s := least vertex in V</code> as an optimization remains. A variation that produces only unique cycles is shown <a href="https://stackoverflow.com/a/35922906/230513">here</a>.</p> <pre class="lang-java prettyprint-override"><code>import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Stack; /** * @see http://dutta.csc.ncsu.edu/csc791_spring07/wrap/circuits_johnson.pdf * @see https://stackoverflow.com/questions/2908575 * @see https://stackoverflow.com/questions/2939877 * @see http://en.wikipedia.org/wiki/Adjacency_matrix * @see http://en.wikipedia.org/wiki/Adjacency_list */ public final class CircuitFinding { final Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;(); final List&lt;List&lt;Integer&gt;&gt; a; final List&lt;List&lt;Integer&gt;&gt; b; final boolean[] blocked; final int n; int s; public static void main(String[] args) { List&lt;List&lt;Integer&gt;&gt; a = new ArrayList&lt;List&lt;Integer&gt;&gt;(); a.add(new ArrayList&lt;Integer&gt;(Arrays.asList(1, 2))); a.add(new ArrayList&lt;Integer&gt;(Arrays.asList(0, 2))); a.add(new ArrayList&lt;Integer&gt;(Arrays.asList(0, 1))); CircuitFinding cf = new CircuitFinding(a); cf.find(); } /** * @param a adjacency structure of strong component K with * least vertex in subgraph of G induced by {s, s + 1, n}; */ public CircuitFinding(List&lt;List&lt;Integer&gt;&gt; a) { this.a = a; n = a.size(); blocked = new boolean[n]; b = new ArrayList&lt;List&lt;Integer&gt;&gt;(); for (int i = 0; i &lt; n; i++) { b.add(new ArrayList&lt;Integer&gt;()); } } private void unblock(int u) { blocked[u] = false; List&lt;Integer&gt; list = b.get(u); for (int w : list) { //delete w from B(u); list.remove(Integer.valueOf(w)); if (blocked[w]) { unblock(w); } } } private boolean circuit(int v) { boolean f = false; stack.push(v); blocked[v] = true; L1: for (int w : a.get(v)) { if (w == s) { //output circuit composed of stack followed by s; for (int i : stack) { System.out.print(i + " "); } System.out.println(s); f = true; } else if (!blocked[w]) { if (circuit(w)) { f = true; } } } L2: if (f) { unblock(v); } else { for (int w : a.get(v)) { //if (v∉B(w)) put v on B(w); if (!b.get(w).contains(v)) { b.get(w).add(v); } } } v = stack.pop(); return f; } public void find() { while (s &lt; n) { if (a != null) { //s := least vertex in V; L3: circuit(s); s++; } else { s = n; } } } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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