# Find cycles easily

**URL:** <https://forum.graphviz.org/t/find-cycles-easily/2764>\
**Category:** Help\
**Created:** [February 25, 2025, 9:17pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764 "2025-02-25T21:17:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![us12](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@us12](https://forum.graphviz.org/u/us12)\
**Post date:** [February 25, 2025, 9:17pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764/1 "2025-02-25T21:17:28Z")

</div>

Is there any easy way to find cycles in graphs?

1. List their node names?
2. Or highlight with red color their edges or nodes somehow to notice them easily on **huge** graph?

For example, here I need nodes:  
10-11-12  
7-7  
or at least the whole chain (9-10-11-12-13-14 and 5-6-7-8), because I have many such chains without cycles in a large graph and a few chains with cycles and want to find them quickly.

```auto
 digraph {rankdir=LR;
1 -> 2
2 -> 3
3 -> 4
5 -> 6
6 -> 7
7 -> 7
7 -> 8
9 -> 10
10 -> 11
11 -> 12
12 -> 10
12 -> 13
13 -> 14
}

```

 ![изображение](https://global.discourse-cdn.com/graphviz/original/2X/2/25bd621bfd1d377d38844a473363135b468e1d66.png)

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [February 25, 2025, 10:46pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764/2 "2025-02-25T22:46:44Z")

</div>

It might be useful to look at the acyclic tool in graphviz.

If it doesn’t do quite what you need, it’s still very hackable If you can code in the ancient “C“ language.

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [February 25, 2025, 11:40pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764/3 "2025-02-25T23:40:38Z")

</div>

Here is a hacked version of the **cycle** ([gvpr](https://www.graphviz.org/pdf/gvpr.1.pdf)) program (written by Emden?) that comes with the Graphviz source.  
Save it in a file named cycleColor.gvpr and use it like so:  
`gvpr -cf cycleColor.gvpr myFile.gv >mybetterFile.gv`

cycleColor:

```auto
/*
  a hacked version of the "cycle" gvpr program found in the Graphviz source

  Detect directed cycles 
*/
BEG_G{
  int cnt, i;
  node_t tp, hp;
  node_t stack[node_t];
  edge_t anEdge;
  string Tail[], Head[];
  $tvtype = TV_prepostfwd;
  $tvroot = fstnode($);
}
N {
  if (stack[$]) {
    stack[$] = NULL;
  } else if ($tvedge == NULL) { /* current root */
    stack[$] = $;
  } else {
    stack[$] = $tvedge.tail;
  }
}
E {
  if (stack[$.head]) {
    tp = $.tail;
    hp = $.head;
    $.color="red";
    tp.color="green";
    while (tp != $.head) {
      tp.color="green";
      hp.color="green";
      hp = tp;
      tp = stack[tp];
      anEdge=isEdge(tp, hp, "");
      anEdge.color="red";
    }
  }
}

```

Giving:

 ![o](https://global.discourse-cdn.com/graphviz/original/2X/5/5c9f641deb0706c803be5f5050e74d20f46d70ed.png)

---

<div class="post-metadata">

**Author:** ![us12](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@us12](https://forum.graphviz.org/u/us12)\
**Post date:** [February 26, 2025, 2:07pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764/4 "2025-02-26T14:07:27Z")

</div>

Wow!!! Unbelievable!  
That’s exactly what I need!  
Thanks!

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [February 26, 2025, 3:09pm UTC](https://forum.graphviz.org/t/find-cycles-easily/2764/5 "2025-02-26T15:09:39Z")

</div>

Somebody should make an AI assistant that can help people to do all these useful things, just using natural language, and some kind of nice display front end probably d3-graphviz.
