You are on page 1of 9

C Program to Find shortest Path using

dijkstras algorithm
Mr Coder February 17, 2013 13

inShare

C Program to Find shortest Path using Dijkstras algorithm : Dijkstras algorithm is a


graph search algorithm that solves the single-source shortest path problem for a graph with
non-negative edge path costs, producing a shortest path tree. This algorithm is often used in
routing and as a subroutine in other graph algorithm.

Dijkstras Algorithm to find shortest Path :


Let the node at which we are starting be called the initial node. Let the distance of node Y be
the distance from the initial node to Y. Dijkstras algorithm will assign some initial distance
values and will try to improve them step by step.
1. Assign to every node a tentative distance value: set it to zero for our initial node and to
infinity for all other nodes.
2. Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited
nodes called the unvisited set consisting of all the nodes except the initial node.
3. For the current node, consider all of its unvisited neighbors and calculate their
tentative distances. For example, if the current node A is marked with a distance of 6,
and the edge connecting it with a neighbor B has length 2, then the distance to B
(through A) will be 6+2=8. If this distance is less than the previously recorded
tentative distance of B, then overwrite that distance. Even though a neighbor has been
examined, it is not marked as visited at this time, and it remains in the unvisited set.
4. When we are done considering all of the neighbors of the current node, mark the
current node as visited and remove it from the unvisited set. A visited node will never
be checked again.
5. If the destination node has been marked visited (when planning a route between two
specific nodes) or if the smallest tentative distance among the nodes in the unvisited
set is infinity (when planning a complete traversal), then stop. The algorithm has
finished.

6. Select the unvisited node that is marked with the smallest tentative distance, and set it
as the new current node then go back to step 3.
Lets see how to write a C program to implement Dijkstras Algorithm to find Shortest Path.

C Program to Find shortest Path using


Dijkstras algorithm :
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<math.h>
#define IN 99
#define N 6
int dijkstra(int cost[][N], int source, int target);
int dijsktra(int cost[][N],int source,int target)
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
char path[N];
for(i=1;i< N;i++)
{
dist[i] = IN;
prev[i] = -1;
}
start = source;
selected[start]=1;
dist[start] = 0;
while(selected[target] ==0)
{
min = IN;
m = 0;
for(i=1;i< N;i++)
{
d = dist[start] +cost[start][i];
if(d< dist[i]&&selected[i]==0)
{
dist[i] = d;
prev[i] = start;
}

if(min>dist[i] && selected[i]==0)


{
min = dist[i];
m = i;
}
}
start = m;
selected[start] = 1;
}
start = target;
j = 0;
while(start != -1)
{
path[j++] = start+65;
start = prev[start];
}
path[j]='\0';
strrev(path);
printf("%s", path);
return dist[target];
}
int main()
{
int cost[N][N],i,j,w,ch,co;
int source, target,x,y;
printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");
for(i=1;i< N;i++)
for(j=1;j< N;j++)
cost[i][j] = IN;
for(x=1;x< N;x++)
{
for(y=x+1;y< N;y++)
{
printf("Enter the

weight of the path between node %d and %d:

",x,y);
scanf("%d",&w);
cost [x][y] = cost[y][x] = w;
}
printf("\n");
}

printf("\nEnter The Source:");


scanf("%d", &source);
printf("\nEnter The target");
scanf("%d", &target);
co = dijsktra(cost,source,target);
printf("\nShortest Path: %d",co);
}

We hope you all have enjoyed the Shortest Path Dijkstras Algorithm. If you have any doubts
or any queries ask me in form of comments.

Read original Article: C Program to Find shortest Path using dijkstras algorithm | Learn C
Programming | C Language | C programs

C program of Regula Falsi method to find


root
Mr Coder August 20, 2012 1

inShare

C program of Regula Falsi method to find root of polynomials : The RegulaFalsi


Method is a numerical method for estimating the roots of a polynomial f(x). A value x
replaces the midpoint in the Bisection Method and serves as the new approximation of a root
of f(x). The objective is to make convergence faster. Assume that f(x) is continuous.
Algorithm for the RegulaFalsi Method: Given a continuous function f(x)

1. Find points a and b such that a < b and f(a) * f(b) < 0.
2. Take the interval [a, b] and determine the next value of x1.
3. If f(x1) = 0 then x1 is an exact root, else if f(x1) * f(b) < 0 then let a = x1,
else if f(a) * f(x1) < 0 then let b = x1.
4. Repeat steps 2 & 3 until f(xi) = 0 or |f(xi)| DOA, where DOA stands for
degree of accuracy.

Now let us see how to write a C program of Regula Falsi method to find root of polynomials
which accepts Maximum power of X , co-efficient of each x^ and Interval lower & upper
bound as input and then displays all the iterations & calculates the root of the polynomial.
For Example : Consider f(x) = x3 + 3x 5, where [ a = 1, b = 2 ] and DOA = 0.001
Now Maximum power of X =3, Coeffients x^0 = -5, x^1 = 3, x^2 = 0, x^3 = 1 , Interval lower
bound = 1 and Interval upper bound =2. (Note : Follow these conventions else code may not
produce proper results).
C program of Regula Falsi method to find root of polynomials :
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define DOA 0.001

int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1,x2,x3=0,t=0;
float fx1=0,fx2=0,fx3=0,temp=0;
int check()
{
printf("\n\n\tInterval lower bound a = ");
scanf("%f",&x1);

printf("\n\tInterval upper bound b = ");


scanf("%f",&x2);

fx1=fx2=fx3=0.0;

for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
fx2+=coef[i] * (pow(x2,i))

}
fx1+=coef[0];
fx2+=coef[0];
if( (fx1*fx2)>0)
{
printf("\n\tInterval lower and upper bounds are not perfect.");
return(1);
}
return(0);
}

int main()
{
printf("-----------------------------------------------------------\n");
printf("----------------------Made by C code
champ-----------------\n");
printf("-----------------------------------------------------------\n\n"
);
printf("\n\n\t

PROGRAM FOR REGULAR-FALSI METHOD\n");

printf("\n\n\nENTER THE MAXIMUM POWER OF X = ");


scanf("%d",&user_power);

for(i=0;i<=user_power;i++)

{
printf("\n\t x^%d = ",i);
scanf("%d",&coef[i]);
}

printf("\n");

printf("\n\n\t THE POLYNOMIAL IS = ");


for(i=user_power;i>=0;i--)/*for printing coefficients*/
{
printf(" %dx^%d",coef[i],i);
}

while(1)
{
if(check()==0)
{
flag=1;
break;
}
check();
}

printf("-----------------------------------------------------------\n");
printf("\n ITERATION
\n");

f(a)

f(b)

f(x)

printf("-----------------------------------------------------------\n");

if(flag==1)
{

do
{
cnt++;
fx1=fx2=fx3=0;
for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
fx2+=coef[i] * (pow(x2,i))

}
fx1+=coef[0];
fx2+=coef[0];
temp=x3;
x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);

for(i=user_power;i>=1;i--)
{
fx3+=coef[i]*(pow(x3,i));
}
fx3+=coef[0];

printf("\n \t%d
%.4f
%.4f",cnt,x1,fx1,x2,fx2,x3,fx3);
t=fx1*fx3;
if(t>0)
{
x1=x3;

}
if(t<0)

%.4f

%.4f

%.4f

%.4f

{
x2=x3;
}
fx3=0;
}while((fabs(temp-x3))>=DOA);
printf("\n\n\n\tROOT OF POLYNOMIAL EQUATION IS = %f",x3);
}
getch();
}

We hope you all have enjoyed the C program of Regula Falsi method to find root. If you have
any queries related to the code ask us in form of comments.
Read original Article: C program of Regula Falsi method to find root | Learn C
Programming | C Language | C programs

You might also like