combinatorics

Proving a set of requirements can be met with a set of values using LINQ

孤者浪人 提交于 2019-12-06 01:01:59
This is a subset of the question posted here . Given a set of buckets of volume B={x1, x2, ..., xn} and a set of vials with liquid of volumes V={v1, v2, ..., vn } what is the best way to prove that the number of buckets can be filled with the contents of the vials assuming that vials must be poured all into one bucket. Overflow is permitted. Some obvious invariants here are that the cardinality of the buckets |B| must be less than or equal to the cardinality of the vials |V| and that the combined volume of the buckets Sum(B) must be less than or equal to the combined volume of the vials Sum(V)

How can I calculate n-th permutation (or tell the lexicographic order of a given permutation)? [duplicate]

风格不统一 提交于 2019-12-06 00:39:00
This question already has an answer here: Ranking and unranking of permutations with duplicates 4 answers This question has two parts, though since I'm trying to compe up with a Prolog implementation, solving one will probably immediately lead to a solution of the other one. Given a permutation of a list of integers {1,2,...,N} , how can I tell what is the index of that permutation in lexicographic ordering? Given a number k , how can I calculate k -th permutation of numbers {1,2...,N} ? I'm looking for an algorithm that can do this reasonably better than just iterating a next permutation

Number of subsets of {1,2,3,…,N} containing at least 3 consecutive elements

倖福魔咒の 提交于 2019-12-05 22:07:29
Suppose we have a set like {1,2,3} then there is only one way to choose 3 consecutive numbers... it's the set {1,2,3}... For a set of {1,2,3,4} we have 3 ways: 123 234 1234 (technically these are unordered sets of numbers, but writing them consecutively helps) f(5) ; {1,2,3,4,5} -> 8 ways: 123 1234 1235 12345 234 2345 345 1345 f(6) ; {1,2,3,4,5,6} -> 20 ways: ... f(7) ; {1,2,3,4,5,6,7} -> 47 ways: ... So for a given N, I can get the answer by applying brute force, and calculating all such subset having 3 or more consecutive number. Here I am just trying to find out a pattern, a technique to

What is the probability that the first 4 bytes of MD5 hash computed from file contents will collide?

安稳与你 提交于 2019-12-05 19:58:00
问题 This is a combinatorics question with some theory in hashing algorithms required. Let's say the input can be any random sequence of bytes 30 kB to 5 MB of size (I guess that makes quite a few combinations of input values :)) What is the probability that the first 4 bytes (or first n bytes) of a MD5 hash computed from the byte sequence will be the same for distinct files? In case this can not be computed specifically for MD5 hash, then what is the probability that any hash function that

Check if two nested lists are equivalent upon substitution

五迷三道 提交于 2019-12-05 19:07:08
For some context, I'm trying to enumerate the number of unique situations that can occur when calculating the Banzhaf power indices for four players, when there is no dictator and there are either four or five winning coalitions. I am using the following code to generate a set of lists that I want to iterate over. from itertools import chain, combinations def powerset(iterable): s = list(iterable) return chain.from_iterable(map(list, combinations(s, r)) for r in range(2, len(s)+1)) def superpowerset(iterable): s = powerset(iterable) return chain.from_iterable(map(list, combinations(s, r)) for

How do I iterate over a large number of tuples of integers in the order of their sum?

坚强是说给别人听的谎言 提交于 2019-12-05 18:49:50
I'm using itertools.combinations() to iterate over tuples of integers. I am interested in the tuple with the lowest sum that satisfies some conditions: def findLowestNiceTuple: for tup in itertools.combinations(range(1, 6), 2): if niceTuple(tup): return tup The generator's default order is not in the order of the elements' sum. For example: >>> itertools.combinations(range(1, 6), 2) gives a generator which will yield the following elements: [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)] As you can see, the sum of (1, 5) is larger than that of (2,3). For early

possible distributions and their probabilities after putting identical items into anonymous buckets

蹲街弑〆低调 提交于 2019-12-05 18:47:17
Apologies if the answer to this is readily found elsewhere. My math and stats are weak and thus I don't even know the search terms for what I'm trying to do . . . I have b anonymous indistinguishable buckets into which I'm putting i identical items. I want to know all possible distributions and their probabilities. For example, if I have 3 buckets and 3 items, the answer I want is: [3,0,0] -> 1/9 [2,1,0] -> 6/9 [1,1,1] -> 2/9 Notice that the buckets are anonymous and thus I want identical distributions to be combined as I have done above. For example, the [2,1,0] case is actually the sum of

How to do Combinatorics on the Fly

匆匆过客 提交于 2019-12-05 17:15:51
I have a very weird problem which has some constrains that make it difficult to solve. I have a list of lists and I want to do the combinations of all items in those lists. Each item has a name and a value. Here is an example: Main List: List 01: Item 01: name:name01, value:value01 Item 02: name:name02, value:value02 List 02: Item 01: name:name03, value:value03 List 03: Item 01: name:name04, value:value04 Item 02: name:name05, value:value05 The end result should look like this: Some List: Item 01: name01:value01, name03:value03, name04:value04 Item 02: name02:value02, name03:value03, name04

Generate Combinations of generic list

岁酱吖の 提交于 2019-12-05 14:00:20
I need to create a list from another list that contains every combination possible. In researching possible solutions I have found many interesting approaches but all seem to generate results based on the count of records provided. I need the combinations to increase to a max threshold. i.e. consider the following array 1,2,3,4,5 I need the results to look similar to (threshold is 3 in this example) 1 1,2 1,2,3 1,2,4 1,2,5 1,3,4 2,3,5... etc In actuality, the data will be IEnumerable. I used a simple int[] to illustrate the desired results. My solution uses a simple recursive algorithm to

List all k-tuples with entries summing to n, ignoring rotations

风格不统一 提交于 2019-12-05 12:22:53
Is there an efficient algorithm for finding all sequences of k non-negative integers that sum to n , while avoiding rotations (completely, if possible)? The order matters, but rotations are redundant for the problem I'm working on. For example, with k = 3 and n = 3, I would want to get a list like the following: (3, 0, 0), (2, 1, 0), (2, 0, 1), (1, 1, 1). The tuple (0, 3, 0) should not be on the list, since it is a rotation of (3, 0, 0). However, (0, 3, 0) could be in the list instead of (3, 0, 0). Note that both (2, 1, 0) and (2, 0, 1) are on the list -- I do not want to avoid all