## how to find shortest path between 2 nodes

### FIR (view profile)

on 21 Jan 2014
Latest activity Answered by Walter Roberson

### Walter Roberson (view profile)

on 22 Jan 2014
I have a image in
here i want to find the shorted path between 2 nodes ,say node 1 and node 4,there are twopaths
1-5-4 and 1-5-3-4
can anyone tell how to find these two paths and find the shortest among them
i tried
[dist,path,pred] = graphshortestpath(UG,1,3,'directed',false);
it gives only one path i.e shortest path,i need all paths from node 1 to 4

Walter Roberson

### Walter Roberson (view profile)

on 21 Jan 2014
I am confused. Your title indicates you want to find the shortest path, but your body seems to indicate that you want to find all the possible paths and then figure out which one is shortest and return that, with the effect of finding the shortest path. Algorithms for finding the shortest path do not require finding all paths first.
FIR

### FIR (view profile)

on 22 Jan 2014
ok walter i go on with shortest path,as you indicated in previous mail,to use UDP or tcp for data transfer,now i want to transfer data between node 1 and 4,kindly tell what function or how to proceed for data transfer

on 21 Jan 2014
Edited by Amit

on 21 Jan 2014

### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 22 Jan 2014

Follow the procedure I indicated in that other thread, as many times as necessary, each time getting one step closer to the destination.
There are two different ways you can proceed:
1. At the source node, you work out the complete path to the destination node. When you transmit the data, prefix it with the remaining path. The node you hand over to will be responsible for contacting the next hop and transmitting the data to it with its own address stripped from the path list. Or:
2. At the source node, figure out a node that is "closer" to the destination. There might be enough information to predict a likely complete path, but there might not be, and even if there is enough information, it could be that a node in the predicted path will become unavailable while the data is being relayed. Transmit the data to the predicted next step along the way, prefixed by an indication of the final destination, but not all of the predicted steps in the route. At the receiving node, again predict the best route and forward the packet onwards, hopefully getting closer and closer to the destination each time.
The first option, predicting the complete path and having each node required to follow it, runs the risk that a node will become unavailable, leaving it undefined how the packet should continue. The second option, continually predicting the path along the way, runs the risk that based on the path information as known to the receiving node, the packet will be sent to a node that has already handled it in the trip (possibly right back to the node that just send it here), potentially resulting in an infinite loop for the packet.
Both of these are classic routing problems in networking, and there is no solution that guarantees delivery or shortest delivery. There have been a number of routing protocols invented to work with these problems, with trade-offs necessarily taken. You need to investigate routing protocols and decide which one you are going to use.