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
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.
This browser did not allow copying. The sentence is in the box below, already selected: copy it with your keyboard or the menu.
How to use this calculator
Fill in the fields in this order; each step takes you to its field.
- Method: choose Control-flow graph: edges, nodes, components or Decision points in one function.
- Edges E (graph method): enter 0 or more.
- Nodes N (graph method): enter 1 or more.
- Connected components P (1 for one function): enter 1 or more.
- Decision points (decision method): enter 0 or more.
- 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
| Symbol | Meaning |
|---|---|
| Method (1: Control-flow graph: edges, nodes, components; 2: Decision points in one function) | |
| Edges E (graph method) | |
| Nodes N (graph method) | |
| Connected components P (1 for one function) | |
| Decision points (decision method) | |
| Cyclomatic complexity M |
Worked example
| Method | Control-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 |
|---|
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.