Skip to main content

Distance Vector Algorithm in C

Distance Vector Routing


Distance Vector Routing Algorithm in C

In computer communication theory relating to packet-switched networks, a distance-vector routing protocol is one of the two major classes of routing protocols, the other major class being the link-state protocol. A distance-vector routing protocol uses the Bellman-Ford algorithm to calculate paths.

A distance-vector routing protocol requires that a router informs its neighbors of topology changes periodically and, in some cases, when a change is detected in the topology of a network. Compared to link-state protocols, which require a router to inform all the nodes in a network of topology changes, distance-vector routing protocols have less computational complexity and message overhead.

Distance Vector means that Routers are advertised as vector of distance and direction. 'Direction' is represented by next hop address and exit interface, whereas 'Distance' uses metrics such as hop count.

Routers using distance vector protocol do not have knowledge of the entire path to a destination. Instead DV uses two methods:

  1. Direction in which or interface to which a packet should be forwarded.
  2. Distance from its destination.

Examples of distance-vector routing protocols include Routing Information Protocol Version 1 & 2, RIPv1 and RIPv2 and IGRP. EGP and BGP are not pure distance-vector routing protocols because a distance-vector protocol calculates routes based only on link costs whereas in BGP, for example, the local route preference value takes priority over the link cost.

Distance vector algorithm routing implementation in C


struct node
	unsigned dist[20];
	unsigned from[20];

int main()
	int dmat[20][20];
	int n,i,j,k,count=0;
	printf("\nEnter the number of nodes : ");
	printf("\nEnter the cost matrix :\n");
			printf("\n\nState value for router %d is \n",i+1);
				printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);


	gcc -ggdb distanceVector.c
	rm a.out *~

Example output

Enter the number of nodes : 4

Enter the cost matrix :
0 3 5 99
3 0 99 1
5 4 0 2
99 1 2 0

State value for router 1 is
node 1 via 1 Distance0   
node 2 via 2 Distance3   
node 3 via 3 Distance5   
node 4 via 2 Distance4

State value for router 2 is
node 1 via 1 Distance3   
node 2 via 2 Distance0   
node 3 via 4 Distance3   
node 4 via 4 Distance1

State value for router 3 is
node 1 via 1 Distance5   
node 2 via 4 Distance3   
node 3 via 3 Distance0   
node 4 via 4 Distance2

State value for router 4 is
node 1 via 2 Distance4   
node 2 via 2 Distance1   
node 3 via 3 Distance2   
node 4 via 4 Distance0


adithya on February 28, 2013:

I need in java can u try it...

unknown on September 12, 2012:

plz add comments next to code... and elucidate it!

santhosh.m on August 14, 2012:

it more helpful for all

prabhakar gouda (author) from Bangalore on April 26, 2012:

@tarun can you please try for the matrix which kishore has mentioned

Tarun Kumar on April 21, 2012:

I would definitely love to help but first give me a matrix where DV fails to work,

i want this to push my code to a limit and want to check how efficient it is .

prabhakar gouda (author) from Bangalore on April 20, 2012:

Scroll to Continue

Hey guy please take tarun help for more efficient code .... Thanks Tarun

Tarun Kumar on April 20, 2012:

hey guys can you give me a matrix or scenario where distance vector does not work, i am doing project on DV algo and i need to prove that in these kind of cases DV fails to work.

Quit on April 13, 2012:


its not for negative costs !

its only for positive values.

if you want it for negative costs, then implement it through dijkstra algorithm.

thanks. :)

yo baby on March 21, 2012:


kishore on March 19, 2012:

could you brief how this works


kishore on March 19, 2012:

I get an infinite loop on cost matrix

0 6 2 7

4 0 4 -1

8 5 0 2

7 -1 7 0



raj on March 15, 2012:

i understood jst nw

raj on March 15, 2012:

implementation is nice bt starting one is bad

Sid Vj on January 16, 2012:

I deserve to be winner of big boss. But don't know why Jui is winner. Anyways love you Sunny.

DjRocky on January 16, 2012:

Wow ............. Idiot's !

Pradeep on November 21, 2011:

Last post agreed Cost[2][3]is ? then how come the value ofCost[3][2] is 4?????????????

? on November 18, 2011:

Shouldn't the example cost matrix be symmetric? Cost[2][3] is 99 (99 means "infinity" here, I guess) but Cost[3][2] is 4...

Dan P on October 10, 2011:

are the [20]'s assuming that there won't be more than 20 routers? How would you do that dynamically if you can't guarantee that?

prabhakar gouda (author) from Bangalore on September 27, 2011:

hey vygi if you know java then you can convert it into java right .... it is not a big deal to convert to c to java

prabhakar gouda (author) from Bangalore on September 27, 2011:

@haribabu thanks ra

vygi on September 27, 2011:

i want in java

haribabu on August 25, 2011:

mastu undi ra bai program.........

Related Articles