combinatorics

Efficient algorithm to calculate the sum of all k-products

一世执手 提交于 2019-12-03 03:20:57
Suppose you are given a list L of n numbers and an integer k<n . Is there an efficient way to calculate the sum of all products of k distinct numbers in L ? As an example, take L=[1,3,4,6] and k=2 . Then the number I am looking for is 1*3 + 1*4 + 1*6 + 3*4 + 3*6 + 4*6 . Can you think of a way of doing it which avoids generating all the subsets of size k ? Let F(X,k,n) be the k-product sum of first n elements of array X. F(X,k,n) = F(X,k,n-1)+F(X,k-1,n-1)*X[n] which you can solve using dynamic programming. Complexity = O(kn). End conditions for F(X,k,n): When n=k F(X,k,k) = X[1]* X[2]*...*X[n]

Assigning people to buildings while respecting preferences?

允我心安 提交于 2019-12-03 02:55:18
A friend asked me a question today about an assignment problem. I found a quite straightforward solution, but I feel that it can be made simpler and faster. Your help would be appreciated. The problem: Assuming that I have N people, I need to assign them into M buildings, each building can house K people. Not all people are willing to live with each other, so i have a matrix of N*N cells and a 1 that marks the people that are willing to live with each other. If a cell contains 1 it means that I and J can live together. Obviously the matrix is symmetrical around the main diagonal. My solution

Permutations excluding repeated characters

佐手、 提交于 2019-12-03 02:48:13
I'm working on a Free Code Camp problem - http://www.freecodecamp.com/challenges/bonfire-no-repeats-please The problem description is as follows - Return the number of total permutations of the provided string that don't have repeated consecutive letters. For example, 'aab' should return 2 because it has 6 total permutations, but only 2 of them don't have the same letter (in this case 'a') repeating. I know I can solve this by writing a program that creates every permutation and then filters out the ones with repeated characters. But I have this gnawing feeling that I can solve this

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

主宰稳场 提交于 2019-12-03 01:23:49
I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1 .) Let's look at the space of all permutations on that space, and call it S . The i th member of S , which can be marked S[i] , is the permutation with the lexicographic number i . For example, if N is 3, then S is this list of permutations: S[0]: 0, 1, 2 S[1]: 0, 2, 1 S[2]: 1, 0, 2 S[3]: 1, 2, 0 S[4]: 2, 0, 1 S[5]: 2, 1, 0 (Of course, when looking at a big N , this space becomes very large, N! to be exact.) Now, I

number to unique permutation mapping of a sequence containing duplicates

爱⌒轻易说出口 提交于 2019-12-02 19:32:02
I am looking for an algorithm that can map a number to a unique permutation of a sequence. I have found out about Lehmer codes and the factorial number system thanks to a similar question, Fast permutation -> number -> permutation mapping algorithms , but that question doesn't deal with the case where there are duplicate elements in the sequence. For example, take the sequence 'AAABBC'. There are 6! = 720 ways that could be arranged, but I believe there are only 6! / (3! * 2! * 1!) = 60 unique permutation of this sequence. How can I map a number to a permutation in these cases? Edit: changed

How can I prove the “Six Degrees of Separation” concept programmatically?

て烟熏妆下的殇ゞ 提交于 2019-12-02 17:47:14
I have a database of 20 million users and connections between those people. How can I prove the concept of "Six degrees of separation" concept in the most efficient way in programming? link to the article about Six degrees of separation You just want to measure the diameter of the graph. This is exactly the metric to find out the seperation between the most-distantly-connected nodes in a graph. Lots of algorithms on Google, Boost graph too. You can probably fit the graph in memory (in the representation that each vertex knows a list of its neighbors). Then, from each vertex n , you can run a

How to make all possible sum combinations from array elements in VB

时光怂恿深爱的人放手 提交于 2019-12-02 08:43:01
问题 If there is an array with elements: 1,2,3,4, the program should return another array with sum of all combinations: 1 2 3 4 3 (1+2) 4 (1+3) 5 (1+4) 5 (2+3) 6 (2+4) 7 (3+4) 6 (1+2+3) 7 (1+2+4) 8 (1+3+4) 9 (2+3+4) 10 (1+2+3+4) 回答1: This is a function I wrote some time ago to generate all possible subsets of a given array. It's generic, so it supports integers, doubles, strings, etc. Original C# public static List<T[]> CreateSubsets<T>(T[] originalArray) { List<T[]> subsets = new List<T[]>(); for

Find vector elements that sum up to specific number in MATLAB

倖福魔咒の 提交于 2019-12-02 04:51:08
问题 Let us consider that we have a vector VEC . Is ther a way to find which vector elements can be grouped so as they sum up to a given number NUM in MATLAB? For example if VEC = [2 5 7 10] and NUM = 17 The requested algorithm should provide the answer that subvectors [2 5 10] and [7 10] sum up to given NUM . 回答1: Here is a way to solve this using conbntns, a function from the Mapping Toolbox that retrieves all possible combinations of set of values (if you don't have this toolbox, you can use

Is there a function to generate a specific n Multichoose r combination, given the index number?

為{幸葍}努か 提交于 2019-12-02 01:38:14
问题 For example, 3 multichoose 2 has the following combinations: i combo 0 = [0,0] 1 = [0,1] 2 = [0,2] 3 = [1,1] 4 = [1,2] 5 = [2,2] Could a function be written whose arguments are n,r,i and returns the combination in question, without iterating through every combination before it? 回答1: Could a function be written whose arguments are n,r,i and returns the combination in question, without iterating through every combination before it? Yes. We have to do a little counting to get at the heart of

Need help in building efficient exhaustive search algorithm

我是研究僧i 提交于 2019-12-02 00:32:55
问题 There are a 10 buttons. These buttons can unlock the lock if pressed in correct order (5 presses in sequence). Every button press triggers unlock check. Example: "password" is 123456 and I press buttons 0 1 2 3 4 5 6 I unlock the lock from 6th button press. I need to design algorithm that tries all possible combinations in the most efficient way (i.e. minimum amount of buttons should be pressed). I can interpret button number as digit and number of pressed button in sequence as digit position