0

Given a squared matrix where each row represent an individual and each cell whether that person gave a gift to another one. For Example (0,1) -> 1 means individual 0 gave something to individual 1, thus, (1,0) is also -> 1. The goal is to create a function such that it creates groups base on the gift of each individual following the logic that if ind 1 gave something to ind 2 and ind 2 gave something to ind 3 then ind 1 through 3 are part of that group.

The input is a matrix as follow:

input
[
    [1,1,0,0], 
    [1,1,1,0], 
    [0,1,1,0], 
    [0,0,0,1]
]

output -> [[0,1,2], [3]]

I got this question in an interview but I could solve it, I'm not looking for an answer but for pointers on what do i need to know in order to solve it.

Thanks

Steven Daniel Anderson
  • 1,435
  • 2
  • 11
  • 17
  • Treat this as a graph: matrix representation and just do BFS for grouping – vish4071 Mar 23 '22 at 22:22
  • 1
    Does this answer your question? [Find connected components in a graph](https://stackoverflow.com/questions/21078445/find-connected-components-in-a-graph) –  Mar 23 '22 at 22:28

0 Answers0