forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbiconnectivity.cpp
More file actions
279 lines (219 loc) · 5.34 KB
/
biconnectivity.cpp
File metadata and controls
279 lines (219 loc) · 5.34 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* This software is distributed under the GNU Lesser General Public License */
//==========================================================================
//
// biconnectivity.cpp
//
//==========================================================================
// $Id: biconnectivity.cpp,v 1.20 2002/02/28 15:40:52 raitner Exp $
#include <GTL/biconnectivity.h>
__GTL_BEGIN_NAMESPACE
biconnectivity::biconnectivity() : dfs()
{
add_edges = false;
store_preds(true);
store_comp = false;
scan_whole_graph(true);
num_of_components = 0;
}
void biconnectivity::reset()
{
dfs::reset();
if (store_comp) {
while (!node_stack.empty()) {
node_stack.pop();
}
while (!edge_stack.empty()) {
edge_stack.pop();
}
components.erase(components.begin(), components.end());
}
if (add_edges) {
additional.erase(additional.begin(), additional.end());
}
cut_points.erase(cut_points.begin(), cut_points.end());
num_of_components = 0;
}
int biconnectivity::check(graph& G)
{
return G.is_undirected() && preds &&
dfs::check(G) == GTL_OK ? GTL_OK : GTL_ERROR;
}
//--------------------------------------------------------------------------
// Handler
//--------------------------------------------------------------------------
void biconnectivity::init_handler(graph& G)
{
if (add_edges) {
dfs D;
D.scan_whole_graph(true);
D.check(G);
D.run(G);
roots_iterator it, end;
it = D.roots_begin();
end = D.roots_end();
start = *(*it);
++it;
for (; it != end; ++it) {
additional.push_back(G.new_edge(start, *(*it)));
}
first_child.init(G, node());
}
low_num.init(G);
in_component.init(G);
cut_count.init(G, 0);
//
// Detect self loops and hide them.
//
assert(self_loops.empty());
graph::edge_iterator eit = G.edges_begin(),
eend = G.edges_end();
while (eit != eend) {
edge e = *eit;
eit++;
if (e.target() == e.source()) {
self_loops.push_back(e);
G.hide_edge(e);
}
}
}
void biconnectivity::entry_handler(graph& /*G*/, node& curr, node& father)
{
if (add_edges) {
if (father != node()) {
if (first_child[father] == node()) {
first_child[father] = curr;
}
}
}
low_num[curr] = dfs_number[curr];
}
void biconnectivity::new_start_handler(graph& /*G*/, node& st)
{
cut_count[st] = -1;
//
// If this node has no adjacent edges, we
// must write down the component right here. This is because
// then the method after_recursive_call_handle is never
// executed.
//
// 28/2/2002 MR
//
if (st.degree() == 0) {
++num_of_components;
if (store_comp) {
component_iterator li = components.insert(
components.end(),
std::pair<nodes_t, edges_t>(nodes_t(), edges_t()));
li->first.push_back(st);
in_component[st] = li;
}
}
}
void biconnectivity::before_recursive_call_handler(graph& /*G*/, edge& /*e*/, node& n)
{
if (store_comp) {
node_stack.push(n);
}
}
void biconnectivity::after_recursive_call_handler(graph& G, edge& e, node& n)
{
node curr = n.opposite(e);
if (low_num[n] < low_num[curr]) {
low_num[curr] = low_num[n];
}
if (low_num[n] >= dfs_num(curr)) {
//
// Component found
//
if (store_comp) {
component_iterator li = components.insert(
components.end(),
std::pair<nodes_t, edges_t>(nodes_t(), edges_t()));
nodes_t& component = li->first;
edges_t& co_edges = li->second;
//
// Nodes of biconnected component
//
node tmp = node_stack.top();
while (dfs_num(tmp) >= dfs_num(n)) {
node_stack.pop();
component.push_back(tmp);
in_component[tmp] = li;
if (node_stack.empty()) break;
else tmp = node_stack.top();
}
component.push_back(curr);
//
// edges of biconnected component
//
edge ed = edge_stack.top();
while ((dfs_num(ed.source()) >= dfs_num(n) &&
dfs_num(ed.target()) >= dfs_num(n)) ||
(dfs_num(ed.source()) == dfs_num(curr) &&
dfs_num(ed.target()) >= dfs_num(n)) ||
(dfs_num(ed.source()) >= dfs_num(n) &&
dfs_num(ed.target()) == dfs_num(curr))) {
edge_stack.pop();
co_edges.push_back(ed);
if (edge_stack.empty()) break;
else ed = edge_stack.top();
}
}
++num_of_components;
//
// curr is cut point; increase counter
//
++cut_count[curr];
if (add_edges) {
node father = (*preds)[curr];
node first = first_child[curr];
if (father != node() && n == first) {
additional.push_back(G.new_edge(father, first));
}
if (n != first) {
additional.push_back(G.new_edge(n, first));
}
}
}
}
void biconnectivity::old_adj_node_handler(graph& /*G*/, edge& e, node& n)
{
node curr = n.opposite(e);
//
// Store backedges at lower endpoint
//
if (store_comp) {
if (dfs_num(curr) > dfs_num(n)) {
edge_stack.push(e);
}
}
if (dfs_num(n) < low_num[curr]) {
low_num[curr] = dfs_number[n];
}
}
void biconnectivity::leave_handler(graph& /*G*/, node& n, node& /*f*/)
{
if (cut_count[n] > 0)
{
cut_points.push_back(n);
}
}
void biconnectivity::end_handler(graph& G)
{
edges_t::iterator it = self_loops.begin();
edges_t::iterator end = self_loops.end();
while (it != end)
{
G.restore_edge(*it);
if (store_comp)
{
component_iterator cit = in_component[it->target()];
cit->second.push_back(*it);
}
it = self_loops.erase(it);
}
}
__GTL_END_NAMESPACE
//--------------------------------------------------------------------------
// end of file
//--------------------------------------------------------------------------