1 / 33

A Complexity Measure

A Complexity Measure. THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat. INTRODUCTION. How to modularize a software system so the resulting modules are both testable and maintainable? Currently is to limit programs by physical size Not adequate

taima
Download Presentation

A Complexity Measure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat

  2. INTRODUCTION • How to modularize a software system so the resulting modules are both testable and maintainable? • Currently is to limit programs by physical size • Not adequate • 50 line program consisting of 25 consecutive “IF-THEN” constructs.

  3. A COMPLEXITY MEASURE • Measure and control the number of paths through a program. Definition 1 The cyclomatic number V(G) of a graph G with n vertices, e edges, and p connected components is V(G) = e – n + 2p

  4. A COMPLEXITY MEASURE • THEOREM 1: In a strongly connected graph G, the cyclomatic number is equal to the maximum number of linearly independent circuits

  5. A COMPLEXITY MEASURE • Given a program we will associate with it a directed graph that has unique entry and exit nodes. Each node in the graph corresponds to a block of code in the program where the flow is sequential and the arcs correspond to branches taken in the program. • Known as “program control graph”.

  6. A COMPLEXITY MEASURE

  7. A COMPLEXITY MEASURE • From the graph, the maximum number of linearly independent circuits in G is 9-6+2. • One could choose the following 5 indepentdent circuits in G: B1: (abefa), (beb), (abea), (acfa), (adcfa).

  8. A COMPLEXITY MEASURE • Several properties of cyclomatic complexity: • v(G) ≥ 1. • v(G) is the maximum number of linearly independent paths in G; it is the size of a basis set. • Inserting or deleting functional statements to G does not affect v(G). • G has only one path if and only if v(G) = 1.

  9. A COMPLEXITY MEASURE • Inserting a new edge in G increases v(G) by unity. • v(G) depends only on the decision structure of G.

  10. DECOMPOSITION • v = e – n + 2p • P is the number of connected components. • All control graphs will have only one connected component.

  11. DECOMPOSITION

  12. DECOMPOSITION • Now, since p = 3, we calculate complexity as v(M∪A∪B) = e – n + 2p = 13-13+2x3 = 6 • Notice that v(M∪A∪B) = v(M) + v(A) + v(B) = 6.

  13. SIMPLIFICATION • 2 ways • Allows the complexity calculations to be done in terms of program syntactic constructs. • Calculate from the graph from.

  14. SIMPLIFICATION • First way, the cyclomatic complexity of a structured program equals the number of predicates plus one

  15. SIMPLIFICATION

  16. SIMPLIFICATION • From the graph, v(G) = ¶ + 1 = 3 + 1 = 4

  17. SIMPLIFICATION • The second way is by using Euler’s formula: • If G is a connected plane graph with n vertices, e edges, and r regions, then n – e + r = 2 • So the number of regions is equal to the cyclomatic complexity.

  18. SIMPLIFICATION

  19. NONSTRUCTURED PROGRAMMING • There are four control structures to generate all nonstructured programs.

  20. NONSTRUCTURED PROGRAMMING

  21. NONSTURCTURED PROGRAMMING

  22. NONSTRUCTURED PROGRAMMING • Result 1: A necessary and sufficient condition that a program is nonstructured is that it contains as a subgraph either a), b), or c). • Result 2: A nonstructured program cannot be just a little nonstructured. That is any nonstructured program must contain at least 2 of the graphs a) – d).

  23. NONSTRUCTURED PROGRAMMING • Case 1: E is “before” the loop. E is on a path from entry to the loop so the program must have a graph as follows:

  24. NONSTRUCTURED PROGRAMMING • Case 2: E is “after” the loop. The control graph would appear as follows:

  25. NONSTRUCTURED PROGRAMMING • Case 3: E is independent of the loop. • The graph c) must now be present with b). • If there is another path that can go to a node after the loop from E then a type d) graph is also generated.

  26. NONSTRUCTURED PROGRAMMING • Result 3: A necessary and sufficient condition for a program to be nonstructured is that it contains at least one of: (a,b), (a,d), (b,c), (c,d).

  27. NONSTRUCTURED PROGRAMMING • Result 4: The cyclomatic complexity if a nonstructured program is at least 3. • If the graphs (a,b) through (c,d) have their directions taken off, they are all isomorphic.

  28. NONSTRUCTURED PROGRAMMING • Result 5: A structured program can be written by not branching out of loops or into decisions - a) and c) provide a basis. • Result 6: A structured program can be written by not branching into loops or out of decisions – b) and d) provide a basis. • Result 7: A structured program is reducible( process of removing subgraphs (subroutines) with unique entry and exit nodes) to a program of unit complexity.

  29. NONSTRUCTURED PROGRAMMING • Let m be the number of proper subgraphs with unique entry and exit nodes. The following definition of essential complexity ev is used to reflect the lack of structure. ev = v – m • Result 8: The essential complexity of a structured program is one.

  30. A TESTING METHODOLOGY • Let a program p has been written, its complexity v has been calculated, and the number of paths tested is ac (actual complexity). If ac is less than v then one of the following conditions must be true: • There is more testing to be done (more paths to be tested); • The program flow graph can be reduced in complexity by v-ac (v-ac decisions can be taken out); and • Portions of the program can be reduced to in line code (complexity has increased to conserve space).

  31. A TESTING METHODOLOGY • Suppose that ac = 2 and the two tested paths are [E,a1,b,c2,x] and [E,a2,b,c1,x]. Then given that paths [E,a1,b,c1,x] and [E,a2,b,c2,x] cannot be executed.

  32. A TESTING METHODOLOGY • We have ac < c so case 2 holds and G can be reduced by removing decision b. • Now v = ac and the new complexity is less than the previous one.

  33. A TESTING METHODOLOGY • It should be noted that v is only the minimal number of independent paths that should be tested. • It should be noted that this procedure will by no means guarantee or prove the software- all it can do is surface more bugs and improve the quality of the software

More Related