目录
2. Dynamic programming non-trivial solution
Problem Description
A challenge from Hackerrank under link Cut Tree | HackerRank :
Given a tree T with n nodes, how many subtrees (T') of T have at most K edges connected to (T-T’)?
Take below tree at Graph 1 as example, there are total 11 subtrees with nodes,{},{A,B,C,D},{A},{B},{C},{D},{A,B},{A,D},{B,C},{A,B,C}, and {A,B,D}. The number of edges connected can be interpreted as the number of edges are cut. For example subtree {A},which is formed by cut edge A-B and A-D. the number of edges cut is two,so the number of edges connected to remaining portion of the tree is 2.
Graph 1
1. Brute force solution
If you use brute force to solve this challenge, there are N-1 th power of 2 samples, which you need to check. The time complexity is definitely two large. Here I will not discuss this method further.
2. Dynamic programming non-trivial solution
Graph 2
With dynamic solution, simply put, take graph 2 as example, the number of zero to K cuts with A as root can be the combinations of zero to K cut of its subtrees with root B,C,D,E,and L. The number of 0 to K cut at root E and parent A is the the combinations of 0 to K cut of its subtrees root at F,G,and H.
The number of 0 cut is 2 that is composed of empty set and full set of the nodes of this tree. The number of 1 cut is 2*(N-1), it is the twice of the number of edges. Then the formula used to calculate the number of 0 to K cuts is 2+2*(N-1)+the number of the 2 to K cuts. It can be shrunk to 2*N+the number of the 2 to K cuts. Now we can focus on the calculation of the number of 2 to K cuts.
There are two categories of difference for the calculation of cuts based on a certain root:
1.The number of 2 to K cut based on root A, can be the combination of the cuts based on the number of zero to K cuts of its children. It is the sum of the number of the 2 to K cut of these combinations.
2.The number 2 to K of cut based on root except A, can be the combination of the cuts based on the number of zero to K cuts of its children. It is the sum of the number of 1 to K-1 cuts of this combination. This is different portion and interesting part. When we add on the cut in branch from its parent to this root, then the number of cut will be 2 to K at this root.
As graph 3,Take root E as example, its direct parent is A. The combination of the cuts based on the number of 0 to K cuts of its children is the cuts at {root:F,parent:E},{root:G,parent:E},and {root:H,parent:E}. The 1 to K-1 cut of these children, add on the cut at edge A to E, then the number of cut will become 2 to K.
Graph 3
We use above two procedures in depth first search order to visit all nodes as root, then we can just build the array of the number of the 0 to K cut for any root once and use once.
The DFS, N multiplies the combination of the number of 0 to K cut which is K square,the time complexity is O(N*K*K). Only need arrays to store the number of 0 to K cuts, the space complexity is N.
Below is the java implementation:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//java implementation to cut tree challenge
public class CutTree {
public static int N, K;
public static List<Integer>[] adjacencyList;
//number of cut totally
public static long nk = 0;
public static void main(String[] args) throws Exception {
/*
case 1:
3 1
2 1
2 3
*/
Scanner s = new Scanner(System.in);
int a, b;
long t;
N = s.nextInt();
K = s.nextInt();
K = K >= N - 1 ? N - 1 : K;
t = N - 1;
adjacencyList = new List[N + 1];
while (t-- > 0) {
a = s.nextInt();
b = s.nextInt();
if (adjacencyList[a] == null) {
adjacencyList[a] = new ArrayList<Integer>();
}
if (adjacencyList[b] == null) {
adjacencyList[b] = new ArrayList<Integer>();
}
adjacencyList[a].add(b);
adjacencyList[b].add(a);
}
kCutArrayAtBranchP2Root(1, -1);
nk += 2 * N;
System.out.println(nk);
s.close();
}
// a array of number of 0 to K cuts at branch p to root
public static long[] kCutArrayAtBranchP2Root(int root, int parent) {
// 0 to K cuts in previous branches
long[] i1 = new long[K + 1];
// 0 to K cuts in a new branch
long[] i2 = null;
// 0 to K cuts of the combination of previous branches and new branch
long[] i3;
i1[0] = 1;
// depth first search to visit all node as root
for (int i : adjacencyList[root]) {
if (i != parent) {
i3 = new long[K + 1];
i2 = kCutArrayAtBranchP2Root(i, root);
// plus one cut between edge root to i
i2[1]++;
for (int j = 0; j <= K && i1[j] > 0; j++) {
for (int k = 0; j + k <= K && i2[k] > 0; k++) {
i3[j + k] += i1[j] * i2[k];
}
}
i1 = i3;
}
}
// subtrees is root at 1,no parent p, then sum the number of 2 to K cut
if (root == 1)
calNumberOfCutAtRoot(i1);
// subtrees isn't root at 1, then sum the number of 1 to K-1 cut as the cut at
// current root as portion of 2 to K cut at branch p to root
else
calNumberOfCutForCuttedTree(i1);
return i1;
}
// number of cut at root without parent
public static void calNumberOfCutForCuttedTree(long[] ar) {
for (int i = 1; i < K && ar[i] > 0; i++) {
nk += ar[i];
}
}
// number of cut at root has parent
public static void calNumberOfCutAtRoot(long[] ar) {
for (int i = 2; i <= K && ar[i] > 0; i++) {
nk += ar[i];
}
}
}
3. Solution overview
I think this solution for this challenge is interesting, so I want to share it with everyone. I generalize my solution as the statement: the cut is the combination of cut at children of a certain root,cut at two sides of some root A , then cut at two sides of another root B doesn’t go beyond A, the later cut will not have redundance.