liveBook · Manning

Control flow graph

As explained in Practices of the Python Pro

A control flow graph is a directed graph whose nodes represent program execution points and whose edges represent possible transfers of control.

A control flow graph maps how execution can move through a function. Nodes mark execution points such as the function start, conditions, loops, loop ends, and returns; directed edges mark possible paths. section 9.1.3

The graph helps measure cyclomatic complexity, which indicates how many independent paths a function contains. Count edges and nodes, then calculate M = E - N + 2. section 9.1.3

A function with branching and a loop

For has_long_words, the graph includes an input-type condition, a loop over words, a length condition inside the loop, and return paths. With 10 edges and 8 nodes, its complexity is 4. section 9.1.3

How Practices of the Python Pro covers it

How deep it goes
Defines nodes, edges, and the complexity formula, then works through a function with a loop and nested condition.
Examples
Uses a has_long_words function, its control-flow diagram, and an 8-node, 10-edge complexity calculation.
What you can do after
Evaluate function complexity by diagramming execution paths and counting nodes and edges.
How it connects
Connects control flow graphs with cyclomatic complexity, conditionals, loops, execution paths, and refactoring.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.