Facebook Problem



Facebook, LinkedIn and others have detailed data on how users are "connected" to each other. Suppose we say that two users are friends if there's a direct connection between them. Next, what we'd like to do is examine the data to identify communities, which we are going to define in the following way. Suppose the records show that "Ali" is a friend of "Bill", "Bill" is a friend of "Chen", and "Dave" is a friend of "Ella". Then, we can identify two communities: "Ali-Bill-Chen" and "Dave-Ella". Thus, a community includes pairs who may not be direct friends, but linked via a sequence of users who are friends.

The starting template for this problem is Communities.java.

Data will be provided in a 2D array of int's:


  static int findNumCommunities (int[][] friends)
  {
    // friends[i][j] == 1 if i is a friend of j.
    // friends[i][j] == 0 otherwise.
    // if friends[i][j] == 1 then friends[j][i] == 1
    
  }

Start by drawing pictures for some examples and then writing high-level pseudocode.