Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. Set CUT_VERTICES = {}
  2.  
  3. int dfsnum = 0
  4. // define v.low to be the lowest dfs number    
  5. // of a non-outputted vertex reachable
  6. // by using at most one-non tree edge.
  7.  
  8. foreach v in V
  9.     if (v.dfsnum is not defined)
  10.         findcut(v,null)
  11.  
  12.  
  13. // parent is the vertex that called findcut
  14. // if u is root, parent = null
  15. void findcut(vertex v, vertex parent)
  16. {
  17.     // children here refer to children of v in the dfs tree
  18.     // hence, children cannot be connected except thru v (in the tree)
  19.     int children = 0
  20.  
  21.     v.dfsnum = dfsnum++
  22.     v.low = v.dfsnum
  23.  
  24.     foreach (v,u) in E
  25.     {
  26.        
  27.         if (u.dfsnum is not defined) {
  28.             // u is a child of v
  29.             // i.e. v is a parent of u
  30.             children++
  31.             findcut(u,v)
  32.             v.low = min(v.low, u.low)
  33.            
  34.         }else if (u.dfsnum is defined && u != parent) {
  35.             // i.e. u is not parent of v
  36.             // u must be an ancestor
  37.             v.low = min(v.low, u.dfsnum)
  38.         }
  39.  
  40.        // v is not a root...
  41.        if (parent != null && v.low >= parent.dfsnum) {
  42.            // Since the low of parent's childrens are higher than
  43.            // parent's low, there is no back edge to parent's ancestors
  44.            // i.e. if parent is removed, there is no way children can be
  45.            // connected to ancestors
  46.            // so parent has to be a cut vertex
  47.            CUT_VERTICES.add(parent)
  48.        }
  49.     }
  50.  
  51.     // v is a root (special case)
  52.     if (parent == null && children >= 2) {
  53.         CUT_VERTICES.add(v)
  54.     }
  55. }