How Graphs Are Stored and Traversed in Java (DFS & BFS Explained)
Graphs are everywhere: social networks, maps, workflows, dependencies, and more.
In this post, we’ll see:
-
How to store a graph in Java
-
How to print the graph
-
How DFS (Depth-First Search) and BFS (Breadth-First Search) work
-
A simple implementation with main()
We’ll keep everything very simple and beginner-friendly.
1. The Example Graph
We’ll use this small undirected graph with 5 nodes: 0, 1, 2, 3, 4.
🔹 Visual graph (text-based)
🔹 Edges
We have these connections (edges):
-
0 — 1
-
0 — 3
-
1 — 2
-
1 — 4
This is an undirected graph, so:
-
If 0 is connected to 1 → 1 is also connected to 0.
2. How We Store the Graph in Java
The most common way to store a graph is using an Adjacency List.
2.1 What is an adjacency list?
For each node, we store a list of its neighbors.
For our graph:
2.2 Java data structure
We use:
-
graphis a list -
Each element of
graphis another list of integers -
graph.get(i)gives the list of neighbors for nodei
So:
2.3 Step-by-step: building the adjacency list
Assume we have n = 5 nodes: 0..4.
Step 1: Create the outer list
Now graph is empty:
Step 2: Create an empty neighbor list for each node
Now we have:
Step 3: Add edges (undirected)
For undirected edge 0 — 1:
For 0 — 3:
For 1 — 2:
For 1 — 4:
Now the adjacency list looks like:
3. Printing the Graph
We can print the adjacency list like this:
Output:
This gives a clear picture of how the graph is stored in memory.
4. DFS (Depth-First Search) – Step by Step
DFS means: go as deep as possible along one path before backtracking.
We will start DFS from node 0.
4.1 DFS idea in simple words
-
Start at a node (e.g., 0)
-
Visit it and mark as visited
-
For each neighbor:
-
If not visited → go into that neighbor (recursively)
-
-
When no unvisited neighbors → go back (backtrack)
4.2 Run DFS on our graph
Adjacency list reminder:
Start: DFS(0)
Let’s track:
-
visited[]= keeps track of nodes already seen -
output= order of printing
Step-by-step DFS from 0
-
Start at 0
-
Visit 0 →
visited[0] = true -
Output:
0 -
Neighbors:
[1, 3]
-
-
Go to neighbor 1 (first neighbor of 0)
-
Visit 1 →
visited[1] = true -
Output:
0 1 -
Neighbors of 1:
[0, 2, 4] -
0 is already visited → skip
-
Next: 2
-
-
Go to neighbor 2
-
Visit 2 →
visited[2] = true -
Output:
0 1 2 -
Neighbors of 2:
[1](already visited) -
No more new neighbors → go back to 1
-
-
Back at 1, next neighbor is 4
-
Visit 4 →
visited[4] = true -
Output:
0 1 2 4 -
Neighbors of 4:
[1](already visited) -
No more new neighbors → go back to 1 → back to 0
-
-
Back at 0, next neighbor is 3
-
Visit 3 →
visited[3] = true -
Output:
0 1 2 4 3 -
Neighbors of 3:
[0](already visited) -
Done
-
Final DFS order starting from 0:
4.3 DFS Java code
5. BFS (Breadth-First Search) – Step by Step
BFS means: visit nodes level by level, like waves going outwards.
We use a queue.
5.1 BFS idea in simple words
-
Start at a node and push it into a queue
-
While queue is not empty:
-
Remove (poll) one node
-
Visit it
-
Add all its unvisited neighbors to the queue
-
5.2 Run BFS from node 0
Adjacency list again:
Let’s track:
-
visited[] -
queue -
output
Initial:
Step 1:
-
Take from queue → node = 0
-
Output:
0 -
Neighbors:
[1, 3]-
1 not visited → mark visited, add to queue
-
3 not visited → mark visited, add to queue
-
Now:
Step 2:
-
Take from queue → node = 1
-
Output:
0 1 -
Neighbors of 1:
[0, 2, 4]-
0 already visited → skip
-
2 not visited → mark visited, add to queue
-
4 not visited → mark visited, add to queue
-
Now:
Step 3:
-
Take from queue → node = 3
-
Output:
0 1 3 -
Neighbors of 3:
[0](already visited) -
Queue:
[2, 4]
Step 4:
-
Take from queue → node = 2
-
Output:
0 1 3 2 -
Neighbors of 2:
[1](already visited) -
Queue:
[4]
Step 5:
-
Take from queue → node = 4
-
Output:
0 1 3 2 4 -
Neighbors of 4:
[1](already visited) -
Queue:
[]→ done
Final BFS order from 0:
5.3 BFS Java code
6. Full Simple Java Program (With main)
You can copy-paste this file and run directly.
No comments:
Post a Comment