transitive reduction algorithm pseudocode?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The transitive reduction of a directed graph is the smallest graph that has the same reachability as the original — if vertex u can reach vertex v in the original graph, it can also reach v in the reduction, and no edge can be removed without losing reachability. Transitive reduction is the inverse of transitive closure and is used to simplify dependency graphs, remove redundant edges in DAGs, and visualize minimal relationships. For a DAG, the transitive reduction is unique.
Definitions
- Reachability: Vertex
ucan reach vertexvif there is a directed path fromutov - Transitive closure: Add a direct edge
u → vfor every pair whereucan reachvindirectly - Transitive reduction: Remove every edge
u → vwhereucan still reachvthrough other edges
Example: Given edges A → B, B → C, A → C, the edge A → C is redundant because A can reach C via B. The transitive reduction removes A → C.
Pseudocode (Brute Force)
Time complexity: O(V * E) where V is vertices and E is edges, since each edge removal triggers a BFS/DFS taking O(V + E).
Pseudocode (Matrix-Based)
Using the adjacency matrix and transitive closure:
Time complexity: O(V^3) due to Warshall's algorithm.
Python Implementation
Using NetworkX
nx.transitive_reduction() works on DAGs and raises an error for graphs with cycles.
Applications
- Dependency management: In build systems (Make, Gradle), the transitive reduction shows the minimal set of direct dependencies needed
- Database schema visualization: Simplifying foreign key relationships by removing redundant transitive references
- Task scheduling: Identifying the essential ordering constraints in a task dependency graph
- Version control: Simplifying commit ancestry graphs to show only direct parent relationships
Common Pitfalls
- Applying to graphs with cycles: For DAGs, the transitive reduction is unique. For general directed graphs with cycles, the transitive reduction is not unique and the algorithm must handle strongly connected components separately. NetworkX's
transitive_reductionrejects cyclic graphs. - Confusing transitive reduction with transitive closure: Transitive closure adds edges (makes all indirect reachability explicit). Transitive reduction removes edges (keeps only the essential ones). They are inverse operations.
- Modifying the graph during edge iteration: Removing edges while iterating over them causes missed checks or errors. Copy the edge list before iterating, or collect edges to remove and apply them after the loop.
- Not checking edge direction: Transitive reduction is defined for directed graphs. Applying it to undirected graphs requires converting to a DAG first (e.g., by choosing an ordering), which changes the problem semantics.
- Assuming O(V + E) time complexity: The brute-force algorithm is O(V * E) because each of the E edges requires a BFS/DFS of O(V + E). The matrix-based approach is O(V^3). There is no known algorithm faster than O(V * E) for sparse graphs.
Summary
- Transitive reduction removes redundant edges while preserving all reachability
- For each edge
u → v, check ifucan reachvvia other paths — if yes, remove the edge - The brute-force approach runs BFS/DFS per edge at O(V * E); the matrix approach uses Warshall's algorithm at O(V^3)
- For DAGs, the transitive reduction is unique; for cyclic graphs, it is not
- Use
networkx.transitive_reduction()in Python for a ready-made implementation - Applications include dependency management, database visualization, and task scheduling
Related reading
- Transpose a 1 dimensional array, that does not represent a square, in place
- Transposition table in Monte Carlo Tree Search algorithm unintended effect on UCT score
- Traveling salesman example with known global optimum
- Travelling Salesman with multiple salesmen with a limit on number of cities per salesman?
- Transpose 1 Dimensional Array
- Transpose list of lists
- Travelling salesman with repeat nodes dynamic weights
- Traversal of an n-dimensional space

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.