powerset

Generate all possible subgraphs of a directed graph keeping the number of vertices

我怕爱的太早我们不能终老 提交于 2019-12-06 05:07:29
I have two lists of vertices: V and S . I would like to generate all possible directed graphs from V and S so, that each vertex from V has only one out-edge and exactly one in-edge, and each vertex from S can have any number of in- and out- edges. Each graph in the result should contain exactly all vertices from V and from S . The result can contain both connected and disconnected graphs. First I thought it was a powerset-related problem, but powerset has many other sets that may contain just one element (and I do not need those). My current strategy is to: find all pairs between vertices from

How to get the largest possible column sequence with the least possible row NAs from a huge matrix?

自闭症网瘾萝莉.ら 提交于 2019-12-06 04:12:56
I want to select columns from a data frame so that the resulting continuous column-sequences are as long as possible, while the number of rows with NAs is as small as possible, because they have to be dropped afterwards. (The reason I want to do this is, that I want to run TraMineR::seqsubm() to automatically get a matrix of transition costs (by transition probability) and later run cluster::agnes() on it. TraMineR::seqsubm() doesn't like NA states and cluster::agnes() with NA states in the matrix doesn't necessarily make much sense.) For that purpose I already wrote a working function that

Python Power Set of a List [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-06 02:21:00
This question already has an answer here: How to get all subsets of a set? (powerset) 17 answers I am trying to implement a function to generate the powerset of a list xs . The general idea is that we walk through the elements of xs and choose whether to include x or not. The problem I'm facing is that withX ends up being equal to [None] (a singleton list with None ) because (I think) s.add(x) returns None . This isn't a homework assignment, it's an exercise in Cracking the Coding Interview. def powerSetBF(xs): powerSet = [] powerSet.append(set([])) for x in xs: powerSetCopy = powerSet[:]

efficient powerset algorithm for subsets of minimal length

笑着哭i 提交于 2019-12-05 22:41:48
i am using the following C# function to get a powerset limited to subsets of a minimal length string[] PowerSet(int min_len, string set) { IEnumerable<IEnumerable<string>> seed = new List<IEnumerable<string>>() { Enumerable.Empty<string>() }; return set.Replace(" ", "") .Split(',') .Aggregate(seed, (a, b) => a.Concat(a.Select(x => x.Concat(new[] { b })))) .Where(subset => subset.Count() >= min_len) .Select(subset => string.Join(",", subset)) .ToArray(); } the problem is that when the original set is large, the algorithm has to work very hard even if the minimal length is large as well. e.g:

Largest Subset whose sum is less than equal to a given sum

℡╲_俬逩灬. 提交于 2019-12-04 02:15:58
问题 A list is defined as follows: [1, 2, 3] and the sub-lists of this are: [1], [2], [3], [1,2] [1,3] [2,3] [1,2,3] Given K for example 3 the task is to find the largest length of sublist with sum of elements is less than equal to k. I am aware of itertools in python but it will result in segmentation fault for larger lists. Is there any other efficient algorithm to achieve this? Any help would be appreciated. My code is as allows: from itertools import combinations def maxLength(a, k): #print a

Power set generated by bits

我们两清 提交于 2019-12-03 11:33:28
问题 I have this code which generates power set of an array of size 4 (number is just example, less combinations to write...). #define ARRAY_SIZE 4 unsigned int i, j, bits, i_max = 1U << ARRAY_SIZE; int array[ARRAY_SIZE]; for (i = 0; i < i_max ; ++i) { for (bits = i, j = 0; bits; bits >>= 1, ++j) { if (bits & 1) printf("%d", array[j]); } } Output: {} {1} {2} {1, 2} {3} {1, 3} {2, 3} {1, 2, 3} {4} {1, 4} {2, 4} {1, 2, 4} {3, 4} {1, 3, 4} {2, 3, 4} {1, 2, 3, 4} I need that output to be like this one

Get all 1-k tuples in a n-tuple

依然范特西╮ 提交于 2019-12-02 22:39:39
问题 With n=5 and k=3 the following loop will do it List<String> l=new ArrayList<String>(); l.add("A");l.add("B");l.add("C");l.add("D");l.add("E"); int broadcastSize = (int) Math.pow(2, l.size()); for (int i = 1; i < broadcastSize; i++) { StringBuffer buffer = new StringBuffer(50); int mask = i; int j = 0; int size=0; System.out.println(); while (mask > 0) { if ((mask & 1) == 1) { System.out.println(".. "+mask); buffer.append(l.get(j)); if (++size>3){ buffer = new StringBuffer(50); break; } }

powerset(all combinations) of a resultset in T-SQL

不问归期 提交于 2019-12-02 14:24:39
I need a t-sql code to get powerset of a resultset. example input : ColumnName 1 2 3 Example Output(one columns as nvarchar) : 1 2 3 1,2 1,3 2,3 1,2,3 Output set may contain duplicate values such as (1,3 vs 3,1). You can use this one and get full powerset. Enjoy it. --EXEC PowerSet 'A,B,C,D' Create PROCEDURE PowerSet(@Members NVARCHAR(64)) As Begin Declare @SubSet NVARCHAR(Max), @SubSetCount int, @Counter1 int, @Counter2 int, @ID int Create table #Members ( ID int IDENTITY(1,1) PRIMARY KEY, Member NVARCHAR(50) ) Create table #PowerSets ( SubSet NVARCHAR(50) ) -- Start Convert @Members To Table

Get all 1-k tuples in a n-tuple

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 13:28:48
With n=5 and k=3 the following loop will do it List<String> l=new ArrayList<String>(); l.add("A");l.add("B");l.add("C");l.add("D");l.add("E"); int broadcastSize = (int) Math.pow(2, l.size()); for (int i = 1; i < broadcastSize; i++) { StringBuffer buffer = new StringBuffer(50); int mask = i; int j = 0; int size=0; System.out.println(); while (mask > 0) { if ((mask & 1) == 1) { System.out.println(".. "+mask); buffer.append(l.get(j)); if (++size>3){ buffer = new StringBuffer(50); break; } } System.out.println(" "+mask); mask >>= 1; j++; } if (buffer.length()>0) System.out.println(buffer.toString(

Algorithm to print all combination of letters of the given string in lexicographical order

好久不见. 提交于 2019-12-01 12:59:18
I tried to create the code to generate all possible combination of the given string in the lexicographical order: The code that I wrote is: void get(char *n) { int l=strlen(n); sort(n,n+l); int k=0,m,i,j,z; while(k<l) { m=k; for(i=k;i<l;i++) { for(j=k;j<=i;j++) cout<<n[j]; cout<<"\n"; } for(z=m+2;z<l;z++) cout<<n[m]<<n[z]<<"\n"; k++; } } int main() { char n[100]; cin>>n; get(n); return 0; } Suppose the string is : abcde My code is not generating combinations like: abd abe The output I am getting for the string abcde are: a ab abc abcd abcde ac ad ae b bc bcd bcde bd be c cd cde ce d de e My