Chapter 14 · Simulation

Undirected Graph Simulator

← Chapter 14

Example graph loaded. Drag the vertices or pick an operation.

Vertices0
Edges0
Components0
Density0.00

Traversal order

—

Frontier

Adjacency list

Adjacency matrix

Undirected Graph Simulator

A graph is a set of vertices joined by edges. This simulator works with a simple undirected graph: edges have no direction, self-loops are rejected, and the same edge cannot be added twice.

How to use it

  • Add or remove vertices (Remove Vertex deletes the vertex selected as Endpoint 1); pick two endpoints and press Add / Remove Edge to toggle the edge between them. Drag vertices to tidy up the drawing.
  • Choose a start vertex and run BFS or DFS; the traversal order and the current frontier (queue or stack) appear on the right. Connected Components colors each component in turn.
  • The adjacency list and adjacency matrix update after every change.

Graph ideas

  • Breadth-first search: BFS uses a queue and discovers vertices in layers of increasing distance. In an unweighted graph it finds shortest paths measured in edges.
  • Depth-first search: DFS uses recursion or a stack and goes as deep as it can along one branch before backing up.
  • Disconnected graphs: a traversal from one start vertex reaches only that vertex's connected component. Connected Components restarts the search from every vertex not yet visited.
  • Two representations: an adjacency list takes little space for sparse graphs; a matrix tests for an edge in constant time but needs quadratic space.

Comparing the representations

OperationAdjacency listAdjacency matrix
SpaceO(n + m)O(n²)
Edge testO(degree)O(1)
Visit neighborsO(degree)O(n)
Best forSparse graphsDense graphs