1

I have a set of numbers:

1,22
1,46
32,1
1,9
32,22
1,14
1,45
1,33
33,22
45,22
32,46
32,9
3,1
3,9
3,22
3,32
3,46
9,22
46,22
46,45
46,33
15,1
15,46
15,6
15,22
15,3
15,9
15,45
15,33
15,32
15,14

I need to get combinations from them with a rule that each new pair can only be appended if the latter number is the same as the first in the pair.

For example if I have a pair {15,1}, the next on can be only {1,46} and the next {46,45}, and the final pair must end with the first number of the whole set. In this case it could be for example {45,1}.

So the end result of sets with 4 set limit would be

{15,1,1,46,46,45,45,1}

I can do basic power sets and generate all possible combinations from set of numbers but this seems to be too advanced for me.

I can do C, Javascript or PHP so all the help or solutions to this are highly appreciated. And for clarification, this is not a homework, this is just something I would like to learn and understand.

jimmy
  • 1,313
  • 2
  • 10
  • 23

1 Answers1

0

This looks as if some graph data structure, and some graph algorithms, would be appropriate. Your graph would comprise nodes (each of which is a number) and edges (each of which represents one of your pairs). Then write the appropriate routine for walking round the graph. It's not entirely clear from your question what the rules for the walk are, but I guess you know.

EDIT

Of course, I should point out that what you have is already a graph data structure, it's called an adjacency list. Google around for algorithms and representations.

High Performance Mark
  • 74,067
  • 7
  • 97
  • 147