Home > Software engineering >  Get weight combining edges in Networkx
Get weight combining edges in Networkx

Time:12-31

Hello I am using Networkx with Python to make some graphs.

After extracting the edges from a .CSV structured as "source" and "target" I got a list of edges like this:

edges = [(a,b), (a,c), (b,c), (a,b), (a,b), (a,c)]

Is there any way to build ( after creating a MultiDiGraph with these edges) a Directed Graph combining the equal edges getting the weights as the number of occurrences of the same edge, resulting in something like this:

edges = [(a, b, weight = 3), (b, c, weight = 1), (a, c, weight = 2)]

I have achieved something similar transforming the csv to list and then to counter using the following code:

reader = csv.reader(open('edges_list.csv', 'r')) 
next(reader, None) 
list_edges = list(reader)  
count = Counter(map(tuple, list_edges))  
list= [list(i) for i in count.items()] 
list= [[k,v,w] for (k,v),w in list]

But I would like to know if there is some direct function in Networkx to combine parallel edges in a Multidirected Graph as weight to a Directed Graph.

CodePudding user response:

After a cursory search through the docs and related posts on this site, it seems like networkx does not have this kind of functionality.

  • Related