Given an undirected graph, check if it contains an independent set of size **k**. Print ‘Yes’ if there exists an independent set of size **k**. Print ‘No’ otherwise.

**Independent Set:** An independent set in a graph is a set of vertices which are not directly connected to each other.

**Example 1:**

Input : K = 4, graph = [[1, 1, 0, 0, 0], [1, 1, 1, 1, 1], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]] Output : Yes

The above graph contains an independent set of size 4 (vertices 1, 2, 3, 4 are not directly connected to each other). Hence the output is ‘Yes’.

**Example 2:**

Input : K = 4, graph = [[1, 1, 0, 0, 0], [1, 1, 1, 1, 1], [0, 1, 1, 0, 0], [0, 1, 0, 1, 1], [0, 1, 0, 1, 1]] Output : No

The above graph doesn’t contain an independent set of size 4. Hence output is ‘No’.

**Approach:**

- Initialize a variable
**sol**with boolean*False*value. - Find all the possible sets of vertices of size
**K**from the given graph. - If an independent set of size
**k**is found, change the value of**sol**to True and return. - Else continue checking for other possible sets.
- In the end, if
**sol**is True, print ‘Yes’ else print ‘No’.

Below is the implementation of the above approach:

## C++

`// C++ code to check if a given graph ` `// contains an independent set of size k ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function prototype ` `bool` `check(` `int` `[][5], vector<` `int` `> &, ` `int` `); ` ` ` `// Function to construct a set of given size k ` `bool` `func(` `int` `graph[][5], vector<` `int` `> &arr, ` ` ` `int` `k, ` `int` `index, ` `bool` `sol[]) ` `{ ` ` ` `// Check if the selected set is independent or not. ` ` ` `// Change the value of sol to True and return ` ` ` `// if it is independent ` ` ` `if` `(k == 0) ` ` ` `{ ` ` ` `if` `(check(graph, arr, arr.size())) ` ` ` `{ ` ` ` `sol[0] = ` `true` `; ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Set of size k can be formed even if we don't ` ` ` `// include the vertex at current index. ` ` ` `if` `(index >= k) ` ` ` `{ ` ` ` `vector<` `int` `> newvec(arr.begin(), arr.end()); ` ` ` `newvec.push_back(index); ` ` ` `return` `(func(graph, newvec, k - 1, ` ` ` `index - 1, sol) or ` ` ` `func(graph, arr, k, index - 1, sol)); ` ` ` `} ` ` ` ` ` `// Set of size k cannot be formed if we don't ` ` ` `// include the vertex at current index. ` ` ` `else` ` ` `{ ` ` ` `arr.push_back(index); ` ` ` `return` `func(graph, arr, k - 1, ` ` ` `index - 1, sol); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to check if the given set is ` `// independent or not ` `// arr --> set of size k (contains the ` `// index of included vertex) ` `bool` `check(` `int` `graph[][5], vector<` `int` `> &arr, ` `int` `n) ` `{ ` ` ` `// Check if each vertex is connected to any other ` ` ` `// vertex in the set or not ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) ` ` ` `if` `(graph[arr[i]][arr[j]] == 1) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `graph[][5] = {{1, 1, 0, 0, 0}, ` ` ` `{1, 1, 1, 1, 1}, ` ` ` `{0, 1, 1, 0, 0}, ` ` ` `{0, 1, 0, 1, 0}, ` ` ` `{0, 1, 0, 0, 1}}; ` ` ` `int` `k = 4; ` ` ` `vector<` `int` `> arr; ` `// Empty set ` ` ` `bool` `sol[] = {` `false` `}; ` ` ` `int` `n = ` `sizeof` `(graph) / ` ` ` `sizeof` `(graph[0]); ` ` ` `func(graph, arr, k, n - 1, sol); ` ` ` ` ` `if` `(sol[0]) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to check if a given graph ` `# contains an independent set of size k ` ` ` `# Function to construct a set of given size k ` `def` `func(graph, arr, k, index, sol): ` ` ` ` ` `# Check if the selected set is independent or not. ` ` ` `# Change the value of sol to True and return ` ` ` `# if it is independent ` ` ` `if` `k ` `=` `=` `0` `: ` ` ` `if` `check(graph, arr) ` `=` `=` `True` `: ` ` ` `sol[` `0` `] ` `=` `True` ` ` `return` ` ` ` ` `else` `: ` ` ` `# Set of size k can be formed even if we don't ` ` ` `# include the vertex at current index. ` ` ` `if` `index >` `=` `k: ` ` ` `return` `(func(graph, arr[:] ` `+` `[index], k` `-` `1` `, index` `-` `1` `, sol) ` `or` ` ` `func(graph, arr[:], k, index` `-` `1` `, sol)) ` ` ` ` ` `# Set of size k cannot be formed if we don't ` ` ` `# include the vertex at current index. ` ` ` `else` `: ` ` ` `return` `func(graph, arr[:] ` `+` `[index], k` `-` `1` `, index` `-` `1` `, sol) ` ` ` `# Function to check if the given set is ` `# independent or not ` `# arr --> set of size k (contains the ` `# index of included vertex) ` `def` `check(graph, arr): ` ` ` ` ` `# Check if each vertex is connected to any other ` ` ` `# vertex in the set or not ` ` ` `for` `i ` `in` `range` `(` `len` `(arr)): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, ` `len` `(arr)): ` ` ` ` ` `if` `graph[arr[i]][arr[j]] ` `=` `=` `1` `: ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver Code ` `graph ` `=` `[[` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `0` `], ` ` ` `[` `1` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `], ` ` ` `[` `0` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `], ` ` ` `[` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `], ` ` ` `[` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `]] ` ` ` `k ` `=` `4` `arr ` `=` `[] ` `# Empty set ` `sol ` `=` `[` `False` `] ` ` ` `func(graph, arr[:], k, ` `len` `(graph)` `-` `1` `, sol) ` ` ` `if` `sol[` `0` `] ` `=` `=` `True` `: ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** where **V** is the number of vertices in the graph and **k** is the given size of set.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximal Independent Set in an Undirected Graph
- Maximal independent set from a given Graph using Backtracking
- Proof that Independent Set in Graph theory is NP Complete
- Find all cliques of size K in an undirected graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Perl | DBI(Database Independent Interface) Module Set - 1
- Perl - DBI(Database Independent) Module | Set - 2
- Mathematics | Independent Sets, Covering and Matching
- Find k-cores of an undirected graph
- Find minimum weight cycle in an undirected graph
- Program to find Circuit Rank of an Undirected Graph
- Find if there is a path between two vertices in an undirected graph
- Find any simple cycle in an undirected unweighted Graph
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Print all shortest paths between given source and destination in an undirected graph
- Minimize cost to color all the vertices of an Undirected Graph using given operation
- Eulerian path and circuit for undirected graph

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.