Connected Groups (Friend Circles)
Problem
Given an n × n binary relation matrix supplied as strings (related[i][j] = '1' means person i directly knows person j), and treating friendship as transitive, count the number of connected friend groups (circles).
Input / Output
- Input: a string array
relatedof n rows, each n characters of'0'/'1'. - Output: the number of friend groups.
Constraints
- n ≤ 300, so an O(n^2) scan of the matrix is fine.
- The relation is symmetric and transitive;
related[i][i] = '1'.
Example
["110","110","001"]→ 2 (persons 0 and 1 form one circle; person 2 is alone).
asked …