📜  networkx - 路径的唯一组合 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:06.177000             🧑  作者: Mango

代码示例1
# 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)