Skip to content
Advertisement

Find all possible paths in a python graph data structure without using recursive function

I have a serious issue with finding all possible paths in my csv file that looks like this :

Source Target Source_repo Target_repo
SOURCE1 Target2 repo-1 repo-2
SOURCE5 Target3 repo-5 repo-3
SOURCE8 Target5 repo-8 repo-5

There a large amount of lines in the datasets, more than 5000 lines. I want to generate all possible paths like this in and return a list (Target5 is equal to SOURCE5):

  • SOURCE1 Target2
  • SOURCE8 Target5 Target3

I want to implement this solution without using recursive functions, since causes problems (maximum recursion depth exceeded).

This is the current code example :

JavaScript

Here is the output after executing the preceding code :

Result of the preceding code

Here is the data link Google drive

I tried to solve the problem using recursive function, but the code failed with the problem of depth exceeded. I aim to solve it without recursive functions.

Advertisement

Answer

I’m not sure if you want all paths or paths specifically from node to another node. Either way this looks like a job for networkx.

Setup (nx.from_pandas_edgelist)

JavaScript

All paths (nx.all_simple_paths)

JavaScript

From one node to another

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement