Can we have a negative cyclomatic complexity

cyclomatic-complexity

I am having a hard time to understand cyclomatic complexity. I went through some videos in youtube for this. I got negative value for cyclomatic complexity when I use this formula M = E – N + P. I found this formula as well E = E – N + 2P. I would be happy to get some brief description about cyclomatic complexity and why it can or cant be negative?

Best Answer

In each connected component of a graph the number of edges must be at least greater or equal to the number of nodes minus 1 (that follows by induction, a component with one node does not need any edge, but whenever you add an additional node to a component, you need another edge to connect it with the previous ones: two nodes need at least one edge, three nodes at least two, and so on). So summing up the number of edges and nodes over P components, this leads to

E >= N - P,

and thus

E - N + P >=0

Since P is >=1 for any graph with at least one node, finally

E - N + 2P > 0

(except for the "empty" graph, where this is 0).

Related Topic