I currently have a list of connections stored in a list where each connection is a directed link that connects two points and no point ever links to more than one point or is linked to by more than one point. For example:
connections = [ (3, 7), (6, 5), (4, 6), (5, 3), (7, 8), (1, 2), (2, 1) ]
Should produce:
ordered = [ [ 4, 6, 5, 3, 7, 8 ], [ 1, 2, 1 ] ]
I have attempt to do this using an algorithm that takes an input point and a list of connections and recursively calls itself to find the next point and add it to the growing ordered list. However, my algorithm breaks down when I don't start with the correct point (though this should just be a matter of repeating the same algorithm in reverse), but also when there are multiple unconnected strands
What would be the best way of writing an efficient algorithm to order these connections?
Something like this:
output:
I think you can probably do it in
O(n)
with something like this:At the end you will be left with something like this, but you can convert that rather easily
Algorithm for a Solution
You're looking for a topological sort algorithm:
Here is the output for your sample data:
The runtime is linearly proportional to the number of edges (dependency pairs).
HOW IT WORKS
The algorithm is organized around a lookup table called num_heads that keeps a count the number of predecessors (incoming arrows). Consider an example with the following connections:
a->h b->g c->f c->h d->i e->d f->b f->g h->d h->e i->b
, the counts are:The algorithm works by "visting" nodes with no predecessors. For example, nodes
a
andc
have no incoming edges, so they are visited first.Visiting means that the nodes are output and removed from the graph. When a node is visited, we loop over its successors and decrement their incoming count by one.
For example, in visiting node
a
, we go to its successorh
to decrement its incoming count by one (so thath 2
becomesh 1
.Likewise, when visiting node
c
, we loop over its successorsf
andh
, decrementing their counts by one (so thatf 1
becomesf 0
andh 1
becomesh 0
).The nodes
f
andh
no longer have incoming edges, so we repeat the process of outputting them and removing them from the graph until all the nodes have been visited. In the example, the visitation order (the topological sort is):If num_heads ever arrives at a state when there are no nodes without incoming edges, then it means there is a cycle that cannot be topologically sorted and the algorithm exits to show the requested results.