Skip to content
Related Articles

Related Articles

Improve Article
Maximal Independent Set in an Undirected Graph
  • Last Updated : 13 Jul, 2020

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.


Input: V = 3, E = { (1, 2), (2, 3) }
Output: {1, 3}
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}

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:


# 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] = []
# 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(2N)
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up
Recommended Articles
Page :