0

I am currently using networkx library for Python with BFS and DFS. I need to get a tree and then explore it to get a path from a start node to an end node.

For the BFS part I am using bfs_successorsand it returns an iterator of successors in breadth-first-search from source.

For the DFS part I am using: dfs_successors and it returns a dictionary of successors in depth-first-search from source.

I need to get a list of nodes from source to end from both the algorithms. Each node is (x, y) and is a cell in a grid.

Do you have any advice about how to do it? Can you help me please?

MWE:

DFS = nx.bfs_successors(mazePRIM,start)
print(dict(BFS))

DFS = nx.dfs_successors(mazePRIM, start)
print(DFS)

and I get this:

{(0, 0): [(0, 1), (1, 0)], (1, 0): [(1, 1)], (1, 1): [(1, 2)], (1, 2): [(0, 2), (1, 3)], (0, 2): [(0, 3)]}

{(0, 0): [(0, 1), (1, 0)], (1, 0): [(1, 1)], (1, 1): [(1, 2)], (1, 2): [(0, 2), (1, 3)], (0, 2): [(0, 3)]}

But I need an output like this:

[(0, 0), (1, 0), (1, 1), (1, 2), (1, 3)]

which is the list of nodes from start to end.

hellomynameisA
  • 546
  • 1
  • 7
  • 28
  • What do you mean by extrapolating a list of nodes from both algorithms? Can you share a minimal reproducible example? – yatu Jun 16 '20 at 16:22
  • hi! I mean get the tree and then find a path from a start node to an end node... I added the example – hellomynameisA Jun 16 '20 at 16:23

1 Answers1

1

IIUC you're not really interested in finding all successors encourtered with nx.bfs_successors, since you only need the path between a source and a target nodes.

For that you can either find the shortest path (in the case there are multiple):

nx.shortest_path(G, source, target)

Or find all simple paths between them:

nx.all_simple_paths(G, source, target)

Which returns a generator with all simple paths between both nodes.

yatu
  • 86,083
  • 12
  • 84
  • 139
  • But why is that? From your question, I understand that you're only interested in the path between those nodes @hello – yatu Jun 16 '20 at 16:38
  • I want to use both and compare their speed to see which one is faster – hellomynameisA Jun 16 '20 at 16:38
  • My code has lots of lines and I am trying to see which algorithm does the job faster and then print a visual output. I tried Dijkstra and Bellman-Ford, but I would like to see what happens with these ones. This output dict thing is the only part I can't fix... – hellomynameisA Jun 16 '20 at 16:41
  • But you're not really addressing my question... @hello Why find all successors using `dfs_successors`, if you're only interested in the path between two nodes? – yatu Jun 16 '20 at 16:42
  • Oh ops... I thought it was the right one for doing it – hellomynameisA Jun 16 '20 at 16:43
  • No... Please take your time to read the answer, and try out the suggested methods @hello IIUC they are what you need – yatu Jun 16 '20 at 16:43
  • by seeing they had the same output format (dict), I figured out it would have been easier to get the result... like write a single one function to convert both – hellomynameisA Jun 16 '20 at 16:44
  • ```nx.shortest_path(G, source, target)``` uses Dijkstra or bellman-ford... I can't use it to get bfs or dfs. Do you know what does ```nx.all_simple_paths(G, source, target)``` use? – hellomynameisA Jun 16 '20 at 16:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216073/discussion-between-hellomynameisa-and-yatu). – hellomynameisA Jun 16 '20 at 16:51