Answers for "networkx - unique combinations of paths"

0

networkx - unique combinations of paths

# Unique combinations of paths
gg = nx.complete_graph(6)         # Create a complete graph
nx.draw(gg, with_labels = True)   # Plot graph
source = 0                        # Set source node
target = 3                        # Set target node
gg.remove_edge(0, 3)

paths = nx.all_simple_paths(gg, source=source, target=target, cutoff=5)    # Find all paths between two nodes

# Method 1
s = set(map(frozenset, paths))                                             # Remove duplicates {frozenset({0, 3, 4}), frozenset({0, 2, 3})...  
non_redundant_paths = [[source, *[*p-{source,target}],target] for p in s]

# Method 2
non_redundant_paths = []
seen = []
for p in paths:
    if set(p) not in seen:                 # Keep track of the seen ones using a set
        non_redundant_paths.append(p)
        seen.append({*p})
        
print(non_redundant_paths)
Posted by: Guest on September-18-2020

Python Answers by Framework

Browse Popular Code Answers by Language