permutation

Create many constrained, random permutation of a list

I need to make a random list of permutations. The elements can be anything but assume that they are the integers 0 through x-1. I want to make y lists, each containing z elements. The rules are that no list may contain the same element twice and that over all the lists, the number of times each elements is used is the same (or as clos...

Calculating permutations in F#

Inspired by this question and answer, how do I create a generic permutations algorithm in F#? Google doesn't give any useful answers to this. EDIT: I provide my best answer below, but I suspect that Tomas's is better (certainly shorter!) ...

Finding all permutations within a nested PHP Array

Given the following sample array, how can I find all permutations of times available such that the amountNeeded is satisfied? In others words the follow array should produce the following: Available on 2008-05-14 from 08:00 to 08:10 using resource 10 and 13 Available on 2008-05-14 from 08:10 to 08:20 using resource 10 and 13 pri...

Minimize function in adjacent items of an array

I have an array (arr) of elements, and a function (f) that takes 2 elements and returns a number. I need a permutation of the array, such that f(arr[i], arr[i+1]) is as little as possible for each i in arr. (and it should loop, ie. it should also minimize f(arr[arr.length - 1], arr[0])) Also, f works sort of like a distance, so f(a,b) ...

Help with a special case of permutations algorithm (not the usual)

Hi, Friends. I have always been interested in algorithms, sort, crypto, binary trees, data compression, memory operations, etc. I read Mark Nelson's article about permutations in C++ with the STL function next_perm(), very interesting and useful, after that I wrote one class method to get the next permutation in Delphi, since that is t...

Base 3 Combinatorics

I've never been much for math and I'm hoping that someone can help me out with the following. I have 5 boxes: 1 2 3 4 5 [ ] [ ] [ ] [ ] [ ] The boxes can either be white, gray, or black (or think of it as 0, 1, 2) How many possible states can the box set be in? What is the pseudocode (or in any language) to generate all t...

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

How to calculate permutations in linear time, with a twist

I have a resource scheduling issue in Java where things need to be sequenced, but there are restrictions on what resources can be next to each other. A good analogy is a string of "digits", where only certain digits can be next to each other. My solution was recursive, and works fine for small strings, but run time is O(X^N), where X i...

Removing Duplicates in Large Text Files

I've been trying to calculate all the unique permutations for a very long word (antidisestablishmentarianism), and although I can calculate the permutations for the words, I am having problems with stopping the production of duplications. Normally I would just run the List<T>.Contains() method on my string, but the list of permutations...

Creating multiple numbers with certain number of bits set

Problem I need to create 32 Bit numbers (signed or unsigned doesn't matter, the highest bit will never be set anyway) and each number must have a given number of Bits set. Naive Solution The easiest solution is of course to start with the number of zero. Within a loop the number is now increased by one, the number of Bits is counted, ...

Permutation of a vector

Hello, suppose I have a vector: 0 1 2 3 4 5 [45,89,22,31,23,76] And a permutation of its indices: [5,3,2,1,0,4] Is there an efficient way to resort it according to the permutation thus obtaining: [76,31,22,89,45,23] Using at most O(1) additional space? ...

permute i and T[i]

Hi, Assuming that I have an array of int T, I am looking for an in-place algorithm that permute i and T[i] I have : [3 2 0 1] (a) I want : [2 3 1 0] (b) eg. in (b) T[0] = 2 because, in (a) T[2] was equal to 0. I was expecting to find a simple O(n) time, O(1) space algorithm, but I can't find it. Any ideas ? Note : There is one ...

How can I generate all permutations of an array in Perl?

What's the best (elegant, simple, efficient) way to generate all n! permutations of an array in perl? For example, if I have an array @arr = (0, 1, 2), I want to output all permutations: 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 It should probably be a function that returns an iterator (lazy/delayed evaluation because n! can become so impo...

Where's the bug in my C program which generate all possible permutations of a string?

Hi all I have been trying to write a C program which generates all possible permutations of a string (eg 123 in code below). I succeeded but it generates some garbage values after each possible permutation. Please help me in finding the possible cause. Is it something to do with initialization? Code: #include <stdio.h> void permute(c...

Permuting a binary tree without the use of lists

I need to find an algorithm for generating every possible permutation of a binary tree, and need to do so without using lists (this is because the tree itself carries semantics and restraints that cannot be translated into lists). I've found an algorithm that works for trees with the height of three or less, but whenever I get to greater...

C# Permutation of an array of arraylists?

I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays. EXAMPLE: (all values are strings) myList[0] = { "1", "5", "3", "9" }; myList[1] = { "2", "3" }; myList[2] = { "93" }; The count of myList can be varied so its length is not known beforehand. I would like to be able to ...

Listing all permutations of a string/integer

A common task in programming interviews (not from my experience of interviews though) is to take a string or an integer and list every possible permutation. Is there an example of how this is done and the logic behind solving such a problem? I've seen a few code snippets but they weren't well commented/explained and thus hard to follow...

string permutations

Why doesn't this produce the correct number of string permutations? For perm("ABC", 3) it should print 27 different permutations. private static List<string> permutated = new List<string>(30000); public static List<string> perm(string s, int k) { return comb(s, "", k); } private static List<string> perm(string s, string prefix, int...

How can I get permutations of items from two subqueries in T-SQL?

Lets say I have two subqueries: SELECT Id AS Id0 FROM Table0 => Id0 --- 1 2 3 and SELECT Id AS Id1 FROM Table1 => Id1 --- 4 5 6 How do I combine these to get the query result: Id0 Id1 ------- 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 ...

algorithm to find closest string using same characters

Given a list L of n character strings, and an input character string S, what is an efficient way to find the character string in L that contains the most characters that exist in S? We want to find the string in L that is most-closely made up of the letters contained in S. The obvious answer is to loop through all n strings and check to...