DAG
algorithm
graph theory
inversion
data structures

Seeking algorithm to invert reverse? mirror? turn inside-out a DAG

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

If by "invert" a DAG you mean reverse the direction of every edge, the algorithm is simple: create a new graph with the same vertices and flip each edge u -> v into v -> u. There is no special DAG-only magic required beyond standard graph traversal.

The nice property is that reversing all edges of a DAG still gives you a DAG. In other words, edge reversal preserves acyclicity for directed acyclic graphs.

Reverse Every Edge

Suppose the original graph contains edges:

text
1A -> B
2A -> C
3B -> D
4C -> D

The reversed graph is:

text
1B -> A
2C -> A
3D -> B
4D -> C

That is usually what people mean by mirroring or turning the DAG inside out.

A Simple Adjacency-List Algorithm

Here is a straightforward implementation in Python.

python
1def reverse_dag(graph):
2    reversed_graph = {node: [] for node in graph}
3
4    for source, neighbors in graph.items():
5        for target in neighbors:
6            reversed_graph.setdefault(target, [])
7            reversed_graph[target].append(source)
8
9    return reversed_graph
10
11
12dag = {
13    "A": ["B", "C"],
14    "B": ["D"],
15    "C": ["D"],
16    "D": []
17}
18
19print(reverse_dag(dag))

This runs in O(V + E) time because it touches each vertex and each edge once.

Why the Result Is Still a DAG

A DAG has no directed cycles. If reversing all edges created a cycle, then reversing that cycle again would create a cycle in the original graph too. That would contradict the assumption that the original graph was acyclic.

So the reversed graph must also be acyclic.

This is a useful fact because it means you do not need special cycle-repair logic when reversing a known DAG.

Topological Order Relationship

There is also a neat relationship with topological sorting. If v1, v2, ..., vn is a topological order of the original DAG, then the reverse order vn, ..., v2, v1 is a valid topological order of the reversed DAG.

That gives you an intuitive way to think about edge reversal: all dependency directions are flipped, so the order of "must come before" also flips.

When Reversing a DAG Is Useful

Reversed DAGs appear in practical tasks such as:

  • dependency analysis from outputs back to prerequisites
  • reverse reachability queries
  • computing parent links from child-oriented data
  • graph algorithms that need incoming edges instead of outgoing edges

For example, if your build graph stores module -> dependencies, reversing it can help answer "which modules depend on this one."

In-Place Versus New Graph

It is usually safer to build a new graph rather than mutating the old one in place. In-place mutation can be awkward because you are changing the structure while still reading it.

A new graph is simpler to reason about and avoids accidental corruption of traversal state.

If memory is a concern and the graph is very large, you can still build a reversed adjacency structure incrementally in one pass without holding complicated extra state beyond the output representation.

Be Precise About the Meaning of "Invert"

Graph questions often use words like reverse, transpose, invert, or mirror loosely. For directed graphs, the standard term is often transpose: reverse every directed edge.

That matters because some people hear "invert a DAG" and think of reversing node order, computing transitive closure, or building some kind of dual graph. If the goal is just edge reversal, say so explicitly.

Common Pitfalls

  • Overcomplicating the problem when the real task is just reversing each edge.
  • Forgetting to preserve vertices that have no incoming edges in the reversed graph.
  • Mutating the original adjacency list while iterating over it.
  • Using vague terms like "inside-out" without defining the exact transformation.
  • Assuming the reversed graph might stop being acyclic when the original graph was a DAG.

Summary

  • Reversing a DAG means replacing every edge u -> v with v -> u.
  • The algorithm is a simple O(V + E) pass over the adjacency list.
  • The reversed graph is still a DAG.
  • The reverse of a topological order for the original DAG is a topological order for the reversed DAG.
  • In practice, it is usually best to build a new reversed adjacency structure rather than mutating the original graph in place.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.