Skip to content

Commit 674c532

Browse files
authored
Non-recursive implementation
1 parent aba953b commit 674c532

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/graph/euler_path.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The program below searches for and outputs a Eulerian loop or path in a graph, o
6666

6767
First, the program checks the degree of vertices: if there are no vertices with an odd degree, then the graph has an Euler cycle, if there are $2$ vertices with an odd degree, then in the graph there is only an Euler path (but no Euler cycle), if there are more than $2$ such vertices, then in the graph there is no Euler cycle or Euler path.
6868
To find the Euler path (not a cycle), let's do this: if $V1$ and $V2$ are two vertices of odd degree, then just add an edge $(V1, V2)$, in the resulting graph we find the Euler cycle (it will obviously exist), and then remove the "fictitious" edge $(V1, V2)$ from the answer.
69-
We will look for the Euler cycle exactly as described above (recursive version), and at the same time at the end of this algorithm we will check whether the graph was connected or not (if the graph was not connected, then at the end of the algorithm some edges will remain in the graph, and in this case we need to print $-1$).
69+
We will look for the Euler cycle exactly as described above (non-recursive version), and at the same time at the end of this algorithm we will check whether the graph was connected or not (if the graph was not connected, then at the end of the algorithm some edges will remain in the graph, and in this case we need to print $-1$).
7070
Finally, the program takes into account that there can be isolated vertices in the graph.
7171

7272
```cpp
@@ -82,18 +82,6 @@ void add_edge(int u, int v) {
8282
g[v].push_back(idx);
8383
}
8484

85-
void dfs(int v) {
86-
while (!g[v].empty()) {
87-
int idx = g[v].back();
88-
g[v].pop_back();
89-
if (used[idx]) continue;
90-
used[idx] = true;
91-
auto [u, w] = edges[idx];
92-
dfs(u ^ w ^ v);
93-
}
94-
res.push_back(v);
95-
}
96-
9785
int main() {
9886
ios::sync_with_stdio(0);
9987
cin.tie(0);
@@ -129,7 +117,24 @@ int main() {
129117
}
130118

131119
used.assign((int) edges.size(), false);
132-
dfs(first);
120+
121+
stack <int> s;
122+
s.push(first);
123+
while (!s.empty()) {
124+
int v = s.top();
125+
if (g[v].empty()) {
126+
res.push_back(v);
127+
s.pop();
128+
continue;
129+
}
130+
int idx = g[v].back();
131+
g[v].pop_back();
132+
if (used[idx]) continue;
133+
used[idx] = true;
134+
auto [u, w] = edges[idx];
135+
int nxt = v ^ u ^ w;
136+
s.push(nxt);
137+
}
133138

134139
if (v1 != -1) {
135140
for (int i = 0; i + 1 < (int) res.size(); i++) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy