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!!!!!!!!!

10
  • 1
    Path starting from 1 to 8? Commented Apr 18, 2021 at 17:16
  • @AmitVikramSingh Yes, path from 1 to any bus Commented Apr 18, 2021 at 17:17
  • 1
    You dont need pandas but you can set your data structure as dict() and use bfs or dfs Commented Apr 18, 2021 at 17:18
  • @user1462442 I just want to find the path from a start to any required point. Commented Apr 18, 2021 at 17:20
  • 3
    This has nothing to do with pandas. Look up some path finding algorithms like Dijkstra and A* Commented Apr 18, 2021 at 17:21

2 Answers 2

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)]
Sign up to request clarification or add additional context in comments.

2 Comments

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
BFS 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).
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")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.