计算圈复杂度

问题描述

计算以下代码片段的 CC(圈复杂度)。

我不确定我是否正确绘制了控制流图,而且我对圈复杂度的计算是否正确。对于公式M = E - N + 2P,P 定义为“P = number of nodes that have exit points”。我这里只计算回报吗?那么在 GCD2 中,如果该方法递归调用自身,这是否也算作返回?

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);
    }
}

计算

M = E – N + 2P​​p>

哪里,

  • E = 控制流图中的边数
  • N = 控制流图中的节点数
  • P = 具有出口点的节点数

以上GCD1的控制流图

M = 7 - 6 + 2*1 = 3

enter image description here

以上 GCD2 的控制流图

M = 8 - 7 + 2*1 = 3

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)