Professional Documents
Culture Documents
Numerical Analysis
1. Gauss Elimination 2
2. Gauss Jordon 5
3. Gauss Jacobi 8
4. Gauss Seidel 11
5. Bisection Method 14
6. Regula Falsi 17
7. Secant Method 20
8. Newton Raphson 23
9. Trapezoidal 25
Graph Theory
11. BFS 28
12. DFS 31
13. Dijkstra 34
14. Kruskal 37
1
//Gauss Elimination
#include<stdio.h>
#include<stdlib.h>
int n;
float A[100][100], X[100];
void printAr()
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n+1; j++)
{
printf("%6g", A[i][j]);
}
printf("\n\n");
}
}
void printsol()
{
int i;
puts("Hence the solution : ");
for(i=0;i<n;i++)
{
printf("\nValue of x%d is %g.\n", i+1, X[i]);
}
}
void input()
{
int i, j;
puts("No. of variables and equations involved : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nFor Equation %d\n", i+1);
for(j=0; j<n; j++)
{
printf("\nEnter coefficient for x%d : \n", j+1);
scanf("%f", &A[i][j]);
}
printf("\nEnter the constant term : \n");
scanf("%f", &A[i][j]);
}
printAr();
}
void GaussEli()
{
int i, j, k;
float sum, m;
for(i=0; i<n-1; i++)
{
if(A[i][i]==0)
{
//printf("\n%d : %g\n", i, A[i][i]);
puts("No solution.");
exit(0);
}
for(j=i+1; j<n; j++)
{
m=A[j][i]/A[i][i];
for(k=i; k<n+1; k++)
A[j][k]=A[j][k]-m*A[i][k];
2
}
}
if(A[n-1][n-1]==0)
{
//printf("\n%d : %g\n", n-1, A[n-1][n-1]);
puts("No solution.");
exit(0);
}
X[n-1]=A[n-1][n]/A[n-1][n-1];
for(i=n-2; i>=0; i--)
{
sum=0;
for(j=i+1; j<n; j++)
{
sum=sum+A[i][j]*X[j];
}
X[i]=(A[i][n]-sum)/A[i][i];
}
}
int main()
{
puts("Program to illustrate Gauss elimination.");
input();
GaussEli();
printsol();
system("pause");
}
/*Output
Program to illustrate Gauss elimination.
No. of variables and equations involved :
3
For Equation 1
For Equation 2
1 -2 1 8
3 -1 2 11
Value of x1 is 1.
Value of x2 is -2.
Value of x3 is 3.
Press any key to continue . . .
*/
4
//Gauss Jordon
#include<stdio.h>
#include<stdlib.h>
int n;
float A[100][100], X[100];
void printAr()
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n+1; j++)
{
printf("%6g", A[i][j]);
}
printf("\n\n");
}
}
void printsol()
{
int i;
puts("Hence the solution : ");
for(i=0;i<n;i++)
{
printf("\nValue of x%d is %g.\n", i+1, X[i]);
}
}
void input()
{
int i, j;
puts("No. of variables and equations involved : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nFor Equation %d\n", i+1);
for(j=0; j<n; j++)
{
printf("\nEnter coefficient for x%d : \n", j+1);
scanf("%f", &A[i][j]);
}
printf("\nEnter the constant term : \n");
scanf("%f", &A[i][j]);
}
printAr();
}
void GaussJor()
{
int i, j, k;
float temp, sum, m;
for(i=0; i<n; i++)
{
temp=A[i][i];
if(temp==0)
{
puts("No solution.");
exit(0);
}
for(j=0; j<n+1; j++)
A[i][j]=A[i][j]/temp;
for(j=0; j<n; j++)
{
if(j!=i)
5
{
m=A[j][i];
for(k=i; k<n+1; k++)
A[j][k]=A[j][k]-m*A[i][k];
}
}
}
printAr();
for(i=0; i<n; i++)
X[i]=A[i][n];
}
int main()
{
puts("Program to illustrate Gauss Jordon.");
input();
GaussJor();
printsol();
system("pause");
}
/*Output
Program to illustrate Gauss Jordon.
No. of variables and equations involved :
3
For Equation 1
For Equation 2
For Equation 3
1 -2 1 8
3 -1 2 11
1 0 0 1
-0 1 0 -2
-0 -0 1 3
Value of x1 is 1.
Value of x2 is -2.
Value of x3 is 3.
Press any key to continue . . .
*/
7
//Gauss Jacobi
#include<stdio.h>
#include<stdlib.h>
int n;
float A[100][100], X[100], OldX[100];
void printAr()
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n+1; j++)
{
printf("%6g", A[i][j]);
}
printf("\n\n");
}
}
void printsol()
{
int i;
puts("Hence the solution : ");
for(i=0;i<n;i++)
{
printf("\nValue of x%d is %g.\n", i+1, X[i]);
}
}
void input()
{
int i, j;
puts("No. of variables and equations involved : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nFor Equation %d\n", i+1);
for(j=0; j<n; j++)
{
printf("\nEnter coefficient for x%d : \n", j+1);
scanf("%f", &A[i][j]);
}
printf("\nEnter the constant term : \n");
scanf("%f", &A[i][j]);
}
printAr();
}
void GaussJac()
{
int i, j, k, count=0;
float sum, m, error, temp;
for(i=0; i<n; i++)
{
sum=0;
for(j=0; j<n; j++)
sum+=A[i][j];
if(sum>=2*A[i][i])
{
puts("Not strictly diagonally dominating.");
system("pause");
exit(0);
}
}
for(i=0; i<n; i++)
8
OldX[i]=0;
do
{
for(i=0; i<n; i++)
{
sum=0;
for(j=0; j<n; j++)
{
if(j!=i)
sum+=OldX[j]*A[i][j];
}
X[i]=(A[i][n]-sum)/A[i][i];
}
sum=0;
for(i=0; i<n; i++)
{
temp=X[i]-OldX[i];
if(temp>=0)
sum+=temp;
else
sum-=temp;
}
error=sum/n;
for(i=0; i<n; i++)
OldX[i]=X[i];
count++;
}while(error>0.00000000001);
printf("\n\nTotal number of iterations : %d\n\n", count);
}
int main()
{
puts("Program to illustrate Gauss Jacobi.");
input();
GaussJac();
printsol();
system("pause");
}
/*Output
Program to illustrate Gauss Jacobi.
No. of variables and equations involved :
3
For Equation 1
For Equation 2
For Equation 3
3 100 4 200
2 9 300 120
Value of x1 is 0.697496.
Value of x2 is 1.96562.
Value of x3 is 0.336381.
Press any key to continue . . .
*/
10
//Gauss Seidel
#include<stdio.h>
#include<stdlib.h>
int n;
float A[100][100], X[100], OldX[100];
void printAr()
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n+1; j++)
{
printf("%6g", A[i][j]);
}
printf("\n\n");
}
}
void printsol()
{
int i;
puts("Hence the solution : ");
for(i=0;i<n;i++)
{
printf("\nValue of x%d is %g.\n", i+1, X[i]);
}
}
void input()
{
int i, j;
puts("No. of variables and equations involved : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nFor Equation %d\n", i+1);
for(j=0; j<n; j++)
{
printf("\nEnter coefficient for x%d : \n", j+1);
scanf("%f", &A[i][j]);
}
printf("\nEnter the constant term : \n");
scanf("%f", &A[i][j]);
}
printAr();
}
void GaussSei()
{
int i, j, k, count=0;
float sum, m, error, temp;
for(i=0; i<n; i++)
{
sum=0;
for(j=0; j<n; j++)
sum+=A[i][j];
if(sum>=2*A[i][i])
{
puts("Not strictly diagonally dominating.");
system("pause");
exit(0);
}
}
for(i=0; i<n; i++)
11
OldX[i]=X[i]=0;
do
{
for(i=0; i<n; i++)
{
sum=0;
for(j=0; j<n; j++)
{
if(j!=i)
sum+=X[j]*A[i][j];
}
X[i]=(A[i][n]-sum)/A[i][i];
}
sum=0;
for(i=0; i<n; i++)
{
temp=X[i]-OldX[i];
if(temp>=0)
sum+=temp;
else
sum-=temp;
}
error=sum/n;
for(i=0; i<n; i++)
OldX[i]=X[i];
count++;
}while(error>0.00000000001);
printf("\n\nTotal number of iterations : %d\n\n", count);
}
int main()
{
puts("Program to illustrate Gauss Seidel.");
input();
GaussSei();
printsol();
system("pause");
}
/*Output
Program to illustrate Gauss Seidel.
No. of variables and equations involved :
3
For Equation 1
For Equation 2
For Equation 3
3 100 4 200
2 9 300 120
Value of x1 is 0.697496.
Value of x2 is 1.96562.
Value of x3 is 0.336381.
Press any key to continue . . .
*/
13
//Bisection Method for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double u, l, poly[100];
int n;
void inputpoly()
{
int i;
puts("Enter the degree of polynomial : ");
scanf("%d", &n);
for(i=n; i>=0; i--)
{
printf("\nEnter the coefficient of X^%d\n", i);
scanf("%lf",&poly[i]);
}
printf("\n\nHence the polynomial equation :\n");
for(i=n; i>0; i--)
printf("(%lg, %d) + ", poly[i], i);
printf("(%lg, %d) = 0\n\n", poly[i], i);
}
void inputint()
{
while(1){
puts("Enter the lower limit of interval to search root : ");
scanf("%lf", &l);
puts("Enter the upper limit of interval to search root : ");
scanf("%lf", &u);
if(fx(l)*fx(u)>=0)
puts("Root may not be present, choose another interval.");
else
break;
}
}
void Bisect()
{
double error;
double c, oldc, fl, fu, fc;
int count=0;
fl=fx(l);
fu=fx(u);
c=(l+u)/2;
do
{
oldc=c;
count++;
fc=fx(c);
14
if(fc==0)
{
break;
}
else if(fc>0)
{
if(fu<0)
l=c;
else
u=c;
}
else
{
if(fu>0)
l=c;
else
u=c;
}
c=(l+u)/2;
if(c>=oldc)
error=c-oldc;
else
error=oldc-c;
}while(error>0.00000000001);
printf("\nTotal number of iterations : %d\n", count);
printf("\nThe root is : %lg\n", c);
}
int main()
{
puts("Program to illustrate Bisection Method for Polynomials.");
inputpoly();
inputint();
Bisect();
system("pause");
}
/*Output:
Program to illustrate Bisection Method for Polynomials.
Enter the degree of polynomial :
3
16
//Regula Falsi Method for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double u, l, poly[100];
int n;
void inputpoly()
{
int i;
puts("Enter the degree of polynomial : ");
scanf("%d", &n);
for(i=n; i>=0; i--)
{
printf("\nEnter the coefficient of X^%d\n", i);
scanf("%lf",&poly[i]);
}
printf("\n\nHence the polynomial equation :\n");
for(i=n; i>0; i--)
printf("(%lg, %d) + ", poly[i], i);
printf("(%lg, %d) = 0\n\n", poly[i], i);
}
void inputint()
{
while(1){
puts("Enter the lower limit of interval to search root : ");
scanf("%lf", &l);
puts("Enter the upper limit of interval to search root : ");
scanf("%lf", &u);
if(fx(l)*fx(u)>=0)
puts("Root may not be present, choose another interval.");
else
break;
}
}
void Regula()
{
double error;
double c, oldc, fl, fu, fc;
17
int count=0;
fl=fx(l);
fu=fx(u);
c=cnew(fl, fu);
do
{
oldc=c;
count++;
fc=fx(c);
if(fc==0)
break;
else if(fc>0)
{
if(fu<0)
{
l=c;
fl=fc;
}
else
{
u=c;
fu=fc;
}
}
else
{
if(fu>0)
{
l=c;
fl=fc;
}
else
{
u=c;
fu=fc;
}
}
c=cnew(fl, fu);
if(c>=oldc)
error=c-oldc;
else
error=oldc-c;
}while(error>0.00000000001);
printf("\nTotal number of iterations : %d\n", count);
printf("\nThe root is : %lg\n", c);
}
int main()
{
puts("Program to illustrate Regula Falsi Method for Polynomials.");
inputpoly();
inputint();
Regula();
system("pause");
}
/*Output:
Program to illustrate Regula Falsi Method for Polynomials.
Enter the degree of polynomial :
3
19
//Secant Method for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double u, l, poly[100];
int n;
void inputpoly()
{
int i;
puts("Enter the degree of polynomial : ");
scanf("%d", &n);
for(i=n; i>=0; i--)
{
printf("\nEnter the coefficient of X^%d\n", i);
scanf("%lf",&poly[i]);
}
printf("\n\nHence the polynomial equation :\n");
for(i=n; i>0; i--)
printf("(%lg, %d) + ", poly[i], i);
printf("(%lg, %d) = 0\n\n", poly[i], i);
}
void inputint()
{
puts("Enter the lower estimate in neighbourhood : ");
scanf("%lf", &l);
puts("Enter the upper estimate in neighbourhood : ");
scanf("%lf", &u);
}
void Secant()
{
double error;
double c, oldc, fc;
int count=0;
c=cnew(l, u);
do
{
oldc=c;
count++;
fc=fx(c);
20
if(fc==0)
break;
else
{
c=cnew(u, c);
u=oldc;
}
if(c>=oldc)
error=c-oldc;
else
error=oldc-c;
}while(error>0.00000000001);
printf("\nTotal number of iterations : %d\n", count);
printf("\nThe root is : %lg\n", c);
}
int main()
{
puts("Program to illustrate Secant Method for Polynomials.");
inputpoly();
inputint();
Secant();
}
/*Output:
Program to illustrate Secant Method for Polynomials.
Enter the degree of polynomial :
5
--------------------------------
Process exited after 32.07 seconds with return value 23
21
Press any key to continue . . .*/
22
//Newton Raphson for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double l, poly[100];
int n;
void inputpoly()
{
int i;
puts("Enter the degree of polynomial : ");
scanf("%d", &n);
for(i=n; i>=0; i--)
{
printf("\nEnter the coefficient of X^%d\n", i);
scanf("%lf",&poly[i]);
}
printf("\n\nHence the polynomial equation :\n");
for(i=n; i>0; i--)
printf("(%lg, %d) + ", poly[i], i);
printf("(%lg, %d) = 0\n\n", poly[i], i);
}
void inputint()
{
puts("Enter a number in the neighbourhood to search root : ");
scanf("%lf", &l);
}
void NewRaph()
{
double error;
double c, oldc, fl, fc;
23
int count=0;
c=cnew(l);
do
{
oldc=c;
count++;
fc=fx(c);
if(fc==0)
break;
else
c=cnew(c);
if(c>=oldc)
error=c-oldc;
else
error=oldc-c;
}while(error>0.00000000001);
printf("\nTotal number of iterations : %d\n", count);
printf("\nThe root is : %lg\n", c);
}
int main()
{
puts("Program to illustrate Newton Raphson Method for Polynomials.");
inputpoly();
inputint();
NewRaph();
}
/*Output:
Program to illustrate Newton Raphson Method for Polynomials.
Enter the degree of polynomial :
3
--------------------------------
Process exited after 44.74 seconds with return value 24
Press any key to continue . . .*/
24
//Trapezoidal
#include<stdio.h>
#define N 10
float func(float *co,int n,float x)
{
int i;
float m=1,s=0;
for(i=n;i>=0;i--)
{
s=s+co[i]*m;
m=m*x;
}
return s;
}
float composite_trapezoid(float a[N],int n,float l,float u,int I)
{
float h=(u-l)/I,s;
int i;
s=h*(func(a,n,l)+func(a,n,u))/2;
for(i=1;i<I;i++)
{
s=s+h*func(a,n,l+i*h);
}
return s;
}
int main()
{
int i,n,I;
float ar[N],a,b;
printf("Enter the highest degree of the polynomial:- ");
scanf("%d",&n);
printf("Enter the coefficients :-\n");
for(i=0;i<=n;i++)
{
scanf("%f",&ar[i]);
}
printf("Enter the lower and upper limit of integration:-\n");
scanf("%f %f",&a,&b);
printf("Enter the number of subintervals:- ");
scanf("%d",&I);
printf("The result of integration is:- %g\n",composite_trapezoid(ar,n,a,b,I));
}
/*Output:
Enter the highest degree of the polynomial:-
4
Enter the coefficients :-
2
0
4
0
-5
Enter the lower and upper limit of integration:-
02
Enter the number of subintervals:- 2000
The result of integration is:- 13.4667
--------------------------------
Process exited after 26.32 seconds with return value 40
Press any key to continue . . .*/
25
//Simpson's 1/3rd rule
#include<stdio.h>
#define N 10
float func(float *co,int n,float x)
{
int i;
float m=1,s=0;
for(i=n;i>=0;i--)
{
s=s+co[i]*m;
m=m*x;
}
return s;
}
float composite_simpson(float a[N],int n,float l,float u,int I)
{
I=2*I;
float h=(u-l)/I,s;
int i;
s=h*(func(a,n,l)+func(a,n,u))/3;
for(i=1;i<I;i++)
{
if(i%2==0)
{
s=s+2*h*func(a,n,l+i*h)/3;
}
else
{
s=s+4*h*func(a,n,l+i*h)/3;
}
}
return s;
}
int main()
{
int i,n,I;
float ar[N],a,b;
printf("Enter the highest degree of the polynomial:- ");
scanf("%d",&n);
printf("Enter the coefficients :-\n");
for(i=0;i<=n;i++)
{
scanf("%f",&ar[i]);
}
printf("Enter the lower and upper limit of integration:-\n");
scanf("%f %f",&a,&b);
printf("Enter the number of subintervals(even):- ");
scanf("%d",&I);
printf("The result of integration is:- %g\n",composite_simpson(ar,n,a,b,I/2));
}
/*Output:
Enter the highest degree of the polynomial:- 5
Enter the coefficients :-
5
-2
0
3
-2
3
Enter the lower and upper limit of integration:-
03
Enter the number of subintervals(even):- 30000
26
The result of integration is:- 537.3
--------------------------------
Process exited after 30.02 seconds with return value 38
Press any key to continue . . .*/
27
//BFS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int data, link[100];
}graph[100];
void create()
{
int i,j,ln[100];
char s[500], *tok;
puts("Enter total number of nodes : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter data in node-%d : ", i+1);
scanf("%d", &graph[i].data);
for(j=0;j<n;j++)
{
graph[i].link[j]=0;
}
}
while ((getchar()) != '\n');
for(i=0;i<n;i++)
{
printf("\nEnter the node indexes that node-%d is linked to, separated by spaces : ", i+1);
gets(s);
tok=strtok(s, " ");
for(j=0;tok!=NULL;j++)
{
sscanf(tok, "%d", &ln[j]);
tok=strtok(NULL, " ");
}
ln[j]=-1;
for(j=0;ln[j]!=-1;j++)
{
graph[i].link[ln[j]-1]=1;
}
}
}
int deQ()
{
int res;
if(F==-1)
return -1;
if(F==R)
{
res=Q[F];
F=-1;
R=-1;
}
else
{
res=Q[F];
F=(F+1)%n;
return res;
}
}
int main()
{
puts("Program to implement BFS Graph traversal : ");
create();
disp();
bfs(0);
}
29
/*Output:
Program to implement BFS Graph traversal :
Enter total number of nodes :
5
Enter data in node-1 : 12
Enter data in node-2 : 14
Enter data in node-3 : 16
Enter data in node-4 : 18
Enter data in node-5 : 20
Enter the node indexes that node-1 is linked to, separated by spaces : 2 3
Enter the node indexes that node-2 is linked to, separated by spaces : 4 1 3
Enter the node indexes that node-3 is linked to, separated by spaces : 5 2 1 4
Enter the node indexes that node-4 is linked to, separated by spaces : 5 3 2
Enter the node indexes that node-5 is linked to, separated by spaces : 4 3
0 1 1 0 0
1 0 1 1 0
1 1 0 1 1
0 1 1 0 1
0 0 1 1 0
--------------------------------
Process exited after 65.95 seconds with return value 0
Press any key to continue . . .*/
30
//DFS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int data, link[100];
}graph[100];
void create()
{
int i,j,ln[100];
char s[500], *tok;
puts("Enter total number of nodes : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter data in node-%d : ", i+1);
scanf("%d", &graph[i].data);
for(j=0;j<n;j++)
{
graph[i].link[j]=0;
}
}
while ((getchar()) != '\n');
for(i=0;i<n;i++)
{
printf("\nEnter the node indexes that node-%d is linked to, separated by spaces : ", i+1);
gets(s);
tok=strtok(s, " ");
for(j=0;tok!=NULL;j++)
{
sscanf(tok, "%d", &ln[j]);
tok=strtok(NULL, " ");
}
ln[j]=-1;
for(j=0;ln[j]!=-1;j++)
{
graph[i].link[ln[j]-1]=1;
}
}
}
int pop()
{
int res;
if(top==-1)
return -1;
res=S[top--];
return res;
}
int checkstate()
{
int i;
for(i=0; i<n; i++)
{
if(state[i]==0)
return 1;//Unvisited vertex exists
}
return 0;//Whole graph visited
}
int main()
{
puts("Program to implement DFS Graph traversal : ");
create();
disp();
dfs(0);
}
/*Output:
32
Program to implement DFS Graph traversal :
Enter total number of nodes :
8
Enter data in node-1 : 16
Enter data in node-2 : 18
Enter data in node-3 : 20
Enter data in node-4 : 22
Enter data in node-5 : 24
Enter data in node-6 : 26
Enter data in node-7 : 28
Enter data in node-8 : 30
Enter the node indexes that node-1 is linked to, separated by spaces : 2 3
Enter the node indexes that node-2 is linked to, separated by spaces : 1 4 3
Enter the node indexes that node-3 is linked to, separated by spaces : 1 2 6 8
Enter the node indexes that node-4 is linked to, separated by spaces : 2
Enter the node indexes that node-5 is linked to, separated by spaces : 6
Enter the node indexes that node-6 is linked to, separated by spaces : 3 5 7
Enter the node indexes that node-7 is linked to, separated by spaces : 6
Enter the node indexes that node-8 is linked to, separated by spaces : 3
0 1 1 0 0 0 0 0
1 0 1 1 0 0 0 0
1 1 0 0 0 1 0 1
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 1 0 1 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
33
//Dijkstra
#include<stdio.h>
#include<stdlib.h>
#define TEMP 20
int adjmat[TEMP][TEMP],vn,i,j,k,visit[TEMP],value[TEMP],vsearch;
void initialise()
{
for(i=0;i<TEMP;i++)
{
for(j=0;j<TEMP;j++)
adjmat[i][j]=0;
visit[i]=0;
value[i]=999;
}
}
void input()
{
printf("\nEnter number of vertex in graph:\t");
scanf("%d",&vn);
for(i=0;i<vn;i++)
{
for(j=i+1;j<vn;j++)
{
printf("\nEnter value of edge between vertex %d and vertex %d, 0 or less if no edge
exists:\t",(i+1),(j+1));
scanf("%d",&adjmat[i][j]);
if(adjmat[i][j]<=0)
adjmat[i][j]=0;
adjmat[j][i]=adjmat[i][j];
}
}
}
int findleast()
{
int flag=0,min;
for(i=0;i<vn;i++)
{
if(visit[i]==0)
{
min=i;
flag=1;
break;
}
}
if(flag==0)
return -1;
for(i=0;i<vn;i++)
{
if(value[i]<value[min]&&visit[i]==0)
min=i;
}
return min;
}
void Dijkstra()
{
printf("\nEnter starting vertex:\t");
scanf("%d",&vsearch);
int cur=vsearch-1;
value[vsearch-1]=0;
visit[cur]=1;
while(1)
{
34
for(i=0;i<vn;i++)
{
if(adjmat[cur][i]>0&&visit[i]==0&&cur!=i)
{
if(value[i]>(value[cur]+adjmat[cur][i]))
value[i]=(value[cur]+adjmat[cur][i]);
}
}
cur=findleast();
if(cur==-1)
break;
visit[cur]=1;
}
}
void display()
{
printf("\nShortest distance from vertex %d to other vertexes:",vsearch);
for(i=0;i<vn;i++)
{
if(i!=(vsearch-1))
printf("\nVertex %d:\t%d",(i+1),value[i]);
}
}
int main()
{
initialise();
input();
Dijkstra();
display();
}
/*Output:
Enter value of edge between vertex 1 and vertex 2, 0 or less if no edge exists: 2
Enter value of edge between vertex 1 and vertex 3, 0 or less if no edge exists: -1
Enter value of edge between vertex 1 and vertex 4, 0 or less if no edge exists: -1
Enter value of edge between vertex 1 and vertex 5, 0 or less if no edge exists: 5
Enter value of edge between vertex 2 and vertex 3, 0 or less if no edge exists: 4
Enter value of edge between vertex 2 and vertex 4, 0 or less if no edge exists: 3
Enter value of edge between vertex 2 and vertex 5, 0 or less if no edge exists: 1
Enter value of edge between vertex 3 and vertex 4, 0 or less if no edge exists: 2
Enter value of edge between vertex 3 and vertex 5, 0 or less if no edge exists: -1
Enter value of edge between vertex 4 and vertex 5, 0 or less if no edge exists: 3
36
//Kruskal
#include<stdio.h>
#include<stdlib.h>
#define TEMP 20
int adjmat[TEMP][TEMP],vn,beg,mined,end,ne,i,j,k,visit[TEMP],value[TEMP];
int mst[TEMP][TEMP],minsum;
void initial()
{
for(i=0;i<TEMP;i++)
{
for(j=0;j<TEMP;j++)
{
adjmat[i][j]=999;
mst[i][j]=0;
}
visit[i]=0;
value[i]=i;
}
ne=0;
minsum=0;
}
void getData()
{
printf("\nEnter number of vertex in graph:\t");
scanf("%d",&vn);
for(i=0;i<vn;i++)
{
for(j=i+1;j<vn;j++)
{
printf("\nEnter value of edge between vertex %d and vertex %d, 0 or less if no edge
exists:\t",(i+1),(j+1));
scanf("%d",&adjmat[i][j]);
if(adjmat[i][j]<=0)
adjmat[i][j]=999;
adjmat[j][i]=adjmat[i][j];
}
}
}
int checkcycle(int u,int v)
{
if(value[u]==value[v])
return 0;
return 1;
}
void Kruskal()
{
beg=0;end=0;mined=adjmat[beg][end];
while(ne<vn-1)
{
mined=999;
for(i=0;i<vn;i++)
{
for(j=i+1;j<vn;j++)
{
if(mined>adjmat[i][j])
{
mined=adjmat[i][j];
beg=i;
end=j;
}
}
}
37
if(checkcycle(beg,end)==1)
{
mst[beg][end]=mined;
mst[end][beg]=mined;
int status=value[beg];
for(i=0;i<vn;i++)
{
if(value[i]==value[end])
value[i]=status;
}
minsum=minsum+mined;
ne++;
}
adjmat[beg][end]=999;
adjmat[end][beg]=999;
}
}
void display()
{
int e=0,count=0;
for(i=0;i<vn;i++)
{
count=0;
for(j<0;j<vn;j++)
{
if(mst[i][j]!=0)
count++;
}
if(count==1)
{
end=i;
break;
}
}
printf("\n%d ",(end+1));
i=end;
for(k=0;k<vn;k++)
{
for(j=0;j<vn;j++)
{
if(mst[i][j]>0)
{
printf(" (%d) %d",mst[i][j],(j+1));
e=e+mst[i][j];
mst[i][j]=0;
mst[j][i]=0;
i=j;
break;
}
}
}
printf("\nMinimum Spanning Tree weight summation is %d",e);
}
int main()
{
initial();
getData();
Kruskal();
display();
}
/*Output:
38
Enter number of vertex in graph: 5
Enter value of edge between vertex 1 and vertex 2, 0 or less if no edge exists: 2
Enter value of edge between vertex 1 and vertex 3, 0 or less if no edge exists: -1
Enter value of edge between vertex 1 and vertex 4, 0 or less if no edge exists: -1
Enter value of edge between vertex 1 and vertex 5, 0 or less if no edge exists: 5
Enter value of edge between vertex 2 and vertex 3, 0 or less if no edge exists: 4
Enter value of edge between vertex 2 and vertex 4, 0 or less if no edge exists: 3
Enter value of edge between vertex 2 and vertex 5, 0 or less if no edge exists: 1
Enter value of edge between vertex 3 and vertex 4, 0 or less if no edge exists: 2
Enter value of edge between vertex 3 and vertex 5, 0 or less if no edge exists: -1
Enter value of edge between vertex 4 and vertex 5, 0 or less if no edge exists: 3
4 (3) 2 (2) 1
Minimum Spanning Tree weight summation is 5
--------------------------------
Process exited after 55.93 seconds with return value 44
Press any key to continue . . .*/
39
//Floyd Warshall
#include <stdio.h>
#include <string.h>
#define INF (int)1e9
int main(void)
{
int a[10][10],b[10][10],i,j,k,nv;
char path[10][10][10],nw[10];
nw[0]='\0';
printf("\nEnter no. of vertex ");
scanf("%d",&nv);
for(i=0;i<nv;i++)
for(j=0;j<nv;j++)
{
printf("\nEnter a[%d][%d] = ", i, j);
scanf("%d",&a[i][j]);
path[i][j][0]=i+1;
path[i][j][1]=j+1;
path[i][j][2]='\0';
if(i!=j && a[i][j]==0)
a[i][j]=INF;
}
for(k=0;k<nv;k++)
{
for(i=0;i<nv;i++)
{
for(j=0;j<nv;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
path[i][j][0]='\0';
strcpy(path[i][j],path[i][k]);
path[i][j][strlen(path[i][j])-1]='\0';
strcat(path[i][j],path[k][j]);
}
}
}
}
for(i=0;i<nv;i++)
for(j=0;j<nv;j++)
{
if(i!=j)
{
printf("\nPath for v%d to v%d is:-\n",i,j);
for(k=0;k<strlen(path[i][j]);k++)
printf("v%d->",path[i][j][k]-1);
printf("\nAnd minimum cost is %d\n",a[i][j]);
}
}
return 0;
}
/*Output:
Enter a[0][0] = 0
Enter a[0][1] = 2
Enter a[0][2] = 0
40
Enter a[0][3] = 0
Enter a[0][4] = 5
Enter a[1][0] = 2
Enter a[1][1] = 0
Enter a[1][2] = 4
Enter a[1][3] = 3
Enter a[1][4] = 1
Enter a[2][0] = 0
Enter a[2][1] = 4
Enter a[2][2] = 0
Enter a[2][3] = 2
Enter a[2][4] = 0
Enter a[3][0] = 0
Enter a[3][1] = 3
Enter a[3][2] = 2
Enter a[3][3] = 0
Enter a[3][4] = 3
Enter a[4][0] = 5
Enter a[4][1] = 1
Enter a[4][2] = 0
Enter a[4][3] = 3
Enter a[4][4] = 0
42
--------------------------------
Process exited after 79.42 seconds with return value 0
Press any key to continue . . .*/
43