为了计算代码的圈复杂度,我画了一个由节点和边组成的控制流程图,这有助于我计算 V (G) = E - N + 2 在我的例子中,E = 15,N = 11。导致圈复杂度为 6。
现在为了确认我的答案,我需要一些帮助来找到代码的线性独立路径:
int maxValue = m[0][0];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if ( m[i][j] > maxValue )
{
maxValue = m[i][j];
}
}
}
cout << maxValue << endl;
int sum = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
sum = sum + m[i][j];
}
}
cout << sum << endl;
这应该等于我的 V (G) 的结果,否则我的计算是错误的。感谢您的帮助。
最佳答案
McCabe 的圈复杂度给出了一个上限。考虑以下因素:
void func (const bool do_special) {
if (do_special) {
do_something_special_at_the_start();
}
always_do_this_stuff_in_the_middle();
if (do_special) {
do_something_special_at_the_end();
}
从图论的角度来看,其圈复杂度为三。但是,由于 do_special
是常量,因此代码中只有两条独立的路径。图论模型不知道有些路径是不可能的。图中可能路径的数量有时小于圈复杂度。
关于c++ - McCabe 的圈复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10365912/