Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given some inputs, which consist of a left and right symbol, output chains which link the inputs.

Imagine the inputs to be dominoes which you cannot flip horizontally and need to chain them together. Creating big circular chains (ignore if you cannot physically do it with real dominos) is preferred over small circular chains which are preferred over chains where the start and end does not match.

Outputs with more circular chains (regardless of how many or chain length) are what we are looking for. For example, an output of 3 circular chains is better than 1 big chain and a leftover single domino.

Can someone point me in the right direction? What group of problems does this belong and are there existing algorithms for solving this?

Examples (outputs may be incorrect!):

in[0]=(A,B)
in[1]=(B,C)
in[2]=(C,A)
out[0]=(0,1,2)

in[0]=(A,B)
in[1]=(B,A)
in[2]=(C,D)
in[3]=(D,C)
out[0]=(0,1)
out[1]=(2,3)

in[0]=(A,B)
in[1]=(B,A)
in[2]=(C,D)
in[3]=(E,F)
out[0]=(0,1)
out[1]=(2)
out[2]=(3)

in[0]=(A,B)
in[1]=(B,A)
in[2]=(C,D)
in[3]=(D,E)
out[0]=(0,1)
out[1]=(2,3)

in[0]=(A,B)
in[1]=(B,C)
in[2]=(C,D)
out[0]=(0,1,2)
share|improve this question
    
@Lieven Thanks for the correction. –  zaf Mar 28 '11 at 11:28
    
don't mention it but I would have rather known the answer. I was thinking in terms of graphs myself but it's an area where my skills are seriously lacking. Trying to better myself by reading amazon.co.uk/Algorithm-Design-Manual-Steven-Skiena/dp/…. I can highly recommend it if you, like me, don't have a strong enough mathematical background. –  Lieven Keersmaekers Mar 28 '11 at 11:36
1  
@Lieven Hey! I do have a strong mathematical background. I just haven't played dominoes enough :) –  zaf Mar 28 '11 at 11:41

4 Answers 4

Dominoes which cannot be flipped horizontally == directed graphs.

Putting dominoes one after the other is called a "path", if it is a closed path, it's a circuit.

A circuit that includes all the vertices of a graph is a Hamiltonian circuit.

Your problem in graph theory terms is: how to split (decompose) your graph into a minimum number of subgraphs that have Hamiltonian circuits. (a.k.a. Hamiltonian graphs)

share|improve this answer
    
Thanks for the tip. I'll go off check out the Hamiltonian stuff. –  zaf Mar 28 '11 at 11:43

The problem as it is now is not as clearly stated as it could be - how exactly are solutions rated? What is the most important criterion? Is it the length of the longest chain? Is there a penalty for creating chains of length one?

It is often helpful in such problems to visualize the structure as a graph - say, assign a vertex (V[i]) to each tile. Then for each i, j create an edge between vertices V[i], V[j] if you can place V[i] to the left of V[j] in a chain (so if V[i] corresponds to (X, A) then V[j] corresponds to (A, Y) for some X, Y, A).

In such a graph chains are paths, cycles are closed ("circular") chains and the problem has been reduced to finding some cycle and/or path covering for a graph. This type of problems can in turn often be reduced to matching or *-flow problems (max-flow, max-cost-max-flow, min-cost-max-flow or what have you).

But before you can reduce further you have to establish the precise rules according to which one solution is determined to be "better" than another.

share|improve this answer
    
I've added abit more info. The most important criteria is to minimise single dominos leftover. Its not totally clear what you are suggesting but I'll look up flow problems to see if there are any leads. Thanks. –  zaf Mar 28 '11 at 11:39

It is easy to check whether there exists a circular chain consisting of all dominoes. First you need to make the following directed graph G:

  • Nodes of G are symbols on the dominoes (A,B,C..) in your example,
  • For each domino (A,B) you put a directed edge from A to B.

There exists a circular chain consisting of all dominoes iff there exists a Eulerian cycle in G. To check whether there exists Eulerian cycle in G it is sufficient to check weather each node has even degree.

share|improve this answer
    
Thanks. Look like I've got more reading todo. –  zaf Mar 28 '11 at 11:44

I'm not sure if this is really the case, but judging by your examples, your problem looks similar to the problem of decomposing a permutation into a product of disjoint cycles. Each tile (X,Y) can be seen as P(X) = Y for a permutation P. If this agrees with your assumptions, the good (or bad) news is that such decomposition is unique (up to the cycle order) and is very easy to find. Basically, you start with any tile, find a matching tile on the other hand and follow this until no matching can be found. Then you move to the next untouched point. If that's not what you are looking for, the more general graph-based solution by t.dubrownik looks like the way to go.

share|improve this answer
    
Thanks for the simple idea and the permutation lead. –  zaf Mar 28 '11 at 11:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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