Weighted Nodes

49 views
Skip to first unread message

Spencer Gardner

unread,
Aug 17, 2016, 3:42:53 PM8/17/16
to networkx...@googlegroups.com
Is it possible to get the shortest path using node weights in place of (or in addition to) edge weights? I have a graph with weights but the shortest path algorithms appear to ignore node weights.

For reference I've attached an output of my network nodes and edges. At this point my edges are all weighted at zero. When I run shortest_path it gives me the path with the fewest links as opposed to the one with the lowest weight based on the node weightings.

I'm running version 1.11 on Ubuntu 16.04.

Thanks for your help.
network output.txt

Daniel Schult

unread,
Aug 18, 2016, 8:12:32 AM8/18/16
to networkx...@googlegroups.com
There is a pull request to add this feature explicitly, but it is not yet in the most recent version.

In the development version of NetworkX (from github), the 'weight' keyword of shortest_path can be any function which takes (u, v, data) and returns a number. You can probably use this feature to create a weight function which includes the node weights instead of (or in addition to) the edges weights. 

# untested -- try it on a simple network first
get_node_weight = lambda u,v,d: G.node[v].get('weight', 0)
nx.shortest_path(G, source=3, weight=get_node_weight)



--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.

sndr....@gmail.com

unread,
Mar 28, 2017, 6:38:53 PM3/28/17
to networkx-discuss

good question 
Reply all
Reply to author
Forward
0 new messages