Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculation of Cyclomatic Complexity for Pseudocode

while(m<n)
  if(x>y) AND (a<b) then
    a=a+1
    y=y-1
  end if 
m=m+1
end while

i was calculating Cyclomatic Complexity of above pseudo code, and i came to conclusion with the short cut method that

M= (Decision Point) + 1

Where M is Cyclomatic Complexity

i Got answer 3

is it True?

like image 425
Ravi Mehta Avatar asked Aug 27 '15 06:08

Ravi Mehta


People also ask

What is the formula for calculating cyclomatic complexity?

3) Cyclomatic complexity V(G) = P +1 V (G) = 2 + 1 = 3 Where P is predicate nodes (node 1 and node 2) are predicate nodes because from these nodes only the decision of which path is to be followed is taken.

How many ways we can calculate cyclomatic complexity?

Cyclomatic complexity can be calculated by using control flow graphs or with respect to functions, modules, methods or classes within a software program. Independent path is defined as a path that has at least one edge which has not been traversed before in any other paths.

What is cyclomatic complexity explain with example?

Cyclomatic complexity is a metric that indicates the possible number of paths inside a code artifact, e.g., a function, class, or whole program. Thomas J. McCabe Sr. developed this metric, first describing it in a 1976 paper.

Why do we calculate cyclomatic complexity?

It is a software metric that measures the logical complexity of the program code. It counts the number of decisions in the given program code. It measures the number of linearly independent paths through the program code.


2 Answers

First, let identify every statement. Im using letters here, but it could be numbers instead.

A    while(m<n)              
B,C    if(x>y) AND (a<b) then
D        a=a+1
E        y=y-1
       end if 
F      m=m+1
G    end while
  • Note that the second statement have two conditions/predicates/decision points (or whatever you call them), so i name them B and C.
  • Im going to use end while (G) as the exit point.

Now we can draw the Control Flow Graph (CFG):

CFG

Finally, compute the Cyclomatic Complexity (M) in three different ways:

  1. M = E-V+2*K = 9-7+2*1 = 4

  2. M = C + 1 = 3 + 1 = 4

  3. M = Regions(CFG) = 4

where:

  • E=number of edges

  • V=number of vertices

  • K=number of graph components

  • C=number of conditions/decision points

like image 188
JosEduSol Avatar answered Sep 22 '22 00:09

JosEduSol


As compare to above logic, @osEduSol mentioned is correct i.e.

M= (Decision Point) + 1

Where M is Cyclomatic Complexity

There are 3 decision point

i Got answer 4

like image 37
Nagesh Pathrut Avatar answered Sep 21 '22 00:09

Nagesh Pathrut