# 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

## More by this Author

- 76
Depending on whether you are formatting your master hard drive or slave hard drive the ways of formatting will differ. To format a Slave Hard drive (Not the hard drive with your current Operating System) you can simply...

- 6
I'm sure if you delved deep into computer networking you may have come across the Maximum flow Minimum Cut algorithm also referred to as the Ford Fulkerson Algorithm. The basic idea behind this algorithm is to take a...

- 11
Three simple exercises to help you relax those muscles and help resolve your pelvic floor dysfunction. Gain control over your pelvic muscles with stretching and soft-tissue massage.

## Comments 9 comments

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

how about in c++

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

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

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

I've written better algorithms myself..

What is the shortest distance between Node 1 to Node 5

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

9