Open In App

Java Program to Find Size of the Largest Independent Set(LIS) in a Given an N-array Tree

Last Updated : 06 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Basically, an N-array tree is such a tree structure in which each node can have a max of up to ‘N’ number of children. The largest independent set is a set of vertices in which no two vertexes are adjacent to each other. So basically in this program, we have to see that how can we find the size of such a largest set in the N-array tree. Here we are implementing the tree using a vector in java and its idea of adjacency list from the graph theory is used. 

The algorithm goes as:

What mainly we are going to do is that we will create a LIS(int a) method to which the number of the current node is passed. This is a recursive method. In this method, we will check the base conditions first. Before discussing the base conditions, we have to first see the rest part of the method. 

So in the code, for each node, we mainly see that whether including that node inset gives the larger set or not. If not then that particular node is not included but since this node is not included, so we have to repeat the same process for its children. But if the node is included then we cannot include its children, so we have to check for its grandchildren the same thing ad done for the parent, and in this way we can get to know whether to include this node or not.

Now coming to the base cases: 

  1. If the node is null, which means the node does not exist then we cannot include anything.
  2. If the node is a leaf then we must include it.

Why to always include those nodes which are leaf node, when method is called upon them?

The main part to think here is that some node may be passed to this method only if its parent are not included in the set except the root one. So if parents are not there in set and this node is leaf one then we should include this node in the set. This is basically a greedy approach.

We will also be making a small change in our code to print the set elements also.

Java




// Java Program to Find Size of the Largest Independent
// Set(LIS) in a Given an N-array Tree
import java.io.*;
import java.util.*;
 
// save the file named as GFG2.java
public class GFG2 {
   
    // declaring static list of vector.
    public static Vector<Vector<Integer> > v
        = new Vector<Vector<Integer> >();
   
    // 7 nodes initially
    public static int n = 7;
    public static void main(String[] args)
    {
        System.out.println("Initializing an n array tree");
 
        Vector<Integer> v0 = new Vector<Integer>();
        v0.add(1);
        v0.add(2);
        v.add(v0);
        Vector<Integer> v1 = new Vector<Integer>();
        v1.add(3);
        v1.add(4);
        v.add(v1);
        Vector<Integer> v2 = new Vector<Integer>();
        v2.add(5);
        v.add(v2);
        Vector<Integer> v3 = new Vector<Integer>();
        v3.add(6);
        v.add(v3);
        Vector<Integer> v4 = new Vector<Integer>();
        v.add(v4);
        v.add(v4);
        v.add(v4);
 
        /*
        the tree looks like
                                                0
                                        /        \
                                        1        2
                                         /\       /
                                         3  4      5
                                        /
                                        6
        so initially the vector looks like
        v = {
                {1,2},
                {3,4},
                {5},
                {6},
                {},
                {},
                {},
        }
        */
 
        System.out.println(
            "Finding the elements to be included in the set");
       
        // calling the function with the first node.
        int x = LIS(0);
        System.out.println("process finished and size is "
                           + x);
    }
 
    public static int LIS(int a)
    {
        // if no node is there with labelling a
        if (a >= n)
            return 0;
       
        // if it is leaf node
        if (v.get(a).size() == 0) {
            System.out.println(a);
            return 1;
        }
        // if not considering that node
        int ifno = 0;
       
        // if considering that node.
        int ifyes = 1;
       
        // since not considering this node
        // so we should call the same function
        // on the children of this node
        for (int i = 0; i < v.get(a).size(); ++i)
            ifno += LIS(v.get(a).get(i));
       
        // if including this node
        // then call the same function recursively on the
        // grand children of this node.
        for (int i = 0; i < v.get(a).size(); ++i) {
            int k = v.get(v.get(a).get(i)).size();
            --k;
            while (k >= 0) {
                ifyes += LIS(v.get(v.get(a).get(i)).get(k));
                --k;
            }
        }
       
        // if found that including this node is beneficial
        if (ifyes > ifno)
            System.out.println(a);
        return Math.max(ifyes, ifno);
    }
}


 
 

Output

Initializing an n array tree
Finding the elements to be included in the set
6
4
6
5
4
6
5
0
process finished and size is 4

Since the output shows the elements of the set more than once, so we have to basically implement a map or a set datatype to get the elements uniquely.

Second thing to note is that this method is recursive approach so this can lead to exponential time complexity. This method can be enhanced using DP approach. We can achieve memorization through map data structure.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads