基本上,N 数组树是这样一种树结构,其中每个节点最多可以有“N”个子节点。最大的独立集是一组顶点,其中没有两个顶点彼此相邻。所以基本上在这个程序中,我们要看到如何在N-array树中找到这样一个最大集合的大小。在这里,我们使用Java的向量来实现树,并使用了图论中的邻接表思想。
算法如下:
我们主要要做的是创建一个 LIS(int a) 方法,将当前节点的编号传递给该方法。这是一种递归方法。在此方法中,我们将首先检查基本条件。在讨论基本条件之前,我们必须先看看方法的其余部分。
所以在代码中,对于每个节点,我们主要看是否包含该节点 inset 会给出更大的集合。如果不是,则不包括该特定节点,但由于不包括该节点,因此我们必须为其子节点重复相同的过程。但是如果包含节点,那么我们就不能包含它的子节点,所以我们必须检查它的孙子节点,就像为父节点所做的一样,这样我们就可以知道是否包含这个节点。
现在来看看基本情况:
- 如果节点为空,这意味着该节点不存在,那么我们不能包含任何内容。
- 如果节点是叶子,那么我们必须包含它。
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.
我们还将在我们的代码中进行一些小的更改以打印 set 元素。
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 > v
= new Vector >();
// 7 nodes initially
public static int n = 7;
public static void main(String[] args)
{
System.out.println("Initializing an n array tree");
Vector v0 = new Vector();
v0.add(1);
v0.add(2);
v.add(v0);
Vector v1 = new Vector();
v1.add(3);
v1.add(4);
v.add(v1);
Vector v2 = new Vector();
v2.add(5);
v.add(v2);
Vector v3 = new Vector();
v3.add(6);
v.add(v3);
Vector v4 = new Vector();
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 wihe 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 recursivelly 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);
}
}
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.
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live