File size: 3,263 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let hopcroftTarjanBiconnected = function() {
  let eles = this;
  let nodes = {};
  let id = 0;
  let edgeCount = 0;
  let components = [];
  let stack = [];
  let visitedEdges = {};

  const buildComponent = (x, y) => {
    let i = stack.length-1;
    let cutset = [];
    let component = eles.spawn();

    while (stack[i].x != x || stack[i].y != y) {
      cutset.push(stack.pop().edge);
      i--;
    }
    cutset.push(stack.pop().edge);

    cutset.forEach(edge => {
      let connectedNodes = edge.connectedNodes()
                               .intersection(eles);
      component.merge(edge);
      connectedNodes.forEach(node => {
        const nodeId = node.id();
        const connectedEdges = node.connectedEdges()
                                   .intersection(eles);
        component.merge(node);
        if (!nodes[nodeId].cutVertex) {
          component.merge(connectedEdges);
        } else {
          component.merge(connectedEdges.filter(edge => edge.isLoop()));
        }
      });
    });
    components.push(component);
  };

  const biconnectedSearch = (root, currentNode, parent) => {
    if (root === parent) edgeCount += 1;
    nodes[currentNode] = {
      id : id,
      low : id++,
      cutVertex : false
    };
    let edges = eles.getElementById(currentNode)
                    .connectedEdges()
                    .intersection(eles);

    if (edges.size() === 0) {
      components.push(eles.spawn(eles.getElementById(currentNode)));
    } else {
      let sourceId, targetId, otherNodeId, edgeId;

      edges.forEach(edge => {
        sourceId = edge.source().id();
        targetId = edge.target().id();
        otherNodeId = (sourceId === currentNode) ? targetId : sourceId;

        if (otherNodeId !== parent) {
          edgeId = edge.id();

          if (!visitedEdges[edgeId]) {
            visitedEdges[edgeId] = true;
            stack.push({
              x : currentNode,
              y : otherNodeId,
              edge
            });
          }

          if (!(otherNodeId in nodes)) {
            biconnectedSearch(root, otherNodeId, currentNode);
            nodes[currentNode].low = Math.min(nodes[currentNode].low,
                                              nodes[otherNodeId].low);

            if (nodes[currentNode].id <= nodes[otherNodeId].low) {
              nodes[currentNode].cutVertex = true;
              buildComponent(currentNode, otherNodeId);
            }
          } else {
            nodes[currentNode].low = Math.min(nodes[currentNode].low,
                                              nodes[otherNodeId].id);
          }
        }
      });
    }
  };

  eles.forEach(ele => {
    if (ele.isNode()) {
      let nodeId = ele.id();

      if (!(nodeId in nodes)) {
        edgeCount = 0;
        biconnectedSearch(nodeId, nodeId);
        nodes[nodeId].cutVertex = (edgeCount > 1);
      }
    }
  });

  let cutVertices = Object.keys(nodes)
    .filter(id => nodes[id].cutVertex)
    .map(id => eles.getElementById(id));

  return {
    cut: eles.spawn(cutVertices),
    components
  };
};

export default {
  hopcroftTarjanBiconnected,
  htbc: hopcroftTarjanBiconnected,
  htb: hopcroftTarjanBiconnected,
  hopcroftTarjanBiconnectedComponents: hopcroftTarjanBiconnected
};