networkx unique combinations of paths

Solutions on MaxInterview for networkx unique combinations of paths by the best coders in the world

showing results for - "networkx unique combinations of paths"
Philipp
14 Mar 2019
1# Unique combinations of paths
2gg = nx.complete_graph(6)         # Create a complete graph
3nx.draw(gg, with_labels = True)   # Plot graph
4source = 0                        # Set source node
5target = 3                        # Set target node
6gg.remove_edge(0, 3)
7
8paths = nx.all_simple_paths(gg, source=source, target=target, cutoff=5)    # Find all paths between two nodes
9
10# Method 1
11s = set(map(frozenset, paths))                                             # Remove duplicates {frozenset({0, 3, 4}), frozenset({0, 2, 3})...  
12non_redundant_paths = [[source, *[*p-{source,target}],target] for p in s]
13
14# Method 2
15non_redundant_paths = []
16seen = []
17for p in paths:
18    if set(p) not in seen:                 # Keep track of the seen ones using a set
19        non_redundant_paths.append(p)
20        seen.append({*p})
21        
22print(non_redundant_paths)