Breadth-First Traversal and Shortest Path
Breadth-first traversal starts from a vertex v, first visits this node and marks it as visited, then sequentially visits all unvisited adjacent nodes {vi,..,vj} of node v and marks them as visited, then repeats the visiting method of node v for each node in {vi,...,vj}, until all nodes have been visited.
We can break it down into three steps:
- (1) Use an auxiliary queue q, first enqueue vertex v, mark it as visited, then loop to check whether the queue is empty.
- (2) If the queue is not empty, take the first element out of the queue, enqueue all unvisited nodes associated with that element, and mark those nodes as visited.
- (3) If the queue is empty, it means all nodes have been traversed in breadth-first order.
As shown in the figure below, the blue on the right indicates the order of traversing nodes starting from 0, and below records the distance from 0, showing that breadth-first traversal can find the shortest path in an unweighted graph.

The code below shows how to complete the traversal using breadth-first traversal and query the shortest path. Based on the code from the previous section, we add a global variable ord array to record the order of nodes in the path. ord[i] represents the order of node i in the path. The constructor is adjusted accordingly: when traversing adjacent nodes, each time an unvisited node is visited, ord[i] = ord[v] + 1 records the distance. The time complexity of breadth-first traversal for an adjacency list is O(V+E), and for an adjacency matrix it is O(V^2).
...
// Constructor, pathfinding algorithm, find paths from point s to other points in graph graph
public
ShortestPath
(
Graph graph,
int
s
)
{
// Algorithm initialization
G
=
graph
;
assert
s
=
0
&&
s
<
G.
V
(
)
;
visited
=
new
boolean
[
G.
V
(
)
]
;
from
=
new
int
[
G.
V
(
)
]
;
ord
=
new
int
[
G.
V
(
)
]
;
for
(
int
i
=
0
;
i
<
G.
V
(
)
;
i
++
)
{
visited
[
i
]
=
false
;
from
[
i
]
=
1
;
ord
[
i
]
=
1
;
}
this
.
s
=
s
;
// Shortest path algorithm for undirected graphs, breadth-first traverse the entire graph starting from s
LinkedList
<
Integer
q
=
new
LinkedList
<
Integer
(
);
q.
push
(
s
)
;
visited
[
s
]
=
true
;
ord
[
s
]
=
0
;
while
(
!
q.
isEmpty
(
)
)
{
int
v
=
q.
pop
(
)
;
for
(
int
i
:
G.
adj
(
v
)
)
if
(
!
visited
[
i
]
)
{
q.
push
(
i
)
;
visited
[
i
]
=
true
;
from
[
i
]
=
v
;
ord
[
i
]
=
ord
[
v
]
1
;
}
}
}
...
View the shortest path length from point s to point w; if w is unreachable from s, return -1.
...
public
int
length
(
int
w
)
{
assert
w
=
0
&&
w
<
G.
V
(
)
;
return
ord
[
w
]
;
}
...
Java Example Code
Source code package download:Download
src/runoob/graph/ShortestPath.java file code:
package
runoob.graph
;
import
runoob.graph.read.Graph
;
import
java.util.LinkedList
;
import
java.util.Stack
;
import
java.util.Vector
;
/**
* Breadth-First Traversal and Shortest Path
*/
public
class
ShortestPath
{
// Reference to the graph
private
Graph G
;
// Starting point
private
int
s
;
// Record whether a node is visited during dfs
private
boolean
[
]
visited
;
// Record the path, from[i] represents the previous node of i on the searched path
private
int
[
]
from
;
// Record the order of nodes in the path. ord[i] represents the order of node i in the path.
private
int
[
]
ord
;
// Constructor, pathfinding algorithm, find paths from point s to other points in graph graph
public
ShortestPath
(
Graph graph,
int
s
)
{
// Algorithm initialization
G
=
graph
;
assert
s
=
0
&&
s
<
G.
V
(
)
;
visited
=
new
boolean
[
G.
V
(
)
]
;
from
=
new
int
[
G.
V
(
)
]
;
ord
=
new
int
[
G.
V
(
)
]
;
for
(
int
i
=
0
;
i
<
G.
V
(
)
;
i
++
)
{
visited
[
i
]
=
false
;
from
[
i
]
=
1
;
ord
[
i
]
=
1
;
}
this
.
s
=
s
;
// Shortest path algorithm for undirected graphs, breadth-first traverse the entire graph starting from s
LinkedList
<
Integer
q
=
new
LinkedList
<
Integer
(
);
q.
push
(
s
)
;
visited
[
s
]
=
true
;
ord
[
s
]
=
0
;
while
(
!
q.
isEmpty
(
)
)
{
int
v
=
q.
pop
(
)
;
for
(
int
i
:
G.
adj
(
v
)
)
if
(
!
visited
[
i
]
)
{
q.
push
(
i
)
;
visited
[
i
]
=
true
;
from
[
i
]
=
v
;
ord
[
i
]
=
ord
[
v
]
1
;
}
}
}
// Query whether there is a path from point s to point w
public
boolean
hasPath
(
int
w
)
{
assert
w
=
0
&&
w
<
G.
V
(
)
;
return
visited
[
w
]
;
}
// Query the path from point s to point w, stored in vec
public
Vector
<
Integer
path
(
int
w
)
{
assert
hasPath
(
w
);
Stack
<
Integer
s
=
new
Stack
<
Integer
(
);
// Find the path from s to w by tracing back through the from array, store it in a stack
int
p
=
w
;
while
(
p
!=
1
)
{
s.
push
(
p
)
;
p
=
from
[
p
]
;
}
// Take elements out of the stack one by one to obtain the path from s to w in order
Vector
<
Integer
res
=
new
Vector
<
Integer
(
);
while
(
!
s.
empty
(
)
)
res.
add
(
s.
pop
(
)
)
;
return
res
;
}
// Print the path from point s to point w
public
void
showPath
(
int
w
)
{
assert
hasPath
(
w
)
;
Vector
<
Integer
vec
=
path
(
w
);
for
(
int
i
=
0
;
i
<
vec.
size
(
)
;
i
++
)
{
System
.
out
.
(
vec.
elementAt
(
i
)
)
;
if
(
i
==
vec.
size
(
)
1
)
System
.
out
.
println
(
)
;
else
System
.
out
.
(
" -> "
)
;
}
}
// View the shortest path length from point s to point w
// If w is unreachable from s, return -1
public
int
length
(
int
w
)
{
assert
w
=
0
&&
w
<
G.
V
(
)
;
return
ord
[
w
]
;
}
}