11

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 ?

5
  • Could you explain how the N numbers come into this? What would the answer be if the N's where {1, 2, 3, 4}? Feb 27, 2010 at 0:44
  • From what I can see, the N numbers you're given are irrelevant to the question you're asking. Is this correct?
    – sykora
    Feb 27, 2010 at 0:44
  • N is how many numbers there are, in this case N = 4 because there are four numbers, 1..4 .
    – VaioIsBorn
    Feb 27, 2010 at 0:57
  • 1
    In what sense did your brute force solution not work? Was it just way too slow or did it crash, or ... ?
    – Tyler
    Feb 27, 2010 at 1:00
  • 2
    If bruteforcing didn't work, your code is wrong. Feb 27, 2010 at 1:32

3 Answers 3

9

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.

1
  • 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,4 4,5 5,8 You know you can permute [3458] with prepends, inserts and appends of your integers unconstrained by precedence rules (this of course generalizes into the above) Feb 27, 2010 at 1:31
4

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.

3
  • actually he could check the rules, while creating permutations. This would cut the time considerably, and he would end up with a result.
    – Kugel
    Feb 27, 2010 at 0:55
  • 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. Feb 27, 2010 at 0:56
  • 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
    Feb 27, 2010 at 0:57
1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.