Sei sulla pagina 1di 10

Lab Programs on Design and Analysis of Algorithms

Program 10:Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.

/*Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.*/
#include
#include
#include

void prims(int n, int cost[10][10])


{
int i,j,u,v,min;
int sum;
int k;
int t[10][2];
int p[10];
int d[10];
int s[10];
int source;

min=9999;
source=0;

for(i=0;i{
for(j=0;j{
if(cost[i][j] !=0 && cost[i][j]<=min)
{
min=cost[i][j];
source=i;
}
}
}

for(i=0;i{
d[i]=cost[source][i];
s[i]=0;
p[i]=source;
}

s[source]=1;

sum=0;
k=0;

for(i=1;i{
min=9999;
u=-1;
for(j=0;j{
if(s[j]==0)
{
if(d[j]<=min)
{
min=d[j];
u=j;
}
}
}

t[k][0]=u;
t[k][1]=p[u];
k++;

sum+=cost[u][p[u]];

s[u]=1;

for(v=0;v{
if(s[v]==0 && cost[u][v]{
d[v]=cost[u][v];
p[v]=u;
}
}
}

if(sum>=9999)
printf("Spanning tree does not exist\n");
else
{
printf("Spanning tree exists and minimum spanning tree is \n");
for(i=0;i{
printf("%d %d \n",t[i][0],t[i][1]);
}
printf("\n The cost of the spanning tree = %d",sum);
}
}

void read_matrix(int n, int a[][10])


{
int i,j;
for(i=0;i{
for(j=0;j{
scanf("%d",&a[i][j]);
}
}
}
void main()
{
int cost[10][10],n;

clrscr();

printf("Enter the number of nodes\n");


scanf("%d",&n);

printf("Enter the cost adjacency matrix\n");


read_matrix(n,cost);

prims(n,cost);
getch();
}

/*--------------------------OUTPUT:Sample-------------------------
Enter the number of nodes
4
Enter the cost adjacency matrix
0 20 10 9999
20 0 9999 30
10 9999 0 40
9999 30 40 0
Spanning tree exists and minimum spanning tree is
02
10
31

The cost of the spanning tree = 60


*/

Program 11: Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.

/*Implement All-Pairs Shortest Paths Problem using Floyd's algorithm. */


#include
#include

int min(int a,int b)


{
return a}

void floyd(int n,int cost[10][10], int d[10][10])


{
int i,j,k;

for(i=0;i{
for(j=0;j{
d[i][j]=cost[i][j];
}
}

for(k=0;k{
for(i=0;i{
for(j=0;j{
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
}

void read_adjacency_matrix(int n,int a[10][10])


{
int i,j;

for(i=0;i{
for(j=0;jscanf("%d",&a[i][j]);
}
}

void write_matrix(int n,int a[10][10])


{
int i,j;

for(i=0;i{
for(j=0;j{
printf("%5d",a[i][j]);
}
printf("\n");
}
}

void main()
{
int n, cost[10][10],d[10][10];

clrscr();

printf("Enter the number of nodes:");


scanf("%d",&n);

printf("\n Enter the cost adjacency matrix\n");


read_adjacency_matrix(n,cost);

floyd(n,cost,d);
printf("Solution to all-pairs shortest-paths problem is\n");
write_matrix(n,d);

getch();
}
/*------------------OUTPUT:Sample--------------------------------------
Enter the number of nodes:4

Enter the cost adjacency matrix


0 9999 3 9999
2 0 9999 9999
9999 7 0 1
6 9999 9999 0
Solution to all-pairs shortest-paths problem is
0 10 3 4
2056
7701
6 16 9 0
*/

Program 12: Implement N Queen's problem using Back Tracking.


/*Implement N Queen's problem using Back Tracking*/
#include<stdio.h>
#include<conio.h>
#include<math.h>

#define TRUE 1
#define FALSE 0

void print_solution(int n, int x[])


{
char c[10][10];
int i,j;

/* No queen has placed initially*/


for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
c[i][j]= '*';
}
}

/*Place the queens on the chess board*/


for(i=1; i<=n; i++)
{
c[i][x[i]]='Q';
}

/* Print where the queens have been placed*/


for(i=1;i<=n;i++)
{
for(j=1; j<=n; j++)
{
printf("%c",c[i][j]);
}
printf("\n");
}
}

/* Function to check whether the queen can be placed successfully or no */


int place(int x[],int k)
{
int i;

for(i=1; i<k; i++)


{

/* Check whether two queens attach vertically or diagonally*/


if(x[i]==x[k] || i-x[i]==k-x[k] || i+x[i]==k+x[k])
{

/*Queen cannot be placed in the kth column*/


return FALSE;
}
}
/* kth Queen can be successfully placed*/
return TRUE;
}

void nqueens(int n)
{
int x[10];
int count=0;/*Number of solutions*/
int k=1;/*Select the first queen*/

x[k]=0; /* But, not placed on the chess board*/

while(k!=0)/*A queen exists?*/


{
x[k]+=1; /*Place the kth queen in next column*/
while((x[k]<=n) && (!place(x,k)))
{
x[k]+=1; /* Place queen in next column*/
}

/* If queen successfully placed?*/


if(x[k]<=n)
{
/*If all queens are placed*/
if(k==n)
{
count++;
printf("\nSolution %d is \n",count);
print_solution(n,x);
}
else
{
k++;/*Select the next queen*/
x[k]=0;
}
}
else
{
/* Backtrack and select privious queen*/
k--;
}
}
/* No more solution exists*/
return;
}

void main()
{
int n;

clrscr();

printf("Enter the number of queens:");


scanf("%d", &n);

if(n==1||n==2||n==3)
{
printf("\nNo solution exists");
}
else

nqueens(n);
getch();
}
/*------------------OUTPUT:Sample-------------------------
Enter the number of queens:4

Solution 1 is
*Q**
***Q
Q***
**Q*

Solution 2 is
**Q*
Q***
***Q
*Q**

*/

Program 9: Implement Travelling Salesperson Problem


/*Program 9:C program to demonstrate travelling salesperson problem using dynamic
programming. */

#include<stdio.h>
#include<conio.h>

#define max 100


#define infinity 999

int tspdp(int c[][max],int tour[],int start,int n);

void main()
{
int n;/* Number of cities. */
int i,j,c[max][max];/* Loop counters. */
int tour[max];/* Cost matrix. */
cost;/* Least cost. */

clrscr();

printf("Travelling Salesman Problem Using Dynamic Programming\n");

printf("\nEnter number of cities to traverse:");


scanf("%d",&n);

printf("Enter cost matrix:\n");


for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&c[i][j]);
if(c[i][j]==0)
c[i][j]=999;
}

for(i=0;i<n;i++)
tour[i]=i;

cost=tspdp(c,tour,0,n);

printf("Minimum Cost: %d",cost);

printf("\nTour:\n");

for(i=0;i<n;i++)
printf("%d - ",tour[i]+1);
printf("1\n");

getch();

int tspdp(int c[][max],int tour[],int start,int n)


{
int i,j,temp[max],mintour[max],k;/* Loop counters. */
int temp[max];/* Temporary during calculations. */
int mintour[max];/* Minimal tour array. */
int mincost;/* Minimal cost. */
int cost;/* Current cost. */

/* End of recursion condition. */


if(start==n-2)
return c[tour[n-2]][tour[n-1]]+c[tour[n-1]][0];

/* Compute the tour starting from the current city. */


mincost=infinity;
for(i=start+1;i<n;i++)
{
for(j=0;j<n;j++)
temp[j]=tour[j];

/* Adjust positions. */
temp[start+1]=tour[i];
temp[i]=tour[start+1];

/* Found a better cycle? (Recurrence derivable.) */


if(c[tour[start]][tour[i]]+(cost=tspdp(c,temp,start+1,n))<mincost)
{
mincost=c[tour[start]][tour[i]]+cost;
for(k=0;k<n;k++)
mintour[k]=temp[k];
}
}

/* Set the minimum-tour array. */


for(i=0;i<n;i++)
tour[i]=mintour[i];
return mincost;
}
/*------------------OUTPUT:Sample-----------------
Travelling Salesman Problem Using Dynamic Programming

Enter number of cities to traverse:4


Enter cost matrix:
0 20 10 15
20 0 25 30
10 25 0 40
15 30 40 0
Minimum Cost:80
Tour:
1-3-2-4-1
*/

Potrebbero piacerti anche