You are on page 1of 43

Index

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

10. Simpson’s 1/3rd Rule 26

Graph Theory
11. BFS 28

12. DFS 31

13. Dijkstra 34

14. Kruskal 37

15. Floyd Warshall 40

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

Enter coefficient for x1 :


2

Enter coefficient for x2 :


-1

Enter coefficient for x3 :


2

Enter the constant term :


10

For Equation 2

Enter coefficient for x1 :


1

Enter coefficient for x2 :


-2

Enter coefficient for x3 :


1

Enter the constant term :


8
3
For Equation 3

Enter coefficient for x1 :


3

Enter coefficient for x2 :


-1

Enter coefficient for x3 :


2

Enter the constant term :


11
2 -1 2 10

1 -2 1 8

3 -1 2 11

Hence the solution :

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

Enter coefficient for x1 :


2

Enter coefficient for x2 :


-1

Enter coefficient for x3 :


2

Enter the constant term :


10

For Equation 2

Enter coefficient for x1 :


1

Enter coefficient for x2 :


-2

Enter coefficient for x3 :


1

Enter the constant term :


8

For Equation 3

Enter coefficient for x1 :


3

Enter coefficient for x2 :


-1
6
Enter coefficient for x3 :
2

Enter the constant term :


11
2 -1 2 10

1 -2 1 8

3 -1 2 11

1 0 0 1

-0 1 0 -2

-0 -0 1 3

Hence the solution :

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

Enter coefficient for x1 :


200

Enter coefficient for x2 :


5

Enter coefficient for x3 :


2

Enter the constant term :


150

For Equation 2

Enter coefficient for x1 :


9
3

Enter coefficient for x2 :


100

Enter coefficient for x3 :


4

Enter the constant term :


200

For Equation 3

Enter coefficient for x1 :


2

Enter coefficient for x2 :


9

Enter coefficient for x3 :


300

Enter the constant term :


120
200 5 2 150

3 100 4 200

2 9 300 120

Total number of iterations : 8

Hence the solution :

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

Enter coefficient for x1 :


200

Enter coefficient for x2 :


5

Enter coefficient for x3 :


2

Enter the constant term :


150

For Equation 2

Enter coefficient for x1 :


12
3

Enter coefficient for x2 :


100

Enter coefficient for x3 :


4

Enter the constant term :


200

For Equation 3

Enter coefficient for x1 :


2

Enter coefficient for x2 :


9

Enter coefficient for x3 :


300

Enter the constant term :


120
200 5 2 150

3 100 4 200

2 9 300 120

Total number of iterations : 5

Hence the solution :

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;

double fx(double arg)


{
int i;
double sum=0;
for(i=n;i>=0;i--)
{
sum=sum+poly[i]*pow(arg,i);
}
return sum;
}

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

Enter the coefficient of X^3


1

Enter the coefficient of X^2


0

Enter the coefficient of X^1


-12

Enter the coefficient of X^0


8

Hence the polynomial equation :


(1, 3) + (0, 2) + (-12, 1) + (8, 0) = 0

Enter the lower limit of interval to search root :


0
Enter the upper limit of interval to search root :
1
15
Total number of iterations : 36

The root is : 0.694593


Press any key to continue . . .*/

16
//Regula Falsi Method for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double u, l, poly[100];
int n;

double fx(double arg)


{
int i;
double sum=0;
for(i=n;i>=0;i--)
{
sum=sum+poly[i]*pow(arg,i);
}
return sum;
}

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;
}
}

double cnew(double fl, double fu)


{
if(fl<0)
fl=-fl;
if(fu<0)
fu=-fu;
return l + (fl/(fl+fu))*(u-l);
}

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

Enter the coefficient of X^3


18
1

Enter the coefficient of X^2


0

Enter the coefficient of X^1


-12

Enter the coefficient of X^0


8

Hence the polynomial equation :


(1, 3) + (0, 2) + (-12, 1) + (8, 0) = 0

Enter the lower limit of interval to search root :


0
Enter the upper limit of interval to search root :
1

Total number of iterations : 10

The root is : 0.694593


Press any key to continue . . .*/

19
//Secant Method for polynomials
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double u, l, poly[100];
int n;

double fx(double arg)


{
int i;
double sum=0;
for(i=n;i>=0;i--)
{
sum=sum+poly[i]*pow(arg,i);
}
return sum;
}

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);
}

double cnew(double x0, double x1)


{
double f0, f1;
f0=fx(x0);
f1=fx(x1);
return (x0*f1-x1*f0)/(f1-f0);
}

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

Enter the coefficient of X^5


1

Enter the coefficient of X^4


-3

Enter the coefficient of X^3


5

Enter the coefficient of X^2


-7

Enter the coefficient of X^1


9

Enter the coefficient of X^0


-11

Hence the polynomial equation :


(1, 5) + (-3, 4) + (5, 3) + (-7, 2) + (9, 1) + (-11, 0) = 0

Enter the lower estimate in neighbourhood :


-1
Enter the upper estimate in neighbourhood :
1

Total number of iterations : 11

The root is : 1.85698

--------------------------------
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;

double fx(double arg)


{
int i;
double sum=0;
for(i=n;i>=0;i--)
sum=sum+poly[i]*pow(arg,i);
if(sum==0)
{
puts("Derivative is zero, Newton Raphson fails.");
exit(0);
}
return sum;
}

double dfx(double arg)


{
int i;
double sum=0;
for(i=n;i>0;i--)
sum=sum+poly[i]*i*pow(arg,i-1);
return sum;
}

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);
}

double cnew(double fl)


{
return fl - fx(fl)/dfx(fl);
}

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

Enter the coefficient of X^3


1

Enter the coefficient of X^2


0

Enter the coefficient of X^1


-12

Enter the coefficient of X^0


8

Hence the polynomial equation :


(1, 3) + (0, 2) + (-12, 1) + (8, 0) = 0

Enter a number in the neighbourhood to search root :


1

Total number of iterations : 4

The root is : 0.694593

--------------------------------
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];

int state[100], n, Q[100], F=-1, R=-1;


void disp()
{
int i,j;
printf("\nAdjacency matrix of Graph :\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%4d", graph[i].link[j]);
printf("\n\n");
}
}

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;
}
}
}

void enQ(int index)


{
if(R==-1)
{
F++;
28
Q[++R]=index;
return;
}
if(F==(R+1)%n)
{
puts("Overflow");
exit(0);
}
R=(R+1)%n;
Q[R]=index;
}

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;
}
}

void bfs(int vstart)


{
int i,j,res=1;
for(i=0;i<n;i++)
state[i]=0;//0 = unvisited
enQ(vstart);
state[vstart]=1;//1 = visited
printf("(%2d, %2d)", vstart+1, graph[vstart].data);
while(res!=-1)//-1 : Queue is empty
{
res=deQ();
for(i=0;i<n;i++)
{
if(graph[res].link[i]==1&&state[i]==0)
{
printf(" (%2d, %2d)", i+1, graph[i].data);
state[i]=1;
enQ(i);
}
}
state[res]=2;//2 = explored
}
}

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

Adjacency matrix of Graph :

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

( 1, 12) ( 2, 14) ( 3, 16) ( 4, 18) ( 5, 20)

--------------------------------
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];

int state[100], n, S[100], top=-1;


void disp()
{
int i,j;
printf("\nAdjacency matrix of Graph :\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%4d", graph[i].link[j]);
printf("\n\n");
}
}

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;
}
}
}

void push(int index)


{
if(top==n-1)
{
printf("\nStack overflow.\n");
31
exit(0);
}
S[++top]=index;
}

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
}

void dfs(int vstart)


{
int i,j,v,found;
for(i=0;i<n;i++)
state[i]=0;//0 = unvisited
state[vstart]=1;//1 = visited
printf("(%2d, %2d)", vstart+1, graph[vstart].data);
v=vstart;
while(checkstate())//Unvisited vertex exists
{
found=0;
for(i=0; i<n; i++)
{
if(graph[v].link[i]==1&&state[i]==0)
{
found=1;
printf(" (%2d, %2d)", i+1, graph[i].data);
state[i]=1;
push(v);
v=i;
break;
}
}
if(found==0)
v=pop();
}
}

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

Adjacency matrix of Graph :

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

( 1, 16) ( 2, 18) ( 3, 20) ( 6, 26) ( 5, 24) ( 7, 28) ( 8, 30) ( 4, 22)


--------------------------------
Process exited after 209.3 seconds with return value 0
Press any key to continue . . .*/

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 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

Enter starting vertex: 1

Shortest distance from vertex 1 to other vertexes:


Vertex 2: 2
Vertex 3: 6
Vertex 4: 5
Vertex 5: 3
35
--------------------------------
Process exited after 52.83 seconds with return value 5
Press any key to continue . . .*/

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 no. of vertex 5

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

Path for v0 to v1 is:-


v0->v1->
And minimum cost is 2

Path for v0 to v2 is:-


v0->v1->v2->
And minimum cost is 6

Path for v0 to v3 is:-


v0->v1->v3->
And minimum cost is 5

Path for v0 to v4 is:-


v0->v1->v4->
And minimum cost is 3

Path for v1 to v0 is:-


v1->v0->
41
And minimum cost is 2

Path for v1 to v2 is:-


v1->v2->
And minimum cost is 4

Path for v1 to v3 is:-


v1->v3->
And minimum cost is 3

Path for v1 to v4 is:-


v1->v4->
And minimum cost is 1

Path for v2 to v0 is:-


v2->v1->v0->
And minimum cost is 6

Path for v2 to v1 is:-


v2->v1->
And minimum cost is 4

Path for v2 to v3 is:-


v2->v3->
And minimum cost is 2

Path for v2 to v4 is:-


v2->v1->v4->
And minimum cost is 5

Path for v3 to v0 is:-


v3->v1->v0->
And minimum cost is 5

Path for v3 to v1 is:-


v3->v1->
And minimum cost is 3

Path for v3 to v2 is:-


v3->v2->
And minimum cost is 2

Path for v3 to v4 is:-


v3->v4->
And minimum cost is 3

Path for v4 to v0 is:-


v4->v1->v0->
And minimum cost is 3

Path for v4 to v1 is:-


v4->v1->
And minimum cost is 1

Path for v4 to v2 is:-


v4->v1->v2->
And minimum cost is 5

Path for v4 to v3 is:-


v4->v3->
And minimum cost is 3

42
--------------------------------
Process exited after 79.42 seconds with return value 0
Press any key to continue . . .*/

43

You might also like