I have a list of lists, each list is a node and contains the edges to other nodes. e.g
[[1, 1, 0], [1, 1, 0], [0, 0, 1]]
The node has a 1
when it refers to its own position, as well as when it has an edge to another node, and a 0
when no edge exists.
This means that node 0
([1, 1, 0]
) is connected to node 1
, and node 2
([0,0,1]
) is not connected to any other nodes. Therefore this list of lists can be thought of as an adjacency matrix:
1 1 0 <- node 0
1 1 0 <- node 1
0 0 1 <- node 2
Adding on to this, whether a node is connected with another is transitive, meaning that if node 1
is connected to node 2
and node 2
is connected to node 3
, nodes 1
and 3
are also connected (by transitivity).
Taking all this into account, I want to be able to know how many connected groups there are given a matrix. What algorithm should I use, recursive DFS? Can someone provide any hints or pseudocode as to how this problem can be approached?