acyclic.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @file
  3. * @brief make directed graph acyclic, implements @ref graphviz_acyclic, used
  4. * in cmd/tools/acyclic.c
  5. *
  6. * @ingroup cgraph_app
  7. *
  8. * Copyright (c) 2011 AT&T Intellectual Property
  9. * All rights reserved. This program and the accompanying materials
  10. * are made available under the terms of the Eclipse Public License v1.0
  11. * which accompanies this distribution, and is available at
  12. * https://www.eclipse.org/legal/epl-v10.html
  13. *
  14. * Authors: Stephen North, Emden Gansner
  15. * Contributors: Details at https://graphviz.org
  16. */
  17. #include <cgraph/cghdr.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. typedef struct {
  21. Agrec_t h;
  22. int mark;
  23. bool onstack : 1;
  24. } Agnodeinfo_t;
  25. #define ND_mark(n) (((Agnodeinfo_t *)((n)->base.data))->mark)
  26. #define ND_onstack(n) (((Agnodeinfo_t *)((n)->base.data))->onstack)
  27. #define graphName(g) (agnameof(g))
  28. /* Add a reversed version of e. The new edge has the same key.
  29. * We also copy the attributes, reversing the roles of head and
  30. * tail ports.
  31. * This assumes we've already checked that such an edge does not exist.
  32. */
  33. static void addRevEdge(Agraph_t *g, Agedge_t *e) {
  34. Agsym_t *sym;
  35. Agedge_t *f = agedge(g, aghead(e), agtail(e), agnameof(e), 1);
  36. agcopyattr(e, f);
  37. sym = agattr(g, AGEDGE, TAILPORT_ID, 0);
  38. if (sym)
  39. agsafeset(f, HEADPORT_ID, agxget(e, sym), "");
  40. sym = agattr(g, AGEDGE, HEADPORT_ID, 0);
  41. if (sym)
  42. agsafeset(f, TAILPORT_ID, agxget(e, sym), "");
  43. }
  44. /// Return true if the graph has a cycle.
  45. static bool dfs(Agraph_t *g, Agnode_t *t, bool hasCycle, size_t *num_rev) {
  46. Agedge_t *e;
  47. Agedge_t *f;
  48. Agnode_t *h;
  49. ND_mark(t) = 1;
  50. ND_onstack(t) = true;
  51. for (e = agfstout(g, t); e; e = f) {
  52. f = agnxtout(g, e);
  53. if (agtail(e) == aghead(e))
  54. continue;
  55. h = aghead(e);
  56. if (ND_onstack(h)) {
  57. if (agisstrict(g)) {
  58. if (agedge(g, h, t, 0, 0) == 0) {
  59. addRevEdge(g, e);
  60. ++*num_rev;
  61. }
  62. } else {
  63. char *key = agnameof(e);
  64. if (!key || agedge(g, h, t, key, 0) == 0) {
  65. addRevEdge(g, e);
  66. ++*num_rev;
  67. }
  68. }
  69. agdelete(g, e);
  70. hasCycle = true;
  71. } else if (ND_mark(h) == 0)
  72. hasCycle |= dfs(g, h, hasCycle, num_rev);
  73. }
  74. ND_onstack(t) = false;
  75. return hasCycle;
  76. }
  77. bool graphviz_acyclic(Agraph_t *g, const graphviz_acyclic_options_t *opts,
  78. size_t *num_rev) {
  79. bool has_cycle = false;
  80. aginit(g, AGNODE, "info", sizeof(Agnodeinfo_t), true);
  81. for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
  82. if (ND_mark(n) == 0) {
  83. has_cycle |= dfs(g, n, false, num_rev);
  84. }
  85. }
  86. if (opts->doWrite) {
  87. agwrite(g, opts->outFile);
  88. fflush(opts->outFile);
  89. }
  90. return has_cycle;
  91. }