0

I have file like this.

From To
1 2
2 3
3 4
3 5
4 6
5 7
6 8

How can I find the path for each point?

For eg:

For 3 is [(1,2), (2,3)]

for 8 is [(1,2),(2,3),(3,4),(4,6),(6,8)]

Any Help would be appreciable!!!!!!!!!

Abanish Tiwari
  • 167
  • 1
  • 2
  • 14

2 Answers2

3

A data structure like dict is more suitable to store the graph information.

Set To as index, so that given a node x we can find out prev node using df.loc[x, 'From']. Start from the destination node and keep moving to the previous node until you get 1(source node).

Use:

df = df.set_index('To')
x = 8
output = []
while x!=1:
    y = df.loc[x, 'From']
    output = [(y, x)] + output
    x = y

Prints:

>>> output
[(1, 2), (2, 3), (3, 4), (4, 6), (6, 8)]
Amit Vikram Singh
  • 2,090
  • 10
  • 20
  • Thank you, I tried moving from forward to backward and that was must difficult since there can be multiple branches. I didn't think about the reverse that the last one would have only one point prior to it. It worked – Abanish Tiwari Apr 18 '21 at 17:36
  • [BFS](https://stackoverflow.com/questions/23477921/bfs-algorithm-in-python) is the most used algorithm for such purposes. However in your case, graph is a Tree which means there is a unique path from source to destination and we also have the access to back pointer from a node to its parent node, so here we can simply do backtracking(I hope it's correct name). – Amit Vikram Singh Apr 18 '21 at 17:39
2

If it suits your purpose, you can resort to a "specialized" package for working with graphs. I'll give an example with networkx. In the example:

  • I've taken the data you provided;
  • converted it to networkx Graph;
  • printed a list of the nodes (not the edges, note) that make your path;
  • drawn a plot and saved it to a .png file.
import networkx
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(data=[(1, 2), (2, 3), (3, 4), (3, 5), (4, 6), (6, 8)],
                  columns=['from', 'to'])

g = networkx.from_pandas_edgelist(df, source='from', target='to')

# Print a list of the nodes in the path from 1 to 8
print(networkx.shortest_path(g, source=1, target=8))

# Plot the graph
networkx.draw_networkx(g)
plt.savefig("test.png", format="PNG")
cyau
  • 449
  • 4
  • 14