Java Program to Find the Largest Independent Set in a Graph by Complements
In a graph with V vertices and E edges, the LIS (Largest Independent Set) is the set of all the vertices in the graph which are not connected to each other by the E edges.
- We create a HashMap that has a pair of Integers and an Integer as parameters.
- The pair represents two vertices and the Integer represents the edge between those two vertices.
- We iterate through all our vertices and check if there exists an edge between two certain vertices. Failing this condition, we append those vertices to our result HashSet – independent sets.
- In this way, all sets of independent sets are added to the HashSet and in the last step, we find out the largest of them.
Taking a number of vertices and the number of edges as inputs, we can calculate the largest independent set in a graph.
We also need to define a user-defined pair class here which checks if there exists an edge between two vertices.
- In order to find the largest independent set in the graph, we can first find out all the independent sets in the graph and then separate the set with the maximum length for our answer.
 [1, 2, 3] [1, 3, 4]   [1, 2, 4] [1, 2] [2, 3, 4] [2, 3] [3, 4]  [1, 3] [2, 4]  [1, 4] [1, 2, 3, 4] Maximal Independent Set = [1, 2, 3, 4]