Todas las combinaciones posibles de conjuntos

Tengo un conjunto de números:

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

preguntado el 10 de marzo de 12 a las 08:03

may you can find here [here][1] [1]: stackoverflow.com/questions/3742506/php-array-combinations Gracias -

1 Respuestas

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.

EDITAR

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

respondido 10 mar '12, 08:03

That is actually a good idea. I'll look into it but I'm not sure if it helps me to actually create all the different combinations. - palanqueta

If all the paths you want to trace must end at 1 then start there and work backwards. Have fun. - Marca de alto rendimiento

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.