Updated date:

Shortest Path Routing OSPF & Dijkstra's Algorithm - Finding Shortest Path in a Network using MATLAB


OSPF - A little theory first

OSPF or Open Shortest Path First is a dynamic routing protocol, it is a link state routing protocol and is part of the interior gateway protocols group. OSPF is similar to RIP but unlike RIP that keeps track of the closest router for each destination address OSPF keeps track of the complete network topology and all the nodes and connections within that network.

The basic workings of the OSPF routing protocol are as follow

- Startup

When a router is turned on it sends Hello packets to all neighboring devices, and the router receives a Hello packets in response. From here routing connections are synchronized with adjacent routers that agree to synchronize.

- Update

At regular intervals each router will send an update message called its “link state” to describing its router database to all other devices. So that all the routers connected together have an up to date description of each topology that is connected to each router.

- Shortest path tree

Each router will calculate a mathematical data structure called “shortest path tree” that describes the shortest path to the destination address, this is where OSPF gets it name, it will try to open the shortest path first.

OSPF routing protocol is a very important protocol to consider when setting up routing instructions on your network. As OSPF gives the routers the ability to learn the most optimal (shortest) paths it can definitely speed up data transmission from source to destination.

Even with these capabilities OSPF may not be ideal in a network that is prone to bottlenecks or increased traffic across specific parts of the network.


Determining Shortest Path for your Network

For this example I will use the 10 node network I used for my max flow min cut example in my other hub. Instead of link capacities the numbers across each link represent distances or weights.

First I create my network design using the command in matlab to create a matrix

cm = sparse([1 1 2 2 3 4 4 5 5 6 7 8 9 9],[2 3 3 4 5 6 7 6 7 8 9 10 8 10],[15 10 3 8 9 7 5 6 2 12 10 6 10 8],10,10)

note: for more on how to setup your matrix look at Max Flow Min Cut Algorithm HUB

The Network will look like the following to Matlab ->

Source node 1 to destination node 10

Shortest Path

To determine the shortest path I executed the following command

[dist,path,pred] = graphshortestpath(cm,1,10)
with output:

dist =39

path =1 3 5 7 9 10

pred =0 1 1 2 3 5 5 6 7 9

There you have it the shortest path from source to destination, of course this is an extremely easy example

There you have it the shortest path from source to destination, of course this is an extremely easy example


hanaa on March 07, 2015:

I want find shortest path between nodes , but with a condition on the edges for finding shortest path. how can i do it ?

chalue on October 04, 2011:

What is the shortest distance between Node 1 to Node 5

Surf on August 28, 2011:

I've written better algorithms myself..

apurva sachan on March 05, 2011:

how cn we obtain the dynamic graph changing after certain t sec. Nd den apply ospf in each t sec.

Babai on November 13, 2010:

1) Give the codes for cross layer design using Matlab.

2) Give the Matlab codes for Congestion control using TCP/IP

expectus (author) from Land Downunder on May 24, 2010:

I will have a look for it used to have it, but a google search should get the same results

krish on May 20, 2010:

how about in c++

expectus (author) from Land Downunder on April 21, 2010:

This is done in MATLAB , it depends what format you want the code in?

Sushma on April 21, 2010:

how to get the code of this? just explanation is given... how to get dijkstra's algorithm code?