Given a list of tuples, I need to find all unique paths from this list :
Input: [('a','b'),('b','c'),('c','d'),('g','i'),('d','e'),('e','f'),('f','g'),('c','g')]
Output: [['a','b','c','d','e','f','g'],['a','b','c','g','i']]
(the 2 possible unique paths)
Two tuples can connect if the second element of the tuple matches with the first element of the other tuple i.e: One tuple is (_,a)
and other tuple is like (a,_)
.
This issue has already been raised there: Getting Unique Paths from list of tuple but the solution is implemented in haskell (and I know nothing about this language).
But do you know if there's an efficient way to do this in Python?
I know the library itertools
has many efficient built in functions for stuff like that, but I'm not too familiar with this.