views:

209

answers:

3

I am given N numbers and for them apply M rules about their order. The rules are represented in a pairs of indexes and every pair (A, B) is telling that the number with index A (A-th number) must be AFTER the B-th number - it doesn't have to be next to him.

Ex: N = 4
    1 2 3 4
    M = 2
    3 2
    3 1

Output: 1234, 4213, 4123, 2134, 2143, 2413, 1423 ...Maybe there are even more:) 

The algorithm should give me all the permutations available that don't break the rules, like in the example - 3 must always be after 2 and after 1.

I tried bruteforcing but it didn't work (although bruteforce should work in here, N is in the range (1,8). )

Any ideas ?

+8  A: 

Just as a hint.

You can treat your set of rules as a graph. Each index is a vertex, each rule is a directed edge.

Any proper ordering of the numbers (i.e. a permutation that satisfies the rules) corresponds to so called topological ordering of the above graph. In order to generate all valid orderings of your numbers you need to generate all possible topological orderings of that graph.

P.S. The first algorithm for topological ordering given at the linked Wikipedia page already allows for a fairly straightforward solution that would enumerate all valid permutations. It will take some effort and some care to implement, but it is not rocket science.

AndreyT
As a step before going to a full topological sort, you can at least create ordered fragments to simplify the brute-forcing. For example, if the precedence rules are:3,44,55,8You know you can permute [3458] with prepends, inserts and appends of your integers unconstrained by precedence rules (this of course generalizes into the above)
Michael Mullany
+3  A: 

Brute forcing would be going through every permutation, which is O(N!), and for each permutation simply looping through every rule to confirm that they aplpy, which is O(M). This ends up O(N!M) which is kind of ridiculous, but it shouldn't "not work" for such a small set.

Tanzelax
actually he could check the rules, while creating permutations. This would cut the time considerably, and he would end up with a result.
Kugel
I agree. If N=8 is the highest that you need to be able to handle, then it probably isn't worth your time to come up with something better than brute force for something like this.
Justin Peel
Yeah, there's definitely a lot of easy optimizations to be made to a brute force solution, but he mentions he's having problems with even that.
Tanzelax
+2  A: 

Honestly, your best bet is to go back and get the brute force solution working. Once that is done (and if you still have time, etc) you can look for a better algorithm.

EDIT to the down voter. The student is (should be) trying to get his homework done on time. By the sounds of it, his homework is a programming exercise where a brute-force solution would be adequate. Helping him to figure out an efficient algorithm is not addressing his REAL problem.

In this case he has tried the simple brute-force approach (which everyone agrees ought to work for small N values) and given up on it prematurely to try something that is probably more difficult. Any experienced developer will tell you that this is a bad idea. The student needs and deserves to be told so, and if he is sensible he will pay attention. But obviously, it his choice ...

Stephen C
@serial_downvoter: Canceled you out. Ha ha!
Billy ONeal