Skip to content
Related Articles

Related Articles

Java Program to Find the Largest Independent Set in a Graph by Complements
  • Last Updated : 09 Feb, 2021

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. 

Approach :

  • 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.

Solution :

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.

Java




static class pair {
    int first, second;
    pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
    @Override public String toString()
    {
        return "(" + first + "," + second + ")";
    }
}
  • 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.

Java




// Java Program to Find the Largest Independent Set in a
// Graph by Complements
  
import java.util.*;
class GFG {
    static ArrayList<Integer> vertices = new ArrayList<>();
    static HashMap<pair, Integer> edges = new HashMap<>();
    
    static HashSet<ArrayList<Integer> > independentSets = new HashSet<>();
  
    public static void main(String args[])
    {
        int numOfVertices = 4, numOfEdges = 0;
        for (int i = 1; i <= numOfVertices; i++)
            vertices.add(i);
  
        HashSet<Integer> SolnSet = new HashSet<>();
        
        // this function call adds all sets to the global
        // solution set
        findAllIndependentSets(1, numOfVertices, SolnSet);
        
        // to find the largest result set
        ArrayList<Integer> Max = new ArrayList<>();
  
        for (ArrayList<Integer> i : independentSets) {
            System.out.println(i);
            
            // comparing lengths of all
            // independent sets
            if (i.size() > Max.size()) 
                Max = i;
        }
  
        System.out.println("Maximal Independent Set = "
                           + Max);
    }
    
    // this function finds all independent sets and
    // adds them to the solution object
    static void findAllIndependentSets(int currentVertice, int setSize,
                           HashSet<Integer> SolnSet)
    {
        for (int i = currentVertice; i <= setSize; i++) 
        {
            // checking if vertex is independent
            if (checkSafety(vertices.get(i - 1), SolnSet)) {
                
                // adding to the temporary solution set
                SolnSet.add(vertices.get(i - 1));
                
                findAllIndependentSets(i + 1, setSize,
                                       SolnSet);
                
                // removing previous set
                SolnSet.remove(vertices.get(i - 1));
            }
        }
        
        // appending the temporary solution set to the
        // solution object
        independentSets.add(new ArrayList<Integer>(SolnSet));
    }
    
    // this function checks if there exists an edge between
    // 2 vertices
    static boolean checkSafety(int vertex,
                               HashSet<Integer> SolnSet)
    {
        for (int i : SolnSet) {
            if (edges.containsKey(new pair(i, vertex)))
                
                // if there is an edge, return false
                return false;
        }
        
        // if vertex is independent , return true
        return true;
    }
    
    // user-define pair class
    static class pair {
        int first, second;
        pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
        @Override public String toString()
        {
            return "(" + first + "," + second + ")";
        }
    }
}
Output
[1]
[1, 2, 3]
[1, 3, 4]
[2]
[]
[1, 2, 4]
[1, 2]
[2, 3, 4]
[2, 3]
[3, 4]
[3]
[1, 3]
[2, 4]
[4]
[1, 4]
[1, 2, 3, 4]
Maximal Independent Set = [1, 2, 3, 4]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :