plumatic / plumbing

Prismatic's Clojure(Script) utility belt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clojurescript version of topological-sort incorrectly (and randomly) reports a graph cycle error

stephenbrady opened this issue · comments

Clojurescript topological-sort incorrectly generates a "Graph contains a cycle" error when sorting the following graph (which does not have a cycle):

{10 [1 0]
 20 [2 0]
 2 []
 0 []}

The current algorithm subtly relies on ordering to recognize that there is actually no cycle in a graph. If a key is randomly chosen (with rand-nth for example) instead of first in this line - https://github.com/Prismatic/plumbing/blob/master/src/plumbing/map.cljx#L243 - the above graph will accordingly return a sort or report an error depending on what order the keys are processed from the graph. The current algorithm is (sort of) behaving like depth-first sorting, not topological sorting.

The version of topo sort that was introduced before #71 correctly handles the above graph as does the clj version.

I'll submit a pull request shortly that restores the old topological-sort but introduces the un-implemented include-leaves? option of the older version.

Just cut an 0.5.2 release with the fix, thanks again for the report!

Thanks!