| 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 |
* Written by Stephen North |
| 14 |
* Updated by Emden Gansner |
| 15 |
* Adapted to gvToolTred(g) by John Ellson |
| 16 |
*/ |
| 17 |
|
| 18 |
/* |
| 19 |
* performs an inplace transitive reduction on a graph |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include <stdio.h> |
| 24 |
#include <cgraph/cgraph.h> |
| 25 |
#include <gvc/gvc.h> |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
Agrec_t h; |
| 29 |
int mark; |
| 30 |
} Agmarknodeinfo_t; |
| 31 |
|
| 32 |
#define MARK(n) (((Agmarknodeinfo_t*)(n->base.data))->mark) |
| 33 |
|
| 34 |
#define agrootof(n) ((n)->root) |
| 35 |
|
| 36 |
static int dfs(Agnode_t * n, Agedge_t * link, int warn) |
| 37 |
{ |
| 38 |
Agedge_t *e; |
| 39 |
Agedge_t *f; |
| 40 |
Agraph_t *g = agrootof(n); |
| 41 |
|
| 42 |
MARK(n) = 1; |
| 43 |
|
| 44 |
for (e = agfstin(g, n); e; e = f) { |
| 45 |
f = agnxtin(g, e); |
| 46 |
if (e == link) |
| 47 |
continue; |
| 48 |
if (MARK(agtail(e))) |
| 49 |
agdelete(g, e); |
| 50 |
} |
| 51 |
|
| 52 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 53 |
if (MARK(aghead(e))) { |
| 54 |
if (!warn) { |
| 55 |
warn++; |
| 56 |
fprintf(stderr, |
| 57 |
"warning: %s has cycle(s), transitive reduction not unique\n", |
| 58 |
agnameof(g)); |
| 59 |
fprintf(stderr, "cycle involves edge %s -> %s\n", |
| 60 |
agnameof(agtail(e)), agnameof(aghead(e))); |
| 61 |
} |
| 62 |
} else |
| 63 |
warn = dfs(aghead(e), AGOUT2IN(e), warn); |
| 64 |
} |
| 65 |
|
| 66 |
MARK(n) = 0; |
| 67 |
return warn; |
| 68 |
} |
| 69 |
|
| 70 |
int gvToolTred(Agraph_t * g) |
| 71 |
{ |
| 72 |
Agnode_t *n; |
| 73 |
int warn = 0; |
| 74 |
|
| 75 |
if (agisdirected(g)) { |
| 76 |
aginit(g, AGNODE, "info", sizeof(Agmarknodeinfo_t), true); |
| 77 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 78 |
warn = dfs(n, NULL, warn); |
| 79 |
} |
| 80 |
agclean(g, AGNODE, "info"); |
| 81 |
} else { |
| 82 |
fprintf(stderr, "warning: %s is not a directed graph, not attempting tred\n", |
| 83 |
agnameof(g)); |
| 84 |
} |
| 85 |
return 0; // FIXME - return proper errors |
| 86 |
} |