← Back to challenges

Number of Connected Regions

PythonHardalgorithmsarraysconditionslogic

Instructions

The function is given a square matrix of relations n by n. Each element is numbered from 0 to n - 1. An element i is directly connected to an element j if relations[i][j] == 1. Element A is connected to element B and element B is connected to C, then A is indirectly connected to C. A region consists of all directly and indirectly connected elements. Find the number of separate regions in the given matrix of relations.

Examples

n_regions([
  [1, 1, 0], [1, 1, 0], [0, 0, 1]
]) ➞ 2

# Elements 0 and 1 are connected, element 2 is not connected
# to any other. Thus 2 separate regions.
n_regions([
  [1, 0, 0], [0, 1, 0], [0, 0, 1]
]) ➞ 3

# The three elements are not connected. Thus 3 regions.
n_regions([
  [1, 0, 0, 1],
  [0, 1, 1, 0],
  [0, 1, 1, 1],
  [1, 0, 1, 1]
]) ➞ 1

# Element 0 is connected to 3, 1 and 2 are connected, 3 is
# connected to 2. Thus all are indirectly connected.
# Thus only 1 region.

Notes

The relations matrix is symmetric because if i is connected to j, then j is connected to i. Diagonal elements relations[i][i] == 1.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.