Undirected Graph Simulator
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
Example graph loaded. Drag vertices or choose an operation.
Vertices0
Edges0
Components0
Density0.00
Traversal order
—
Frontier
BFS and DFS
BFS: mark start when enqueued repeatedly dequeue a vertex enqueue each unvisited neighbor DFS: visit a vertex recursively visit each unvisited neighbor Neighbors follow 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
| Operation | Adjacency list | Adjacency matrix |
|---|---|---|
| Space | O(V + E) | O(V²) |
| Check edge | O(degree) | O(1) |
| Iterate neighbors | O(degree) | O(V) |
| Best use | Sparse graphs | Dense graphs |