Given an undirected graph defined by the number of vertex V and the edges E[ ], the task is to find Maximal Independent Vertex Set in an undirected graph.
Independent Set: An independent set in a graph is a set of vertices which are not directly connected to each other.
Note: It is a given that there is at least one way to traverse from any vertex in the graph to another, i.e. the graph has one connected component.
Examples:
Input: V = 3, E = { (1, 2), (2, 3) }
Output: {1, 3}
Explanation:
Since there are no edges between 1 and 3, and we cannot add 2 to this since it is a neighbour of 1, this is the Maximal Independent Set.
Input: V = 8,
E = { (1, 2), (1, 3), (2, 4), (5, 6), (6, 7), (4, 8) }
Output: {2, 3, 5, 7, 8}
Approach:
This problem is an NP-Hard problem, which can only be solved in exponential time(as of right now).
Follow the steps below to solve the problem:
- Iterate through the vertices of the graph and use backtracking to check if a vertex can be included in the Maximal Independent Set or not.
- Two possibilities arise for each vertex, whether it can be included or not in the maximal independent set.
- Initially start, considering all vertices and edges. One by one, select a vertex. Remove that vertex from the graph, excluding it from the maximal independent set, and recursively traverse the remaining graph to find the maximal independent set.
- Otherwise, consider the selected vertex in the maximal independent set and remove all its neighbors from it. Proceed to find the maximal independent set possible excluding its neighbors.
- Repeat this process for all vertices and print the maximal independent set obtained.
Below is the implementation of the above approach:
Python3
# Python Program to implement # the above approach # Recursive Function to find the # Maximal Independent Vertex Set def graphSets(graph): # Base Case - Given Graph # has no nodes if ( len (graph) = = 0 ): return [] # Base Case - Given Graph # has 1 node if ( len (graph) = = 1 ): return [ list (graph.keys())[ 0 ]] # Select a vertex from the graph vCurrent = list (graph.keys())[ 0 ] # Case 1 - Proceed removing # the selected vertex # from the Maximal Set graph2 = dict (graph) # Delete current vertex # from the Graph del graph2[vCurrent] # Recursive call - Gets # Maximal Set, # assuming current Vertex # not selected res1 = graphSets(graph2) # Case 2 - Proceed considering # the selected vertex as part # of the Maximal Set # Loop through its neighbours for v in graph[vCurrent]: # Delete neighbor from # the current subgraph if (v in graph2): del graph2[v] # This result set contains VFirst, # and the result of recursive # call assuming neighbors of vFirst # are not selected res2 = [vCurrent] + graphSets(graph2) # Our final result is the one # which is bigger, return it if ( len (res1) > len (res2)): return res1 return res2 # Driver Code V = 8 # Defines edges E = [ ( 1 , 2 ), ( 1 , 3 ), ( 2 , 4 ), ( 5 , 6 ), ( 6 , 7 ), ( 4 , 8 )] graph = dict ([]) # Constructs Graph as a dictionary # of the following format- # graph[VertexNumber V] # = list[Neighbors of Vertex V] for i in range ( len (E)): v1, v2 = E[i] if (v1 not in graph): graph[v1] = [] if (v2 not in graph): graph[v2] = [] graph[v1].append(v2) graph[v2].append(v1) # Recursive call considering # all vertices in the maximum # independent set maximalIndependentSet = graphSets(graph) # Prints the Result for i in maximalIndependentSet: print (i, end = " " ) |
2 3 8 5 7
Time Complexity: O(2^{N})
Auxiliary Space: O(N)
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 from a given Graph using Backtracking
- Find if an undirected graph contains an independent set of a given size
- Proof that Independent Set in Graph theory is NP Complete
- 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
- Maximal Clique Problem | Recursive Solution
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Eulerian path and circuit for undirected graph
- Number of Triangles in an Undirected Graph
- Find k-cores of an undirected graph
- Clone an Undirected Graph
- Find minimum weight cycle in an undirected graph
- Count number of edges in an undirected graph
- Connected Components in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Cycles of length n in an undirected and connected graph
- Eulerian Path in undirected graph
- Number of single cycle components in an undirected graph
- Sum of the minimum elements in all connected components of 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 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.