Open In App

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. 

Approach :



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.




static class pair {
    int first, second;
    pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
    @Override public String toString()
    {
        return "(" + first + "," + second + ")";
    }
}




// 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]

Article Tags :