簡體   English   中英

用權重從邊列表中獲得不相交集的算法是什么

[英]What is the algorithm to get disjoint set from edgelist with weights

我有一個帶有權重的邊的列表,我想從中得到不相交的集合。 但是,我也想在集合中跟蹤權重。 例如,如果我有一個數據集,

N1 N2 Weight
a1 a2 1.0
a2 a3 0.5
a3 a5 1.0
a4 a8 1.0
a8 a9 0.8

這將導致兩套

[(a1,1.0), (a2,1.0), (a3,1.0*0.5), (a5,0.5*1.0)] and [(a4,1.0),(a8,1.0), (a9,1.0*0.8)]

本質上,關系中的權重乘以權重。 除了強行強制外,是否有任何有效的算法可追蹤此情況? 選擇的語言是python。

對我來說,你要問的不是很清楚。 但是,我相信您正在尋找圖形的Connected組件 在這種情況下,您可以提取與原始圖中不相交的邊集相對應的2個子圖,如下所示:

G = nx.Graph()
G.add_weighted_edges_from([('a1', 'a2', 1.0), ('a2', 'a3', 0.5), 
                           ('a3', 'a5', 1.0), ('a4', 'a8', 1.0), ('a8', 'a9', 0.8)])


graphs = list(nx.connected_component_subgraphs(G))
for g in graphs:
    print(g.edges(data=True))

結果

[('a1', 'a2', {'weight': 1.0}), ('a3', 'a2', {'weight': 0.5}), 
 ('a3', 'a5', {'weight': 1.0})]

[('a9', 'a8', {'weight': 0.8}), ('a8', 'a4', {'weight': 1.0})]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM