简体   繁体   中英

how to get the links between two nodes using R or python

I am new to R and I need code for the following requirement:

There are src and trg records like 1 matched with 2, 2-3,3-4.

Now when I ask the system to provide link between 1 and 4 it should give me o/p as 1-2-3-4.

When I ask the system to give link between 2 and 4 it should give 2-3-4 as o/p.

Please help me with your valuable suggestions.

Just use networkx :

>>> import networkx as nx
>>> G = nx.Graph()    
>>> G.add_nodes_from([1, 2, 3, 4])   
>>> G.add_edges_from([(1, 2), (2, 3), (3, 4)])
>>> nx.algorithms.shortest_path(G, 1, 4)
[1, 2, 3, 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM