You are on page 1of 29

(1)Recursive Binary

------------------------------------------------------------------------------#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int bin_search(int key,int a[10],int l,int h)
{
if(l>h)
return -1;
int mid=(l+h)/2;
if(key==a[mid])
return mid;
if(key<a[mid])
return bin_search(key,a,l,mid-1);
return bin_search(key,a,mid+1,h);
}
/*void main()
{
int a[10],n,i,key,pos;
clrscr();
cout<<"Enter the elements";
cin>>n;
cout<<"Enter the element in an ascending order";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements to be searched";
cin>>key;
pos=bin_search(key,a,0,n-1);
if(pos==-1)
cout<<"key not found";
else
cout<<"Key found at"<<pos+1<<"position";
getch();
}*/
/*clock cycle for Binary search*/
int main()
{
int a [1000],i,key=0,pos,step=10;
double clockspermillis=double(CLOCKS_PER_SEC)/1000;
cout<<"the worst_case time,in milliseconds are"<<endl;
cout<<"n\t Repetitions\t Total Ticks\t Time/sort"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepetitions=0;
clock_t startTime=clock();
do

{
numberofrepetitions++;
for(int i=0;i<n;i++)
a[i]=n-i;
pos=bin_search(key,a,0,n-1);
}
while(clock()-startTime <1000);
double elapsedmillis=(clock()-startTime)/clockspermillis;
cout<<n<<'\t'<<numberofrepetitions<<'\t'<<elapsedmillis<<'\t'<<elapsed
millis/numberofrepetitions<<endl;
if(n==100)step=100;
}
if(pos==-1)
cout<<"Key not found"<<endl;
else
cout<<"key found at"<<pos+1<<"position"<<endl;
return 0;
}
Linear Search.
------------------------------------------------------------------------------#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int lin_search(int a[],int key,int n)
{
if(n<0)
return n-1;
if(key==a[n])
return n;
return lin_search(a,key,n-1);
}
/*void main()
{
int a[10],n,key,pos;
clrscr();
cout<<"Enter the element";
cin>>n;
cout<<"Enter the no of elements";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements to be searched";
cin>>key;
pos=lin_search(a,key,n-1);

if(pos==-1)
cout<<"Key not found";
else
cout<<"key is found at"<<pos+1<<"position";
getch();
}*/
int main()
{
int a[1000],i,key=0,pos,step=10;
double clockspermillis=double (CLOCKS_PER_SEC)/1000;
cout<<"the worst_case time,in milliseconds are"<<endl;
cout<<"n\t Repetitions\t Total Ticks\t Time/sort"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepetitions=0;
clock_t startTime=clock();
do
{
numberofrepetitions++;
for(i=0;i<n;i++)
a[i]=n-i;
pos=lin_search(a,key,n-1);
}
while(clock()-startTime <1000);
double elapsedmillis=(clock()-startTime)/clockspermillis;
cout<<n<<'\t'<<numberofrepetitions<<'\t'<<elapsedmillis<<'\t'<<elapsed
millis/numberofrepetitions<<endl;
if(n==100)step=100;
}
if(pos==-1)
cout<<"Key not found"<<endl;
else
cout<<"key found at"<<pos+1<<"position"<<endl;
return 0;
}
(2)HEAPSORT
#include<iostream.h>
#include<conio.h>
#include<time.h>
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;

}
void heapify(int a[],int i,int n)
{
int lc=2*i,rc=lc+1,m;
if(lc>n) return;
m=(rc>n)?lc:(a[lc]>a[rc])?lc:rc;
if(a[i]>=a[m]) return;
swap(a[i],a[m]);
heapify(a,m,n);
}
void heapsort(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
heapify(a,i,n);

for(i=1;i<=n;i++)
{
swap(a[1],a[n-i+1]);
heapify(a,1,n-i);
}

/*void main()
{
int a[20],n,i;
clrscr();
cout<<"enter no.of items\n";
cin>>n;
cout<<"enter "<<n<<" items\n";
for(i=1;i<=n;i++)
cin>>a[i];
heapsort(a,n);
cout<<"\nSORTED LIST\n";
for(i=1;i<=n;i++)
cout<<a[i]<<"\t";
getch();
} */

int main()
{
int a [1000],i,pos,step=10;
double clockspermillis=double(CLOCKS_PER_SEC)/1000;
cout<<"the worst_case time,in milliseconds are"<<endl;
cout<<"n\t Repetitions\t Total Ticks\t Time/sort"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepetitions=0;
clock_t startTime=clock();
do
{
numberofrepetitions++;
for(int i=0;i<n;i++)
a[i]=n-i;
heapsort(a,n);
}
while(clock()-startTime <1000);
double elapsedmillis=(clock()-startTime)/clockspermillis;
cout<<n<<'\t'<<numberofrepetitions<<'\t'<<elapsedmillis<<'\t'<<elapsed
millis/numberofrepetitions<<endl;
if(n==100)step=100;
}
/*if(pos==-1)
cout<<"Key not found"<<endl;
else
cout<<"key found at"<<pos+1<<"position"<<endl;*/
return 0;
}

(3)Merge Sort.
------------------------------------------------------------------------------#include<iostream.h>
#include<time.h>
#include<conio.h>
void merge(int a[],int l,int m,int h)
{
int i=l;
int j=m+1;
int k=l;
int b[1000];
while(i<=m && j<=h)
{
if(a[i]<a[j])
{
b[k]=a[i];

i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
while(i<=m)
b[k++]=a[i++];
while(j<=h)
b[k++]=a[j++];
for(i=l;i<=h;i++)
a[i]=b[i];
}
void merge_sort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(a,low,mid);
merge_sort(a,mid+1,high);
merge(a,low,mid,high);
}
}
/*void main()
{
int a[1000];
clrscr();
int n,i;
cout<<"Enter size of array";
cin>>n;
cout<<"Enter the elements to be sorted";
for(i=0;i<n;i++)
cin>>a[i];
merge_sort(a,0,n-1);
cout<<"\n The sorted array is:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}*/
void main()
{

clrscr();
int pos;
int a[1000],step=10;
double clockpermillis=double(CLOCKS_PER_SEC)/1000;
cout<<"the worst case time in milisecs are"<<endl;
cout<<"n\trepitations \t total ticks \ttime per search"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepitations=0;
clock_t starttime=clock();
do
{
numberofrepitations++;
merge_sort(a,0,n-1);
}
while(clock()-starttime<1000);
double elapsedmillis=(clock()-starttime)/clockpermillis;
cout<<n<<"\t"<<numberofrepitations<<"\t"<<elapsedmillis;
cout<<"\t"<<elapsedmillis/numberofrepitations;
cout<<endl;
if(n==100)
step=100;
}
getch();
}
SELECTION SORT
------------------------------------------------------------------------------#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void sort(int a[],int n)
{
int i,j,pos;
for(i=0;i<n-1;i++)
{

pos=i;
for(j=i+1;j<n;j++)
{
if(a[pos]>a[j])
pos=j;
}
swap(a[pos],a[i]);
}
}
/*void main()
{
int a[10],n,i;
clrscr();
cout<<"enter no.of items\n";
cin>>n;
cout<<"enter "<<n<<" items\n";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
cout<<"\nSORTED LIST\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
} */
int main()
{
int a [1000],i,step=10;
double clockspermillis=double(CLOCKS_PER_SEC)/1000;
cout<<"the worst_case time,in milliseconds are"<<endl;
cout<<"n\t Repetitions\t Total Ticks\t Time/sort"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepetitions=0;
clock_t startTime=clock();
do
{
numberofrepetitions++;
for(int i=0;i<n;i++)
a[i]=n-i;
sort(a,n);
}
while(clock()-startTime <1000);
double elapsedmillis=(clock()-startTime)/clockspermillis;
cout<<n<<'\t'<<numberofrepetitions<<'\t'<<elapsedmillis<<'\t'<<elapsed
millis/numberofrepetitions<<endl;
if(n==100)step=100;

}
/*if(pos==-1)
cout<<"Key not found"<<endl;
else
cout<<"key found at"<<pos+1<<"position"<<endl;*/
return 0;
}
(7)QUICK SORT
------------------------------------------------------------------------------#include<iostream.h>
#include<time.h>
#include<dos.h>
#include<conio.h>
void quick_sort(int a[],int low,int high)
{
int key,t;
int left,right,flag;
if(low<high)
{
key=a[low];
left=low+1;
right=high;
flag=1;
while(flag)
{
while(key>=a[left] && left<high)
left=left+1;
while(key<a[right])
right=right-1;
if(left<right)
{
t=a[left];
a[left]=a[right];
a[right]=t;
}
else
flag=0;
}
t=a[low];
a[low]=a[right];
a[right]=t;
quick_sort(a,low,right-1);
quick_sort(a,right+1,high);
}
return;
}

void main()
{
int i,n,a[20];
//int a[1000],step=10;
clrscr();
cout<<"\n Enter the array size:";
cin>>n;
cout<<"\n enter the array elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
quick_sort(a,0,n-1);
cout<<"\n The sorted array is:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
/*int main()
{
int a[1000],step=10;
clrscr();
double clockpermillis=double(CLOCKS_PER_SEC)/1000;
cout<<"the worst case time in milisecs are"<<endl;
cout<<"n\trepitations \t total ticks \ttime per search"<<endl;
for(int n=0;n<=1000;n+=step)
{
long numberofrepitations=0;
clock_t starttime=clock();
do
{
numberofrepitations++;
for(int i=0;i<n;i++)
a[i]=n-i;
quick_sort(a,0,n-1);
}
while(clock()-starttime<1000);
double elapsedmillis=(clock()-starttime)/clockpermillis;
cout<<n<<"\t"<<numberofrepitations<<"\t"<<elapsedmillis;
cout<<"\t"<<elapsedmillis/numberofrepitations;
cout<<endl;
if(n==100)
step=100;
}
getch();
}

(5)KNAPSACK
------------------------------------------------------------------------------#include<iostream.h>
#include<conio.h>
class knapsack
{
int n,m,w[10],p[10];
int v[10][10];
public:
void read_data()
{
cout<<"Enter the no. items\n";
cin>>n;
cout<<"Enter the capacity\n";
cin>>m;
cout<<"\nEnter the weights of the items\n";
for(int i=1;i<=n;i++)
cin>>w[i];
cout<<"\nEnter the profits of the items\n";
for(i=1;i<=n;i++)
cin>>p[i];
}
int max(int a,int b)
{
return(a>b?a:b);
}
void compute()
{
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
{
if(i==0 || j==0)
v[i][j]=0;
else if(w[i]>j)
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]
+p[i]);
}
}
void solution()
{
int x[10];
cout<<"The output is \n";
for(int i=0;i<=n;i++)
{

for(int j=0;j<=m;j++)
cout<<v[i][j]<<"\t";
cout<<endl;

}
cout<<"The optimal solution is : "<<v[n][m];
for(i=1;i<=n;i++)
x[i]=0;
i=n;
int j=m;
while(i!=0 && j!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
}
i--;
}
cout<<"\n\nX[ ";
for(i=1;i<=n;i++)
cout<<i<<",";
cout<<" ] = ";
for(i=1;i<=n;i++)
cout<<x[i]<<" ";
}
};
void main()
{
knapsack k;
clrscr();
k.read_data();
k.compute();
k.solution();
getch();
}
/**********************OUTPUT*********************
Enter the no. items
4
Enter the capacity
5
Enter the weights of the items
1
2
3
1

Enter the profits of the items


12
14
32
20
The output is
0
0
0
0
0
0
0
12
12
12
12
12
0
12
14
26
26
26
0
12
14
32
44
46
0
20
32
34
52
64
The optimal solution is : 64
67
X[ 1,2,3,4, ] = 1 0 1 1
****************************************************/
(13) a. FLOYD ALGORITHM
#include<iostream.h>
#include<conio.h>
int a[10][10],f[10][10],n;
int min(int a,int b)
{
return(a<b?a:b);
}
void floyd()
{
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
f[i][j]=a[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
f[i][j]=min(f[i][j],(f[i][k]+f[k][j]));
}
void main()
{
int i,j;
clrscr();
cout<<"enter no.of nodes\n";

cin>>n;
cout<<"enter cost matrix\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
floyd();
cout<<"\ndistance matrix is\n\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<f[i][j]<<"\t";
cout<<"\n";
}
getch();
}
/********************OUTPUT****************
enter no.of nodes
4
enter cost matrix
0
99
3
99
2
0
99
99
99
7
0
1
6
99
99
0
distance matrix is
0
10
3
4
2
0
5
6
7
7
0
1
6
16
9
0
******************************************/

(13) b. WARSHALL ALGORTHM


------------------------------------------------------------------------------#include<iostream.h>
#include<conio.h>
int a[10][10],w[10][10],n;
void warshall()
{
int i,j,k;
for(i=1;i<=n;i++)

for(j=1;j<=n;j++)
w[i][j]=a[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
w[i][j]=w[i][j] || w[i][k] && w[k][j];
}
void main()
{
int i,j;
clrscr();
cout<<"enter no.of nodes\n";
cin>>n;
cout<<"enter adjacency matrix\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
warshall();
cout<<"\ntransitive closure matrix is\n\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<w[i][j]<<"\t";
cout<<"\n";
}
getch();
}

/**********************OUTPUT***************
enter no.of nodes
4
enter adjacency matrix
0
1
0
0
0
0
0
1
0
0
0
0
1
0
1
0
transitive closure matrix is
1
1
0

1
1
0

1
1
0

1
1
0

*****************************************/
DIJKSTRAS ALGORITHM
#include<iostream.h>
#include<conio.h>
int c[10][10],n,d[10],vis[10];
void dijkstra(int s)
{
int i,u,min,ne=0;
for(i=1;i<=n;i++)
{
vis[i]=0;
d[i]=c[s][i];
}
vis[s]=1;
d[s]=0;
while(ne<n-1)
{
min=99;
for(i=1;i<=n;i++)
if(d[i]<min && vis[i]==0)
{
min=d[i];
u=i;
}
vis[u]=1;
for(i=1;i<=n;i++)
if(d[u]+c[u][i]<d[i] && vis[i]==0)
d[i]=d[u]+c[u][i];
ne++;
}
}
void main()
{
int i,j,src;
clrscr();
cout<<"enter no.of nodes\n";
cin>>n;
cout<<"enter cost matrix\n";

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>c[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(c[i][j]==0)
c[i][j]=99;
cout<<"\n enter source\n";
cin>>src;
dijkstra(src);
cout<<"\n shortest path from the source are\n\n";
for(i=1;i<=n;i++)
if(i!=src)
cout<<src<<"-->"<<i<<"="<<d[i]<<"\n";
getch();
}
/**********************OUTPUT********************
enter no.of nodes
5
enter cost matrix
0
3
99
7
99
3
0
4
5
6
99
4
0
5
6
7
2
5
0
4
99
99
6
4
0
enter source
1
shortest path from the source are
1-->2=3
1-->3=7
1-->4=7
1-->5=9
*******************************************************
KRUSKALS ALGORITHM
#include<iostream.h>
#include<conio.h>
int p[10];
void kruskal(int w[10][10],int n)
{
int min,sum=0,ne=0,i,j,u,v,a,b;

for(i=1;i<=n;i++)
p[i]=0;
while(ne<n-1)
{
min=99;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(w[i][j]<min)
{
min=w[i][j];
u=a=i;
v=b=j;
}
}
while(p[u])
u=p[u];
while(p[v])
v=p[v];
if(u!=v)
{
ne++;
sum+=min;
cout<<"\nedge "<<a<<"-->"<<b<<" is "<<min;
p[v]=u;
}
w[a][b]=w[b][a]=999;
}
cout<<"\nmin cost spanning tree= "<<sum;
}
void main()
{
int w[10][10],n,i,j;
clrscr();
cout<<"enter no.of vertices\n";
cin>>n;
cout<<"enter weight matrix\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>w[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)

if(w[i][j]==0)
w[i][j]=99;
kruskal(w,n);
getch();
}
/***********************OUTPUT****************
enter no.of vertices
6
enter weight matrix
99
3
99
99
6
5
3
99
1
99
99
4
99
1
99
6
99
4
99
99
6
99
8
5
6
99
99
8
99
2
5
4
4
5
2
99
edge 2-->3 is 1
edge 5-->6 is 2
edge 1-->2 is 3
edge 2-->6 is 4
edge 4-->6 is 5
min cost spanning tree= 15
***************************************************/
BFS ALGORITHM
#include<iostream.h>
#include<conio.h>
int a[10][10],visited[10],n;
void bfs(int v)
{
int b[10],i,j;
visited[v]=1;
j=0;
for(i=0;i<n;i++)
{
if(a[v][i]==1 && visited[i]!=1)
{
cout<<v<<"-->"<<i<<"\n";
visited[i]=1;
b[j++]=i;
}
}
if(j!=0)
{
for(i=0;i<j;i++)

bfs(b[i]);

}
void main()
{
int i,j,start;
clrscr();
cout<<"enter no.of vertices\n";
cin>>n;
cout<<"enter adjacency matrix\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"enter starting vertex\n";
cin>>start;
for(i=0;i<n;i++)
visited[i]=0;
bfs(start);
getch();
}
/**********************OUTPUT******************
enter no.of vertices
4
enter adjacency matrix
0
1
1
0
1
0
0
1
1
0
0
1
0
1
1
0
enter starting vertex
0
0-->1
0-->2
1-->3
******************************************/
DFS ALGORITHM
#include<iostream.h>
#include<conio.h>
int a[10][10],visited[10],n,count=0;
void dfs(int v)
{
int i;
visited[v]=1;
count++;
for(i=0;i<n;i++)
{

if(a[v][i]==1 && visited[i]!=1)


{
cout<<v<<"-->"<<i<<"\n";
dfs(i);
}
}
}
void main()
{
int i,j,start;
clrscr();
cout<<"enter no.of vertices\n";
cin>>n;
cout<<"enter adjacency matrix\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"enter starting vertex\n";
cin>>start;
for(i=0;i<n;i++)
visited[i]=0;
dfs(start);
if(count==n)
cout<<"\ngraph is connected";
else
cout<<"\ngraph is not connected";
getch();
}
/**********************OUTPUT********************
enter no.of vertices
4
enter adjacency matrix
0
1
1
0
0
0
1
0
0
0
0
0
0
0
0
0
enter starting vertex
0
0-->1
1-->2
graph is not connected
******************************************/
SUBSET ALGORITHM
#include<iostream.h>
#include<conio.h>
int n,d,w[10],x[10],count=0;

void subset(int cs,int k,int r)


{
int i;
x[k]=1;
if(cs+w[k]==d)
{
cout<<"\n\nSolution "<<++count<<" is:";
for(i=0;i<=k;i++)
if(x[i]==1)
cout<<w[i]<<" ";
}
else if(cs+w[k]+w[k+1]<=d)
subset(cs+w[k],k+1,r-w[k]);
if(cs+w[k+1]<=d && cs+r-w[k]>=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
void main()
{
int sum=0,i;
clrscr();
cout<<"enter n\n";
cin>>n;
cout<<"enter "<<n<<" elements\n";
for(i=0;i<n;i++)
cin>>w[i];
for(i=0;i<n;i++)
sum+=w[i];
cout<<"enter value of sum\n";
cin>>d;
if(sum<d)
cout<<"INVALID SOLUTION\n";
else
subset(0,0,sum);
getch();
}
/************************OUTPUT******************
enter n
5
enter 5 elements

1
2
3
4
5
enter value of sum
5
Solution 1 is:1 4
Solution 2 is:2 3
Solution 3 is:5
********************************************/
HORSPOOL ALGORITHM
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
int s[128],n,m;
char t[100],p[20];
void shift_table()
{
int i,j;
for(i=0;i<128;i++)
s[i]=m;
for(j=0;j<m-1;j++)
s[p[j]]=m-1-j;
}
int horspool()
{
int i,k;
shift_table();
i=m-1;
while(i<n)
{
k=0;
while(t[i-k]==p[m-1-k] && k<m)
k++;
if(k==m) return i-(m-1);
else
i+=s[t[i]];
}
return -1;
}
void main()
{
int i;

clrscr();
cout<<"enter the text\n";
gets(t);
cout<<"enter pattern to be searched\n";
gets(p);
n=strlen(t);
m=strlen(p);
i=horspool();
if(i==-1)
cout<<"not found\n";
else
cout<<"found at pos "<<i+1;
getch();
}
/*********************OUTPUT*********************
enter the text
Jim_saw_me_in_a_barbershop
enter pattern to be searched
barber
found at pos 17
*************************************************/
BINOMIAL Co-efficient Using Dynamic Prgramming
#include<iostream.h>
#include<conio.h>
int c[100][100];
int min(int a,int b)
{
if(a<b) return a;
return b;
}
int binomial_coeff(int n,int r)
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=min(i,r);j++)
{
if(j==0 || j==i) c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
return c[n][r];
}

void main()
{
int n,r,nCr;
clrscr();
cout<<"enter value for n & r\n";
cin>>n>>r;
nCr=binomial_coeff(n,r);
cout<<"\nnCr="<<nCr;
getch();
}
/******************OUTPUT***********************
enter value for n & r
6
3
nCr=20
********************************************/
PRIMS ALGORITHM
#include<iostream.h>
#include<conio.h>
int vis[10];
void prims(int w[10][10],int n)
{
int min,sum=0,ne=0,i,j,u,v;
for(i=1;i<=n;i++)
vis[i]=0;
vis[1]=1;
while(ne<n-1)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(vis[i]==1 && w[i][j]<min)
{
min=w[i][j];
u=i;
v=j;
}
}
if(vis[v]==0)
{
ne++;

sum+=min;
cout<<"\nedge weight "<<u<<"-->"<<v<<" is "<<min;
vis[v]=1;
}
w[u][v]=w[v][u]=999;
}
cout<<"\n\nmin cost spanning tree= "<<sum;

}
void main()
{
int w[10][10],n,i,j;
clrscr();
cout<<"enter no.of vertices\n";
cin>>n;
cout<<"enter weight matrix\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>w[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(w[i][j]==0)
w[i][j]=99;
prims(w,n);
getch();
}
/********************OUTPUT*******************
enter no.of vertices
3
enter weight matrix
0
10
1
10
0
6
1
6
0
edge weight 1-->3 is 1
edge weight 3-->2 is 6
min cost spanning tree= 7
*******************************************/
N QUEENS PROLEM using Back tracking
#include<iostream.h>
#include<conio.h>+
int place(int x[],int k)
{
int i;
for(i=1;i<k;i++)
if(x[i]==x[k] || i-x[i]==k-x[k] || i+x[i]==k+x[k])

return 0;
return 1;
}
void print(int n,int x[])
{
char c[10][10];
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
c[i][j]='X';
for(i=1;i<=n;i++)
c[i][x[i]]='Q';
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
}
}

void nqueens(int n)
{
int x[10],k=1,count=0;
x[k]=0;
while(k!=0)
{
x[k]++;
while(x[k]<=n && !place(x,k))
x[k]++;
if(x[k]<=n)
{
if(k==n)
{
cout<<"\nsolution "<<++count<<" is\n";
print(n,x);
}
else
{
k++;
x[k]=0;
}
}
else
k--;
}
}

void main()
{
int n;
clrscr();
cout<<"enter no.of queens\n";
cin>>n;
if(n<4)
cout<<"\nNO SOLUTION";
else
nqueens(n);
getch();
}
/************************OUTPUT*******************
enter no.of queens
4
solution 1 is
X
Q
X
X
X
X
X
Q
Q
X
X
X
X
X
Q
X
solution 2 is
X
X
Q
X
Q
X
X
X
X
X
X
Q
X
Q
X
X
*******************************************************/

You might also like