EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Software Testing Tutorial Cyclomatic Complexity
 

Cyclomatic Complexity

Pooja Gupta
Article byPooja Gupta
EDUCBA
Reviewed byRavi Rathore

 

Cyclomatic Complexity

 

 

Introduction to Cyclomatic Complexity

Cyclomatic complexity in software testing is used to measure the number of logical paths of a program. Basically, it evaluates the complexity of linearly independent paths in the source code of a program. For example, if the number of paths/points is more, the program will be more complex. This is calculated using Control Flow Graph. Cyclomatic complexity is of two types: Essential and Accidental complexity. In this article, the calculation, working, advantages, and types of cyclomatic complexity are discussed in detail.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Calculate Cyclomatic Complexity?

It is calculated by developing a Control Flow Graph of the code that measures the number of linear-independent pass through a program module.

It can be represented using the following formula:

Method 1:

Cyclomatic Complexity = E – N + 2P, where

  • E = The number of edges of the graph
  • N = The number of nodes in the graph
  • P = The number of connected components

Method 2:

This formula is used when the exit point is considered, which returns to the entry point. This will create a cycle.

Cyclomatic Complexity = E – N + P

Cyclomatic Complexity = E – N + 1

Method 3:

This is the easier way of representation in the graph form:

  1. Draw flow graph
  2. Connect the exit point to the entry point
  3. And then count holes in the graph

Consider the following figure:

01

The following are some of the representations of Flow Graph Notations:

If-then-else:  

If-then-else

While:

While

Do-While:

Do-While

For: 

For  

If the program/ source code does not have any loop or no control flow statement, then the cyclomatic complexity is 1. If the program/ source code encounters any loop, the complexity increases accordingly. For example, If the source code contains one ‘if’ condition then the cyclomatic complexity will be 2. Because the condition is having two paths one for true and the other for false. Continue reading to understand it better.

This technique is mostly used in basic testing of the white box. It represents a minimum no. of tests required to execute each path in the code. Different programming languages have different tools to measure this complexity of the program.

Steps to Calculate Cyclomatic Complexity with McCabe’s Formula

The classical steps to calculate cyclomatic complexity through McCabe’s  (M) formula are as follows.

  1. Draw the flowchart or a graph diagram with nodes and edges from the code.
  2. Identify how many independent paths are there.
  3. Then calculate the cyclomatic complexity by the formula mentioned below:
  4. Measure test cases.

M = E –N +2P

Before calculating through a flowchart/graph, let us consider the following example of Java code for better understanding:

The below program calculates the Fibonacci series:

0+1=1

1+1=2

2+1=3

3+2=5

5+3=8

8+5=13

Code:

//Following program is to just print the Fibonacci series
class Main {
public static void main(String[] args){
int sum;
int max = 20 ;
int pre = 0;
int next = 1;
System.out.println("The Fibonacii series is : " +pre);
while(next<= max){
System.out.println(next);
sum = pre + next;
pre = next;
next = sum;
}
}
}

Output:

Fibonacci Series

This above program consists of only one while loop.

Let us understand this same program through a flowchart:

Flowchart:

Flowchart

                           

To calculate the complexity of the preceding program, we must first calculate the total number of edges i.e lines in the flowchart:

Total no. of edges (E) =6

Now, calculate the total no.of nodes i.e shapes in the flowchart.

Total no.of Nodes (N) =5

Further, calculate the total no. of predicate nodes (node that contains condition)

Total no. of connected components (P) =1

Add the values in the formula: M = E – N +2p

M = 6 – 5 + 2

M = 3

So, the cyclomatic complexity for this program is 3. Note that the complexity should not exceed 10.

Complex codes are difficult to maintain, update and modify.

Types of Complexity

The two major types of complexity are as follows.

1. Essential Complexity: Essential complexity is the measurement of no. of entry points, non-deductible nodes, and termination points to solve a problem. This is a type of code that cannot be ignored. Ex. The flight management system.

2. Accidental Complexity: Accidental complexity refers to challenges like fixing, patching, modification, etc., in the system to solve problems. Ex. Fixing a software bug in a mobile device.

Advantages of Cyclomatic complexity:

  • Paths in complexity determine whether a program is complex, or whether there is a need to reduce complexity. The higher no. of complexity means the code is more complex.
  • It reduces the coupling of code
  • The risk involved with the program can be analyzed.
  • The use of these metrics in the first place reduces the problems associated with the program.
  • Developers and testers can determine an independent path for executions.

Tools used for Calculating Cyclomatic Complexity

  • Cyclo
  • CCCC
  • McCabe IQ
  • GCov
  • Bullseye Coverage
  • PMD
  • LC2
  • Findbugs
  • Jarchitect

Recommended Articles

This has been a guide to Cyclomatic Complexity. Here we have discussed its calculate and the tools used. Also the types and benefits of complexity. You can also learn more by reading the articles listed below:-

  1. Introduction to Linux
  2. Introduction To C++
  3. What is HTML5?
  4. What is Hibernate?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW