0% found this document useful (0 votes)
42 views19 pages

Wa0005

The document contains code implementations of various sorting and graph algorithms: 1) The N-Queens problem to print all possible configurations of N queens on an N×N chessboard using backtracking. 2) Kruskal's algorithm and Prim's algorithm to find the minimum spanning tree (MST) of a connected, undirected graph. 3) The 0/1 Knapsack problem solved using memory-based and dynamic programming approaches. 4) Heapsort and mergesort algorithms to sort an array of numbers. 5) Quicksort algorithm using Lomuto's partitioning scheme.

Uploaded by

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

Wa0005

The document contains code implementations of various sorting and graph algorithms: 1) The N-Queens problem to print all possible configurations of N queens on an N×N chessboard using backtracking. 2) Kruskal's algorithm and Prim's algorithm to find the minimum spanning tree (MST) of a connected, undirected graph. 3) The 0/1 Knapsack problem solved using memory-based and dynamic programming approaches. 4) Heapsort and mergesort algorithms to sort an array of numbers. 5) Quicksort algorithm using Lomuto's partitioning scheme.

Uploaded by

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

Part - B

1.N-Queen’s Problem
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int place(int k, int a[])
{
int i;
for(i=1;i<k;i++)
{
if( (a[k]==a[i]) || ( abs(i-k) == abs(a[i]-a[k]) ) )
return 0;
}
return 1;
}
void nqueen(int n)
{
int k=1,i,count=0,a[10];
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while( (a[k]<=n) && (place(k,a)==0) )
{
a[k]=a[k]+1;
}
if(a[k]<=n)
{
if(k==n)
{
count++;
printf("Solution No %d:\n", count);
for(i=1;i<=n;i++)
{
printf("Queen No %d is placed in \nRow No: %d and Column No: %d\n\n",i,i,a[i]);
}
}
else
{
k++;
a[k]=0;
}
}
else
{
k--;
}
}
}
int main()
{
int n;
printf("Enter the number of queens : ");
scanf("%d",&n);
if(n<=3 && n>1)
printf("No solutions\n");
else
nqueen(n);
return 0;
}
2.Kruskal’s Algorithm
#include<stdio.h>
#include<stdlib.h>
int A[10][10],B[10][10],n,e,root[10];
void update(int v1, int v2)
{
int i, t;
t=root[v2];
for(i=1;i<=n;i++)
{
if(root[i]==t)
root[i]=root[v1];
}
}
void FindMin(int *v1, int *v2)
{
int edge=0, i,j;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if( (edge>A[i][j] || edge==0) && A[i][j]>0 )
{
edge=A[i][j];
*v1=i;
*v2=j;
}
}
}
int Kruskals()
{
int i,j,ecounter=0,v1,v2,cost,total_cost=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
B[i][j]=0;
else
B[i][j]=9999;
}
while(ecounter<(n-1))
{
FindMin(&v1, &v2);
cost=A[v1][v2];
A[v1][v2]=A[v2][v1]=0;
if(root[v1]!=root[v2])
{
B[v1][v2]=B[v2][v1]=cost;
update(v1, v2);
total_cost=total_cost+cost;
ecounter++;
}
}
return total_cost;
}
int main()
{
int v1,v2,i,j,cost;
printf("Enter no of vertices : ");
scanf("%d",&n);
printf("Enter no of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
A[i][j]=0;
else
A[i][j]=9999;
}
printf("Enter the Edges one by one\n");
for(i=1;i<=e;i++)
{
printf("Edge %d: ",i);
scanf("%d%d",&v1,&v2);
printf("Weight: ");
scanf("%d",&cost);
A[v1][v2]=A[v2][v1]=cost;
}
for(i=1;i<=n;i++)
{
root[i]=i;
}
printf("The given graph is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
cost=Kruskals();
printf("The minimum spanning tree for the given graph is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nThe total cost is: %d",cost);
return 0;
}
3.Prim’s Algorithm
#include<stdio.h>
#include<stdlib.h>
int A[10][10],B[10][10],n,e,visited[10];
void FindMin(int *v1,int *v2)
{
int edge=0,i,j;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if( (visited[i]==1 && visited[j]!=1) || (visited[i]!=1 && visited[j]==1) )
{
if( (edge>A[i][j] || edge==0) && A[i][j]>0 )
{
edge=A[i][j];
*v1=i;
*v2=j;
}
}
}
}
int Prims()
{
int i,j,v1,v2,cost,total_cost=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
B[i][j]=0;
else
B[i][j]=9999;
}
for(i=1;i<n;i++)
{
FindMin(&v1, &v2);
cost=A[v1][v2];
B[v1][v2]=B[v2][v1]=cost;
visited[v1]=visited[v2]=1;
total_cost=total_cost+cost;
}
return total_cost;
}
int main()
{
int v1,v2,i,j,cost;
printf("Enter no of vertices : ");
scanf("%d",&n);
printf("Enter no of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
A[i][j]=0;
else
A[i][j]=9999;
}
printf("Enter the Edges one by one\n");
for(i=1;i<=e;i++)
{
printf("Edge %d: ",i);
scanf("%d%d",&v1,&v2);
printf("Weight: ");
scanf("%d",&cost);
A[v1][v2]=A[v2][v1]=cost;
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
visited[1]=1;
cost=Prims();
printf("The given graph is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
printf("The The minimum spanning tree for the given graph is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nThe total cost is: %d",cost);
return 0;
}
4.Knapsack Problem – Memory functions
#include<stdio.h>
#include<stdlib.h>
int Values[15],Weights[15],W,n,V[15][15];
int max(int i,int k)
{
if (i>k )
return i;
else
return k ;
}
int MFKnapsack(int i,int j)
{
int value;
if(V[i][j]<0)
{
if(j<Weights[i])
value=MFKnapsack(i-1,j);
else
value=max(MFKnapsack(i-1,j), Values[i]+MFKnapsack(i-1,j-Weights[i]));
V[i][j]=value;
}
return V[i][j];
}
int main()
{
int i,j,val;
printf("Enter no of items : ");
scanf("%d",&n);
printf("Enter the capacity of knapsack\n");
scanf("%d",&W);
printf("Enter the weight and value of each item\n");
for(i=1;i<=n;i++)
{
printf("\nItem No: %d ->",i);
printf("Weight: ");
scanf("%d",&Weights[i]);
printf("Value: ");
scanf("%d",&Values[i]);
}
for(i=0;i<=n;i++)
for(j=0;j<=W;j++)
{
if(i==0 || j==0)
{
V[i][j]=0;
}
else
{
V[i][j]=-1;
}
}
val=MFKnapsack(n,W);
printf("The optimal value is %d\n",val);
printf("The items added are:\n");
for(i=n,j=W;i>0;i--)
{
if(V[i][j]!=V[i-1][j])
{
printf("%d\t",i);
j=j-Weights[i];
}
}
return 0;
}
5.Knapsack Problem – Dynamic Programming
#include<stdio.h>
#include<stdlib.h>
int Values[15],Weights[15],W,n,V[15][15];
int max(int i,int k)
{
if (i>k)
return i;
else
return k ;
}
int Knapsack()
{
int i,j;
for(i=1;i<=n;i++)
{
for (j=1;j<=W;j++)
{
if(j-Weights[i]<0)
{
V[i][j]=V[i-1][j];
}
else
{
V[i][j]=max(V[i-1][j],Values[i]+V[i-1][j-Weights[i]]);
}
}
}
return V[n][W];
}
int main()
{
int i,j,val;
printf("Enter no. of items : ");
scanf("%d",&n);
printf("Enter the capacity of knapsack : ");
scanf("%d",&W);
printf("Enter the weight and value of each item\n");
for(i=1;i<=n;i++)
{
printf("\nItem No: %d ->\n",i);
printf("Weight : ");
scanf("%d",&Weights[i]);
printf("Value : $");
scanf("%d",&Values[i]);
}
for(i=0;i<=n;i++)
V[i][0]=0;
for(j=0;j<=W;j++)
V[0][j]=0;
val=Knapsack();
printf("The Knapsack table :\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=W;j++)
{
printf("%d\t",V[i][j]);
}
printf("\n");
}
printf("The optimal value is $ %d\n",val);
printf("The items added are:\n");
for(i=n,j=W;i>0;i--)
{
if(V[i][j]!=V[i-1][j])
{
printf("%d\t",i);
j=j-Weights[i];
}
}
return 0;
}
6.Heapsort
#include<stdio.h>
#include<stdlib.h>
void heapifybottomup(int h[],int n)
{
int i,k,v,heapify,j;
for(i=(n/2);i>=1;i--)
{
k=i;
v=h[k];
heapify=0;
while(heapify==0 && 2*k<=n)
{
j=2*k;
if(j<n) //if there are 2 children
{
if(h[j]<h[j+1]) //checking both children
j=j+1;
}
if(v>=h[j])
{
heapify=1;
}
else
{
h[k]=h[j];
k=j;
}
}
h[k]=v;
}
}
void heapsort(int h[],int n)
{
int i,temp;
for(i=n;i>1;i--)
{
heapifybottomup(h,i);
temp=h[1];
h[1]=h[i];
h[i]=temp;
}
}
int main()
{
int i,n,h[20];
printf("Enter size : ");
scanf("%d",&n);
printf("Enter %d elements :\n",n);
for(i=1;i<=n;i++)
scanf("%d",&h[i]);
heapsort(h,n);
printf("The sorted array :\n");
for(i=1;i<=n;i++)
{
printf("%d\n",h[i]);
}
return 0;
}
7.Mergesort
#include<stdio.h>
#include<conio.h>
void MergeSort(int a[], int n);
void Merge(int b[], int p, int c[], int q, int a[])
{
int i=0,j=0,k=0;
while(i<p && j<q)
{
if(b[i]<=c[j])
{
a[k]=b[i];
i++;
}
else
{
a[k]=c[j];
j++;
}
k++;
}
while(i<p)
{
a[k]=b[i];
i++;
k++;
}
while(j<q)
{
a[k]=c[j];
j++;
k++;
}
}
void MergeSort(int a[],int n)
{
int i,j,p,q,b[10],c[10];
if(n>1)
{
for(i=0;i<n/2;i++)
{
b[i]=a[i];
}
p=i;
for(i=n/2,j=0;i<n;i++,j++)
{
c[j]=a[i];
}
q=j;
MergeSort(b,p);
MergeSort(c,q);
Merge(b,p,c,q,a);
}
}
int main()
{
int i,a[20],n;
printf("Enter size : ");
scanf("%d",&n);
printf("Enter %d elements : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
MergeSort(a,n);
printf("Sorted array :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
8.Quick Sort
#include<stdio.h>
#include<stdlib.h>
int a[20];
void QuickSort(int, int);
int partition(int l,int r)
{
int i,j,p,temp;
p=a[l];
i=l;
j=r+1;
do
{
do
{
i++;
}
while(a[i]<=p);
do
{
j--;
}
while(a[j]>p);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
while(i<=j);
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=a[l];
a[l]=a[j];
a[j]=temp;
return j;
}
void QuickSort(int l,int r)
{
int s;
if(l<r)
{
s=partition(l,r);
QuickSort(l,s-1);
QuickSort(s+1,r);
}
}
int main()
{
int i,n;
printf("Enter size : ");
scanf("%d",&n);
printf("Enter %d elements :\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
QuickSort(0,n-1);
printf("Sorted array :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
9.Topological sort – Source removal method
#include<stdio.h>
#include<stdlib.h>
int order[10],A[10][10],n,in[10],visited[10],e,k=0;
int is_all_nodes_visited()
{
int i;
for(i=1;i<=n;i++)
{
if(visited[i]==0)
return 0;
}
return 1;
}
int main()
{
int i,j,v1,v2,w;;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
A[i][j]=0;
}
}
for(i=1;i<=n;i++)
{
in[i]=0;
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
printf("Enter the edges of the graph one by one:\n");
for(i=1;i<=e;i++)
{
scanf("%d %d",&v1,&v2);
A[v1][v2]=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(A[i][j]==1)
in[j]=in[j]+1;
}
}
while(1)
{
if(is_all_nodes_visited())
break;
else
for(i=1;i<=n;i++)
{
if(in[i]==0 && visited[i]==0)
{
order[k]=i;
k++;
visited[i]=1;
for(w=1;w<=n;w++)
{
if(A[i][w]==1 && visited[w]==0)
{
in[w]--;
}
}
}
}
}
printf("The Topological ordering is:\n");
for(i=0;i<n;i++)
{
printf("%d\t",order[i]);
}
return 0;
}
10.Topological sort – DFS method
#include<stdio.h>
#include<conio.h>
int A[20][20],visited[20],count=0,n,order[10],x=0;
void dfs(int v)
{
int w;
count=count+1;
visited[v]=count;
for(w=1; w<=n; w++)
{
if(A[v][w] && visited[w]==0)
{
dfs(w);
}
}
order[x]=v;
x++;
}
int main()
{
int i,j,k,v1,v2,e;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
A[i][j]=0;
}
}
printf("Enter the edges of the graph one by one:\n");
for(i=1;i<=e;i++)
{
scanf("%d %d",&v1,&v2);
A[v1][v2]=1;
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
for(k=1;k<=n;k++)
{
if(visited[k]==0)
dfs(k);
}
printf("The topological ordering is:\n");
for(i=n-1;i>=0;i--)
{
printf("%d\t",order[i]);
}
return 0;
}

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