0

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 1.0
a2 a3 0.5
a3 a5 1.0
a4 a8 1.0
a8 a9 0.8

It will result in two sets

[(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)]

essentially the weights in the relationships gets multiplied by the weight. Is there any efficient algorithm to track this other than brute forcing? The language of choice is python.

DonDyck
  • 1,451
  • 5
  • 20
  • 35

1 Answers1

0

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})]
Kirell
  • 9,228
  • 4
  • 46
  • 61