Undirected
The relationship works both ways: {A, B} = {B, A}.
Algorithms · Data structures
A graph describes what is connected to what. BFS advances level by level with a queue; DFS follows one path to its end with a stack. The lab lets you watch both strategies, one edge at a time.
A graph is a pair G = (V, E): V is the set of vertices (or nodes), and E is the set of edges connecting pairs of vertices. It is more general than a list or tree: it may contain cycles, several paths between the same nodes and separate components.
The relationship works both ways: {A, B} = {B, A}.
An edge is an ordered pair: A→B does not imply B→A.
Each edge carries a cost, distance or capacity.
Not every pair of vertices is joined by a path.
Two vertices are adjacent if they share an edge. Degree counts incident edges; directed graphs distinguish in-degree and out-degree.
A path is a sequence of connected vertices. In an unweighted graph, distance is the minimum number of edges in a path.
A cycle is a path returning to its start. That is why a traversal must remember already discovered vertices.
A connected component is a maximal set of mutually reachable vertices. One source only visits its component.
A tree is an undirected, connected, acyclic graph. Every pair of vertices has one simple path; with n vertices it has exactly n − 1 edges.
| Structure | Memory | List neighbours of u | Check (u, v) | Best suited for |
|---|---|---|---|---|
| Adjacency lists | Θ(|V| + |E|) | Θ(deg(u)) | O(deg(u)) | Sparse graphs and BFS/DFS |
| Adjacency matrix | Θ(|V|²) | Θ(|V|) | Θ(1) | Dense graphs or frequent edge queries |
| Edge list | Θ(|E|) | Θ(|E|) | Θ(|E|) | Input, sorting or global edge scans |
A: [B, C]
B: [A, D]
C: [A, D]
D: [B, C]
A B C D
A [ 0 1 1 0 ]
B [ 1 0 0 1 ]
C [ 1 0 0 1 ]
D [ 0 1 1 0 ]
In the lab, neighbours are always examined in alphabetical order. Without a fixed order, BFS and DFS remain correct but may produce different trees and sequences.
| Aspect | BFS · breadth first | DFS · depth first |
|---|---|---|
| Frontier | FIFO queue | LIFO stack or recursion |
| Strategy | Completes one level before the next | Follows a branch, then backtracks |
| Guarantees | Shortest distances in unweighted graphs | Discovery/finish times and nested structure |
| Typical uses | Shortest paths, levels, bipartiteness | Cycles, topological sorting, components |
| Time | Θ(|V| + |E|) | Θ(|V| + |E|) |
| Auxiliary space | O(|V|) | O(|V|) |
Choose an algorithm and source, then move edge by edge. Click a node to make it the source, edit the edges or try a guided case.
When a vertex u leaves the queue, every vertex at a smaller distance has already left it, and newly discovered vertices have distance d[u] + 1. FIFO prevents a later level from overtaking the current one, so the first assigned distance is minimal.
The stack always contains a path from the root to the current vertex. A vertex is finished only after all outgoing edges are examined; discovery and finish times create nested intervals that reveal graph structure.
Store a predecessor when discovering a vertex, then reconstruct the path from destination back to source.
Degrees of separation, turn-based propagation and bipartiteness tests naturally follow levels.
An edge to an active vertex reveals a directed cycle; reverse finishing order yields a topological order when the graph is acyclic.
Mazes, components, bridges and articulation points exploit the deep structure of the DFS tree.
BFS does not solve shortest paths with arbitrary weights: depending on those weights, use 0-1 BFS, Dijkstra or Bellman–Ford.
On the cyclic graph, start at A: predict BFS order, distances and predecessors before running it.
Load the disconnected graph. Compare the result with and without “All components”: how many trees are in the forest?
Find a graph where BFS and DFS have the same order and one where they differ greatly. What role does neighbour order play?