0

Say I have an adjacency matrix

boolean[][] adjMatrix = [n][n]

and I want a function that returns a boolean[] of all the connected components of a given vertex.

public static boolean[] connected(boolean[][] aMatrix, int vertexCount, int givenVertex){

boolean[] connections = [vertexCount]
connections[givenVertex] = true;

for(int i = 0; i < vertexCount, i++){
If(...) connections[i] = true; 
...
}

return connections;
}

For example on this graph

pNu6Iyn

connected(adjMatrix, 6, 0)
//returns [true, true, false, false, true, true, false]

I've been working on this for a while, and I think I need to use Breadth First Search or Depth First Search, but I am still kind of confused on using them.

user1546716
  • 101
  • 1
  • 7
  • How are you supposed to represent all connected components as just a `boolean[]`? Do you mean you want to figure out which nodes are connected to a *specific* node? – user2357112 Apr 11 '14 at 04:45
  • For any vertex I input (givenVertex), I would like to return a boolean[] of each vertex that is "reachable" by this vertex. So the true values would be all the vertices in the same connected graph of the given vertex. Such as for the vertex 0 it would return [true, true, false, false, true, true, false] because they are all in the same connected graph (Vertex 2, 6, 3 would be "unreachable" and be false. If there was an edge between 1 and 6, all values in the boolean[] would be true. Sorry if I said it in a vague way, is it not possible to do this? – user1546716 Apr 11 '14 at 05:06
  • Start a breadth-first search at the node (or depth-first, or whatever your favorite search algorithm is). Mark every node you find reachable. That's pretty much it. Do you know how to implement a breadth-first search? (It may help to just implement *a* search, without thinking about whether it's depth- or breadth-first.) – user2357112 Apr 11 '14 at 05:09

0 Answers0