# Maximal Independent Set in an Undirected Graph

• Last Updated : 27 Oct, 2022

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}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## Java

 `// Java program for above approach` `import` `java.util.*;`   `public` `class` `Solution {`   `  ``// Recursive Function to find the` `  ``// Maximal Independent Vertex Set` `  ``static` `ArrayList` `    ``graphSets(HashMap > graph)` `  ``{` `    ``// Base Case - Given Graph` `    ``// has no nodes` `    ``if` `(graph.size() == ``0``) {` `      ``return` `new` `ArrayList<>();` `    ``}`   `    ``// Base Case - Given Graph` `    ``// has 1 node` `    ``if` `(graph.size() == ``1``) {` `      ``return` `new` `ArrayList(graph.keySet());` `    ``}` `    ``// Select a vertex from the graph` `    ``int` `vCurrent` `      ``= (``new` `ArrayList(graph.keySet()))` `      ``.get(``0``);`   `    ``// Case 1 - Proceed removing` `    ``// the selected vertex` `    ``// from the Maximal Set` `    ``HashMap > graph2` `      ``= ``new` `HashMap<>(graph);`   `    ``// Delete current vertex` `    ``// from the Graph` `    ``graph2.remove(vCurrent);`   `    ``// Recursive call - Gets` `    ``// Maximal Set,` `    ``// assuming current Vertex` `    ``// not selected` `    ``ArrayList res1 = graphSets(graph2);`   `    ``// Case 2 - Proceed considering` `    ``// the selected vertex as part` `    ``// of the Maximal Set`   `    ``// Loop through its neighbours` `    ``for` `(Integer v : graph.get(vCurrent)) {`   `      ``// Delete neighbor from` `      ``// the current subgraph` `      ``if` `(graph2.containsKey(v)) {` `        ``graph2.remove(v);` `      ``}` `    ``}`   `    ``// This result set contains VFirst,` `    ``// and the result of recursive` `    ``// call assuming neighbors of vFirst` `    ``// are not selected` `    ``ArrayList res2 = ``new` `ArrayList<>();` `    ``res2.add(vCurrent);` `    ``res2.addAll(graphSets(graph2));`   `    ``// Our final result is the one` `    ``// which is bigger, return it` `    ``if` `(res1.size() > res2.size())` `      ``return` `res1;` `    ``return` `res2;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``int` `V = ``8``;`   `    ``// Defines edges` `    ``int``[][] E = { { ``1``, ``2` `}, { ``1``, ``3` `}, { ``2``, ``4` `},` `                 ``{ ``5``, ``6` `}, { ``6``, ``7` `}, { ``4``, ``8` `} };`   `    ``HashMap > graph` `      ``= ``new` `HashMap<>();`   `    ``// Constructs Graph as a dictionary` `    ``// of the following format-`   `    ``// graph[VertexNumber V]` `    ``// = list[Neighbors of Vertex V]` `    ``for` `(``int``[] e : E) {` `      ``int` `v1 = e[``0``];` `      ``int` `v2 = e[``1``];` `      ``if` `(!graph.containsKey(v1)) {` `        ``graph.put(v1, ``new` `ArrayList<>());` `      ``}` `      ``if` `(!graph.containsKey(v2)) {` `        ``graph.put(v2, ``new` `ArrayList<>());` `      ``}` `      ``graph.get(v1).add(v2);` `      ``graph.get(v2).add(v1);` `    ``}`   `    ``// Recursive call considering` `    ``// all vertices in the maximum` `    ``// independent set` `    ``ArrayList maximalIndependentSet` `      ``= graphSets(graph);`   `    ``// Prints the Result` `    ``for` `(Integer i : maximalIndependentSet)` `      ``System.out.print(i + ``" "``);` `  ``}` `}`   `// This code is contributed by karandeep1234.`

## 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 ``=``" "``)`

Output:

`2 3 8 5 7`

Time Complexity: O(2N) Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles