0

Calculate the CC (Cyclomatic Complexity) of the following code snippets.

I am not sure if I have drawn the Control Flow Graph correctly and furthermore if my calculation for the Cyclomatic Complexity is correct. And as for the formula M = E - N + 2P, the P is defined as "P = number of nodes that have exit points". Am I only counting the return here? How is it then with GCD2, if the method calls itself recursively does this also count as a return?

GCD1

public class GCD1 {
    public int gcd(int a, int b) {
        if (a < b) {
            int t = a;
            a = b;
            b = t;
        }
        while (a % b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return b;
    }
}

GCD2

public class GCD2 {
    public int gcd(int a, int b) {
        if (a < b)
            return gcd(b, a);
        else if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
}

Calculation

M = E – N + 2P

where,

  • E = the number of edges in the control flow graph
  • N = the number of nodes in the control flow graph
  • P = number of nodes that have exit points

Control Flow Graph of above GCD1

M = 7 - 6 + 2*1 = 3

enter image description here

Control Flow Graph of above GCD2

M = 8 - 7 + 2*1 = 3

enter image description here

Mr. Hankey
  • 954
  • 1
  • 5
  • 12
  • Hey, this question would be better suited to [Computer Science Stack Exchange](https://cs.stackexchange.com/). =) – Elliott Jul 04 '21 at 09:14
  • 1
    Cross-posted: https://stackoverflow.com/q/68242940/781723, https://cs.stackexchange.com/q/141983/755. Please [do not post the same question on multiple sites](https://meta.stackexchange.com/q/64068). – D.W. Jul 05 '21 at 08:23
  • 1
    @Elliott, may I make a request for the future? If you're going to suggest another site, it would be nice if you could let people know not to cross-post: you can suggest they delete the copy here before posting elsewhere. I'm hoping that will lead to a better experience for both the poster and for answerers. Thank you! – D.W. Jul 05 '21 at 08:24
  • @D.W., thanks for the feedback. Good point. 'Will do. – Elliott Jul 07 '21 at 01:30

0 Answers0