Definition , so the program's cyclomatic complexity is . There are multiple ways to define cyclomatic complexity of a section of
source code. One common way is the number of linearly independent
paths within it. A set S of paths is linearly independent if the edge set of any path P in S is not the union of edge sets of the paths in some subset of S/P. If the source code contained no
control flow statements (conditionals or decision points) the complexity would be 1, since there would be only a single path through the code. If the code had one single-condition
IF statement, there would be two paths through the code: one where the IF statement is TRUE and another one where it is FALSE. Here, the complexity would be 2. Two nested single-condition IFs, or one IF with two conditions, would produce a complexity of 3. Another way to define the cyclomatic complexity of a program is to look at its
control-flow graph, a
directed graph containing the
basic blocks of the program, with an edge between two basic blocks if control may pass from the first to the second. The complexity is then defined as M = E - N + 2P, where • = the number of edges of the graph. • = the number of nodes of the graph. • = the number of
connected components. , which also results in a cyclomatic complexity of 3 using the alternative formulation (). An alternative formulation of this, as originally proposed, is to use a graph in which each exit point is connected back to the entry point. In this case, the graph is
strongly connected. Here, the cyclomatic complexity of the program is equal to the
cyclomatic number of its graph (also known as the
first Betti number), which is defined as M = E - N + 2. Cyclomatic complexity may be applied to several such programs or subprograms at the same time (to all of the methods in a class, for example). In these cases, will equal the number of programs in question, and each subprogram will appear as a disconnected subset of the graph. McCabe showed that the cyclomatic complexity of a structured program with only one entry point and one exit point is equal to the number of decision points ("if" statements or conditional loops) contained in that program plus one. This is true only for decision points counted at the lowest, machine-level instructions. Decisions involving compound predicates like those found in
high-level languages like IF cond1 AND cond2 THEN ... should be counted in terms of predicate variables involved. In this example, one should count two decision points because at machine level it is equivalent to IF cond1 THEN IF cond2 THEN .... Cyclomatic complexity may be extended to a program with multiple exit points. In this case, it is equal to \pi - s + 2, where \pi is the number of decision points in the program and is the number of exit points.
Interpretation In his presentation "Software Quality Metrics to Identify Risk" for the Department of Homeland Security, Tom McCabe introduced the following categorization of cyclomatic complexity: • 1–10: Simple procedure, little risk • 11–20: More complex, moderate risk • 21–50: Complex, high risk • > 50: Untestable code, very high risk == In algebraic topology ==