DP26树的最大独立子集问题 Largest Independent Set Problem @geeksforgeeks
来源:互联网 发布:乐器管子价格淘宝 编辑:程序博客网 时间:2024/06/03 14:36
Given a Binary Tree, find size of the Largest Independent Set(LIS) in it. A subset of all tree nodes is an independent set if there is no edge between any two nodes of the subset.
For example, consider the following binary tree. The largest independent set(LIS) is {10, 40, 60, 70, 80} and size of the LIS is 5.
A Dynamic Programming solution solves a given problem using solutions of subproblems in bottom up manner. Can the given problem be solved using solutions to subproblems? If yes, then what are the subproblems? Can we find largest independent set size (LISS) for a node X if we know LISS for all descendants of X? If a node is considered as part of LIS, then its children cannot be part of LIS, but its grandchildren can be. Following is optimal substructure property.
1) Optimal Substructure:
Let LISS(X) indicates size of largest independent set of a tree with root X.
LISS(X) = MAX { (1 + sum of LISS for all grandchildren of X), (sum of LISS for all children of X) }
The idea is simple, there are two possibilities for every node X, either X is a member of the set or not a member. If X is a member, then the value of LISS(X) is 1 plus LISS of all grandchildren. If X is not a member, then the value is sum of LISS of all children.
2) Overlapping Subproblems
Following is recursive implementation that simply follows the recursive structure mentioned above.
遇到树的结构可以改变节点的结构,在节点里面增加一个memo值用来存储
package DP;public class LargestIndependentSet {public static void main(String[] args) {Node root = new Node(20);root.left = new Node(8);root.left.left = new Node(4);root.left.right = new Node(12);root.left.right.left = new Node(10);root.left.right.right = new Node(14);root.right = new Node(22);root.right.right = new Node(25);System.out.println("Size of the Largest Independent Set is " + LISS_Rec(root));NodeMemo root2 = new NodeMemo(20);root2.left = new NodeMemo(8);root2.left.left = new NodeMemo(4);root2.left.right = new NodeMemo(12);root2.left.right.left = new NodeMemo(10);root2.left.right.right = new NodeMemo(14);root2.right = new NodeMemo(22);root2.right.right = new NodeMemo(25);System.out.println("Size of the Largest Independent Set is " + LISS_Memo(root2));}public static int LISS_Rec(Node root){if(root == null){return 0;}// 计算不包含当前节点的size,即为左右孩子的LISS sizeint sizeExcl = LISS_Rec(root.left) + LISS_Rec(root.right);// 计算包含当前节点的size,即为1+孙子的LISS sizeint sizeIncl = 1;if(root.left != null){sizeIncl += LISS_Rec(root.left.left) + LISS_Rec(root.left.right);}if(root.right != null){sizeIncl += LISS_Rec(root.right.left) + LISS_Rec(root.right.right);}return Math.max(sizeExcl, sizeIncl);}public static int LISS_Memo(NodeMemo root){if(root == null){return 0;}if(root.liss != 0){return root.liss;}if(root.left==null && root.right==null){root.liss = 1;return root.liss;}int sizeExcl = LISS_Memo(root.left) + LISS_Memo(root.right);int sizeIncl = 1;if(root.left != null){sizeIncl += LISS_Memo(root.left.left) + LISS_Memo(root.left.right);}if(root.right != null){sizeIncl += LISS_Memo(root.right.left) + LISS_Memo(root.right.right);}root.liss = Math.max(sizeExcl, sizeIncl);// 保存结果return root.liss;}public static class Node{int data;Node left, right;public Node(int data){this.data = data;}}public static class NodeMemo{int data;NodeMemo left, right;int liss;public NodeMemo(int data){this.data = data;}}}
http://www.geeksforgeeks.org/largest-independent-set-problem/
- DP26树的最大独立子集问题 Largest Independent Set Problem @geeksforgeeks
- 找最大独立集问题-Finding a Maximal Independent Set
- Independent Set 独立集问题
- 树的最大独立子集
- DP25 子集和问题 Subset Sum Problem @geeksforgeeks
- Independent set problem
- hdu 1068 最大独立子集问题
- HDU3829 【最大独立子集】
- 最大独立子集
- HDU3829-最大独立子集
- @陈利人 : #面试题#LIS问题:这个LIS问题,可不是Longest Increasing Subsequence,而是Largest Independent Set。
- 【新手向】最大团问题和最大独立子集的懒人算法(随机化)
- 无向图的最大独立子集:Girls ans Boys
- DP18 分割区间问题 Partition problem @geeksforgeeks
- GeeksforGeeks Dynamic Programming | Set 37 (Boolean Parenthesization Problem)
- 网络分析优化独立集Independent Set算法初探
- 最大独立子集模板!!!(树形dp)
- hdu1068 二分图最大独立子集
- memcached操作分类
- 普通Java工程中调用Hibernate的8个执行步骤(properties做配置文件)
- 在C#调用C++的DLL简析(一)——生成非托管dll
- 【LeetCode】Single Number II
- LeetCode 之 Longest Substring Without Repeating Characters
- DP26树的最大独立子集问题 Largest Independent Set Problem @geeksforgeeks
- PHP MVC结构系统架构设计
- 初识“产品经理”
- Windows Socket编程
- oracle
- 字符串匹配算法之Sunday算法
- 可变字符串由StringBuffer类和StringBuilder类来管理
- 几种内部排序算法总结!(冒泡排序、快速排序、直接插入排序、拆半插入排序、简单选择排序)
- 遮罩层