Select Page

# Ns2 Code for Shortest Path Routing

Learn to how to implement Ns2 code for shortest Path Routing

## When to use Shortest Path routing?

An algorithm that is designed essentially to find a path of minimum length between two specified vertices of a connected weighted graph

General procedure to find out shortest path:

• Initialize the array smallestWeight so that smallestWeight[u] = weights[vertex, u].
• Set smallestWeight[vertex] = 0.
• Find the vertex, v, that is closest to vertex for which the shortest path has not been determined.
• Mark v as the (next) vertex for which the smallest weight is found.
• For each vertex w in G, such that the shortest path from vertex to w has not been determined and an edge (v, w) exists, if the weight of the path to w via v is smaller than its current weight, update the weight of w to the weight of v + the weight of the edge (v, w).

#### NS2 Projects Video Output

See our Latest Video Output of Ns2 Projects on Various Domain.

#### Ns2 Projects

Customized NS2 Projects for B.E/B.Tech/M.E/M.Tech/Ms/PhD Scholars.

#### Ns2 Projects Screen Shots

Ns2 Projects Screen Shots.Regular Update of NS2 Projects Screenshots here!

Sample code for shortest path routing:

This code contains main function of dijikstra’s routing algorithm.

```

int main()
{
Node N0(0);
Node N1(1);
Node N2(2);
Node N3(3);
Node N4(4);
RoutingVec_t NextHop;
RoutingVec_t Parent;

Nodes.push_back(&amp;N0);
Nodes.push_back(&amp;N1);
Nodes.push_back(&amp;N2);
Nodes.push_back(&amp;N3);
Nodes.push_back(&amp;N4);

for (nodeid_t i = 0; i &lt; Nodes.size(); i++)
{ // Get shortest path for each root node
printf("\nFrom root %ld\n", i);
Dijkstra(Nodes, i, NextHop, Parent);
PrintParents(Parent);
for (unsigned int k = 0; k &lt; Nodes.size(); k++)
printf("Next hop for node %d is %ld\n", k, NextHop[k]);
printf("Printing paths\n");
for (nodeid_t j = 0; j &lt; Nodes.size(); j++)
{
PrintRoute(i, j, Parent);
}
}
return(0);
}

```
##### Journal Support for Research Scholars
###### Ns2 Projects Work Progress
• MANET – Mobile Ad Hoc Network 95%
• VANET – Vechicle Ad Hoc Netwok 97%
• LTE – Long Term Evolution 78%
• IoT – Internet of Things 90%
• Wireless Sensor Network 89%
• Network Security 89%
• Ns2 Attacks 96%