Get all subset of a set

原创转载请注明出处:http://agilestyle.iteye.com/blog/2360659

 

找出一个Set集合中的所有子集

比如全集为 {1, 2, 3}

子集则有{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}

 

此算法的核心思想:递归

package org.fool.java.collections;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class GetAllSubSetTest {
    public static void main(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);

        // set to array
        Integer[] nums = set.toArray(new Integer[set.size()]);
        // use lambda to transform Integer[] to int[]
        printSubSet(Arrays.stream(nums).mapToInt(i -> i).toArray());
    }

    // step 1: decide how many elements in a sub-set to be printed
    public static void printSubSet(int[] nums) {
        for (int i = 0; i <= nums.length; i++) {
            boolean[] ifPrint = new boolean[nums.length];
            printSubSet(nums, ifPrint, 0, i);   // start from 0th index, and the size varies for the loop
        }
    }

    // step 2: Recursively process elements from left to right to print out all possible combination of elements with fixed size
    // we need three additional variables to keep track of status
    // boolean array to know whether printed out or not
    // start is the start index to be printed to prevent duplicates
    // remain is keeping track of how many remaining elements to be processed for the subset action
    public static void printSubSet(int[] nums, boolean[] ifPrint, int start, int remain) {
        // firstly if remain == 0, we done!
        if (remain == 0) {
            System.out.print("{");
            // check each ifPrint status to decide print or not
            for (int i = 0; i < ifPrint.length; i++) {
                if (ifPrint[i]) {
                    System.out.print(nums[i] + ", ");
                }
            }

            System.out.print("}\n");
        } else {
            // we need process char by char from the start position until end
            // before that, we need determine whether we proceed or not to check if (start + remain > nums.length)
            if (start + remain > nums.length) {

            } else {
                for (int i = start; i < nums.length; i++) {
                    // now before we come to recursive part we have to make sure this position is not used
                    if (!ifPrint[i]) {
                        // now assign its value to true as used indicator
                        ifPrint[i] = true;
                        // recursive call
                        printSubSet(nums, ifPrint, i + 1, remain - 1);
                        // set the position back to false and proceed from next element
                        ifPrint[i] = false;
                    }
                }
            }
        }
    }
}

Console Output


 

Reference

https://www.youtube.com/watch?v=wVNV3JEcikA 

 

Write a C program that can be run in the Microsoft Visual Studio to partitions a hypergraph G = (V, E) into 2 partitions. The Assignment Write a computer program that takes a netlist represented by a weighted hypergraph and partitions it into two partitions. Each node is associated with an area value and each edge has an edge cost. Your program should minimize the total cost of the cut set, while satisfying the area constraint that the total area of partition 1 should satisfy the balance criteria as described in the class. That is, if the area sum of all the nodes is A, then the area of partition 1 should be greater than or equal to ra-tio_factor *A – amax and less than or equal to ratio_factor *A + amax, where amax is the maximum value among all cell areas. The program should prompt the user for the value of ratio_factor. Assumptions and Requirements of the Implementation Your program should not have any limitation on the maximum number of nodes and the edges of the hypergraph. Each hyperedge could connect any subset of nodes in the hypergraph. Each node area is a non-negative integer, and each edge cost is a non-negative floating- point value. All the ids are 0-based. Namely, the id of the first element is 0, instead of 1. The output of each partition should include the list of node ids, sorted in the ascending order. The partition with the smaller minimum node id is listed first in the output. Use balance criteria as the tiebreaker when there are multiple cell moves giving the max-imum gain, as described in the class. Use the input and output formats given in the Sample Test Cases section. Sample Test Cases Test: Please enter the number of nodes: 4 Please enter each of the 4 nodes with its id and the node area: 0 1 1 4 2 2 3 1 Please enter the number of edges: 3 Please enter each of the 3 edges with the number of connected nodes and their node ids, followed by the edge cost: 3 0 1 2 5 3 0 2 3 3 3 0 1 3 4 Please enter the percentage of ratio factor: 50 The node ids of the partition 0 are 3 The node ids of the partition 1 are 0, 1, 2 The total cut cost is 7
07-08
KCONFIG-CONF(1) October 2017 KCONFIG-CONF(1) NAME kconfig-conf - Standalone implementation of the Linux Kconfig parser SYNOPSIS kconfig-conf [options] <KConfig_file> DESCRIPTION The kconfig toolkit support the Kconfig language and implement the parser and the configuration support associated to the KConfig files respecting the Linux kernel KConfig convention. At configuration time: • kconfig-mconf is based on ncurses (menuconfig) • kconfig-conf is based on dialog (config) • kconfig-gconf is based on GTK+ (gconfig) • kconfig-qconf is based on QT (qconfig) Associated tools: • kconfig-diff displays symbols diff between .config files OPTIONS –silentoldconfig Only for kconfig-conf, reload a given .config and regenerate header and command files accordingly. --allnoconfig Set all boolean configuration to no. --allyesconfig Set all boolean configuration to yes. --randconfig Generates a random configuration. ENVIRONMENT Environment variables for ‘*config’ KCONFIG_CONFIG This environment variable can be used to specify a default kernel config file name to override the default name of ".config". KCONFIG_OVERWRITECONFIG If you set KCONFIG_OVERWRITECONFIG in the environment, Kconfig will not break symlinks when .config is a symlink to somewhere else. CONFIG_ If you set CONFIG_ in the environment, Kconfig will prefix all symbols with its value when saving the configuration, instead of using the default, "CONFIG_". Environment variables for ‘{allyes/allmod/allno/rand}config’ KCONFIG_ALLCONFIG The allyesconfig/allmodconfig/allnoconfig/randconfig variants can also use the environment variable KCONFIG_ALLCONFIG as a flag or a filename that contains config symbols that the user requires to be set to a specific value. If KCONFIG_ALLCONFIG is used without a filename where KCONFIG_ALLCONFIG == "" or KCONFIG_ALLCONFIG == "1", "make *config" checks for a file named "all{yes/mod/no/def/random}.config" (corresponding to the *config command that was used) for symbol values that are to be forced. If this file is not found, it checks for a file named "all.config" to contain forced values. This enables you to create "miniature" config (miniconfig) or custom config files containing just the config symbols that you are interested in. Then the kernel config system generates the full .config file, including symbols of your miniconfig file. This 'KCONFIG_ALLCONFIG' file is a config file which contains (usually a subset of all) preset config symbols. These variable settings are still subject to normal dependency checks. Examples: KCONFIG_ALLCONFIG=custom-notebook.config make allnoconfig or KCONFIG_ALLCONFIG=mini.config make allnoconfig or make KCONFIG_ALLCONFIG=mini.config allnoconfig These examples will disable most options (allnoconfig) but enable or disable the options that are explicitly listed in the specified mini-config files. Environment variables for ‘randconfig’ KCONFIG_SEED You can set this to the integer value used to seed the RNG, if you want to somehow debug the behaviour of the kconfig parser/frontends. If not set, the current time will be used. KCONFIG_PROBABILITY This variable can be used to skew the probabilities. See /usr/share/doc/kconfig-frontends/kconfig.txt.gz. Environment variables for 'silentoldconfig' KCONFIG_NOSILENTUPDATE If this variable has a non-blank value, it prevents silent kernel config updates (requires explicit updates). KCONFIG_AUTOCONFIG This environment variable can be set to specify the path name of the "auto.conf" file. Its default value is "include/config/auto.conf". KCONFIG_TRISTATE This environment variable can be set to specify the path name of the "tristate.conf" file. Its default value is "include/config/tristate.conf". KCONFIG_AUTOHEADER This environment variable can be set to specify the path name of the "autoconf.h" (header) file. Its default value is "include/generated/autoconf.h". HISTORY June 2017, Man page originally compiled by Philippe Thierry (phil at reseau-libre dot com) Philippe Thierry kconfig-conf Man Page KCONFIG-CONF(1) 这是手册
最新发布
08-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值