TEXT

Entered

Thursday, July 22, 2010

Cyclomatic complexity - Graphical Explanation

Cyclomatic complexity - Graphical Explanation
Cyclomatic complexity measures the amount of decision logic in a single software module.
It gives the number of recommended test for software.
Cyclomatic complexity is based entirely on the structure of software's control flow graph.

Control flow graph: Control flow graphs describe the logic structure of software module.
Each flow consists of nodes and edges.
Nodes: computation statements or expresions.
Edges: represent transfer of control between nodes.
Each possible execution path of a software module has a corresponding path from the entry to the exit node of the module's control flow graph.
Definition of cyclomatic complexity.
v(G) == e-n+2
v: Vector
G: Graph
The cyclomatic number gives the number of independent paths through the control flow graph of the module.
This means that cyclomatic number is precisely the minimal number of paths that can, in linear combination, generate all possible paths through the module.
Flow graph is the notation for the representation of control flow. The flow graph depicts logical control flow using the notation illustrated below.
Sequence
if

while

Until

Switch Case

1) Cyclomatic complexity for linear graph..
V(G)--> (22-17 ) +2= 7
2) Cyclomatic complexity for C Coding

Void ( int i, int j )
{
if ( i>0 && j>0 )
{
while ( i>j )
{
if ( i% 2 && j % 2)
print( "%d\ n", i );
else
print ("%d\ n", j );
i--;
}
}
}
Starting with 1, each of the two "if" statements add 1, the "while" statement adds 1 and each of the two "&&" operators adds1 , for total of 6.
In the above code
For this line if(i>0 && j>0) can write in the following manner
If (J>0) &&
(j>0)
Print (“%d”\n”,i)
Each line considered to be a node
In the same manner for operators && , || in condition to be considered as a separate path not as a single path..
3) Cyclomatic complexity for Switch stments

void ( int i)
{

if (n>o)
{
switch(n)
{
case 0: case1: printf("zero or one\n");
break;
case2: printf("two\n");
break;
case3: case4: printf("three or four\n");

break;
}
}
else
printf ("negative\n");
}

Starting with one unit of complexity, the switch statement has three "case-labeled statements" (although having a total of five "case labels"), which , considering the implicit default fall-through, is a four-way decision that contributes three units of complexity. The "if"statement contributes one. The total complexity is five


4) Given the Following program
if x > y then Add Image
Statement1;
else IF Y> Z THEN
Statement 2;
END



ANS: ' 3 '

5) Given the Following program

Void Test : : Test 5()
{
CString a,b,c,d,e,f
If (a==b)
C=d;
Else
{
C==f;
If(
((a==b)
&&
(e==f))
(f==a)
(a==b))
C==d;
Else
C==f;
}
}


ANS --6

6)



Cyclomatic Complexity Number (CCN)=== 8 − 7 + 2 = 3