Cyclometric Complexity
Cyclometric Complexity
Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
Method-02:
V (G) is the maximum number of independent paths in the graph
V(G) = E - N + 2
Cyclomatic Complexity = E – N + 2
Here-
E = Total number of edges in the control flow graph
N = Total number of nodes in the control flow graph
Method-03:
Cyclomatic Complexity = P + 1
Here,
P = Total number of predicate nodes contained in the control flow graph
Note-
Predicate nodes are the conditional nodes.
They give rise to two branches in the control flow graph.
Flow graph notation for a program
Calculate cyclomatic complexity for the given code-
We draw the following control flow graph for
1.IF A = 354 the given code-
2.THEN IF B > C
3.THEN A = B
4.ELSE A = C
5.END IF
6.END IF
7.PRINT A
Using the above control flow graph, the cyclomatic complexity may be calculated as-
Method-01:
Cyclomatic Complexity
=2+1
=3
Method-02: Method-03:
Cyclomatic Complexity
Cyclomatic Complexity
=E–N+2
=P+1
=8–7+2
=2+1
=3
=3
Calculate cyclomatic complexity for the given flow graph
Example
01. insertion_procedure (int a[], int p [], int N)
02. {
03. int i,j,k;
04. for (i=0; i<=N; i++) p[i] = i;
05. for (i=2; i<=N; i++)
06. {
07. k = p[i];
08. j = 1;
09. while (a[p[j-1]] > a[k]) {p[j] = p[j-1]; j--}
10. p[j] = k;
11. }
12. }
Numbering the statements
01. insertion_procedure (int a[], int p [], int N)
02. {
03. (1) Int i,j,k;
04. (2) for ((2a)i=0; (2b)i<=N; (2c)i++)
05. (3) p[i] = i;
06. (4) for ((4a)i=2; (4b)i<=N; (4c)i++)
07. {
08. (5) k=p[i];j=1;
09. (6) while (a[p[j-1]] > a[k]) {
10. (7) p[j] = p[j-1];
11. (8) j--
12. }
13. (9) p[j] = k;
14. }
01. insertion_procedure (int a[], int p [], int N)
02. {
07. {
12. }
14. }
Cyclomatic complexity