Adding a new node onto an edge near an object

347 views
Skip to first unread message

Dmitriy Tarasov

unread,
Aug 30, 2021, 1:00:05 PM8/30/21
to networkx-discuss
From what I understand, Dijkstra's algorithm can only find routes to and from nodes. However, I may need to trace paths to objects that are near edges, but not near existing nodes. Is there a way, in Networkx, to add a new node near an object onto the nearest edge?

Nicolas Cadieux

unread,
Aug 30, 2021, 1:49:39 PM8/30/21
to networkx...@googlegroups.com, Dmitriy Tarasov

Hi,

You are correct.  You need to find the closest node and create a new edge between the beginning of your route and the graph.  More knowledgeable people tell you what functions to use in NetworkX.  If I recall, you are doing this with geographic data.  You may chose to use Geopandas to introduce new nodes and edges to the graph. 

In this code, https://gitlab.com/njacadieux/upstream_downstream_shortests_path_dijkstra/-/blob/Beta/En_Construction_upstream_downstream_shortests_path_dijkstra_v2_210506.py,

I created a tie_outside_node() function.  This will not find the nearest node but will find the nearest edge (a road, a river...), split it in two edges, calculate the new lengths,and add the appropriate nodes to the graph.  Is a bit complicated because I modify the graph on the fly in order to keep the smallest graph possible.  This has the advantage of tying the source node (where you start the trip) to the nearest "road" or "river" that may be very close. If you connect only to the closest node, the entire length of the road will be added to the calculation.

This paper explains the algorithm.  https://www.mdpi.com/2306-5729/5/1/8

Nicolas

On 2021-08-30 1:00 p.m., Dmitriy Tarasov wrote:
From what I understand, Dijkstra's algorithm can only find routes to and from nodes. However, I may need to trace paths to objects that are near edges, but not near existing nodes. Is there a way, in Networkx, to add a new node near an object onto the nearest edge?
--
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-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/ba6988e8-6bfa-48d1-9bf7-00dd571ca5ean%40googlegroups.com.
-- 
Nicolas Cadieux
https://gitlab.com/njacadieux

Dan Schult

unread,
Aug 30, 2021, 2:25:46 PM8/30/21
to networkx...@googlegroups.com
Dijkstra's algorithm is for weighted graphs. That means it knows about nodes, edges and weights on edges.
It does not know about positions or any geometrical information. Nicholas Cadieux's comments look quite helpful.
NetworkX does not provide those kinds of capabilities.

Dmitriy Tarasov

unread,
Aug 30, 2021, 10:01:42 PM8/30/21
to networkx-discuss
Thank you! I will look into this.
Reply all
Reply to author
Forward
0 new messages