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!!!!!!!!!
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!!!!!!!!!
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)]
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:
networkx
Graph;.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")