# Largest subset of Graph vertices with edges of 2 or more colors

Given an undirected complete graph with **N** nodes or vertices. Edges of the graph are colored, find the largest subset of vertices with edges of 2 or more colors. We are given graph as adjacency matrix C[][] where C[i][j] is color of edge from vertex i to vertex j. Since graph is undirected, values C[i][j] of C[j][i] are same.

We define C[i][i] to be zero, although there is no such edge present. i.e. the graph does not contain self-loops.

Examples:

Input : C[][]= {{0, 1, 2}, {1, 0, 3}, {2, 3, 0}} Output : 3Example 1:Input : C[][]= {{0, 1, 1}, {1, 0, 3}, {1, 3, 0}} Output : 0Example 2:

Since graph is complete, each edge can be one of n*(n-1)/2 +1 different colors. These colors are labeled from 0 to n*(n-1)/2, inclusive. But not all these n*(n-1)/2 +1 colors need to be used. i.e., it is possible that two different edges could have the same color.

Let’s call a vertex “bad” if all its neighbors are of the same color. Obviously, we can’t have any such bad vertex in our subset, so remove such bad vertex from the graph. This might introduce some more bad vertices, but we can keep repeating this process until we find a subset free of bad vertices. So, at last, we should remain through a graph which does not have any bad vertex means every vertex of our subset has at least two different color edges with other adjacent vertices.

**Example:**

__Input : __

let C[6][6]:

{{0, 9, 2, 4, 7, 8},

{9, 0, 9, 9, 7, 9},

{2, 9, 0, 3, 7, 6},

{4, 9, 3, 0, 7, 1},

{7, 7, 7, 7, 0, 7},

{8, 9, 6, 1, 7, 0}};

__Step I:__ First of all, we can see that row 5(node ‘e’) contains only 7 means node ‘e’ is connected through edges having color code 7 so it does not have more than one color edge so we have to remove 5 from subset. Now, our graph will contain only 5 vertex and are as:

C[5][5]:

{{0, 9, 2, 4, 8},

{9, 0, 9, 9, 9},

{2, 9, 0, 3, 6},

{4, 9, 3, 0, 1},

{8, 9, 6, 1, 0}};

__Step II:__ Further, we can see that row 2 (node ‘b’) also doesn’t contain more than 1 color edge, so we should remove row 2 and column 2 also. Which result in our new graph as:

C[4][4]:

{{0, 2, 4, 8},

{2, 0, 3, 6},

{4, 3, 0, 1},

{8, 6, 1, 0}};

__Step III:__ Now, we can see that each vertex has more than 1 different color edge. So, the total number of vertices in the subset is 4.

## C++

`// C++ program to find size of subset of graph vertex ` `// such that each vertex has more than 1 color edges ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Number of vertices ` `const` `int` `N = 6; ` ` ` `// function to calculate max subset size ` `int` `subsetGraph(` `int` `C[][N]) ` `{ ` ` ` `// set for number of vertices ` ` ` `set<` `int` `> vertices; ` ` ` `for` `(` `int` `i = 0; i < N; ++i) ` ` ` `vertices.insert(i); ` ` ` ` ` `// loop for deletion of vertex from set ` ` ` `while` `(!vertices.empty()) ` ` ` `{ ` ` ` `// if subset has only 1 vertex return 0 ` ` ` `if` `(vertices.size() == 1) ` ` ` `return` `1; ` ` ` ` ` `// for each vertex iterate and keep removing ` ` ` `// a vertix while we find a vertex with all ` ` ` `// edges of same color. ` ` ` `bool` `someone_removed = ` `false` `; ` ` ` `for` `(` `int` `x : vertices) ` ` ` `{ ` ` ` `// note down different color values ` ` ` `// for each vertex ` ` ` `set<` `int` `> values; ` ` ` `for` `(` `int` `y : vertices) ` ` ` `if` `(y != x) ` ` ` `values.insert(C[x][y]); ` ` ` ` ` `// if only one color is found ` ` ` `// erase that vertex (bad vertex) ` ` ` `if` `(values.size() == 1) ` ` ` `{ ` ` ` `vertices.erase(x); ` ` ` `someone_removed = ` `true` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no vertex was removed in the ` ` ` `// above loop. ` ` ` `if` `(!someone_removed) ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `return` `(vertices.size()); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `C[][N] = {{0, 9, 2, 4, 7, 8}, ` ` ` `{9, 0, 9, 9, 7, 9}, ` ` ` `{2, 9, 0, 3, 7, 6}, ` ` ` `{4, 9, 3, 0, 7, 1}, ` ` ` `{7, 7, 7, 7, 0, 7}, ` ` ` `{8, 9, 6, 1, 7, 0} ` ` ` `}; ` ` ` `cout << subsetGraph(C); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find size of ` `// subset of graph vertex such that ` `// each vertex has more than 1 color edges ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Number of vertices ` ` ` `static` `int` `N = ` `6` `; ` ` ` ` ` `// function to calculate max subset size ` ` ` `static` `int` `subsetGraph(` `int` `C[][]) ` ` ` `{ ` ` ` `// set for number of vertices ` ` ` `HashSet<Integer> vertices = ` `new` `HashSet<>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < N; ++i) ` ` ` `{ ` ` ` `vertices.add(i); ` ` ` `} ` ` ` ` ` `// loop for deletion of vertex from set ` ` ` `while` `(!vertices.isEmpty()) ` ` ` `{ ` ` ` ` ` `// if subset has only 1 vertex return 0 ` ` ` `if` `(vertices.size() == ` `1` `) ` ` ` `{ ` ` ` `return` `1` `; ` ` ` `} ` ` ` ` ` `// for each vertex iterate and keep removing ` ` ` `// a vertix while we find a vertex with all ` ` ` `// edges of same color. ` ` ` `boolean` `someone_removed = ` `false` `; ` ` ` `for` `(` `int` `x : vertices) ` ` ` `{ ` ` ` ` ` `// note down different color values ` ` ` `// for each vertex ` ` ` `HashSet<Integer> values = ` `new` `HashSet<>(); ` ` ` `for` `(` `int` `y : vertices) ` ` ` `{ ` ` ` `if` `(y != x) ` ` ` `{ ` ` ` `values.add(C[x][y]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// if only one color is found ` ` ` `// erase that vertex (bad vertex) ` ` ` `if` `(values.size() == ` `1` `) ` ` ` `{ ` ` ` `vertices.remove(x); ` ` ` `someone_removed = ` `true` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no vertex was removed in the ` ` ` `// above loop. ` ` ` `if` `(!someone_removed) ` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `(vertices.size()); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `C[][] = {{` `0` `, ` `9` `, ` `2` `, ` `4` `, ` `7` `, ` `8` `}, ` ` ` `{` `9` `, ` `0` `, ` `9` `, ` `9` `, ` `7` `, ` `9` `}, ` ` ` `{` `2` `, ` `9` `, ` `0` `, ` `3` `, ` `7` `, ` `6` `}, ` ` ` `{` `4` `, ` `9` `, ` `3` `, ` `0` `, ` `7` `, ` `1` `}, ` ` ` `{` `7` `, ` `7` `, ` `7` `, ` `7` `, ` `0` `, ` `7` `}, ` ` ` `{` `8` `, ` `9` `, ` `6` `, ` `1` `, ` `7` `, ` `0` `} ` ` ` `}; ` ` ` `System.out.println(subsetGraph(C)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Breadth First Search or BFS for a Graph
- Find if there is a path between two vertices in a directed graph
- Depth First Search or DFS for a Graph
- Detect Cycle in a Directed Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Graph and its representations
- Transitive closure of a graph
- Check whether a given graph is Bipartite or not
- Shortest Path in Directed Acyclic Graph
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Eulerian path and circuit for undirected graph
- Longest Path in a Directed Acyclic Graph