combinatorics

How to generate all permutations of a list in Python

How do you generate all the permutations of a list in Python, independently of the type of elements in that list. For example: permutations ([]) [] permutations ([1,]) [1] permutations ([1,2]) [1, 2] [2, 1] permutations ([1,2,3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] EDIT: Eliben pointed to a solution that's...

Algorithm for Grouping

I am trying to help someone write a program that I thought would be easy, but of course it never is :) I am trying to take a class roster (usually between 10-20 students) and effectivly uniquely pair off each classmate to another to make unique groups. Therefore in a class of 10 people, you can have 9 groups. It needs to be able to han...

recursion instead of multi-loops

I want this method to work for any given number of arguments, i can do that with code generation(with a lot of ugly code), can it be done with recursion? if so how? I understand recursion, but i dont know how to write this. private static void allCombinations(List<String>... lists) { if (lists.length == 3) { for (String s3 : lists[0]...

Good simple algorithm for generating necklaces in Scheme?

A k-ary necklace of length n is an ordered list of length n whose items are drawn from an alphabet of length k, which is the lexicographically first list in a sort of all lists sharing an ordering under rotation. Example: (1 2 3) and (1 3 2) are the necklaces of length 3 from the alphabet {1 2 3}. More info: http://en.wikipedia.org/wi...

Generating permutations lazily

I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all of the permutations don't have to be stored in memory at once. Alternatively I'm looking...

Reduce Permutation

I need an algorithm that can map the runs in a permutation to a single number, but also reduce the subsequent numbers. So a run is a sequential set of numbers in a permutation that is sorted and in-order. In the list, 1;2;3;5;6;4 there are two runs, 1;2;3 and 5;6. I want to replace these with a single number, a minimum, so if, after re...

Generate combinations ordered by an attribute

Hi, I'm looking for a way to generate combinations of objects ordered by a single attribute. I don't think lexicographical order is what I'm looking for... I'll try to give an example. Let's say I have a list of objects A,B,C,D with the attribute values I want to order by being 3,3,2,1. This gives A3, B3, C2, D1 objects. Now I want to g...

Obtaining all possible states of an object for a NP-Complete(?) problem in Python

Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available. Say you have : class Person: def __init__(self): self.status='unknown' def set(self,value): if value: self.status='happy' else :...

List all possible combinations of k integers between 1...n (n choose k)

Hi, Out of no particular reason I decided to look for an algorithm that produces all possible choices of k integers between 1...n, where the order amongst the k integer doesn't matter (the n choose k thingy). From the exact same reason, which is no reason at all, I also implemented it in C#. My question is: Do you see any mistake in ...

number of possible combinations in a partitioning

Given is a set S of size n, which is partitioned into classes (s1,..,sk) of sizes n1,..,nk. Naturally, it holds that n = n1+...+nk. I am interested in finding out the number of ways in which I can combine elements of this partitioning so that each combination contains exactly one element of each class. Since I can choose n1 elements fr...

Finding Strings Neighbors By Up To 2 Differing Positions

Hi all, Given a seed string, I want to find its neighbors with at most differ in 2 positions. All the digits involve in generating string are only four (i.e. 0,1,2,3). This is the example for what I mean: # In this example, 'first' column # are neighbors with only 1 position differ. # The rest of the columns are 2 positions differ See...

Probability Question

I have x items and y <= x special items. Out of these I pick z <= x items. Given s where 1 <= s <= y, what is the probability that the z items I picked contain s special items? The particular case I want to apply it to: I have 70 items, 14 are special. I pick 5 of them. What's the chance that 1 is special, 2 are special, etc... up to 5...

How do you calculate the total number of all possible unique subsets from a set with repeats?

Given a set** S containing duplicate elements, how can one determine the total number all the possible subsets of S, where each subset is unique. For example, say S = {A, B, B} and let K be the set of all subsets, then K = {{}, {A}, {B}, {A, B}, {B, B}, {A, B, B}} and therefore |K| = 6. Another example would be if S = {A, A, B, B}, the...

How to generate permutations of a list without "reverse duplicates" in Python using generators

This is related to question How to generate all permutations of a list in Python How to generate all permutations that match following criteria: if two permutations are reverse of each other (i.e. [1,2,3,4] and [4,3,2,1]), they are considered equal and only one of them should be in final result. Example: permutations_without_duplicate...

Counting combinations of pairs of items from multiple lists without repetition

Given a scenario where we have multiple lists of pairs of items, for example: {12,13,14,23,24} {14,15,25} {16,17,25,26,36} where 12 is a pair of items '1' and '2' (and thus 21 is equivalent to 12), we want to count the number of ways that we can choose pairs of items from each of the lists such that no single item is repeated. You mu...

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

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 ...

How to get count of next combinations for given set?

I've edited original text to save potential readers some time and health. Maybe someone will actually use this. I know it's basic stuff. Probably like very, very basic. How to get all possible combinations of given set. E.g. string set = "abc"; I expect to get: a b c aa ab ac aaa aab aac aba abb abc aca acb acc baa bab ... and the lis...

Find the permutations where no element stays in place

I'm working with permutations where each element is different from its original location. I would like an algorithm that given {an input length, row and digit}, will give me the output number. Here's an example: If the input length is four, then all the permutations of 0123 are: 0123,0132,0213,0231,0312,0321, 1023,1032,1203,1230,1302...

Enumerating all strings meeting given restrictions

I'm looking for the name of the following class of problem, so that I can google for effective algorithms and more information. I have an alphabet with three characters {-1, 0, 1}. I need to effectively generate all strings of length 24 which are mostly {0} but have zero to eight {1,-1} characters distributed in certain patterns. (The...

Listing combinations WITH repetitions in Scala

Trying to learn a bit of Scala and ran into this problem. I found a solution for all combinations without repetions here and I somewhat understand the idea behind it but some of the syntax is messing me up. I also don't think the solution is appropriate for a case WITH repetitions. I was wondering if anyone could suggest a bit of code th...