What Is The Algorithm To Get Disjoint Set From Edgelist With Weights
I have a list of edges with weights and I want to get the disjoint set from them. However, I want to track the weights also in the set. e.g If I have a dataset, N1 N2 Weight a1 a2
Solution 1:
It is not very clear for me what you are asking. However, I believe that you are looking for the Connected components of your graph. In that case you can extract the 2 subgraphs that correspond to the disjoint set of edges in your original graph like so:
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))
Results
[('a1', 'a2', {'weight': 1.0}), ('a3', 'a2', {'weight': 0.5}),
('a3', 'a5', {'weight': 1.0})]
and
[('a9', 'a8', {'weight': 0.8}), ('a8', 'a4', {'weight': 1.0})]
Post a Comment for "What Is The Algorithm To Get Disjoint Set From Edgelist With Weights"