Detect *all* cycles during topological sort
Previously the cycle detection was limited to detecting when we hit
a currently-being-visited node during a traversal. So something like this would
be correctly found:
A --> B --> C
^ |
| |
+-----+
We start at the root (A), go to B, then to C, then to B, and detect that we're
still working on B and signal the error.
But this doesn't find all cycles, because we *start* at the root nodes, and if
a cycle doesn't have any outcropping branches we'll never reach it at all. For
example:
A --> B
^ |
| |
+-----+
This graph has no roots, so we incorrectly ignore the cycle.
This patch fixes the problem by keeping a count of visited nodes and and making
sure it matches the digraph's size at the end.
Fixes https://github.com/sjl/cl-digraph/issues/4
author |
Steve Losh <steve@stevelosh.com> |
date |
Mon, 14 Dec 2020 20:13:51 -0500 |
parents |
e5f60ffb3dc4 |
children |
7e80eda84170 |
(asdf:defsystem :cl-digraph
:description "Simple directed graphs for Common Lisp."
:author "Steve Losh <steve@stevelosh.com>"
:homepage "http://docs.stevelosh.com/cl-digraph/"
:license "MIT/X11"
:version "1.3.2"
:depends-on ()
:in-order-to ((asdf:test-op (asdf:test-op :cl-digraph.test)))
:serial t
:components ((:module "vendor" :serial t
:components ((:file "quickutils-package")
(:file "quickutils")))
(:file "package")
(:module "src" :serial t
:components ((:file "directed-graph")))))