-
Notifications
You must be signed in to change notification settings - Fork 15
/
tree.cpp
231 lines (166 loc) · 4.95 KB
/
tree.cpp
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
#include "tree.h"
Tree::Tree()
{
is_connected = false;
has_cycles = false;
}
void Tree::CreateFromGraph(const QVector <QVector <bool> > & graph, const int root_node)
{
is_connected = true;
has_cycles = false;
cycles.clear();
nodes.clear();
disconnected_nodes.clear();
if (graph.size() < 1) {
return;
}
nodes = QVector <TreeNode> (graph.size());
for (int i=0; i<nodes.size(); ++i) {
nodes[i].index = i;
nodes[i].visited = 0; //white
}
//maintain a list of nodes that await being visited, use index 0 as root
QList <int> visiting;
nodes[0].visited = 1; //grey
visiting.push_back(root_node);
int num_visited = 0;
//the following performs BFS from the root
//qDebug() << "doing BFS";
while (!visiting.empty()) {
const int cur_node = visiting.first();
//qDebug() << "visiting" << cur_node;
visiting.pop_front();
nodes[cur_node].visited = 2; //black
for (int i=0; i<graph.size(); ++i) {
if (cur_node == i || !graph[cur_node][i]) {
continue;
}
if (nodes[i].visited == 1) { //node is grey (we have discovered it)
has_cycles = true;
//add the cycle to this list
//qDebug() << "found cycle via backedge between" << cur_node << i;
QList <int> new_cycle;
TraceBackCycle(cur_node, i, new_cycle);
cycles.push_back(new_cycle);
}
else if (nodes[i].visited == 0) { //node is white (we have not discovered it)
//set the topology now
nodes[cur_node].children.push_back(i);
nodes[i].visited = 1; //set node to grey (discovered)
nodes[i].parent = cur_node;
visiting.push_back(i);
}
}
++num_visited;
}
if (num_visited != nodes.size()) {
is_connected = false;
for (int i=0; i<nodes.size(); ++i) {
if (nodes[i].visited != 2) {
disconnected_nodes.push_back(i);
}
}
}
//qDebug() << "has cycles?" << has_cycles << "is connected?" << is_connected;
}
int Tree::GetNumNodes()
{
return nodes.size();
}
bool Tree::IsConnected()
{
return is_connected;
}
bool Tree::HasCycles()
{
return has_cycles;
}
int Tree::GetRoot()
{
return 0;
}
bool Tree::HasParent(const int i)
{
return (nodes[i].parent != -1);
}
int Tree::GetParent(const int i)
{
return nodes[i].parent;
}
void Tree::GetChildren(const int i, QList <int> & children)
{
children = nodes[i].children;
}
void Tree::GetBranch(const int i, QList <int> & branch)
{
QSet <int> visited;
QList <int> to_visit;
to_visit.push_back(i);
while (!to_visit.empty()) {
const int j = to_visit.first();
to_visit.pop_front();
for (int k=0; k<nodes[j].children.size(); ++k) {
const int c= nodes[j].children[k];
if (!visited.contains(c)) {
to_visit.push_back(c);
}
}
visited.insert(j);
}
branch = visited.toList();
}
void Tree::GetCycles(QList <QList <int> > & cyc)
{
cyc = cycles;
}
void Tree::GetDisconnectedNodes(QList <int> & disc_nodes)
{
disc_nodes = disconnected_nodes;
}
void Tree::GetPathToRoot(const int i, QList <int> & path)
{
int cur_node = i;
while (cur_node != -1) {
path.push_back(cur_node);
cur_node = nodes[cur_node].parent;
}
}
void Tree::TraceBackCycle(const int i1, const int i2, QList <int> & cycle)
{
//1. get the parent lists of i1 and i2
QList <int> path_i1;
QList <int> path_i2;
GetPathToRoot(i1, path_i1);
GetPathToRoot(i2, path_i2);
//2. for a tree, any i1 and i2 in the tree must share one common (grand)parent
QSet <int> common_parents = path_i1.toSet().intersect(path_i2.toSet());
if (common_parents.empty()) {
qDebug() << "Tree::TraceBackCycle - Problem! Could not find common parent for" << i1 << i2;
qDebug() << path_i1;
qDebug() << path_i2;
return;
}
//3. get the parent with smallest index in path_i1 (since multiple parents can be shared)
int common_parent = -1;
for (int i=0; i<path_i1.size(); ++i) {
if (common_parents.contains(path_i1[i])) {
common_parent = path_i1[i];
break;
}
}
if (common_parent == -1) {
qDebug() << "Tree::TraceBackCycle - Problem! Could not find common parent for" << i1 << i2 << "within sets";
return;
}
//4. form a cycle which goes across the common parent
// 4a. loop up i1's path list
const int ind1 = path_i1.indexOf(common_parent);
const int ind2 = path_i2.indexOf(common_parent);
for (int i=0; i<=ind1; ++i) {
cycle.push_back(path_i1[i]);
}
// 4b. loop down i2's path list
for (int i=ind2-1; i>=0; --i) {
cycle.push_back(path_i2[i]);
}
}