1. Cyclomatic complexity
• Cyclomatic complexity of a code section is the quantitative measure
of the number of linearly independent paths in it.
• It is a software metric used to indicate the complexity of a program.
• It is computed using the Control Flow Graph of the program.
2. Control Flow Graph (CFG)
• A Control Flow Graph (CFG) is the graphical representation of control flow or
computation during the execution of programs or applications.
• Control flow graphs are mostly used in static analysis as well as compiler
applications, as they can accurately represent the flow inside of a program unit.
• The control flow graph was originally developed by Frances E. Allen.
3. Characteristics of Control Flow Graph
• Control flow graph is process oriented.
• Control flow graph shows all the paths that can be traversed during a program
execution.
• Control flow graph is a directed graph.
• Edges in CFG portray control flow paths and the nodes in CFG portray basic
blocks.
9. Advantages of Control Flow Graph (CFG)
• It can easily encapsulate the information per each basic block.
• It can easily locate inaccessible codes of a program and syntactic structures such
as loops are easy to find in a control flow graph.
10. Cyclomatic complexity
• The nodes in the graph indicate the smallest group of commands of a program,
and a directed edge in it connects the two nodes i.e. if second command might
immediately follow the first command.
• For example, if source code contains no control flow statement then its cyclomatic
complexity will be 1 and source code contains a single path in it.
• Similarly, if the source code contains one if condition then cyclomatic complexity
will be 2 because there will be two paths one for true and the other for false.
12. Steps to calculate Cyclomatic complexity
• Construction of graph with nodes and edges from code.
• Identification of independent paths.
• Cyclomatic Complexity Calculation
• Design of Test Cases
15. Use of Cyclomatic Complexity
• Risk associated with program can be evaluated.
• Determining the independent path executions thus proven to be very helpful for
Developers and Testers.
• It can make sure that every path have been tested at least once.
• Thus help to focus more on uncovered paths.
• Code coverage can be improved.
• These metrics being used earlier in the program helps in reducing the risks.
16. Advantages of Cyclomatic Complexity
• It can be used as a quality metric, gives relative complexity of various designs.
• It is able to compute faster than the Halstead’s metrics.
• It is used to measure the minimum effort and best areas of concentration for
testing.
• It is able to guide the testing process.
• It is easy to apply.
#7:The top diamond is the first decision: A == 10
If false, it skips directly to the print statement.
If true, it checks the second condition: B > C.
The second diamond handles the nested if:
If true, A = B
If false, A = C
Both branches then merge back at the end-if and go to print A, B, C.
💡 Teaching Point:
This flowchart helps you visualize the decision points (conditions) and the branches that will become part of the control flow graph when calculating cyclomatic complexity.