-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdfs.cc
115 lines (106 loc) · 2.61 KB
/
dfs.cc
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
#include "dfs.h"
#include <stack>
#include "config.h"
#include <iostream>
struct itobject {
Vertex* pos;
Vertex** children;
Vertex** end;
};
void create_postorder(Vertex* current, Vertex** start) {
std::stack<itobject> nexts;
bool first = true;
Vertex* last;
itobject it;
it.pos = current;
(*current).stat = Vertex::DETECT;
it.children = (*current).succIt().begin();
it.end = (*current).succIt().end();
nexts.push(it);
while(!nexts.empty()) {
if (nexts.top().children != nexts.top().end) {
if ((**nexts.top().children).stat < Vertex::DETECT) {
itobject it2;
it2.pos = *nexts.top().children;
(**nexts.top().children).stat = Vertex::DETECT;
it2.children = (**nexts.top().children).succIt().begin();
it2.end = (**nexts.top().children).succIt().end();
nexts.top().children++;
nexts.push(it2);
}
else {
nexts.top().children++;
}
}
else {
if (first) {
first = false;
nexts.top().pos->setPostorderPos(0);
*start = nexts.top().pos;
}
else {
nexts.top().pos->setPostorderPos((*last).getPostorderPos()+1);
(*last).setNext(nexts.top().pos);
}
last = nexts.top().pos;
nexts.pop();
}
}
}
void create_postorder(Vertex* current, Vertex** start, long unsigned int& altpos) {
std::stack<itobject> nexts;
bool first = true;
bool firstAlt = true;
Vertex* last = NULL;
itobject it;
it.pos = current;
(*current).stat = Vertex::DETECT;
it.children = (*current).succIt().begin();
it.end = (*current).succIt().end();
nexts.push(it);
while(!nexts.empty()) {
Vertex& v =(*nexts.top().pos);
if (nexts.top().children != nexts.top().end ) {
if ((**nexts.top().children).stat < Vertex::DETECT) {
itobject it2;
it2.pos = *nexts.top().children;
(**nexts.top().children).stat = Vertex::DETECT;
it2.children = (**nexts.top().children).succIt().begin();
it2.end = (**nexts.top().children).succIt().end();
nexts.top().children++;
nexts.push(it2);
}
else {
if (*nexts.top().children == current && firstAlt) {
firstAlt = false;
if (first) {
first = false;
(*current).setPostorderPos(0);
altpos = 0;
*start = current;
}
else {
(*current).setPostorderPos((*last).getPostorderPos()+1);
altpos = (*last).getPostorderPos()+1;
(*last).setNext(current);
}
last = current;
}
nexts.top().children++;
}
}
else {
if (first) {
first = false;
v.setPostorderPos(0);
*start = nexts.top().pos;
}
else {
v.setPostorderPos((*last).getPostorderPos()+1);
(*last).setNext(nexts.top().pos);
}
last = nexts.top().pos;
nexts.pop();
}
}
}