Advertisement
updown

Biconnected Components

Jan 1st, 2023
1,268
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. stack<pair<int, int> > BCC_stack;
  2.  
  3. void BCCUtil(vector<int> adj[], int u, bool visited[],
  4.             int disc[], int low[], int& time, int parent)
  5. {
  6.  
  7.     // Mark the current node as visited
  8.     visited[u] = true;
  9.  
  10.     // Initialize discovery time and low value
  11.     disc[u] = low[u] = ++time;
  12.  
  13.     // Go through all vertices adjacent to this
  14.     for (auto v : adj[u]) {
  15.         // If v is not visited yet, then make it a child of u
  16.         // in DFS tree and recur for it
  17.         if (!visited[v]) {
  18.             BCC_stack.push(make_pair(u, v)); // add edge to the stack
  19.  
  20.             BCCUtil(adj, v, visited, disc, low, time, u, isAP);
  21.  
  22.             // Check if the subtree rooted with v has
  23.             // a connection to one of the ancestors of u
  24.             low[u] = min(low[u], low[v]);
  25.  
  26.             // If u is not root and low value of one of
  27.             // its child is more than discovery value of u.
  28.             if (parent != -1 && low[v] >= disc[u]) {
  29.                 // pop values from stack until we see the (u, v) edge and those edges are a BCC
  30.                 vector<pair<int, int> > current_component;
  31.  
  32.                 pair<int, int> value = BCC_stack.top();
  33.                 BCC_stack.pop();
  34.                 current_component.push_back(value);
  35.                 while (value != make_pair(u, v)) {
  36.                     value = BCC_stack.top();
  37.                     BCC_stack.pop();
  38.                     current_component.push_back(value);
  39.                 }
  40.             }
  41.  
  42.             if (parent == -1) { // we are the root node
  43.                 // pop values from stack until we see the (u, v) edge and those edges are a BCC
  44.                 vector<pair<int, int> > current_component;
  45.  
  46.                 pair<int, int> value = BCC_stack.top();
  47.                 BCC_stack.pop();
  48.                 current_component.push_back(value);
  49.                 while (value != make_pair(u, v)) {
  50.                     value = BCC_stack.top();
  51.                     BCC_stack.pop();
  52.                     current_component.push_back(value);
  53.                 }
  54.             }
  55.         }
  56.  
  57.         // Update low value of u for parent function calls.
  58.         else if (v != parent)
  59.             low[u] = min(low[u], disc[v]);
  60.        
  61.     }
  62.  
  63. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement