Shortest Distance in an Unweighted Adjacency-Matrix Graph
BCGForward Deploy Engineer · Technical ScreenLast updated
This problem tests implementing breadth-first search (BFS) on an unweighted, undirected graph represented as an adjacency matrix to find the shortest path distance between two vertices.
Shortest Distance in an Adjacency Matrix
You are given an undirected, unweighted graph with n vertices labeled from 0 to n - 1. The graph is represented by an n x n binary adjacency matrix adj_matrix, where adj_matrix[i][j] == 1 indicates an edge between vertex i and vertex j, and 0 indicates no direct edge.
Given two vertex indices src and dst, compute the minimum number of edges in a path connecting src to dst. If src == dst, the distance is 0. If no path exists, return -1.
Function Signature
def shortest_distance(adj_matrix: list[list[int]], src: int, dst: int) -> int:
pass
Input
adj_matrix: A 2D list of integers of sizen x n, whereadj_matrix[i][j] in (0, 1). The matrix is symmetric (adj_matrix[i][j] == adj_matrix[j][i]) and contains no self-loops (adj_matrix[i][i] == 0).src: An integer representing the starting vertex (0 <= src < n).dst: An integer representing the target vertex (0 <= dst < n).
Output
- An integer representing the shortest path length (number of edges) between
srcanddst, or-1if unreachable.
Constraints
1 <= n <= 10000 <= src, dst < nadj_matrix[i][i] == 0for all0 <= i < nadj_matrix[i][j] == adj_matrix[j][i]for all0 <= i, j < n
Examples
Example 1:
adj_matrix = [
[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 0]
]
src = 0
dst = 3
# Output: 2
# Explanation: The shortest paths are 0 -> 1 -> 3 or 0 -> 2 -> 3, both taking 2 edges.
Example 2:
adj_matrix = [
[0, 1, 0],
[1, 0, 0],
[0, 0, 0]
]
src = 0
dst = 2
# Output: -1
# Explanation: Vertex 2 is disconnected from vertex 0.
Example 3:
adj_matrix = [
[0, 1],
[1, 0]
]
src = 1
dst = 1
# Output: 0
# Explanation: Source and destination are identical.
Edge Cases to Consider
src == dst: Should immediately return0.- Disconnected components: Must return
-1if no path exists. - Dense graphs: Scanning each row in the adjacency matrix takes
O(n)time per vertex visited.
Hints
Hint 1
Breadth-first search (BFS) guarantees finding the minimum edge count in an unweighted graph because it visits nodes in non-decreasing order of their distance.
Hint 2
Because the graph is given as an adjacency matrix, finding the neighbors of node u requires scanning all n columns of row u.
Hint 3
Mark vertices as visited at the time they are enqueued rather than dequeued to prevent duplicate queue entries.
Follow-up questions
- How does the time complexity compare between an adjacency matrix and an adjacency list for sparse graphs?
- How would you modify this solution to return the actual sequence of vertices along the shortest path rather than just the edge count?
- How would bidirectional BFS improve average search time, and what termination condition would you use?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.