Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This approach uses DFS, but is very efficient, because we don't repeat nodes in subsequent DFS's.</p> <p><strong>High-level approach:</strong></p> <p>Initialize the values of all the nodes to <code>-1</code>.</p> <p>Do a DFS from each unexplored node, setting that node's value to that of an auto-incremented value starting from <code>0</code>.</p> <p>For these DFS's, update each node's value with <code>previous node's value + i/n^k</code> where that node is the <code>i</code>th child of the previous node and <code>k</code> is the depth explored, <strong>skipping already explored nodes</strong> (except for checking for a bigger value).</p> <p>So, an example for <code>n = 10</code>:</p> <pre><code> 0.1 0.11 0.111 j - k - p 0 / \ 0.12 i \ 0.2 l m 1 1.1 q - o ... </code></pre> <p>You can also use <code>i/branching factor+1</code> for each node to reduce the significant digits of the numbers, but that requires additional calculation to determine.</p> <p>So above we did a DFS from <code>i</code>, which had 2 children <code>j</code> and <code>m</code>. <code>m</code> had no children, <code>j</code> had 2 children, .... Then we finished with <code>i</code> and started another DFS from the next unexplored node <code>q</code>.</p> <p>Whenever you encounter a bigger value, you know that a cycle occurred.</p> <p><strong>Complexity:</strong></p> <p>You check every node at most once, and at every node you do n checks, so complexity is <code>O(n^2)</code>, which is the same as looking at every entry in the matrix once (which you can't do much better than).</p> <p><strong>Note:</strong></p> <p>I'll also just note that an <a href="http://en.wikipedia.org/wiki/Adjacency_list" rel="nofollow">adjacency list</a> will probably be faster than an adjacency matrix unless it's a very dense graph.</p>
    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. 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.
    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