Graph Ready
vertices 8
edges 10
visited 0
  • Current
  • Waiting
  • Visited
  • Path
  • 1distance (BFS) / visit order (DFS)
Stored as

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Visit everything once

To explore a graph you need two helpers: a VISITED mark for every vertex (graphs can have cycles, so without marks the search would go round forever) and a place to remember which vertices are waiting. BFS keeps them in a QUEUE (nearest first); DFS keeps them on the call STACK (deepest first). Both read the neighbours of a vertex from an adjacency list.

int visited[V];          // 0 = not seen yet

// BFS: first in, first out
visited[s] = 1;  enqueue(s);
while (!isEmpty()) {
    int u = dequeue();
    for (each neighbour v of u)
        if (!visited[v]) { visited[v] = 1; enqueue(v); }
}

// DFS: go deep first (the call stack remembers the way back)
void dfs(int u) {
    visited[u] = 1;
    for (each neighbour v of u)
        if (!visited[v]) dfs(v);
}

BFS (breadth-first)

  1. Mark the start as seen; put it in a QUEUE.
  2. Take the front vertex and visit it.
  3. Enqueue every neighbour not seen yet.
  4. Repeat until the queue is empty — level by level.

DFS (depth-first)

  1. Visit the vertex and mark it.
  2. For each neighbour not visited yet: DFS it now.
  3. Only when all neighbours are done, go back.
  4. Recursion (or a STACK) remembers the way back.

Mark vertices!

  1. Graphs can have cycles: A–B–C–A.
  2. Without a "visited" mark you would loop forever.
  3. Each vertex is visited once, each edge checked once (twice if undirected).
  4. So both searches are O(V + E).

Shortest path (unweighted)

  1. Run BFS from the start.
  2. Store dist[v] = dist[u] + 1 and parent[v] = u.
  3. BFS reaches every vertex by a shortest route first.
  4. Follow parent[] back from the target to read the path.

Where graphs are used

  1. Maps and navigation (road networks).
  2. Social networks: friends of friends.
  3. The web: pages linked to pages.
  4. Task planning: which job must come first.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 BFS(G, s)
2 mark s as visited; Q ← queue with s
3 while Q is not empty
4 u ← DEQUEUE(Q); visit u
5 for each neighbour v of u
6 if v is not visited
7 mark v as visited; ENQUEUE(Q, v)
Complexity

How fast is each operation?

V = number of vertices, E = number of edges. With an adjacency list every search is O(V + E); with a matrix finding the neighbours of a vertex costs O(V), so the search becomes O(V²).

Operation Best Average Worst Speed
BFS (adjacency list) O(V + E) O(V + E) O(V + E) Grows with n
DFS (adjacency list) O(V + E) O(V + E) O(V + E) Grows with n
BFS / DFS (matrix) O(V²) O(V²) O(V²) Grows with n
Is there an edge u–v? (matrix) O(1) O(1) O(1) Instant
Is there an edge u–v? (list) O(1) O(degree) O(V) Grows with n

BFS vs DFS

BFS DFS
Helper structure Queue (FIFO) Stack / recursion (LIFO)
Order Level by level, nearest first One branch as deep as possible
Shortest path (unweighted)? Yes No
Typical uses Shortest routes, "friends of friends" Cycle detection, mazes, topological sort
Time O(V + E) O(V + E)
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

Which data structure does BFS use to decide which vertex to visit next?

Question 2

Why do BFS and DFS mark vertices as visited?

Question 3

An undirected graph has 5 edges. How many entries does its adjacency list have in total?

Question 4

Which search finds the path with the fewest edges in an unweighted graph?

Question 5

A graph has 1,000 vertices and only 2,000 edges. Which representation uses less memory?

Question 6

What is the time complexity of BFS or DFS with an adjacency list?

Assessment

Graphs quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.