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.
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’.
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’.
- 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:
Time Complexity: where V is the number of vertices in the graph and k is the given size of set.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find all cliques of size K in an undirected graph
- Find k-cores of an undirected graph
- Program to find Circuit Rank of an Undirected Graph
- Find minimum weight cycle in an undirected graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Find the maximum component size after addition of each edge to the graph
- Convert undirected connected graph to strongly connected directed graph
- Clone an Undirected Graph
- Detect cycle in an undirected graph using BFS
- Detect cycle in an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Eulerian Path in undirected graph
- Number of Triangles in an Undirected Graph
- Print all the cycles in an 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 email@example.com. 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.
Improved By : sanjeev2552