← Back

Undirected Graph Simulator

What is a Graph?

A graph consists of vertices connected by edges. This simulator uses a simple undirected graph: edges have no direction, self-loops are rejected, and duplicate edges are not allowed.

BFS / DFS timeO(V + E)
Adjacency list spaceO(V + E)
Adjacency matrix spaceO(V²)
Edge typeUndirected

Graph editing and traversal

400 ms

Example graph loaded. Drag vertices or choose an operation.

Vertices0
Edges0
Components0
Density0.00

Traversal order

Frontier

BFS and DFS

BFS:
    mark the start when enqueued
    repeatedly dequeue a vertex
    enqueue each unvisited neighbor

DFS:
    visit a vertex
    recursively visit each
    unvisited neighbor

Neighbors are processed
in vertex insertion order.

Adjacency list

Adjacency matrix

Graph concepts

Breadth-first search

BFS uses a queue and explores vertices by distance layers. In an unweighted graph, it can find shortest paths measured in edge count.

Depth-first search

DFS uses recursion or a stack and follows one branch as deeply as possible before backtracking.

Disconnected graphs

A traversal from one start vertex visits only its connected component. Connected Components scans again from every still-unvisited vertex.

Two representations

Adjacency lists are compact for sparse graphs. Matrices provide constant-time edge checks but require quadratic space.

Representation comparison

OperationAdjacency listAdjacency matrix
SpaceO(V + E)O(V²)
Check edgeO(degree)O(1)
Iterate neighborsO(degree)O(V)
Best useSparse graphsDense graphs