Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not the most efficient solution, but here's something that should get you started:</p> <p>Do a DFS, then compute the intersection of all paths (nodes that exist in every path). Then, among those nodes, find the one that appears closest to the beginning in every path:</p> <pre><code>&gt;&gt;&gt; paths [] &gt;&gt;&gt; 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]) ... &gt;&gt;&gt; dfs(G, 2, []) &gt;&gt;&gt; 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]] &gt;&gt;&gt; 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] &gt;&gt;&gt; nodes = [set(L) for L in paths] &gt;&gt;&gt; commons = functools.reduce(set.intersection, nodes) &gt;&gt;&gt; commons {12, 6} &gt;&gt;&gt; min(commons, key=lambda v: max(L.index(v) for L in paths)) 6 </code></pre> <p>Now, note how <code>6</code> shows up at index 2 in some paths and at index 1 in some other paths. If there was a node (say x), that showed up at index 1 in the paths where 6 shows up at index 2, and at index 2 where 6 shows up at index 1, then, that would be a tie, which this algorithm would break arbitrarily. Depending on your needs, you might want to define how to handle this case better</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload