1

I am using NetworkX, opencv, numpy and python to find shortest_path in a graph. It doesn't always provide what I need. shortest_path function finds the path starting from the top of the image to bottom. My path is always varying. Starting point and target point are always known for each image. Thus, I want to find the shortest path between those points (start and target). However, when the source and target points are not nodes and not in the G, it doesn't give what I need.

shortest_path(G, source=None, target=None, weight=None)

How can I find the shortest_path between two specific coordinate points in the image? Moreover, how can I assign a pixel coordinates as source and target ? For example, source is [45 66] and target is [250 350]

Ender Ayhan
  • 308
  • 3
  • 14

1 Answers1

0

For the second issue, a node can have how many dimensions you want.
For example consider the following grid.

import networkx as nx

g = nx.grid_2d_graph(2,2)

print(g.nodes()) #[(0, 0), (0, 1), (1, 0), (1, 1)]
print(g.edges()) #[((0, 0), (1, 0)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 0), (1, 1))]
print(nx.shortest_path(g, source=(0, 0), target=(1,0))) #[(0, 0), (1, 0)]

or with even more dimensions:

g = nx.grid_graph(dim=[2,2,2,2])
g.nodes()
#[(0, 0, 0, 0), (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1), (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 0, 1, 0), (0, 1, 1, 0), (1, 0, 1, 1), (0, 1, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]

For the other issues:

  • My path is always varying: it depends on the weight on the edges, if the shortest path is not unique it's normal that it changes

  • when the source and target points are not nodes and not in the G: networkx finds a shortest path between 2 nodes of G, if the nodes are not in G it will not work

abc
  • 11,579
  • 2
  • 26
  • 51
  • 1
    Sorry for the wrong expression. For saying "My path is always varying": It is changing because images are changing. I tried to say that my path may start from right to left , top to bottom and vice versa. I just thought that I guess I can use `G.add_nodes_from()` to get starting and target points as nodes in the G, can't I? Lastly, how can I use your solution to assign the coordinates of start and target as source and target ? In order to my understanding, your solution just finds shortest path between random points. – Ender Ayhan Sep 04 '18 at 22:15
  • There are many ways to add nodes and edges, you can choose the one you prefer. It's not clear to me your last question, but if a node is a coordinate (x,y) in the shortest path returned you will have these points that you can directly use. – abc Sep 04 '18 at 22:18
  • 1
    No, unfortunately a coordinate (x,y) is not in the shortest path. Thus, although shortest path returns into a right solution technically, it is not my path. My path must be between (X1,Y1) and (X2,Y2). From (X1,Y1) to (X2,Y2). All in all, I want to find the shortest path between those points. – Ender Ayhan Sep 04 '18 at 22:25