Chapter 14 · Simulation

Directed Graph Simulator

← Chapter 14

Example DAG loaded.

Vertices0
Edges0
Sources0
Sinks0

Traversal / topological order

—

Frontier

Current step

—

Adjacency list

Adjacency matrix

Directed Graph Simulator

A directed graph stores each edge with its direction: an edge from A to B does not give you an edge from B to A. This simulator covers directed traversal, in- and out-degrees, the adjacency representations, and topological sorting of directed acyclic graphs.

How to use it

  • Add or remove vertices; pick From and To and press Add / Remove Edge to toggle that directed edge. Drag vertices to tidy up the drawing.
  • Run BFS or DFS from the start vertex. Topological Sort (Kahn's algorithm) and Find Cycle ignore the start vertex and work on the whole graph. The order, the frontier, and the current step appear below the drawing.
  • DAG Example and Cycle Example load ready-made graphs. Under each vertex, in is its in-degree and out its out-degree.

Directed graph ideas

  • In-degree and out-degree: the in-degree counts incoming edges, the out-degree outgoing ones. A source has in-degree 0; a sink has out-degree 0.
  • Direction matters: A → B and B → A are different edges, so reachability depends on which way the edges point.
  • Topological sort: Kahn's algorithm repeatedly removes a vertex of in-degree 0. A complete order exists only in a directed acyclic graph.
  • Cycle detection: if Kahn's algorithm cannot process every vertex, at least one directed cycle is blocking the topological order.

With an adjacency list, BFS, DFS, and Kahn's algorithm all run in O(V + E) time.