McCabe Metric Calculation Program Complexity

 


 
 

McCabe Metric Calculation Program Complexity

  The McCabe metric is a complexity measurement method based on program control flow proposed by Thomas McCabe. The McCabe complexity measure is also called the cycle measure. It thinks that the complexity of the program depends largely on the complexity of the control. The single sequential program structure is the simplest. complex. This method uses graph theory as a tool, first draws a program graph, and then uses the number of cycles in the graph as a measure of program complexity. The program graph is a degenerated program flow chart, that is to say, every processing symbol in the program flow chart is degenerated into a node, and the original streamlines connecting different processing symbols become directed arcs connecting different points, so that A directed graph of is called a program graph, as shown in the figure below. The program diagram only describes the control flow inside the program, and does not represent the specific operations on data and the specific conditions of branches and loops at all.
program map
According to graph theory, in a strongly connected directed graph G, the number of cycles V(G) is given by the following formula: V ( G )
= m − n + 2 p V(G) = m - n + 2pV ( G )=mn+2 p Among them, V(G) is the number of loops in the directed graph G, m is the number of arcs in the graph G, n is the number of nodes in the graph G, and p is the number of strongly connected components in the graph G. In a program, any node in the graph can always be reached from the entry point of the program graph, so the program is always connected, but not strongly connected. In order to make the program graph a strongly connected graph, a directed edge represented by a dotted line is added from the entry point to the exit point of the graph to make the graph a strongly connected graph. In this way, the complexity of the cycle can be calculated using the above formula.
Take the above figure as an example, where the number of nodes n = 6, the number of arcs m = 9, and the complexity of the program graph in the above figure p = 1, then: V ( G ) =
m − n + 2 p = 9 − 6 + 2 = 5 V(G) = m - n + 2p = 9 - 6 + 2 = 5V ( G )=mn+2p=96+2=5 means that the complex metric value of the McCabe cycle in the above figure is 5.
  Cyclic complexity measures reflect the complexity of a program's (or module's) control structure. McCabe found that V(G) = 10 is an upper limit for a practical module. It becomes particularly difficult to adequately test a module when its cyclomatic complexity exceeds 10.

 
 
I saw three ways to calculate cyclomatic complexity online.
Cyclomatic complexity quantitatively measures the logical complexity of a program. After drawing the flow graph of the program control flow, the cyclomatic complexity can be calculated by any of the following three methods:
(1) The number of regions in the flow graph is equal to the cyclomatic complexity.
(2) The circular complexity V(G) of flow graph G = E - N + 2, where E is the number of edges in the flow graph and N is the number of nodes.
(3) The circular complexity V(G) of the flow graph G = P + 1, where P is the number of decision nodes in the flow graph.

Look at the two real questions:
topic
(1)
1
The complexity of the left picture: 3; the complexity of the right picture: 5

(2) Left image complexity 8 - 7 + 2 = 3; right image complexity = 10 - 7 + 2 = 5
(3) Left image: C, E => 2 + 1 = 3; Right image: C, D , E, G => 4 + 1 = 5

 
 

 
 

 
 

 
 

topic example


For the code shown below (indentation is used to represent program blocks), at least () test cases are required to achieve statement coverage. The cycle complexity of the program flow chart corresponding to this code is calculated by McCabe metric method as ().

Question one
  • A. 1
  • B. 2
  • C. 3
  • D. 4
question two
  • A. 2
  • B. 1
  • C. 3
  • D. 4

[Analysis of test questions]: This question examines the combination of loop complexity and McCabe metric calculation.
As far as the pseudocode is concerned, we can judge according to its relationship. It should be the process of sorting a set of data in order from small to large, which is essentially an insertion sort algorithm.
First of all, for the first question, how many test cases are needed to achieve statement coverage? We only need a set of data to get the output results after repeated sorting.
For the second question, to calculate the complexity of the loop, we need to make a related diagram, as shown in the figure below, according to the loop formula V(G) =m-n+2, you can also directly count the closed loop + 1, and get which results in 3
l Flowchart
analyze


Calculate the cycle complexity of the program shown in the figure below using the McCabe metric method as ( ).

  • A. 1
  • B. 2
  • C. 3
  • D. 4

[Examination question analysis]: Cyclomatic complexity V(G) = E - N + 2, where E is the number of edges in the flow graph, and N is the number of nodes. V(G) = E - N + 2 = 11 - 10 + 2 = 3.
analyze


Calculate the cycle complexity of the following program graph using the McCabe metric method as ().

  • A. 2
  • B. 3
  • C. 4
  • D. 5

[Examination question analysis]: McCabe metric method first draws the program graph, and then uses the formula V(G) = m - n + 2 to calculate the cycle complexity, where m is the number of directed arcs and n is the number of nodes.
The number of nodes in this question: 8, the number of sides: 10. 10 - 8 + 2 = 4.
analyze


There are ( ) different simple paths in the program flowchart shown in the figure below. The cycle complexity of the program graph is calculated by McCabe metric as ( ).

Question one
  • A. 3
  • B. 4
  • C. 5
  • D. 6
question two
  • A. 3
  • B. 4
  • C. 5
  • D. 6

[Examination Question Analysis]: Cyclomatic complexity quantitatively measures the logical complexity of a program. After drawing the flow graph of the program's control flow, the cyclomatic complexity can be calculated in any of the following three ways.
(1) The number of regions in the flow graph is equal to the cyclomatic complexity.
(2) The circular complexity V(G) of the flow graph G = E - N + 2, where E is the number of edges in the flow graph, and N is the number of nodes.
(3) Cyclomatic complexity V(G) = P + 1 of the flow graph G, where P is the number of decision nodes in the flow graph.
The calculation idea of ​​this loop measurement method is as follows: it considers the complexity of the control, that is, the complexity of the branch of the conditional selection.
There are 3 simple judgments in the figure (the number of physical judgments in the loop body is 2, but logically, at least 3 judgments are required (the first judgment needs ⑵ times to reach the exit of the program. So in the figure There are 3 simple judgments.). Therefore, 3 simple paths form 3 circular areas, and the area complexity is 3.
analyze


Calculate the cycle complexity of the following program graph using the McCabe metric method as ().

  • A. 2
  • B. 3
  • C. 4
  • D. 5

【Explanation of test questions】: The McCabe measurement method is to establish the measurement of program complexity by defining the cycle complexity, which is based on the number of loops in the program diagram of a program module. The formula for calculating the cycle complexity of a directed graph G is: V(G) = m - n + 2, where V(G) is the number of cycles in the directed graph G, and m is the directed arc in G number, n is the number of nodes in G.
In the figure, m is 8, n is 6, then m - n + 2 = 4.
analyze


The complexity of software is mainly reflected in the complexity of the program. ( ) is a main parameter to measure software complexity. If the McCabe metric method is used to calculate the cycle complexity, then for the program diagram shown in the figure below, the cycle complexity is ().

Question one
  • A. Lines of Code
  • B. Number of constants
  • C. Number of variables
  • D. Number of library functions called
question two
  • A. 2
  • B. 3
  • C. 4
  • D. 5

[Examination Question Analysis]: The number of lines of code measurement method uses the total number of lines of code of the program as the measure of program complexity.
The McCabe metric first draws the program graph, and then calculates the cycle complexity using the formula V(G) = m - n + 2, where m is the number of directed arcs and n is the number of nodes. In this question, the number of nodes is 9 and the number of arcs is 11, so the cyclomatic complexity is 11 - 9 + 2 = 4.
analyze


Using the McCabe metric method to calculate the cycle complexity of the figure below is ().

  • A. 2
  • B. 3
  • C. 4
  • D. 5

[Analysis of test questions]: This question examines the calculation of the complexity of the loop, which is a knowledge point often tested in the test. For this type of test questions, we can use the formula of the number of edges - the number of nodes + 2 to calculate. In the graph given in this question, the number of nodes is 6, and the number of edges is 8 (note that the top table and the past edges of statement 2 are coincident, so they can be regarded as the same edge), so the loop The path complexity is 4.
analyze


Calculate the cycle complexity of the following program graph using the McCabe metric method as ().

  • A. 2
  • B. 3
  • C. 4
  • D. 5

[Analysis of test questions]: McCabe metric is a complexity measurement method based on program control flow. To use this method, you must first draw a program diagram, and then use the formula V(G) = m - n + 2 to calculate the complexity of the loop. Among them, m is the number of arcs in graph G, and n is the number of nodes in graph G. The number of nodes in this question is 9, and the number of edges is 10, so the cycle complexity is 10 - 9 + 2 = 3.
analyze


According to the McCabe metric, the complexity measure of the following program graph is ( ).

  • A. 4
  • B. 5
  • C. 6
  • D. 7

【Analysis of test questions】: Common program complexity measurement methods mainly include McCabe measurement method and code line measurement method. The McCabe metric is a complexity metric based on program control flow. The program complexity measure defined by McCabe is also called the cycle complexity, which is based on the number of loops in the program graph of a program module. The formula for calculating the cycle complexity of a directed graph G:
V(G) = m - n + 2
where, V(G) is the number of cycles in the directed graph G, and m is the number of directed arcs in the graph G number, n is the number of nodes in graph G. In this question the value of m should be 9, and the value of n is 7. So according to the formula, the final calculation result is 4.
analyze


The program diagram of a program is shown in the figure below, and the McCabe measurement method is used to measure it, and its cycle complexity is ().

  • A. 4
  • B. 5
  • C. 6
  • D. 8

[Analysis of test questions]: McCabe metric is a complexity measurement method based on program control flow. Use this method to draw the program diagram first, and then use the formula V(G) = m - n + 2 to calculate the complexity of the loop. Among them, m is the number of arcs in graph G, and n is the number of nodes in graph G. The number of nodes in the figure is 7, and the number of edges is 11, so the cycle complexity is 11 - 7 + 2 = 6.
analyze

Guess you like

Origin blog.csdn.net/qq_43448856/article/details/123520356