2.1 Find out whether a graph has a cycle, and show one
You find out whether a graph is a DAG (directed acyclic graph), and get one of its cycles as a list of nodes.
Problem
Section titled “Problem”You want to be sure that following the edges of a graph never leads back to the same node. A path that does is called a cycle. If the graph has a cycle, you want to show it, so that someone can find and fix it. You need this wherever edges stand for an order that must exist:
- dependencies between modules that must be built one after the other;
- steps of a process that must run in order;
- a hierarchy of categories, in which no category may contain itself.
Example
Section titled “Example”The example graph dependencies models the dependencies between the packages of a small program.
It has an edge from each package to each package it uses. Is the graph free of cycles, and if not,
where is a cycle? The picture after shows the cycle found: model and storage use each other.
- Edge of the cycle
- Node of the cycle
Solution
Section titled “Solution”boolean dependenciesAcyclic = Graphs.isDag(dependencies); // false// [4, 5] - model, storageIntList cycle = Graphs.findCycle(dependencies);let dependencies_acyclic = graphs::is_dag(&dependencies); // falselet cycle = graphs::find_cycle(&dependencies); // [4, 5] - model, storageconst dependenciesAcyclic = Graphs.isDag(dependencies); // falseconst cycle = Graphs.findCycle(dependencies); // [4, 5] - model, storagebool dependenciesAcyclic = Graphs.IsDag(dependencies); // false// [4, 5] - model, storageImmutableArray<int> cycle = Graphs.FindCycle(dependencies);The packages are not a DAG (directed acyclic graph).
Graphs.findCyclegraphs::find_cycleGraphs.findCycleGraphs.FindCycle returns the nodes of a cycle in the order of its edges. An
edge leads from each node to the next, and from the last node back to the first. Here the cycle is
model -> storage -> model, with the green nodes and the orange edges of the picture after.
Good to know
Section titled “Good to know”- The same cycle every time, but not necessarily the shortest. The search goes depth-first from the nodes in ascending order and returns the first cycle it closes. Here, that is also the shortest one.
- A shortest cycle through a node
v: search fromv, take the in-node ofvwith the smallest distance, and close the path to it with its edge back tov. See Find a shortest path between two nodes for that search. - All nodes on cycles are the nodes of the
SCCs (strongly connected components) for which
Sccs.containsCycleSccs::contains_cycleSccs.containsCycleSccs.ContainsCycleis true. Every other SCC is a single node on no cycle. The SCCs give the nodes, not the cycles themselves. See Find the groups of nodes that reach each other for these calls. - A self-loop is a cycle of one node: an edge from a node to itself.
- Before sorting.
TopOrder.oftop_order::ofTopOrder.ofTopOrder.OfandTopOrderAndLevels.ofTopOrderAndLevels::ofTopOrderAndLevels.ofTopOrderAndLevels.Ofreject a graph with a cycle, so check first where a cycle is possible. See Put the nodes in dependency order and Find which nodes can be processed in parallel. To get rid of the cycles, see Remove edges to break every cycle.