No matter how effiecient the algorithm you finally settle on will be, it will still have to print the same values. So I guess what you really need is an algorithm for printing permutations of a certain range of number without printing the same permutation twice
Also you will need to find a way of storing the values that are restricted in such a way that they are in the order the restrictions keep them in.
The number of things to print is equal to (length of restricted * length of non-restricted)+1
0.o @JLBorges is on the right track here. Take a look at definition of topological sort:
In computer science, a topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks.
@JLBorges That is a very good idea, only which of the two algorithms described in the wiki article would be best suited? I would lean towards the second due to the fact that it is recursive. Also would you please give a little advice on how to perform your extension.