combinatorics

Order bias in wrong implementation of Fisher Yates Shuffle

末鹿安然 提交于 2019-11-28 11:42:34
问题 I implemented the shuffling algorithm as: import random a = range(1, n+1) #a containing element from 1 to n for i in range(n): j = random.randint(0, n-1) a[i], a[j] = a[j], a[i] As this algorithm is biased. I just wanted to know for any n(n ≤ 17) , is it possible to find that which permutation have the highest probablity of occuring and which permutation have least probablity out of all possible n! permutations. If yes then what is that permutation?? For example n=3 : a = [1,2,3] There are 3

Generating all unique pair permutations

孤人 提交于 2019-11-28 11:19:33
I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results. So for example: import itertools for perm in itertools.permutations(range(9)): print zip(perm[::2], perm[1::2]) generates all possible two-paired permutations; here's a small subset of the output: ... [(8, 4), (7, 6), (5, 3), (0, 2)] [(8, 4), (7, 6), (5, 3), (1, 0)] [(8, 4), (7, 6), (5, 3), (1, 2)] [(8, 4), (7, 6), (5, 3), (2, 0)] [(8, 4), (7, 6), (5, 3), (2, 1)] [(8, 5), (0, 1), (2, 3), (4, 6)] [(8, 5), (0, 1), (2, 3), (4, 7)] [(8, 5), (0, 1), (2, 3), (6, 4)] [(8, 5),

Generating natural schedule for a sports league

女生的网名这么多〃 提交于 2019-11-28 10:59:02
I'm looking for an algorithm to generate a schedule for a set of teams. For example, imagine a sports season in which each team plays each other, one time as home team and the other as a visitor team on another teams field. To generate a set of all games in the season is easy, if teams is a list of teams the following would do: set((x, y) for x in teams for y in teams if x != y) But I also want to ORDER the games in chronological order in such a way that it satisfies the constraint of a valid game schedule and also looks "naturally random". The constraint is that the game list should be

How to find multiplicative partitions of any integer?

北城以北 提交于 2019-11-28 08:27:35
I'm looking for an efficient algorithm for computing the multiplicative partitions for any given integer. For example, the number of such partitions for 12 is 4, which are 12 = 12 x 1 = 4 x 3 = 2 x 2 x 3 = 2 x 6 I've read the wikipedia article for this, but that doesn't really give me an algorithm for generating the partitions (it only talks about the number of such partitions, and to be honest, even that is not very clear to me!). The problem I'm looking at requires me to compute multiplicative partitions for very large numbers (> 1 billion), so I was trying to come up with a dynamic

How would you program Pascal's triangle in R?

左心房为你撑大大i 提交于 2019-11-28 08:15:06
问题 I am reading, on my own (not for HW) about programming, and one exercise involved programming Pascal's triangle in R. My first idea was to make a list and then append things to it, but that didn't work too well. Then I thought of starting with a vector, and making a list out of that, at the end. Then I thought of making a matrix, and making a list out of that at the end. Not sure which way to even approach this. Any hints? thanks 回答1: There is one solution on Rosetta Code: pascalTriangle <-

How to find permutation of a given string with its rank?

与世无争的帅哥 提交于 2019-11-28 05:30:30
问题 For example, rank permutation 0 abc 1 acb 2 bac 3 bca 4 cab 5 cba So, if one asks give me permutation with rank 4, the answer is cab. Pls give the java code for this program 回答1: I made it at a first attempt!! :-) Really good homework, nice problem, you made my day! Here is a solution in javascript: function permutation (rank, n, chars) { var fact, char_idx, this_char; if (n == 0) return ""; char_idx = Math.floor(rank / factorial(n - 1)); this_char = chars.splice(char_idx, 1); // returns the

Code-golf: generate pascal's triangle

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:30:10
Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick ): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)] Explanation: the first element of the list comprehension (when the length is 0) is [1] the next elements are obtained the following way: take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end. e.g. for the 2nd step, we take [1] and make [0,1] and [1

Calculating Binomial Coefficient (nCk) for large n & k

风格不统一 提交于 2019-11-28 04:00:55
I just saw this question and have no idea how to solve it. can you please provide me with algorithms , C++ codes or ideas? This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K <= N and the maximum value of N is 1,000,000,000,000,000. Since the value may be very large, you need to compute the result modulo 1009. Input The first line of the input contains the number of test cases T, at most 1000. Each of the next T lines consists of two space separated integers N and K, where 0 <= K <= N and 1 <=

Fast computation of multi-category number of combinations [closed]

给你一囗甜甜゛ 提交于 2019-11-28 02:21:36
问题 I have to evaluate the following formula for permutations with repeated objects n!/(r1! * r2! * r3! * ......... * rn!) where n <= 500 and 1 <= ri <= 10 (there are n objects in total out of which r1 are alike of 1 kind , r2 are alike of 2nd kind and so on and the formula indicates the number of permutations of such objects). I need an efficient coding solution for this because working with big integers in Java doesn't prove to be fruitful for large cases. Thanks in advance. 回答1: You can do

i-th element of k-th permutation

萝らか妹 提交于 2019-11-28 02:04:37
问题 Is there a fast algorithm to compute the i-th element (0 <= i < n) of the k-th permutation (0 <= k < n!) of the sequence 0..n-1? Any order of the permutations may be chosen, it does not have to be lexicographical. There are algorithms that construct the k -th permutation in O(n) (see below). But here the complete permutation is not needed, just its i -th element. Are there algorithms that can do better than O(n) ? Is there an algorithm that has a space complexity less than O(n)? There are