0% found this document useful (0 votes)
29 views22 pages

Manual

Uploaded by

suhaspl1817
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views22 pages

Manual

Uploaded by

suhaspl1817
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm

#include<stdio.h>
#include<conio.h>
int parent[20];
int find(int m)
{
int p = m;
while(parent[p]!=0)
p = parent[p];
return(p);
}
void union_ij(int i,int j)
{
if(i<j)
parent[i] = j;
else
parent[j]=i;
}
void kruskal(int a[10][10], int n)
{
int u,v,min,k=0,i,j,sum=0;
while(k<n-1)
{
min =999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]<min && i!=j)
{
min = a[i][j];
u=i;
v=j;
}
i = find(u);
j=find(v);
if(i!=j)
{
union_ij(i,j);
printf("(%d,%d)=%d",u,v,a[u][v]);
sum = sum + a[u][v];
k++;
}
a[u][v] = a[v][u]=999;
}
printf("\n The minimum cost spanning tree is = %d",sum);
}
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("=======================================\n");
printf(" Find minimum cost spanning tree using Kruskal Algorithm \n");
printf("========================================\n");
I:printf("\n Enter the number of vertices of the graph:\n");
scanf("%d",&n);
if(n>0)
{
printf("\n Enter the cost adjacency matrix\n");
printf(" (0 for loops and 999 if there is no direct edge):\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
kruskal(a,n);
getch();
}
else
{
printf("Enter valid number of vertices");
goto I;
}
}
Output:
==============================================
Find minimum cost spanning tree using Kruskal Algorithm
==============================================
Enter the number of vertices of the graph
4

Enter the cost adjacency matrix


(0 for loops and 999 if there is no direct edge)
0 20 2 99
20 0 15 5
2 15 0 25
99 5 25 99

(1,3)=2
(2,4)=5
(2,3)=15
The minimum cost spanning tree is =22
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.

#include<stdio.h>
#include<conio.h>
void prims(int n,int a[10][10],int source)
{
int s[10],i,j,min=999,sum=0,u,v,k,t=0;
for(i=0;i<n;i++)
s[i]=0;
s[source]=1;
k=1;
while(k<=n-1)
{
min=999;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(s[i]==1 && s[j]==0)
if(i!=j && min>a[i][j])
{
min=a[i][j];
u=i;
v=j;
}
printf("\n(%d,%d)=%d",u,v,min);
sum=sum+min;
s[u]=1;
s[v]=1;
k++;
}
for(i=0;i<n;i++)
{
if(s[i]!=1)
t=1;
}
if(t==0)
printf("\ncost of min spanning tree is %d",sum);
else
printf("\nminimum spanning tree not possible");
}
void main()
{
int a[10][10],n,i,j,source;
clrscr();

printf("==========================================\n");
printf(" Find minimum cost spanning tree using Prim's Algorithm \n");
printf("==========================================\n");
printf("\n Enter the number of vertices");
scanf("%d",&n);
printf(“Enter the cost matrix 0 for self loop and 99 for no edge \n”);
printf(“Enter the n*n matrx\n”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the source node\n");
scanf("%d",&source);
prims(n,a,source);
getch();
}
output:
=========================================
Find minimum cost spanning tree using Prim's Algorithm
=========================================
Enter the number of nodes
6
Enter the cost matrix 0 for self loop and 99 for no edge

Enter the n*n matrx


0 3 99 99 6 5
3 0 1 99 99 4
99 1 0 6 99 4
99 99 6 0 8 5
6 99 99 8 0 2
544520

Enter the source node


1

1->2 =3

2->3 =4

2->6 =8

6->5 =10

6->4 =15
cost of min spanning tree is=15
a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd's
algorithm.

#include<stdio.h>
#include<omp.h>
int min(int a,int b)
{
return a<b?a:b;
}
void floyd(int w[10][10],int n)
{
int i,j,k;
#pragma omp parallel for private(i, j, k) shared(w)
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
}
void main()
{
int a[10][10],n,i,j;
double startTime,endTime;
printf("\n Enter the no. of vertices:");
scanf("%d",&n);
printf("\n Enter the cost matrix, 0-self loop and 999-no edge\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
startTime=omp_get_wtime();
floyd(a,n);
endTime = omp_get_wtime();
printf("\n Shortest path matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Time taken is %10.9f\n",(double)(endTime-startTime));
}
Output:
Enter the no. of vertices:
4
Enter the cost matrix, 0-self loop and 999-no edge
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
Shortest path matrix:
0 10 3 4
2056
7701
6 16 9 0
Time taken is 0.001107683

3. b. Design and implement C/C++ Program to find the transitive closure using Warshal's

Warshall’s Algorithm

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void warshalls(int a[20][20], int n)
{
int i,j,k;
for (k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j] = a[i][j] || a[i][k] && a[k][j];
printf("\n The transitive closure is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
void main()
{
int a[20][20],n,i,j;
clrscr();
printf("=======================================\n");
printf(" Compute transitive closure using Warshalls Algorithm \n");
printf("=======================================\n");
I:printf("\n Enter the number of vertices of the graph:\n");
scanf("%d",&n);
if(n>0 && n <90)
{
printf("\n Enter the adjacency matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
warshalls(a,n);
}
else
{
printf("Enter the valid number of vertices\n");
goto I;
}
getch();
}
Output:
==============================================
Compute transitive closure using Warshalls Algorithm
==============================================
Enter the number of vertices of the graph
4
Enter the adjacency matrix
0100
0001
0000
1010
The transitive closure is
1111
1111
0000
1111

3. Design and implement C/C++ Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra's algorithm.
#include<stdio.h>
#include<conio.h>
int d[10],p[10],visited[10];
void dijktras(int a[10][10],int s,int n)
{
int u,v,i,j,min;
for(v=0;v<n;v++)
{
d[v]=99;
p[v]=-1;
}
d[s]=0;
for(i=0;i<n;i++)
{
//select u which is the minimum among the remaining
min=99;
for(j=0;j<n;j++)
{
if(d[j]<min && visited[j]==0)
{
min=d[j];
u=j;
}
}
visited[u]=1; // Vt=Vt+u
//select v in the remaining vertices(V-Vt) that is adj to u
for(v=0;v<n;v++)
{
if((d[u]+a[u][v]<d[v]) && (u!=v) && visited[v]==0)
{
d[v]=d[u]+a[u][v];
p[v]=u;
}
}
}
}
void path(int v,int s)
{
if(p[v]!=-1)
path(p[v],s);
if(v!=s)
printf("->%d",v);
}
void display(int s,int n)
{
int i;
for(i=0;i<n;i++)
{
if(i!=s)
{
printf("%d",s);
path(i,s);
}
if(i!=s)
printf("=%d\n",d[i]);
}
}
void main()
{
//declaration of variables
int a[10][10],i,j,n,s;
clrscr();
printf("Program for Single Source Shortest Path using Dijktras
Algorithm\n\n");
// Read the weighted graph as cost matrix
printf("Enter the number of vertices (size of the graph)\n");
scanf("%d",&n);
printf("Enter the %d * %d cost matrix\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the source vertex\n");
scanf("%d",&s);
//Calling Dijktras function by passing a graph and source
dijktras(a,s,n);
//Display the result
printf("\t\tOUTPUT\t\t\n");
printf("The Shortest path between %d to remaining vertices are:\n\n",s);
display(s,n);
printf("\t\tThank You\t\t\n");
getch();
}
OUTPUT:
Program for Single Source Shortest Path using Dijktras Algorithm
Enter the number of vertices of the graph:
5

Enter the weights of the edges of the graph:


0 3 99 7 99
3 0 4 2 99
99 4 0 5 6
72504
99 99 6 4 0

Enter the source vertex to find the shortest path:


1
The shortest path from vertex 1 to all other vertices is:
1->2=3
1->2->3=7
1->2->4=5
1->2->4->5=9

5.Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.
#include<stdio.h>
int a[10][10],indegree[10],v[10],stack[10],n,k,top=-1;
void main()
{
int i;
clrscr();
printf("======================================\n");
printf(" To find the topological ordering of the vertices\n");
printf("======================================\n");
readadj();
topological();
if(k==n)
{
printf("\n topological sequence is");
for(i=0;i<n;i++)
printf("\t%d",v[i]);
}
else
printf("\n topological sequence doesnot exist");
getch();
}
readadj()
{
int i,j;
I:printf("\n Enter the number of vertices of a graph\n");
scanf("%d",&n);
if(n>0 && n<50)
{
printf("\nEnter the adjacency matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
return;
}
else
{
printf("Enter valid number of vertices\n");
goto I;
}
}
topological()
{
int i,j,R;
//To find the indegree
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]==1)
indegree[j]++;
}
// Push all the nodes with indegree 0
for(i=0;i<n;i++)
{
if(indegree[i]==0)
stack[++top]=i;
}
while(top!=-1)
{
R = stack[top--];
v[k++] = R;
for(i=0;i<n;i++)
{
if(a[R][i]==1)
{
indegree[i]--;
if(indegree[i]==0)
stack[++top] = i;
}
}
}
}
Output:

==============================================
To find the topological ordering of the vertices
==============================================
Enter the number of vertices of a graph
5
Enter the adjacency matrix
00100
00100
00011
00001
00000
The topological sequence is 2 1 3 4 5

6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
int w[10],v[10],a[10][10],t[10];
int c,i,n,j;
void knapsack(int c,int n)
{
int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=c;j++)
{
if(i==0 || j==0)
a[i][j]=0;
else if(w[i]>j)
a[i][j]=a[i-1][j];
else
a[i][j]=max(a[i-1][j],(a[i-1][j-w[i]]+v[i]));
}
i=n;j=c;
while(i>0 && j>0)
{
if(a[i][j] != a[i-1][j])
{
t[i]=1;
j=j-w[i];
}
i--;
}
}
void read_capacity()
{
printf("\n\n Enter the capacity of the bag:");
scanf("%d",&c);
if(c>0)
read_numofitems();
else
{
printf("\n\n Invalid capacity,please try again...");
read_capacity();
}
}
int read_numofitems()
{
I:printf("\n\n Enter the number of items:\n");
scanf("%d",&n);
if(n>0)
read_weights();
else
{
printf("\n\n Invalid items,please try agian...");
goto I;
}
return 1;
}
int read_weights()
{
J: printf("\n\n Enter the corresponding weights:\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
{
if(w[i]>0 && w[i]<=999)
{
read_values();
}
else
{
printf("\n\n Invalid weights,please try again..");
goto J;
}
}
return 0;
}
int read_values()
{
K: printf("\n\n Enter the corresponding values:\n");
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
for(i=1;i<=n;i++)
if(v[i]>0 && v[i]<=999)
{
knapsack(c,n);
}
else
{
printf("\n\n Invalid values,please try again...");
goto K;
}
return 0;
}

void main()
{
clrscr();
printf("\t\t ***THIS PROGRAM DEMONSTRATES KNAPSACK
PROBLEM***");
read_capacity();
printf("\n\n Resultant matrix is:\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=c;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
printf("\n\n The optimal solution is:%d",a[n][c]);
printf("\n\n Items selected are:\n");
for(i=1;i<=n;i++)
if(t[i]==1)
printf("\t%d",i);
printf("\n\n Thankyou for using program,have a nice day...");
getch();
}
Output:
*** THIS PROGRAM DEMONSTRATES KNAPSACK PROBLEM***
enter the capacity of the knapsack
5
enter no. of items
4
enter the wieght of each item
2
1
3
2
enter the corresponding value of each item
12
10
20
15
Resultant matrix is
000000
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37
The optimal solution is:37
Items selected are:
124
Thank you

7. Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method.

8. Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn} of n
positive integers whose sum is equal to a given positive integer d.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void subset(int num, int n, int x[])
{
int i;
for(i = 1;i<=n;i++)
x[i]=0;
for(i=n;num!=0;i--)
{
x[i] = num%2;
num = num/2;
}
}
void main()
{
int a[50],x[50],n,j,i,d,sum,present = 0;
clrscr();
printf("========================================\n");
printf("Find subset of the given set whose sum is equal to a given integer \n");
printf("=========================================\n");
I:printf("\n Enter the number of elements of the set\n");
scanf("%d",&n);
if(n>0 && n<90)
{
printf("\n Enter the elements of the set \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
}
else
{
printf("Enter the valid number of elements\n");
goto I;
}
J:printf("\n Enter the positive integer sum\n");
scanf("%d",&d);
if(d>0)
{
for(i=1;i<=pow(2,n)-1; i++)
{
subset(i,n,x);
sum=0;
for(j=1;j<=n;j++)
if(x[j]==1)
sum = sum +a[j];
if(d==sum)
{
printf("\n subset={");
present = 1;
for(j=1;j<=n;j++)
if(x[j]==1)
printf("%d ",a[j]);
printf("}=%d",d);
}
}
}
else
{
printf("Enter the valid positive integer\n");
goto J;
}
if(present==0)
printf("solution does not exist");
getch();
}
output:
==========================================
Find the subset of the given set whose sum is equal to a given integer
=========================================
enter number of elements of the set
5
enter the elements of the set
1
2
5
6
8

Enter the positive integer sum


9

subset={1 8 }=9
subset={1 2 6 }=9
9. Design and implement C/C++ Program to sort a given set of n integer elements using Selection
Sort method and compute its time complexity. Run the program for varied values of n> 5000 and
record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read
from a file or can be generated using the random number generator.

#include <stdio.h>

// function to swap the the position of two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void selectionSort(int array[], int size) {


for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {

// To sort in descending order, change > to < in this line.


// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}

// put min at the correct position


swap(&array[min_idx], &array[step]);
}
}

// function to print an array


void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}

// driver code
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
printf("Sorted array in Acsending Order:\n");
printArray(data, size);
}
10. Design and implement C/C++ Program to sort a given set of n integer elements using Quick
Sort method and compute its time complexity. Run the program for varied values of n> 5000
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can be
read from a file or can be generated using the random number generator.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int ct,sw;
void quicksort(int a[],int low,int high)
{
int s;
//To check for boundary condition
if(low<high)
{
s=partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}
int partition(int a[],int low,int high)
{
int p,i,j,temp;
p=a[low];
i=low+1;
j=high;
while(1)
{
while(a[i]<=p && i<high)
i++,ct++; // count for the comparision
while(a[j]>p)
j--,ct++; // count for the comparision
if(i<j)
{
sw++ // count for the swap
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
sw++ // count for the swap
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void main()
{
int a[50],i,n;
clrscr();
I:printf("=====================================\n\n");
printf(" To sort the Integer elements using Quick Sort Method\n\n");
printf("=====================================\n\n");
printf("\nEnter the Number of elements to be sorted which is less than the
array size 50\n");
scanf("%d",&n);
if(n>0 && n<=50) // array bound check
{
randomize();
for(i=0;i<n;i++)
{
a[i] = random(1000);
}
}
else
{
printf("Invalid input.. please try again...\n");
clrscr();
goto I;
}
printf("\n~~~~~~~~~~ OUTPUT ~~~~~~~~~~~~~~\n");
printf("The Inputs generated by Random Number Generated are \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
quicksort(a,0,n-1);
printf("\n Sorted Integer Array \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\n******* Analysis of Quick Sort Algorithm *******\n\n");
printf("The Basic Operation of Quick Sort algorithm are Comparision and
Swap ");
printf(" \nThe number of Comparisions are : %d units",ct);
printf(" \nThe number of Swaps are : %d units",sw);
printf("\n\n ***** Thank You *****\n");
getch();
}
OUTPUT
================================================
To sort the Integer elements using Quick Sort Method
================================================
Enter the Number of elements to be sorted which is less than the array size 50
5
~~~~~~~~~~ OUTPUT ~~~~~~~~~~~~~~
The Inputs generated by Random Number Generated are
463 257 12 940 624
Sorted Integer array
12 257 463 624 940
*********** Analysis of Quick Sort Algorithm ****************
The Basic Operation of Quick Sort algorithm are Comparision and Swap
The number of Comparisions are: 5 units
The number of Swaps are: 3 units
******************* Thank You ************************

11. Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort
method and compute its time complexity. Run the program for varied values of n> 5000, and
record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read
from a file or can be generated using the random number generator.

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>
void simple_merge(int a[],int low,int mid,int high);
void merge_sort(int a[],int low,int high);
void main()
{
int a[200],i,n;
double startTime,endTime;
printf("=========== Parallelized Merge Sort ==========\n");
printf("Enter the number of elements\n");
scanf("%d",&n);
randomize();
for(i=0;i<n;i++)
{
a[i]=random(1000);
}
printf("\n The random numbers generated are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
startTime=omp_get_wtime();
merge_sort(a,0,n-1);
endTime = omp_get_wtime();
printf("Time taken is %10.9f\n",(double)(endTime-startTime));
printf("The sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void simple_merge(int a[],int low,int mid,int high)
{
int i=low,j=mid+1,k=low,c[200];
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
c[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=c[i];
}
void merge_sort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
#pragma omp parallel sections
{
#pragma omp section
merge_sort(a,low,mid);
#pragma omp section
merge_sort(a,mid+1,high);
}
simple_merge(a,low,mid,high);
}
}
Output:
=========== Parallelized Merge Sort ==========
Enter the number of elements
5
The random numbers generated are:
463 257 12 940 624
Time taken is 0.000194626
The sorted elements are
12 257 463 624 940

12. Design and implement C/C++ Program for N Queen's problem using Backtracking.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int b[20],count;
int place(int row,int col)
{
int i;
for(i=1;i<row;i++)
if(b[i]==col || abs(b[i]-col)==abs(row-i))
return 0;
return 1;
}
void dis(int n)
{
int i,j;
printf("\n Solution number:%d\n",++count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(b[i]==j)
printf("\tq");
else
printf("\t*");
}
printf("\n");
}
printf("\n\n");
}
void queen(int row, int n)
{
int col;
for(col=1;col<=n;col++)
{
if(place(row,col))
{
b[row]=col;
if(row==n)
dis(n);
else
queen(row+1,n);
}
}
}
void main()
{
int n;
clrscr();
printf("========================================\n");
printf("\t\t\t n - queens problem\n");
printf("========================================\n");
I:printf("\n Enter the number of queens to be placed on a n*n
chessboard\n”);
scanf("%d",&n);
if(n>0 && n<90)
{
queen(1,n);
printf("\n Total no of solution:%d", count);
}
else
{
printf("Enter the valid number of queens\n");
goto I;
}
getch();
}
Output:
===========================================
N Queens
===========================================
Enter the number of queens to be placed on a n*n chessboard
4

solution:1

*q**

***q
q***

**q*

solution:2

**q*
q***

***q

*q**

Total number of solutions are 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy