Skip to content
FiftyCalc Free calculators for all 50 states

Cyclomatic Complexity Calculator

Test cases replayed : 2 of 2 match · Methodology · No outside source listed

Count the edges and nodes of the control-flow graph, or simply the decision points (if, while, for, case, && and ||) of one function.

Result

Sample figures

Cyclomatic complexity M—

Linearly independent paths to test (basis paths) —

Estimate for planning only, not tax, legal, financial or medical advice. This calculator uses the method explained on this page.

Cyclomatic Complexity Calculator

Method: choose Control-flow graph: edges, nodes, components or Decision points in one function. Prefilled example: Control-flow graph: edges, nodes, components.

Edges E (graph method): enter 0 or more. Prefilled example: 9.

Nodes N (graph method): enter 1 or more. Prefilled example: 8.

Connected components P (1 for one function): enter 1 or more. Prefilled example: 1.

How to use this calculator

Fill in the fields in this order; each step takes you to its field.

  1. Method: choose Control-flow graph: edges, nodes, components or Decision points in one function.
  2. Edges E (graph method): enter 0 or more.
  3. Nodes N (graph method): enter 1 or more.
  4. Connected components P (1 for one function): enter 1 or more.
  5. Decision points (decision method): enter 0 or more.
  6. Read the result: Cyclomatic complexity M; the panel also shows Linearly independent paths to test (basis paths).

How this calculator works

Thomas McCabe's cyclomatic complexity (IEEE Transactions on Software Engineering, 1976): M = E − N + 2P, where E is the number of edges of the control-flow graph, N the number of nodes and P the number of connected components (one per function or program).

For a single function with one entry and one exit, M also equals the number of binary decision points plus 1. M is the number of linearly independent paths through the code, the basis-path count used in structured testing (NIST Special Publication 500-235, Watson and McCabe, 1996).

The formula

  1. M2={e−n+2×pif m=1dp+1otherwise
Symbols used in the formula
SymbolMeaning
mMethod (1: Control-flow graph: edges, nodes, components; 2: Decision points in one function)
eEdges E (graph method)
nNodes N (graph method)
pConnected components P (1 for one function)
dpDecision points (decision method)
M2Cyclomatic complexity M

Worked example

MethodControl-flow graph: edges, nodes, components
Edges E (graph method)9
Nodes N (graph method)8
Connected components P (1 for one function)1
Linearly independent paths to test (basis paths)3
  1. M2=9−8+2×1=3

Cyclomatic complexity M 3

Each step uses the unrounded value of the step before it; figures on this page are rounded for display.

Frequently asked questions

What is the cyclomatic complexity of a graph with 9 edges and 8 nodes?

M = 9 − 8 + 2 × 1 = 3 for one connected function.

How do I count it from the code?

Count each if, while, for, case and each && or || as one decision point, then add 1: four decision points give M = 5.

Related calculators