Distance Vector Routing and Link State Routing: Expt No:7 DATE:28/06/2021

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

EXPT NO:7 DATE:28/06/2021

DISTANCE VECTOR ROUTING AND LINK STATE ROUTING


AIM:

To perform link state routing and distance vector routing for the given
number of nodes using matlab.

SOFTWARE REQUIREMENTS:

Matlab version 2007 or more

THEORY:

DISTANCE VECTOR ROUTING:

A distance-vector routing (DVR) protocol requires that a router inform its


neighbors of topology changes periodically. Historically known as the old
ARPANET routing algorithm (or known as Bellman-Ford algorithm).
Bellman Ford Basics – Each router maintains a Distance Vector table
containing the distance between itself and ALL possible destination nodes.
Distances,based on a chosen metric, are computed using information from
the neighbors’ distance vectors.
Information kept by DV router -
 Each router has an ID
 Associated with each link connected to a router,
 there is a link cost (static or dynamic).
 Intermediate hops

Distance Vector Table Initialization -


 Distance to itself = 0
 Distance to ALL other routers = infinity number.

Distance Vector Algorithm –


1. A router transmits its distance vector to each of its neighbors in a
routing packet.
2. Each router receives and saves the most recently received distance
vector from each of its neighbors.
3. A router recalculates its distance vector when:
 It receives a distance vector from a neighbor containing different
information than before.
 It discovers that a link to a neighbor has gone down.
The DV calculation is based on minimizing the cost to each destination
Dx(y) = Estimate of least cost from x to y
C(x,v) = Node x knows cost to each neighbor v
Dx = [Dx(y): y ∈ N ] = Node x maintains distance vector
Node x also maintains its neighbors' distance vectors
– For each neighbor v, x maintains Dv = [Dv(y): y ∈ N ]
Note –
 From time-to-time, each node sends its own distance vector estimate
to neighbors.
 When a node x receives new DV estimate from any neighbor v, it saves
v’s distance vector and it updates its own DV using B-F equation:
 Dx(y) = min { C(x,v) + Dv(y), Dx(y) } for each node y ∈ N
Example – Consider 3-routers X, Y and Z as shown in figure. Each router have
their routing table. Every routing table will contain distance to the destination

odes.
Consider router X , X will share it routing table to neighbors and neighbors
will share it routing table to it to X and distance from node X to destination
will be calculated using bellmen- ford equation.
Dx(y) = min { C(x,v) + Dv(y)} for each node y ∈ N
As we can see that distance will be less going from X to Z when Y is
intermediate node(hop) so it will be update in routing table X.
Similarly for Z also –

Finally the routing table for all –


Advantages of Distance Vector routing:
It is simpler to configure and maintain than link state routing.
Disadvantages of Distance Vector routing :
 It is slower to converge than link state.
 It is at risk from the count-to-infinity problem.
 It creates more traffic than link state since a hop count change
must be propagated to all routers and processed on each router.
Hop count updates take place on a periodic basis, even if there
are no changes in the network topology, so bandwidth-wasting
broadcasts still occur.
 For larger networks, distance vector routing results in larger
routing tables than link state since each router must know about
all other routers. This can also lead to congestion on WAN links.

 ALGORITHM:
1)Start the program

2)Get the number of nodes from the user using input function

3)Using the rand function the cost matrix is generated such that the cost
between the 2 nodes remains the same in the cost matrix.The matrix is
displayed

4)The upper triangular cost matrix is taken using triu function and the nodes
which are connected is found .

5)Using biograph the graph is displayed using the upper triangular cost matrix

6)The routing matrix is generated by substituting infinite value if traversal is


done through the same node and minimum cost value if traversal done
through neighbour node

7)The routing table for each node is displayed

8)The source node and the destination node value is taken from the user using
input function

9)Using find function and minimum function the best path from the source to
destination node is found and the path gets displayed using biogrpah function

10)Stop the program.


PROGRAM:

clc;
clear all;
disp('Distance vector routing protocol');
node=input('Enter the no. of nodes: ');
over=0;
for i=1:node %initialize
for j=i:node
if(i==j)
matrix(i,j)=0;
else
matrix(i,j)=randint(1,1,9);
matrix(j,i)=matrix(i,j);
end
end
end
disp(matrix);
i=1;
x=1;
mat1=triu(matrix);
for i=1:node
for j=1:node
if(mat1(i,j)~=0)
mat(i,j)=x;
mat(j,i)=mat(i,j);
x=x+1;
end
end
end
view(biograph(triu(matrix),
[],'showarrows','off','ShowWeights','on','EdgeTextColor',[0 0 1]));
for from=1:node% fill initial matrix with via = to
for via=1:node
for to=1:node
if(from~=via&&from~=to)
if(via==to&&matrix(from,to)~=0)
go(to,via,from)=matrix(from,to);
else
go(to,via,from)=100;
end
else
go(to,via,from)=inf;
end
end
end
end
i=0;
while(i<2)
for from=1:node
for to=1:node
if(from~=to)
if(matrix(from,to)~=0)%calculate neighbour node
for x=1:node
for y=1:node
temp(x,y)=matrix(from,to)+min(go(y,:,to));
if(temp(x,y)<go(y,to,from)&&go(y,to,from)<inf)
go(y,to,from)=temp(x,y);
end
end
end
end
end
end
end
i=i+1;
end
disp(go);
choice='y';
while(choice=='y')
source=input('Enter the source node: ');
dest=input('Enter the destination node: ');
trace(1)=source;
j=2;
while(source~=dest)
[row,col]=find(go(dest,:,source)==min(go(dest,:,source)));
trace(j)=col;
source=col;
j=j+1;
end
k=1:j-1;
disp(trace(k));
bg=biograph(triu(matrix),
[],'showarrows','off','ShowWeights','on','EdgeTextColor',[0 0 1]);
for i=1:j-1
set(bg.nodes(trace(i)), 'color', [1 0 0]);
set(bg.nodes(trace(1)),'color',[0 1 0]);
if(i<j-1)
set(bg.edges(mat(trace(i+1),trace(i))),'linecolor',[1 0 0]);
end
end
view(bg);
choice=input('Do you want to try again (y/n):','s');
end
OUTPUT:
DISPLAYING NODES WITH WEIGHT:
EFFECTIVE PATH:
LINK STATE ROUTING:

Link state routing is a technique in which each router shares the knowledge of
its neighborhood with every other router in the internetwork.

The three keys to understand the Link State Routing algorithm:

o Knowledge about the neighborhood: Instead of sending its routing table,


a router sends the information about its neighborhood only. A router
broadcast its identities and cost of the directly attached links to other
routers.
o Flooding: Each router sends the information to every other router on the
internetwork except its neighbors. This process is known as Flooding.
Every router that receives the packet sends the copies to all its
neighbors. Finally, each and every router receives a copy of the same
information.
o Information sharing: A router sends the information to every other
router only when the change occurs in the information.

Link State Routing has two phases:

Reliable Flooding

o Initial state: Each node knows the cost of its neighbors.


o Final state: Each node knows the entire graph.

Route Calculation

Each node uses Dijkstra's algorithm on the graph to calculate the optimal
routes to all nodes.

o The Link state routing algorithm is also known as Dijkstra's algorithm


which is used to find the shortest path from one node to every other
node in the network.
o The Dijkstra's algorithm is an iterative, and it has the property that after
kth iteration of the algorithm, the least cost paths are well known for k
destination nodes.

Let's describe some notations:


o c( i , j): Link cost from node i to node j. If i and j nodes are not directly
linked, then c(i , j) = ∞.
o D(v): It defines the cost of the path from source code to destination v
that has the least cost currently.
o P(v): It defines the previous node (neighbor of v) along with current least
cost path from source to v.
o N: It is the total number of nodes available in the network.

ALGORITHM:

1)Start the program

2)Get the number of nodes from the user using input function

3)Using the rand function the cost matrix is generated and the matrix is
displayed

4)The source node and the destination node value is taken from the user using
input function

5)The distance between the source and the other nodes is taken in a array
,here the distance between the source node to itself is made infinite

6)The node with minimum cost is made as the new source and the minimum
cost get stored in a array , this loop repeats until the source node is equal to
the destination

7)The values taken by the source node during each iteration gets displayed in
form of alphabets .This string denotes the path from source to destination.

8)The values in the minimum cost array gets summed up to give the total cost
of the path.

9)Stop the program.


PROGRAM:

clc;
% Taking number of input nodes from user
n=input('Please enter the number of nodes = ');
% Checking for bad input

% Defining node numbers interms of Alphabets


alphabets= char(65:1:65+n);
adj=zeros(n,n);
for p=1:n
for q=1:n
adj(p,q)=randint(1,1,9);
end
end
%Coversting status of all nodes to Tentative
for p=1:n
a(p).status=0;
end
temp=[];
acc=[];
% Taking input from user regarding source & destination nodes
s=input('Please enter the source node = ');
t=input('Please enter the destination node = ');
% converting source node to status PERMANANT
a(s).status=1;
path=s;
% if (adj(s,t)==0)
% disp('Source and Destination are Same');
% continue
% end
% Main algorithm of Greedy Dijkstra
for k=1:inf
for h=1:n
if a(h).status==0 % If Unknown
temp=[temp adj(s,h)];
else
temp=[temp inf];
end
end
[aa,bb]=min(temp);
acc=[acc aa];
temp=[];
s=bb;
a(s).status=1;
path=[path s];
if s==t
break
end
end
% Creating dialogue box for Output
helpdlg(['Path : ' alphabets(path) ' Total Cost/Weight : '...
num2str(sum(acc))],'Greedy Dijkstra Algorithm')

OUTPUT:

RESULT:

Thus the direction vector routing and link state routing are performed using
matlab and the results are verified.

You might also like