#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
White Box Techniques

CFG (Control Flow Graph)

What is a Control Flow Graph?

A Control Flow Graph (CFG) is a graphical representation of all the paths that might be traversed through a program during its execution, using nodes and edges.

Key Elements of a CFG

  • Nodes: Represent statements or blocks of sequential code.
  • Edges: Represent the flow of control between nodes (e.g., following an if/else branch or a loop).
  • Decision Points: Nodes with more than one outgoing edge, representing branching logic.

Why CFGs Matter for Testing

CFGs are the foundation for structure-based testing techniques like statement, branch, and path coverage, and are used to calculate cyclomatic complexity and design basis path test cases.

Drawing a CFG From Real Code, Step by Step

A control flow graph becomes much easier once you build one by hand. Take this small function that checks an exam score:

def grade(score):
    if score < 0:            # node 1 (decision)
        return "invalid"     # node 2
    result = "pass"          # node 3
    if score < 40:           # node 4 (decision)
        result = "fail"      # node 5
    return result            # node 6

Follow these steps to turn it into a graph:

  1. Number the statements. Give each statement or straight-line block a node number, as in the comments above.
  2. Add a single exit node. Both return statements lead to the end of the function, so add node E to represent the exit.
  3. Draw an edge for every possible move. Node 1 has two edges (to 2 when the score is negative, to 3 otherwise). Node 4 also has two edges (to 5 or straight to 6).
  4. Join the loose ends. Node 5 flows to 6, and both 2 and 6 flow to E.

The finished graph has 7 nodes (1, 2, 3, 4, 5, 6, E) and 8 edges: 1→2, 1→3, 3→4, 4→5, 4→6, 5→6, 2→E and 6→E.

Calculating Cyclomatic Complexity From the Graph

Cyclomatic complexity tells you how many independent paths exist, which is also the minimum number of test cases needed for basis path testing. There are three equivalent ways to compute it:

  • Edges and nodes: V(G) = E − N + 2P, where P is the number of connected components (1 for a single function). Here 8 − 7 + 2 = 3.
  • Decision points: number of binary decisions + 1. The function has two if statements, so 2 + 1 = 3.
  • Regions: count the enclosed areas of the drawn graph plus the outer area. This also gives 3.

If two methods give different answers, recheck your edges. A missing edge is the most common reason.

Turning Basis Paths Into Test Cases

PathRoute through the graphInputExpected output
P11 → 2 → Escore = -5"invalid"
P21 → 3 → 4 → 6 → Escore = 70"pass"
P31 → 3 → 4 → 5 → 6 → Escore = 20"fail"

These three tests together execute every statement and every branch outcome. Adding a boundary test such as score = 40 is still worthwhile, because path testing proves routes are exercised, not that the comparison operator is right.

Where Testers Go Wrong With CFGs

  • Treating every line as its own node. Consecutive statements with no branch can be merged into one block. Merging removes one node and one edge together, so the complexity stays the same.
  • Forgetting the back edge of a loop. A while or for loop needs an edge from the end of the body back to the condition.
  • Ignoring compound conditions. if a and b contains two decisions when you need condition-level testing; split it into two nodes in that case.
  • Trying to test every path in loops. Loops create an unlimited number of paths. Basis paths keep the set finite and practical.

Takeaway: number the nodes, draw every edge, compute V(G) two ways, then write one test per basis path.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course