The segments can be used to form an abstract graph *G* in which they play the role of *nodes*. Consider a node that is the segment (arc) from point P to point Q, PQ. Let R be the closest endpoint among all the other segment endpoints to P and let S be the other endpoint of R's segment. *G* then contains an edge from node PQ to node RS and we will label this edge with the points P and R. If we are to be successful, then *G* is either a linear graph or a single cycle. You can detect which is which by storing the degrees of the nodes as you create the edges. (The degree of a node counts the edges emanating from that node.) All nodes, except possibly two of them, must have degree 2. The other two must both have degree 2 (for a cycle) or degree 1: this marks them as the ends of the polyline. In the first case pick any node to start constructing the polyline; in the second case, pick one of those of degree 1. Any other combination of degrees is an error. The polyline is initialized to the starting node (an arc). Look at one of the edges *e* incident on this node: its other node tells you which arc to process next and its label tells you which vertices of those arcs to join. (Join the vertices with a new line segment if they do not have identical coordinates.) Update the growing polyline in this fashion and, at the same time, remove edge *e* from the graph *G*. Continue until either an error occurs (and report that the edges do not form a non-branching connected polyline) or all edges are removed. If no error is raised, output the polyline you created. ### Example ![Sketch of the arcs][1] In the figure the arcs are AB, CD, EF, and FG. The nodes of the graph are therefore {AB, CD, EF, FG}. The edges are AB--CD (labeled with B and C), CD-EF (labeled with E and F), and EF--FG (labeled with F and F). The degrees of AB and FG are 1 whereas the degrees of CD and EF are 2. Here is a schematic of the abstract graph and its edge labels: ![The graph][2] Let us arbitrarily start with FG, one of the degree-one nodes. Because it has degree 1, there is a unique edge EF--FG connected to it, labeled with F. Initialize the polyline with arc G-->F. Because the label designates a common endpoint of GF and EF, we don't have to make a new connection. Remove edge EF--FG from the graph and extend the polyline with EF via G-->F-->E. This edge removal reduces the degree of EF from 2 to 1, leaving it with a single edge to arc CD labeled with E and D. This tells you to extend the polyline from E to D (with a new line segment there) and thence along arc CD: G-->F-->E-->D-->C. In the same manner, after removing edge ED--CD, you will extend the polyline further to its final form G-->F-->E-->D-->C-->B-->A. You stop because all edges have been removed from the graph: this indicates the procedure was successful. [1]: https://i.sstatic.net/Q9bvh.png [2]: https://i.sstatic.net/f4bA1.png