Given an adjacency matrix of an undirected graph, return the number of connected components (provinces). Pattern focus: Adjacency List. Convert the matrix representation to traversal-friendly neighbors or scan it carefully to discover components.
isConnected = adjacency matrix of an undirected graph
number of connected components
Example 1:
Input:
isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output:
2
Explanation:
Nodes {0,1} form one province and node 2 forms another.
Example 2:
Input:
isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output:
3
Explanation:
Each node is isolated.