How do You Calculate Cyclomatic Complexity?

Cyclomatic Complexity:

McCabe obtained his results by applying graph-theoretic techniques to the control flow graph of a program. McCabe’s cyclomatic complexity defines an upper bound on the number of independent paths in a program. There are three methods to compute the cyclomatic complexity.

Method 1: Given a control flow graph G of a program, the cyclomatic complexity V(G) can be computed as:
V(G) = E – N + 2

where N is the number of nodes in the control flow graph and E is the number of edges in the control flow graph.

Method 2: An alternate way of computing the cyclomatic complexity of a program is based on a visual inspection of the control flow graph as follows: In this method, the cyclomatic complexity V (G) for a graph G is given by the following expression:

V(G) = Total number of non-overlapping bounded areas + 1

Method 3: The cyclomatic complexity of a program can easily be computed by computing the number of decision and loop statements of the program. If N is the number of decision and loop statements of a program then,

McCabe’s metric= N + 1