Pseudo code of recursive method of printing all permutations of $n$ given integers

I really don’t understand this pseudo code. The function prints all permutations of $ n$ given integers, assuming that all numbers are different.

Is there a way to explain this code more easily as I really don’t get the purpose of the swapping.

PERMUTATE(A, start, end)    if start == end return A    else       PERMUTATE(A,start+1,end)       for i = start+1 to end          SWAP(A[i],A[start])          PERMUTATE(A,start+1,end)          SWAP(A[start],A[i]) 

Subset sum problem for permutations

Given permutations $ g_1,\,\ldots, g_m \in S_n$ of size $ n$ and target permutation $ g \in S_n$ , decide if there exists a subset of $ \{g_1,\, \ldots, g_m\}$ , which composition in some order is equal to $ g$ , i.e. $ g_{i_1} \circ g_{i_2} \circ \cdots \circ g_{i_k} = g$ .

This is actually a subset sum problem, but for symmetric group $ (S_n, \circ)$ instead of $ (\mathbb{Z}, +)$ .

The questions are:

  1. Is there a known solution in polynomial time?
  2. Otherwise, is this problem known as NP-complete?

I’ve found a paper on knapsack problems in groups, however, these results seems to be not applicable for symmetric group.

Combinations and Permutations of M sets of distinct items?

I’m wokring on this problem for a while. I want to know:

  1. The correct name of this problem, so I can look it up in textbooks\online.

Here is the problem descirption: enter image description here

The (un-ordered) combinations to be generated are:

  • of 1 item size: {a1}, {a2} …. {c4}
  • of 2 items size: {a1, b1}, {a1, b2} … {b2, c4}
  • of 3 items size: {a1, b1, c1}, … {a3, b2, c4}

The question is: Given any number of sets M where each set has distinc number of items, How many (un-ordered) combinations are there of size 1, 2, …, up to M? And how to generate those combinations?

Applying subproblem technique to permutations with grouping

I am trying to apply overlapping subproblems and dynamic programming to permutations.

Say, we have a set of $ n$ elements in a string. Each of these elements could be a $ 1$ or a $ 0$ .

Given some string, I am trying to count the number of valid permutations- where a valid arrangement is defined as an ordering with no $ 1$ that stands alone from other 1s.

Example input string: $ 0101$

Example of valid arrangements: $ 1100, 0011, 0110$

Not valid: $ 0101, 1010, 1001$


For example:

Say our input string contains four 1s and two 0s.

I suppose my difficulty is coming with defining the sub-problem. I initially thought I could set up a string with only the $ 0$ elements with slots/bins in between. $ [ ] 0 [ ] 0 [] $ , and then continue to use combinatorics to compute the number of additional arrangements possible with each 1 added in.

The first 1: 0 possibilities, because it will never have a 1 to accompany it.

Adding the 2nd 1: 3 possibilities, because the pair can go in either of the 3 slots.

Adding the 3rd 1: 0 new possibilities, because it must go with the other 2 ones, wherever they are.

Adding the 4th 1: We have enough to make 2 groups of $ 1$ s now… here is where I start getting unsure. I’d think it would be an additional 3 possibilities. $ \binom{3}{1} + \binom{3}{2} = 6$ total

Would anyone have an idea if I’m on the wrong track, or if not, how to proceed? This approach doesn’t seem like it works for larger values. Thanks!

Sum of n sums, Permutations of the indices, how to write them in Mathematica?

I was wondering how to write a function $ F (r, q, n, f) $ in Mathematica, defined in this way:

$ $ F(r,q,n,f):=\sum_{i_0=1}^q f(i_0) \Biggl(\sum_{i_1=i_0+1}^{q+1} f(i_1)\biggl(\sum_{i_2=i_1+1}^{q+2} f(i_2)\Bigl(\ldots(\sum_{i_n=i_{n-1}+1}^{q+n} f(i_n))\ldots \Bigl) \biggl) \Biggl)$ $ es. $ $ \sum_{i_0=1}^2 f(i_0) \Biggl(\sum_{i_1=i_0+1}^{3} f(i_1)\biggl(\sum_{i_2=i_1+1}^{4} f(i_2) \biggl) \Biggl)=f(1)f(2)f(3)+f(1)f(2)f(4)+f(1)f(3)f(4)+ +f(2)f(3)f(4)$ $

does an operator already exist that can be used in this way?

trying to write this function on mathematica I realized that the “recursion” is variable and I don’t know how to program in this case.

thank you

$ \ $

$ \ $

further example

$ $ \sum_{i_0=1}^1 f(i_0) \Biggl(\sum_{i_1=i_0+1}^{2} f(i_1)\biggl(\sum_{i_2=i_1+1}^{3} f(i_2)(\sum_{i_3=i_2+1}^{4} f(i_2)) \biggl) \Biggl)=f(1)f(2)f(3)f(4)$ $

Simple generator of pseudo-random permutations of variable length short sequence

The problem in front of me is to write a function (from scratch) to permute n elements, where n is an argument. I decided to break it down to applying Knuth’s shuffles algorithm, therefore I needed to write a pseudorandom number generator.

So now my task is to write a simple function F(seed,n) that will help me generate indices for the pseudorandom permutations of n elements. However, the function has to be extremely simple: My constraint is that I can only use the following operators: +,-,*,/,%, specifically, no address access, binary encoding, bits selection etc. – only arithmetic on numbers. That is fine, I went for linear congruental generators and implemented a (a*x+c)%m procedure.

As I started testing F(seed,2) it immediately went apparent that the results oscilate between zero and one with a period of two.

My problem is: how can I adjust F to avoid that behavior? Maybe I should generate the n-permutations in a different manner?

In practice, I am only interested in n=<32

Generating all possible permutations in the fox, goose and beans problem

I have learned graph theory, and am using it to solve the classic Fox, goose and bag of beans puzzle. If we consider that there are three locations for each of the fox (X), goose (G), farmer (F) and beans (B) (near side of river, in the boat, far side of the river), and there are four characters, there are hence 4^3 = 81 combinations. Two of these combinations would be

(Near)    (Boat)    (Far) F,G,B,X     -         -  (Near)    (Boat)    (Far)  B,X        F,G        - 

…for the starting state and the state when the farmer and the goose have entered the boat respectively.

I want to generate all of these states programatically (in C++, but that is beyond the point), but simply cannot get my head around how to do it. (I know that some of these 81 possible states will be invalid (e.g. that with all characters in the boat at once) but I can search for those and remove them afterwards.)

I am well aware of binary and how I get 81 combinations, and am aware using C++ I could generate all combinations of the characters F,G,X,B, but still cannot figure how to add the three “locations” into this.

Can someone please advise?


OK, can the downvoter explain how “this question does not show any research effort; it is unclear or not useful”. Do you want me to list everything I’ve tried and every single search I’ve done??

(And yes, I have extensively searched for solutions before posting here.)

Is all permutations of a string a good way to index full text search?

I am writing an application to be used as a local disc documents store similar functionality to Firebase or MongoDB. The gist of how it works is a column hash table. For example:

Say I have a user document

{   "first": "stack",   "last": "overflow",    "age": 2147 } 

I basically iterate each field as a column and save a hashed value as a filename for a file, that contains the unique row key(if multiple objects have the same value for a column there will be multiple row keys written to the file). Then save record in a directory with the key as the filename. So for a simple name = “stack” I have an O(1) lookup, hash the value, see if a file exists with the hashed value as a name, if exists, read all row keys from file. iterate each key and load data from file. I guess if the number of rows is large this will be a slower query.

What I am trying to do I want to ad a partial text search capability. My initial idea is to write all permutations of the value into the index and then do the same O(1) – file exists – check that I mentioned above. So if the query is name name contains st I will just look a filename for that value and read all rows keys.

Is this a good way to offer a partial text search? What are some common industry techniques?


All permutations with known letter on one place

Following code is doing what I want but I am looking for a way how to make it nicer and more effective

for i in c:   for j in c:     for k in c:         for l in c:             for m in c:                               if (i != j and i != k and i != l and j != k and k != l and i != m and j != m and k != m and l !=m):                 print i + j + 'u' + k + l + m 
odusdi odusdt odusit odusti oduids oduidt oduist oduits odutds odutdi odutsi ... 

I tried filters, lambdas and generators but that makes everything just worse.

I am new to python so I also have problems with copying and sending python code any hint with comment would be nice.

How to create a dictionary from a list of words and their permutations [on hold]

So I’ve forgot a luks device passphrase, nothing critical but I’d like to have the content back. I kinda remember what it was about and the words included, but not the exact order of words, so instead of trying manually I’d like to create a dictionary.

I’ve already read about crunch, but it has a problem: the passphrase could be from 10 to 30 characters long, and that would create a dictionary of serveral thousands of petabytes (even restricting the charset) and I couldn’t find an option to use a wordset instead of a charset.

So, I have a list of words that I’m quite sure are included in the passphrase (10 at maximum) and create a dictionary with permutation of them in different order, and also some variation (uppercase for first letter, 1337 characters, numbers and symbols at the end, etc..)

For example, this could be a set of words

  • password
  • luks
  • disk
  • 1987 (I’m pretty sure there was a date)

And this is how I think the dictionary should be

luks password password1987 Password1987 LuksPassword1987 Lukspassword1987 PasswordLuks! password1987diskLuks ... 

Is there another dictionary generating tool that could make this possible?