Cyclomatic number

Revision as of 17:12, 27 May 2025 by imported>David Eppstein (typo)
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Template:Short description Template:For

File:6n-graf.svg
This graph has cyclomatic number Template:Math because it can be made into a tree by removing two edges, for instance the edges 1–2 and 2–3, but removing any one edge leaves a cycle in the graph.

In graph theory, a branch of mathematics, the cyclomatic number, circuit rank, cycle rank, or nullity of an undirected graph is the minimum number of edges that must be removed from the graph to break all its cycles, making it into a tree or forest.

FormulaEdit

The cyclomatic number of a graph equals the number of independent cycles in the graph, the size of a cycle basis. Unlike the corresponding feedback arc set problem for directed graphs, the cyclomatic number Template:Mvar is easily computed using the formula: <math display=block>r = e - v + c,</math> where Template:Mvar is the number of edges in the given graph, Template:Mvar is the number of vertices, and Template:Mvar is the number of connected components. <ref name="berge">Template:Citation.</ref>

It is possible to construct a minimum-size set of edges that breaks all cycles efficiently, either using a greedy algorithm or by complementing a spanning forest.

The cyclomatic number can be explained in terms of algebraic graph theory as the dimension of the cycle space of a graph, in terms of matroid theory as the corank of a graphic matroid, and in terms of topology as one of the Betti numbers of a topological space derived from the graph. It counts the ears in an ear decomposition of the graph, forms the basis of parameterized complexity on almost-trees, and has been applied in software metrics as part of the definition of cyclomatic complexity of a piece of code. The concept was introduced and called the cyclomatic number by Gustav Kirchhoff.<ref name="Kotiuga2010">Template:Citation</ref><ref name="Hage1996">Template:Citation</ref>

For hypergraphsEdit

The cyclomatic number of a hypergraph can be derived by its Levi graph, with the same cyclomatic number but reduced to a simple graph. It is <math display=block>r = g - (v + e) + c,</math> where Template:Mvar is the degree sum (and the number of edges in the Levi graph), Template:Mvar is the number of hyperedges in the given hypergraph, Template:Mvar is the number of vertices, and Template:Mvar is the number of connected components.

The degree sum of a hypergraph is the sum of the degrees of all the vertices, reducing to Template:Math for a simple graph, or Template:Math for a Template:Mvar-uniform hypergraph. This formula is symmetric between vertices and edges which demonstrates a hypergraph and its dual hypergraph have the same cyclomatic number.

Matroid rank and construction of a minimum feedback edge setEdit

The cyclomatic number of a graph Template:Mvar may be described using matroid theory as the corank of the graphic matroid of Template:Mvar.<ref>Template:Citation.</ref> Using the greedy property of matroids, this means that one can find a minimum set of edges that breaks all cycles using a greedy algorithm that at each step chooses an edge that belongs to at least one cycle of the remaining graph.

Alternatively, a minimum set of edges that breaks all cycles can be found by constructing a spanning forest of Template:Mvar and choosing the complementary set of edges that do not belong to the spanning forest.

The number of independent cyclesEdit

In algebraic graph theory, the cyclomatic number is also the dimension of the cycle space of <math>G</math>. Intuitively, this can be explained as meaning that the cyclomatic number counts the number of independent cycles in the graph, where a collection of cycles is independent if it is not possible to form one of the cycles as the symmetric difference of some subset of the others.<ref name="berge"/>

This count of independent cycles can also be explained using homology theory, a branch of topology. Any graph Template:Mvar may be viewed as an example of a Template:Nowrap simplicial complex, a type of topological space formed by representing each graph edge by a line segment and gluing these line segments together at their endpoints. The cyclomatic number is the rank of the first (integer) homology group of this complex,<ref>Template:Citation.</ref> <math display=block>r = \operatorname{rank}\left[H_1(G,\Z)\right].</math> Because of this topological connection, the cyclomatic number of a graph Template:Mvar is also called the first Betti number of Template:Mvar.<ref name="BerkolaikoKuchment2013">Template:Citation</ref> More generally, the first Betti number of any topological space, defined in the same way, counts the number of independent cycles in the space.

ApplicationsEdit

Meshedness coefficientEdit

A variant of the cyclomatic number for planar graphs, normalized by dividing by the maximum possible cyclomatic number of any planar graph with the same vertex set, is called the meshedness coefficient. For a connected planar graph with Template:Mvar edges and Template:Mvar vertices, the meshedness coefficient can be computed by the formula<ref>Template:Citation.</ref>

<math>\frac{m-n+1}{2n-5}.</math>

Here, the numerator <math>m-n+1</math> of the formula is the cyclomatic number of the given graph, and the denominator <math>2n-5</math> is the largest possible cyclomatic number of an Template:Mvar-vertex planar graph. The meshedness coefficient ranges between 0 for trees and 1 for maximal planar graphs.

Ear decompositionEdit

The cyclomatic number controls the number of ears in an ear decomposition of a graph, a partition of the edges of the graph into paths and cycles that is useful in many graph algorithms. In particular, a graph is 2-vertex-connected if and only if it has an open ear decomposition. This is a sequence of subgraphs, where the first subgraph is a simple cycle, the remaining subgraphs are all simple paths, each path starts and ends on vertices that belong to previous subgraphs, and each internal vertex of a path appears for the first time in that path. In any biconnected graph with circuit rank <math>r</math>, every open ear decomposition has exactly <math>r</math> ears.<ref>Template:Citation. See in particular Theorems 18 (relating ear decomposition to circuit rank) and 19 (on the existence of ear decompositions).</ref>

Almost-treesEdit

A graph with cyclomatic number <math>r</math> is also called an Template:Mvar-almost-tree, because only Template:Mvar edges need to be removed from the graph to make it into a tree or forest. A 1-almost-tree is a near-tree, and a connected near-tree is a pseudotree, a cycle with a (possibly trivial) tree rooted at each vertex.<ref name=CMC349>Template:Citation</ref>

Several authors have studied the parameterized complexity of graph algorithms on Template:Mvar-near-trees, parameterized by <math>r</math>.<ref>Template:Citation.</ref><ref>Template:Citation.</ref>

Generalizations to directed graphsEdit

The cycle rank is an invariant of directed graphs that measures the level of nesting of cycles in the graph. It has a more complicated definition than cyclomatic number (closely related to the definition of tree-depth for undirected graphs) and is more difficult to compute. Another problem for directed graphs related to the cyclomatic number is the minimum feedback arc set, the smallest set of edges whose removal breaks all directed cycles. Both cycle rank and the minimum feedback arc set are NP-hard to compute.

It is also possible to compute a simpler invariant of directed graphs by ignoring the directions of the edges and computing the circuit rank of the underlying undirected graph. This principle forms the basis of the definition of cyclomatic complexity, a software metric for estimating how complicated a piece of computer code is.

Computational chemistryEdit

In the fields of chemistry and cheminformatics, the cyclomatic number of a molecular graph (the number of rings in the smallest set of smallest rings) is sometimes referred to as the Frèrejacque number.<ref>Template:Citation</ref><ref>Template:Citation</ref><ref>Template:Citation</ref>

Parametrized complexityEdit

Some computational problems on graphs are NP-hard in general, but can be solved in polynomial time for graphs with a small cyclomatic number. An example is the path reconfiguration problem.<ref>Template:Citation</ref>

Related conceptsEdit

Other numbers defined in terms of deleting things from graphs are:

ReferencesEdit

Template:Reflist