Skidlz

Tarjan's

Nov 21st, 2026
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | Source Code | 0 0
  1.         //Tarjan's SCC Algo------------------------------------------------------------------------
  2.         //Finds the earliest node in walk that can be reached by moving forward (lowLink)
  3.         //When we find the node with an index matching this lowLink (root), we emit it and the walk that reached it (SCC)
  4.         //This could be a cycle, or a singleton. The emission order is the reverse topological order
  5.         std::vector<uint8_t> sccStack, dfsStack, output;
  6.         uint8_t nextNode = 0, indexCount = 0;
  7.  
  8.         auto tarjanStep = [&]() {
  9.             const uint8_t node = nextNode;
  10.             auto& [index, lowLink, children] = nodes[node];
  11.  
  12.             if (index == UNDISCOVERED) { //found new node
  13.                 lowLink = index = indexCount++; //give it a new index
  14.                 dfsStack.push_back(node);
  15.                 sccStack.push_back(node);
  16.             }
  17.  
  18.             if (!children.empty()) {
  19.                 const uint8_t child = children.back(), childIndex = nodes[child].index;
  20.                 children.pop_back();
  21.  
  22.                 if (childIndex == UNDISCOVERED) nextNode = child; //dive into child if undiscovered
  23.                 else if (childIndex < lowLink) //if next node is a back-edge, inherit lowLink
  24.                     if (auto it = std::find(sccStack.begin(), sccStack.end(), child); it != sccStack.end())
  25.                         lowLink = childIndex;
  26.             } else { //ran out of undiscovered children
  27.                 if (lowLink == index) { //found a root, splice out members to emit in *reverse*
  28.                     auto rit = std::find(sccStack.rbegin(), sccStack.rend(), node); //find start
  29.                     output.insert(output.end(), sccStack.rbegin(), std::next(rit)); //graft onto output
  30.                     sccStack.erase(std::next(rit).base(), sccStack.end()); //remove from stack
  31.                 }
  32.  
  33.                 dfsStack.pop_back();
  34.                 if (!dfsStack.empty()) {
  35.                     nextNode = dfsStack.back(); //ascend back up the stack
  36.  
  37.                     //make parent inherit lowLink from us
  38.                     if (lowLink < nodes[nextNode].lowLink) nodes[nextNode].lowLink = lowLink;
  39.                 } else { //ran out of parents
  40.                     nextNode = BLANK; //find a new node to start on that hasn't been discovered yet
  41.                     for (uint8_t i = 0; i < nodeCount && nextNode == BLANK; i++)
  42.                         if (nodes[i].index == UNDISCOVERED) nextNode = i;
  43.  
  44.                     if (nextNode == BLANK) return false; //ran out of new nodes, done
  45.                 }
  46.             }
  47.  
  48.             return true; //need another pass
  49.         };
  50.  
  51.         while (tarjanStep()) { } //loop until we run out of nodes
Tags: tarjan
Advertisement
Add Comment
Please, Sign In to add comment