Finding maximum weighted edge in a networkx graph in python

本小妞迷上赌 提交于 2019-12-23 15:26:11

问题


I want to find 'n' maximum weighted edges in a networkx graph. How can it be achieved. I have constructed a graph as follows :

g_test = nx.from_pandas_edgelist(new_df, 'number', 'contactNumber', edge_attr='callDuration')

Now, I want to find top 'n' edge weights, i.e. top 'n' callDurations. I also want to analyse this graph to find trends from it. Please help me how can this be achieved.


回答1:


If your graph is stored as g you can access its edges, including their attributes using:

g.edges(data=True)

This returns a list of tuples. The first two entries are the nodes, and the third entry is a dictionary of the attributes, looking like this:

[(a,b,{"callDuration":10}),(a,c,{"callDuration":7})]

You can sort this list based the callDuration attribute like this:

sorted(g.edges(data=True),key= lambda x: x[2]['callDuration'],reverse=True)

Note we use reverse to see the largest callDuration edges first.

I'm afraid your second question is very broad - you can do a lot of things with networks! Have a look at some tutorials like this one: https://programminghistorian.org/en/lessons/exploring-and-analyzing-network-data-with-python




回答2:


Let's try:

max(dict(g_test.edges).items(), key=lambda x: x[1]['callduration'])

To find the maximum weight edge in this graph network.



来源:https://stackoverflow.com/questions/52440518/finding-maximum-weighted-edge-in-a-networkx-graph-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!