0

I want to group non-connected vertices in a graph and minimize the number of these groups. In another word, given a collection of vertices, divide them into minimum number of groups but at the same time no two connected vertices in the same group

Example

here

Answer should be 3 groups: the first: 0, 3 the second: 1 the third: 2

I tried to solve it using brute force by storing the graph into adjacency matrix but it's not working.

for (int i = 0; i < n; i++) {
    if (!vis[i]) {
        for (int j = 0; j < n; j++) {
            if (!adjMat[i][j] && !vis[j]) {
                vis[j] = true;
            }
        }
        vis[i] = true;
        groups++;
    }
}
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 2
    You want to find chromatic number (https://en.wikipedia.org/wiki/Graph_coloring), there's no polynomial solution – fas Oct 21 '20 at 02:45
  • assuming you have a square matrix, try doing an exclusive or with 1s on one side of the diagonal of the matrix. What you are left over are 1's representing the nodes that arent connected. –  Oct 21 '20 at 02:49
  • ^ Like fas said, NP-complete problem. Hit up the Fields medal guys if you find an efficient solution. – Anonymous1847 Oct 21 '20 at 03:23
  • This is the clique cover problem for the complement of the graph. https://en.wikipedia.org/wiki/Clique_cover – Dave Oct 21 '20 at 03:25

0 Answers0