Not the most effective solution, but here is something you need to get started:
Make DFS, then calculate the intersection of all paths (nodes that exist in each path). Then, among these nodes, find the one that will be closest to the beginning in each path:
>>> paths [] >>> def dfs(G, s, path): ... if s not in G or not G[s]: ... paths.append(path) ... else: ... for t in G[s]: ... dfs({k:[_t for _t in v if _t!=t] for k,v in G.items()}, t, path+[t]) ... >>> dfs(G, 2, []) >>> paths [[3, 4, 6, 7, 12], [3, 4, 6, 8, 9, 10, 12], [3, 4, 6, 8, 9, 12], [3, 4, 6, 8, 11, 12], [3, 5, 6, 7, 12], [3, 5, 6, 8, 9, 10, 12], [3, 5, 6, 8, 9, 12], [3, 5, 6, 8, 11, 12], [4, 6, 7, 12], [4, 6, 8, 9, 10, 12], [4, 6, 8, 9, 12], [4, 6, 8, 11, 12]] >>> for path in paths: print(path) ... [3, 4, 6, 7, 12] [3, 4, 6, 8, 9, 10, 12] [3, 4, 6, 8, 9, 12] [3, 4, 6, 8, 11, 12] [3, 5, 6, 7, 12] [3, 5, 6, 8, 9, 10, 12] [3, 5, 6, 8, 9, 12] [3, 5, 6, 8, 11, 12] [4, 6, 7, 12] [4, 6, 8, 9, 10, 12] [4, 6, 8, 9, 12] [4, 6, 8, 11, 12] >>> nodes = [set(L) for L in paths] >>> commons = functools.reduce(set.intersection, nodes) >>> commons {12, 6} >>> min(commons, key=lambda v: max(L.index(v) for L in paths)) 6
Now notice how 6 displayed in index 2 in some paths and in index 1 in some other paths. If there was a node (say, x) that appeared in index 1 on the paths where 6 is displayed at index 2, and at index 2, where 6 is displayed at index 1, this would be a connection that this algorithm would interrupt arbitrarily. Depending on your needs, you can determine how best to deal with this case.