Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<body>
  <h1>Tarjan's strongly connected components algorithm (JS)</h1>
  <h2>Input</h2>
  <p>
  <pre>[
  [1]
  [4, 6, 7]
  [4, 6, 7]
  [4, 6, 7]
  [2, 3]
  [2, 3]
  [5, 8]
  [5, 8]
  []
  []
  [10]
]</pre>
  </p>
  <h2>Output</h2>
  <div class="jsCyclesCount"></div>
  <p>
  <div class="jsCycles"></div>
  </p>
  <h2>Full article</h2>
  <a href="https://www.vacilando.org/article/javascript-implementation-tarjans-cycle-detection-algorithm">https://www.vacilando.org/article/javascript-implementation-tarjans-cycle-detection-algorithm</a>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                /*
 * Array $G is to contain the source graph in node-adjacency format.
 */
var $G = [];
$G.push([1]);
$G.push([4, 6, 7]);
$G.push([4, 6, 7]);
$G.push([4, 6, 7]);
$G.push([2, 3]);
$G.push([2, 3]);
$G.push([5, 8]);
$G.push([5, 8]);
$G.push([]);
$G.push([]);
$G.push([10]); // This is a self-cycle (aka "loop").

console.log($G);

/*
 * There are 11 results for the above example (strictly speaking: 10 cycles and 1 loop):
 * 2|4
 * 2|4|3|6|5
 * 2|4|3|7|5
 * 2|6|5
 * 2|6|5|3|4
 * 2|7|5
 * 2|7|5|3|4
 * 3|4
 * 3|6|5
 * 3|7|5
 * 1|0
 */
var $cycles = js_tarjan_entry($G);
console.log("Cycles found: " + $cycles.length);
console.log($cycles);

// Print the results in CodePen
var $jsCyclesCount = document.querySelector(".jsCyclesCount");
$jsCyclesCount.innerHTML = "Cycles found: " + $cycles.length;
var $jsCycles = document.querySelector(".jsCycles");
$jsCycles.innerHTML = "['" + $cycles.join("', '") + "']";

// All the following must be global to pass them to recursive function js_tarjan().
var $cycles;
var $marked;
var $marked_stack;
var $point_stack;

/*
 * js_tarjan_entry() iterates through the graph array rows, executing js_tarjan().
 */
function js_tarjan_entry($G_local) {
  // Initialize global values that are so far undefined.
  $cycles = [];
  $marked = [];
  $marked_stack = [];
  $point_stack = [];

  for (let $x = 0; $x < $G_local.length; $x++) {
    $marked[$x] = false;
  }

  for (let $i = 0; $i < $G_local.length; $i++) {
    js_tarjan($i, $i);
    while ($marked_stack.length > 0) {
      $marked[$marked_stack.pop()] = false;
    }
    // console.log($i + 1 + ' / ' + $G_local.length); // Enable if you wish to follow progression through the array rows.
  }

  $cycles = Object.keys($cycles);

  return $cycles;
}

/*
 * Recursive function to detect strongly connected components (cycles, loops).
 */
function js_tarjan($s, $v) {
  // Update global values.
  $marked[$v] = true;
  $marked_stack.push($v);
  $point_stack.push($v);

  // Define local values.
  let $f = false;
  let $t;

  // var $maxlooplength = 3; // Enable to Limit the length of loops to keep in the results (see below).

  $G[$v]?.forEach(($w) => {
    if ($w < $s) {
      $G[$w] = [];
    } else if ($w === $s) {
      // if ($point_stack.length == $maxlooplength){ // Enable to collect cycles of a given length only.
      // Add new cycles as array keys to avoid duplication. Way faster than using array search.
      $cycles[$point_stack.join("|")] = true;
      // }
      $f = true;
    } else if ($marked[$w] === false) {
      // if ($point_stack.length < $maxlooplength){ // Enable to only collect cycles up to $maxlooplength.
      $t = js_tarjan($s, $w);
      // }
      if ($f.length !== 0 || $t.length !== 0) {
        $f = true;
      }
    }
  });

  if ($f === true) {
    while ($marked_stack.slice(-1)[0] !== $v) {
      $marked[$marked_stack.pop()] = false;
    }
    $marked_stack.pop();
    $marked[$v] = false;
  }

  $point_stack.pop();

  return $f;
}

              
            
!
999px

Console