## sorting two paired lists of real numbers to minimize consecutive absolute differences

### Remember to vote up questions/answers you find interesting or helpful (requires 15 reputation points)

Consider a set of $n$ real-valued number pairs: $(x_1,y_1), (x_2,y_2), \dots, (x_n,y_n)$. I want to find a permutation $p$ of the indices which minimizes the sum of consecutive absolute differences:

$$\sum_{j=1}^{n-1} |x_{p(j+1)} - x_{p(j)}| + \sum_{j=1}^{n-1} |y_{p(j+1)} - y_{p(j)}|.$$

I suspect this is reducible to a well known problem, so I'm looking for pointers to literature mainly, but would be happy to see a clever algorithm for doing this from scratch.

Intuitively, I want to shuffle the observations so that the graph of $x$ elements against the index is smooth looking and the same graph of the $y$ elements is also smooth looking. If I cared only about one or the other, I could simply sort with respect to those elements. I want to shuffle in such a way that I compromise between the two coordinates.

My motivation is a statistical problem of estimating a smooth curve in the plane by assuming that the coordinate dimensions are each smooth functions of an unrecorded "time index". The above problem is maximizing the smoothness of the observed data under the assumption of evenly spaced observations in time.

-

1
This sounds like you want a Hamiltonian path in the $L^1$ metric on $\R^2$ of shortest length. – Anthony Quas Jan 23 at 18:19
Yeah, I just remembered that the last time I thought about this I realized it could be formulated as a traveling salesman type problem. I need to go look at algorithms tailored to the version in the plane. – R Hahn Jan 23 at 18:21

## 2 Answers

This is known in complexity circles as rectilinear TSP. Technically this is a path version TSP rather than the more common tour. In any of these specific settings, this is known to be NP-complete since the 70s (see Papadimitriou's paper).

However, unlike some NP-hard problems, Euclidean and rectilinear TSP admit a polynomial-time approximation scheme: one can obtain a tour of cost at most $(1+\epsilon)$ times optimal in time $O(n (\log n)^{O(1/\epsilon)})$. See Arora's web page for some of the key papers on this subject. You'll also find there a nice survey on approximation results (postscript link), outlining the key ideas.

The survey mentions a subexponential $2^{O(\sqrt{n})}$ exact algorithm for the Euclidean case by Smith (1988). While I would not be surprised if it adapts readily to $L^1$, I don't know enough about it to say for certain.

-

### You can accept an answer to one of your own questions by clicking the check mark next to it. This awards 15 reputation points to the person who answered and 2 reputation points to you.

Here is something to try. Even if it fails, knowing why it fails might be useful.

Consider the Steiner tree joining your set of points. It should have minimal length and can give you a goal. If the Steiner tree has branches at points that are not on your vertex set, consider a tour that does one subtree followed by the other subtree followed by the last subtree in decreasing order of branch length.

Now that I think on it, there is potential for enough triple branch points that you might be able to reduce something like exact three cover to this problem. Locally, though, you might consider Steiner tree suggestions as to order. If not Steiner tree, then whatever tree is suggested by the Manhattan metric.

EDIT: indeed, months in the laboratory can save hours spent in the library. As Gerry Myerson suggests in his comment, there are computationally infeasible problems related to Steiner trees, and I have likely suggested one above. Moreover, the problem of rectilinear Steiner trees has been studied, thus giving more search terms for the original poster to use. Even so, there are good approximations to some Steiner tree problems, and I suspect this problem will be similar. END EDIT.

Gerhard "As Lovely As A Tree" Paseman, 2013.01.23

-

1
As a wicked test case, take the vertices of some not too small iterate of Sierpinski's triangle, rotated to produce distinct x and y coordinates, and see what you get. Gerhard "Maybe You Get A Paper" Paseman, 2013.01.23 – Gerhard Paseman Jan 23 at 20:53
How do you propose to find the Steiner tree? That's already computationally infeasible, no? – Gerry Myerson Jan 23 at 23:04
I don't know. I believe a greedy algorithm can find a Steiner tree, at least a local minimum, by adding one vertex at a time. At the time of posting the answer, I thought a global minimum could be found in polytime, and was feasible. Gerhard "Others Can Make Trees Too" Paseman, 2013.01.23 – Gerhard Paseman Jan 23 at 23:22
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 11, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9397596716880798, "perplexity_flag": "middle"}