Search code examples
pythonpython-3.xdata-structurescomputer-science

Find round routes in a list of tuples


I have a list of tuples containing sources and destinations I need to make sure there are no "round routes"

For example:

[(1,2), (3,4), (2,3)]. is OK

but

[(1,2), (3,4), (2,3), (3,1)]. 

It is not OK as we can go from 1 → 2 → 3 → 1

I got nostalgic and remembered my computer science degree, so I thought of Graph data structure. Unfortunately I could not find a good implementation for it using python, and all examples I found googling (and here on stackoverflow) are only for finding shortest path.

Any ideas how can I implement this?


Solution

  • Here is an implementation in Python that works pretty well (also available in Java and C++, but I only tried the first case). https://www.techiedelight.com/check-given-digraph-dag-directed-acyclic-graph-not/