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, ofcourse 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

More by this Author


Comments 9 comments

Sushma 6 years ago

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


expectus profile image

expectus 6 years ago from Land Downunder Author

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


krish 6 years ago

how about in c++


expectus profile image

expectus 6 years ago from Land Downunder Author

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


Babai 6 years ago

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

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


apurva sachan 5 years ago

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


Surf 5 years ago

I've written better algorithms myself..


chalue 5 years ago

What is the shortest distance between Node 1 to Node 5


hanaa 21 months ago

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

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working