| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
* Break cycles in a directed graph by depth-first search. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "config.h" |
| 17 |
|
| 18 |
#include <dotgen/dot.h> |
| 19 |
#include <stdbool.h> |
| 20 |
#include <stddef.h> |
| 21 |
|
| 22 |
void reverse_edge(edge_t * e) |
| 23 |
{ |
| 24 |
edge_t *f; |
| 25 |
|
| 26 |
delete_fast_edge(e); |
| 27 |
if ((f = find_fast_edge(aghead(e), agtail(e)))) |
| 28 |
merge_oneway(e, f); |
| 29 |
else |
| 30 |
virtual_edge(aghead(e), agtail(e), e); |
| 31 |
} |
| 32 |
|
| 33 |
static void |
| 34 |
dfs(node_t * n) |
| 35 |
{ |
| 36 |
int i; |
| 37 |
edge_t *e; |
| 38 |
node_t *w; |
| 39 |
|
| 40 |
if (ND_mark(n)) |
| 41 |
return; |
| 42 |
ND_mark(n) = true; |
| 43 |
ND_onstack(n) = true; |
| 44 |
for (i = 0; (e = ND_out(n).list[i]); i++) { |
| 45 |
w = aghead(e); |
| 46 |
if (ND_onstack(w)) { |
| 47 |
reverse_edge(e); |
| 48 |
i--; |
| 49 |
} else { |
| 50 |
if (!ND_mark(w)) |
| 51 |
dfs(w); |
| 52 |
} |
| 53 |
} |
| 54 |
ND_onstack(n) = false; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
void acyclic(graph_t * g) |
| 59 |
{ |
| 60 |
node_t *n; |
| 61 |
|
| 62 |
for (size_t c = 0; c < GD_comp(g).size; c++) { |
| 63 |
GD_nlist(g) = GD_comp(g).list[c]; |
| 64 |
for (n = GD_nlist(g); n; n = ND_next(n)) |
| 65 |
ND_mark(n) = false; |
| 66 |
for (n = GD_nlist(g); n; n = ND_next(n)) |
| 67 |
dfs(n); |
| 68 |
} |
| 69 |
} |
| 70 |
|