]> granicus.if.org Git - graphviz/commitdiff
Extend acyclic to report the number of reversed edges.
authorEmden Gansner <emdenrg@google.com>
Thu, 26 May 2016 17:29:27 +0000 (13:29 -0400)
committerEmden Gansner <emdenrg@google.com>
Thu, 26 May 2016 17:29:27 +0000 (13:29 -0400)
cmd/tools/acyclic.c

index a16cccdacf1d895b4c63993ae738aea55354fe2d..c5256fcd5ac8b25c1f719c2615d4993880964a54 100644 (file)
@@ -59,6 +59,7 @@ static FILE *outFile;
 static int doWrite = 1;
 static int Verbose;
 static char *cmd;
+static int num_rev;
 
 /* addRevEdge:
  * Add a reversed version of e. The new edge has the same key.
@@ -73,12 +74,16 @@ static void addRevEdge(Agraph_t * g, Agedge_t * e)
 
     agcopyattr (e, f);
 
+    num_rev++;
     sym = agattr (g, AGEDGE, TAILPORT_ID, 0);
     if (sym) agsafeset (f, HEADPORT_ID, agxget (e, sym), "");
     sym = agattr (g, AGEDGE, HEADPORT_ID, 0);
     if (sym) agsafeset (f, TAILPORT_ID, agxget (e, sym), "");
 }
 
+/* dfs:
+ * Return the number of reversed edges for this component.
+ */
 static int dfs(Agraph_t * g, Agnode_t * t, int hasCycle)
 {
     Agedge_t *e;
@@ -104,7 +109,7 @@ static int dfs(Agraph_t * g, Agnode_t * t, int hasCycle)
            agdelete(g, e);
            hasCycle = 1;
        } else if (ND_mark(h) == 0)
-           hasCycle = dfs(g, h, hasCycle);
+           hasCycle |= dfs(g, h, hasCycle);
     }
     ND_onstack(t) = 0;
     return hasCycle;
@@ -202,14 +207,14 @@ int main(int argc, char *argv[])
            }
            if (Verbose) {
                if (rv)
-                   fprintf(stderr, "Graph %s has cycles\n", graphName(g));
+                   fprintf(stderr, "Graph \"%s\" has cycles; %d reversed edges\n", graphName(g), num_rev);
                else
-                   fprintf(stderr, "Graph %s is acyclic\n", graphName(g));
+                   fprintf(stderr, "Graph \"%s\" is acyclic\n", graphName(g));
            }
        } else {
-           rv = 2;
+           rv = -1;
            if (Verbose)
-               fprintf(stderr, "Graph %s is undirected\n", graphName(g));
+               fprintf(stderr, "Graph \"%s\" is undirected\n", graphName(g));
        }
        exit(rv);
     } else