- Computers & Software»
- Computer How-Tos & Tutorials
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
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.
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
To determine the shortest path I executed the following command
[dist,path,pred] = graphshortestpath(cm,1,10)
path =1 3 5 7 9 10
pred =0 1 1 2 3 5 5 6 7 9