hello i m facing problem to find the shortest path from node in 100 * 100 matric where sink node is at center all other nodes find the shortest path from their location to sink node i need code of this problem send code at atifaziz463@yahoo.com
Show older comments
i am writing a thesis to find the shortest path. sink node is at center and all other nodes with in 100*100 sq.m will calculate the shortest path from the sink. if any one can find matlab code plz send me at atifaziz463@yahoo.com
2 Comments
Lakshmipriya M
on 29 Mar 2017
can anyone send me the code for connecting all nodes into base station with calculating the distance pls
Lakshmipriya M
on 29 Mar 2017
can anyone tell me a code for routing in path establishment using hop count
Answers (2)
Walter Roberson
on 6 Oct 2016
0 votes
If this is a regular grid with no obstacles then just follow the edges towards the target.
If instead you have a set of sources that are scattered at integer coordinates within a 100 x 100 grid, such as if you are working on a wireless sensor network, then you need to define the rules about which nodes are connected and the cost of connecting. If you are working on a wireless sensor network then the best path for any one node to transmit in isolation to reach the sink is not the best approach to take if all of the nodes need to communicate with the sink within a short time, because some of the links would get clogged. Just like with a traffic jam, sometimes you can get somewhere faster by taking a longer route that is in less demand.
13 Comments
atif aziz
on 6 Oct 2016
Walter Roberson
on 6 Oct 2016
Attach the picture here. I do not answer much of the MATLAB related email that is sent to me.
atif aziz
on 7 Oct 2016
Walter Roberson
on 7 Oct 2016
If the picture was too big to upload, you could zip it and attach the zip file.
Or upload the picture somewhere else such as Google Drive and post the URL.
Walter Roberson
on 7 Oct 2016
You need to define the rules about which nodes are connected, and define the cost of connecting.
For example are you willing to say that every node is in range of every other node? Is the cost of connecting two nodes equal to the Euclidean distance between the nodes? Or is the cost equal to the square of the Euclidean distance between the nodes?
If every node can reach every other node and the cost of connecting two nodes is equal to the Euclidean distance between the nodes, then always send directly from the source node to the sink node without using any nodes in the middle.
For any other cost between nodes, I suggest you use the new graph() objects together with using the shortestpath() function repeatedly.
atif aziz
on 7 Oct 2016
Walter Roberson
on 7 Oct 2016
You need to define the rules about which nodes are connected, and define the cost of connecting.
For example is the rule that within any one cell, the sensors represented by circles have to transmit to the cell leader, represented by diamonds, and are not permitted to transmit to other circles? And then is the rule that the nodes represented by diamonds can transmit to any other diamond node or to the star node? What is the cost of transmitting from one node to another?
Take a look at

Is it permitted to go from circle to circle to sink (star) as shown by the red arrows, or is it required to go to the cell leader (circle to diamond) and from there to the sink (star), even though that is a longer route?
Will the routes all be pre-programmed at the time that the sensors are put in place? Or, alternately, will all of the sensors always have full information about where they are and where all the other sensors are, and about where the cell boundaries are, and so each sensor can calculate the best route under the assumption that all sensors will continue to work and no path will be blocked and no sensor will be moved?
Or do the sensors transmit information about where they are, relaying all of the information under a different protocol, until all of the sensors have information about where all of the nodes are, at which time they switch to each calculating its best route under the assumption that all sensors will continue to work and no path will be blocked and no sensor will be moved?
Or is there some protocol for relaying information partly, with each sensor just trying to figure out where its cell boundaries are and who the cell leader is, and then after that another protocol for the cell leaders to talk together to figure out where the other cell leaders are, at which time they switch to each calculating its best route under the assumption that all sensors will continue to work and no path will be blocked and no sensor will be moved?
Or is it necessary to update the information as you go, that the route might change as sensors move or paths get blocked? If so then is that information update through a different protocol than sending to the sink?
Or... ?
Anyhow.
If you have a vector of source node numbers, s, and a vector of destination (target) node numbers, t, and for any given index K, the distance from node number s(K) to destination t(K) is given by cost c(K), and you only fill in the list in one direction (assume that the backwards links will automatically be filled in by the software), then
maxnode = max([s(:), t(:)]);
sendpaths = cell(maxnode, 1);
G = graph(s, t, c);
for K = 1 : maxnode
sendpaths{K} = shortestpath(G, K, sinknode);
end
Which is just what I said before, "use the new graph() objects together with using the shortestpath() function repeatedly."
This presumes that the place doing the calculation has perfect information about the costs between all nodes.
atif aziz
on 20 Oct 2016
Walter Roberson
on 20 Oct 2016
Under those circumstances, there is no reason to run a shortest path calculation. The only permitted path for a node that is not a cluster head or the base station is to send to its cluster head, and the only permitted path for a cluster head is to send directly to the base station. As these two routes are fixed, the distance calculation is trivial.
If you later decide that it is okay for the cluster heads to send to other cluster heads to pass along to the sink, then:
create a square matrix A representing only the cluster heads, such as identifying them by their sensor cell number as the index. For any two sensor cell numbers I, J, then A(I,J) should be set to the cost of transmitting from the cluster head of cell I to the cluster head of cell J. Once that is done,
G = graph(A);
num_clusters = length(A);
sendpaths = cell(num_clusters, 1);
for K = 1 : num_clusters
sendpaths{K} = shortestpath(G, K, sinknode);
end
After that, the rule to send from any node would be
- if the node is not a cluster head (or the sink node) then transmit directly to the node's cluster head;
- if the node is a cluster head (but not the sink node) then transmit along the cluster number path given in the sendpaths cell array
atif aziz
on 20 Oct 2016
Walter Roberson
on 20 Oct 2016
No, I will not do that for you. You are having too much difficulty explaining what you want to do; it would take too much of my time to go back and forth with you so that we understand each other, and I think I would end up having to rewrite too much of your code.
atif aziz
on 20 Oct 2016
Sindhuja john
on 29 May 2023
Hi ..! I have 100 nodes and i divided it into 10 cluster and there are 10 cluster head. I want to send a packet from each cluster head to destination (110,110) in shortest path. can anyone have solution for my problem... kindly help me..
cesar silva
on 29 Nov 2019
0 votes
EVEN ITS AN OLD POST, SOMEONE ELSE MAYBE COMES HERE LOOKING FOR A HELP... HERE IT IS:
DESCRIPTION OF THE CODE AVAILABLE IN GITHUB LINK ABOVE:
1) you can choose node number (nodeNo parameter)
2) you can choose node signal range (range parameter - Its a 1x1 Kilometer area, algo range in meters)
3) once network nodes position randomly formed and all nodes neighbors by radius (range) criteria...
3.1) Shorthestpath nodes discovered between node 1 (sender) and node 2 (receiver) start energy consumption due routing (forward) task...
3.2) As soon 1 node dies, current route becames "useless"
3.3) New shortest path is discovered... (restart 3.1 till no more routes available)
4) I assume that every energy consumption loop is 1 packet transmittion
5) At the end, it "reports":
5.1) All routes discovery events from the begging till the end of network (no more routes)
5.2) hop count of each route iteration
5.3) Nodes involved (In the same order they are met in route)
5.4) Hypothetical packets transmitted
5.5) Dead node number (ID) every iteration
6) It also plots the original WSN
7) ALso plot every routing loop result and report
8) Mark all nodes involved ini each route event
9) Mark all dead nodes
10) Write reports to an external file into same folder the main.m file is
I hope it helps some one....
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!