I have a list of tuples with the start and end node ID of a segment. I want to rearrange the tuples (segments) in a way that constructs a continuous path. There can be a loop in the path i.e., one node can be traversed multiple times. I do know the origin and destination of the entire path. There may be some segments that cannot be included in the path as their start or end node cannot be connected to the previous or next node to form that continuous path. These segments should be removed from the rearranged list of tuples.
Example:
The list of tuples may look like the following. Also, I know that the path should start at 101
and end at 205
.
[(101, 203), (104, 202), (203, 104), (104, 208), (185, 205), (202, 185)]
Expected Results:
[(101, 203), (203, 104), (104, 202), (202, 185), (185, 205)]
I would like to solve this in Python. My understanding is that this can be solved using a loop that looks at the end node ID of each segment, finds a segment with a similar start node ID, and adds that append that segment to the list of rearranged tuples. I am not quite sure how it can be solved efficiently. So, any hints or example codes would really help.