简体   繁体   中英

How to add dummy nodes in NetworkX?

I am trying to generate the shortest path but I need to generate dummy nodes to do this as I have several edges from Istanbul to Ankara so I am unable to create a path using the normal method since the model considers those edges as one edge. My nodes are shown in the first two columns of the excel sheet (Node1 and Node2). I wanted to generate the shortest path using Node1_reference and Node2_reference but I am unsure how to go about it or whether I should create dummy nodes as I am unable to call the cities without the suffixes

在此处输入图像描述

在此处输入图像描述

I suppose you already have your graph

g = nx.Graph()

then you add nodes with

g.add_node('Istanbul')
g.add_node('Ankara')
g.add_node('Muscat')

then you should add edges

g.add_edge('Istanbul', 'Ankara')
g.add_edge('Istanbul', 'Muscat')
g.add_edge('Ankara', 'Muscat')

and then you can check if there's a path:

print(nx.has_path(g, 'Istanbul', 'Ankara'))

Granted, if you're looking for the shortest path, you'll have to add attributes to the edges unless they all have the same cost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM