What is cyclomatic complexity?

Cyclomatic complexity is a measure of linearly independent paths in a code. Cyclomatic complexity is used to measure the complexity of a code. Lower cyclomatic complexity means a lower complexity of code.

Note: The cyclomatic complexity of a code can be greater than or equal to 1.

Methods of calculation

The cyclomatic complexity of a code can be calculated as:

Method 1

CC = E - N + 2P

  • E: The number of edges in the control flow graph of the code.
  • N: The number of nodes in the control flow graph of the code.
  • P: The number of connected components in the control flow graph of the code.

Method 2

CC = D + 1

  • D: The number of decisions in the control flow graph of the code.

Why do we need cyclomatic complexity?

  • Cyclomatic complexity makes sure that all paths in the code are tested at least once.
  • Cyclomatic complexity helps to measure the testability and maintainability of the code.
  • Cyclomatic complexity helps to focus more on the paths that are uncovered.
  • Cyclomatic complexity is a measure of the maximum number of independent paths in the code.

Cyclomatic complexity range

The value of cyclomatic complexity indicates how readable and testable a code is:

Value range

Code information

1 - 10

  • Simple and easy to understand
  • High testability

10 - 20

  • Code is complex but still comprehensible
  • Medium testability

> 20

  • Code is very complex and difficult to understand
  • Low testability

> 50

  • Code is very complex and difficult to understand
  • Not testable

Examples

Example 1

Consider the code snippet below, which is a simple program to add two numbers together.

int a = 1;
int b = 2;
cout<<"a + b = "<<a+b;

Control flow graph

The control flow graph of the code snippet above will be as follows:

Cyclomatic complexity

The cyclomatic complexity of the control flow graph will be calculated as:

  • E: the number of edges in the control graph = 2
  • N: the number of nodes in the control graph = 3
  • P: the number of connected components in the control graph = 1
  • D: the number of decisions in the control graph = 0

Method 1

CC = E - N + 2P

CC = 2 - 3 + 2(1)

CC = 1

Method 2

CC = D + 1

CC = 0 + 1

CC = 1

Example 2

Consider the code snippet below, which is a program to get the maximum of two numbers.

int a = 1;
int b = 2;
if (a>b){
cout<<"a is greater";
}else{
cout<<"b is greater";
}

Control flow graph

The control flow graph of the code snippet above will be as follows:

Cyclomatic complexity

The cyclomatic complexity of the control flow graph will be calculated as:

  • E: the number of edges in the control graph = 6
  • N: the number of nodes in the control graph = 6
  • P: the number of connected components in the control graph = 1
  • D: the number of decisions in the control graph = 1

Method 1

CC = E - N + 2P

CC = 6 - 6 + 2(1)

CC = 2

Method 2

CC = D + 1

CC = 1 + 1

CC = 2

Example 3

Consider the code snippet below, which is a program to print numbers from 1 to 5 using a for loop.

int i;
for (i = 1; i <= 5; i++){
cout<<i<<endl;
}

Control flow graph

The control flow graph of the code snippet above will be as follows:

Cyclomatic complexity

The cyclomatic complexity of the control flow graph will be calculated as:

  • E: the number of edges in the control graph = 6
  • N: the number of nodes in the control graph = 6
  • P: the number of connected components in the control graph = 1
  • D: the number of decisions in the control graph = 1

Method 1

CC = E - N + 2P

CC = 6 - 6 + 2(1)

CC = 2

Method 2

CC = D + 1

CC = 1 + 1

CC = 2

Example 4

Consider the code snippet below, which is a program to print numbers from 1 to 5 using a while loop.

int i = 1;
while (i <= 5){
cout<<i<<endl;
i++;
}

Control flow graph

The control flow graph of the code snippet above will be as follows:

Cyclomatic complexity

The cyclomatic complexity of the control flow graph will be calculated as:

  • E: the number of edges in the control graph = 5
  • N: the number of nodes in the control graph = 5
  • P: the number of connected components in the control graph = 1
  • D: the number of decisions in the control graph = 1

Method 1

CC = E - N + 2P

CC = 5 - 5 + 2(1)

CC = 2

Method 2

CC = D + 1

CC = 1 + 1

CC = 2

Example 5

Consider the code snippet below, which is a program to get the minimum element of an array:

int arr[] = {1, 7, 4, 9, 12, 7, -1, 13};
int arrSize = 8;
int min = arr[0];
for(int i = 0; i < arrSize; i++){
if( arr[i] < min){
min = arr[i];
}
}
cout<<"Array minimum = "<<min;

Control flow graph

The control flow graph of the code snippet above will be as follows:

Cyclomatic complexity

The cyclomatic complexity of the control flow graph will be calculated as:

  • E: the number of edges in the control graph = 11
  • N: the number of nodes in the control graph = 10
  • P: the number of connected components in the control graph = 1
  • D: the number of decisions in the control graph = 2

Method 1

CC = E - N + 2P

CC = 11 - 10 + 2(1)

CC = 3

Method 2

CC = D + 1

CC = 2 + 1

CC = 3

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved