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

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

Transcript of A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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

A Complexity Measure

THOMAS J. McCABE

Presented by

Sarochapol Rattanasopinswat

Page 2: 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 50 line program consisting of 25 consecutive “IF-

THEN” constructs.

Page 3: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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

Page 4: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

A COMPLEXITY MEASURE

THEOREM 1:

In a strongly connected graph G, the cyclomatic number is equal to the maximum number of linearly independent circuits

Page 5: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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”.

Page 6: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

A COMPLEXITY MEASURE

a

b dc

e

f

G:

Page 7: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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).

Page 8: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

Page 9: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

A COMPLEXITY MEASURE

Inserting a new edge in G increases v(G) by unity.

v(G) depends only on the decision structure of G.

Page 10: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

DECOMPOSITION

v = e – n + 2pP is the number of connected

components.All control graphs will have only one

connected component.

Page 11: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

DECOMPOSITION

M:

A:

B:

Page 12: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

DECOMPOSITION

Now, since p = 3, we calculate complexity as

v(M∪A∪B) = e – n + 2p

= 13-13+2x3

= 6Notice that v(M∪A∪B) = v(M) + v(A) + v(B)

= 6.

Page 13: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

SIMPLIFICATION

2 ways Allows the complexity calculations to be done in

terms of program syntactic constructs. Calculate from the graph from.

Page 14: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

SIMPLIFICATION

First way, the cyclomatic complexity of a structured program equals the number of predicates plus one

Page 15: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

SIMPLIFICATION

G:

Page 16: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

SIMPLIFICATION

From the graph,

v(G) = ¶ + 1

= 3 + 1

= 4

Page 17: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

Page 18: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

SIMPLIFICATION

12

3

4

5

v(G) = 5

G:

Page 19: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

NONSTRUCTURED PROGRAMMING

There are four control structures to generate all nonstructured programs.

Page 20: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

NONSTRUCTURED PROGRAMMING

A) B)

Page 21: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

NONSTURCTURED PROGRAMMING

C) D)

Page 22: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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).

Page 23: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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:

E

C)

B)

Page 24: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

NONSTRUCTURED PROGRAMMING

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

E

A)

B)

Page 25: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

E C)

B)D)

Page 26: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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).

b

a

(a,b)

d

a

(a,d)

c

(b,c)

b

d

(c,d)

c

Page 27: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

Page 28: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

Page 29: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

Page 30: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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).

Page 31: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

E

a1 a2

b

c1 c2

x

Page 32: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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.

E

a1 a2

c1 c2

x

Page 33: A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.

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