Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

/*BINARY SEARCH*/

#include<iostream.h>
#include<conio.h>
class binary
{
int a[20],n,low,high,x,i,mid;
public:
void get();
void search();
};
void binary::get()
{
cout<<"Enter the
limit"; cin>>n;
cout<<"Enter the
numbers"; for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the number to be
found"; cin>>x;
}
void binary::search()
{
low=0;
high=n-1;
mid=(low+high)/2;
for(i=0;i<n;i++)
{
if(x<a[mid])
{
high=mid-1;
mid=(low+high)/2;
}
else if(x>a[mid])
{
low=mid+1;
mid=(low+high)/2;
}
else if(x==a[mid])
{
cout<<"The number is found";
break;
}
else
{
cout<<"The number is not found";
}
}
}
void main()
{
binary b;
clrscr();
b.get();
b.search();
getch();
}
OUTPUT
/*MERGE SORT*/

#include<iostream.h>
#include<conio.h>
int n,a[20],b[20];
class merge
{

public:
void getdata();
void merge1(int,int,int);
void mergesort(int,int);
};
void merge::mergesort(int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge1(low,mid,high);
}
}
void merge::merge1(int l,int m,int h)
{
int lo=l;
int k;
int i=l;
int j=m+1;
while((lo<=m)&&(j<=h))
{
if(a[lo]<=a[j])
{
b[i]=a[lo];
lo++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(lo>m)
{
for(k=j;k<=h;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=lo;k<=m;k++)
{
b[i]=a[k];
i++;
}
}
for(k=0;k<=h;k++)
a[k]=b[k];
}
void merge::getdata()
{
cout<<"Enter the
limit:"; cin>>n;
cout<<"\nEnter the elements:";
for(int y=0;y<n;y++)
cin>>a[y];
}
void main()
{
merge m;
clrscr();
m.getdata();
m.mergesort(0,n-1);
cout<<"\nThe sorted list
is:"; for(int z=0;z<n;z++)
cout<<a[z]<<"\t";
getch();
}
OUTPUT
/*QUCIK SORT*/

#include<iostream.h>
#include<conio.h>
int n,a[20],lb,ub,i;
class qsort
{
public:
void sort(int lb,int ub)
{
int j;
if(lb>=ub)
return;
j=partition(lb,ub);
sort(lb,j);
sort(j+1,ub);
}
int partition(int lb,int ub)
{
int p,low,high,t;
p=a[lb];
high=ub;
low=lb;
while(low<high)
{
while(a[low]<=p&&low<=ub)
low++;
while(a[high]>p)
high--;
if(low<high)
{
t=a[low];
a[low]=a[high];
a[high]=t;
}
a[lb]=a[high];
a[high]=p;
return high;
}
}
void print()
{
cout<<"The sorted elements are";
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
}
};
void main()
{
qsort q;
clrscr();
textattr(45);
gotoxy(25,3);
gotoxy(25,5); cout<<"\
nQUICK SORT";
cout<<"\n\nEnter the no ofelements";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
lb=0;
ub=n-1;
q.sort(lb,ub);
q.partition(lb,ub);
q.print();
getch();
}
OUTPUT
/*KNAPSACK PROBLEM USING GREEDY METHOD*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
float w[100],v[100],x[100],wt,wgt[100];
class greed
{
private:
int i,j,n;
float r[100],f,total,t,rat,pro,wei,op,opti;
public:
void getdata();
void ratio();
void profit();
void weight();
void optimal();
};
void greedy(int n)
{
int wgt,i;
for(i=0;i<n;i++)
x[i]=0;
wgt=i=0;
while(wgt<wt)
{
if(wgt+w[i]<=wt)
{
x[i]=1;
wgt+=w[i];
}
else
{
x[i]=(wt-wgt)/w[i];
wgt+=(x[i]*w[i]);
}
i++;
}
}
void greed:: getdata()
{
cout<<"enter no of objects\n";
cin>>n;
cout<<"enter the capacity of the bag\n";
cin>>wt;
for(i=0;i<n;i++)
{
cout<<"weight=";
cin>>w[i];
cout<<"profit=";
cin>>v[i];
r[i]=v[i]/w[i];
}
}
void greed:: ratio()
{
cout<<"GREEDY BY RATIO\n";
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(r[i]<r[j])
{
t=w[i];w[i]=w[j];w[j]=t;
t=v[i];v[i]=v[j];v[j]=t;
t=r[i];r[i]=r[j];r[j]=t;
}
greedy(n);
for(i=0;i<n;i++)
{
cout<<"\nweight"<<w[i];
cout<<"\tprofit"<<v[i];
cout<<"\tratio"<<r[i];
cout<<"\tfraction"<<x[i];
total+=(x[i]*v[i]);
}
cout<<"\nfeasible profit="<<total;
rat=total;
getch();
total=0;
}
void greed:: profit()
{
cout<<"GREEDY BY PROFIT\n";
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(v[i]<v[j])
{
t=w[i];w[i]=w[j];w[j]=t;
t=v[i];v[i]=v[j];v[j]=t;
t=r[i];r[i]=r[j];r[j]=t;
}
greedy(n);
for(i=0;i<n;i++)
{
cout<<"\nweight"<<w[i];
cout<<"\tprofit"<<v[i];
cout<<"\tratio"<<r[i];
cout<<"\tfraction"<<x[i];
total+=(x[i]*v[i]);
}
cout<<"\nfeasible profit="<<total;
pro=total;
getch();
total=0;
}
void greed:: weight()
{
cout<<"GREEDY BY WEIGHT\n";
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(w[i]<w[j])
{
t=w[i];w[i]=w[j];w[j]=t;
t=v[i];v[i]=v[j];v[j]=t;
t=r[i];r[i]=r[j];r[j]=t;
}
greedy(n);
for(i=0;i<n;i++)
{
cout<<"\nweight"<<w[i];
cout<<"\tprofit"<<v[i];
cout<<"\tratio"<<r[i];
cout<<"\tfraction"<<x[i];
total+=(x[i]*v[i]);
}
cout<<"\nfeasible profit="<<total;
wei=total;
getch();
}
void greed:: optimal()
{
if(rat>pro)
op=rat;
else
op=pro;
if(op>wei)
opti=op;
else
opti=wei;
cout<<"\nOPTIMAL SOLUTION IS"<<opti;
getch();
}

void main()
{
clrscr();
greed s;
cout<<"KNAPSACK PROBLEM\n";
s.getdata();
s.ratio();
s.profit();
s.weight();
s.optimal();
getch();
}
OUTPUT
/*DIJIKSTRA ALGORITHM USING GREEDY METHOD*/

#include<iostream.h>
#include<conio.h>
class path
{
private:
int i,j,m,n,b[50],cost[20][20],k,d[50];
int u,v,w,num,s[20];
public:
void getdata();
void sp();
void display();
};
void path::getdata()
{
cout<<"\n****SINGLE SOURCE SHORTEST PATH****\n";
cout<<"\nEnter the number of nodes:";
cin>>n;
cout<<"\nEnter the cost\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
cost[i][j]=0;
}
else
{
cout<<"\ncost["<<i<<","<<j<<"]:";
cin>>cost[i][j];
}
}
cout<<"\n";
}
}
void path::sp()
{
k=1;
clrscr();
cout<<"\nThe cost of vertices are:\n";
cout<<"\t";
for(i=1;i<=n;i++)
cout<<i<<"\t";
cout<<"\n\t";
for(i=1;i<=n;i++)
cout<<"---------";
cout<<"\n";
for(i=1;i<=n;i++)
{
cout<<i<<"\t";
for(j=1;j<=n;j++)
{
cout<<cost[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\nEnter the source vertex:";
cin>>v;
for(i=1;i<=n;i++)
{
s[i]=0;
d[i]=cost[v][i];
}
s[v]=1;
b[k++]=v;
d[v]=0;
for(num=2;num<=n;num++)
{
m=9999;
for(j=1;j<=n;j++)
if((m>d[j])&&(s[j]==0))
{
m=d[j];
u=j;
}
s[u]=1;
b[k++]=u;
for(w=1;w<=n;w++)
{
if(d[w]>(d[u]+cost[u][w])&&(s[w]==0))
{
d[w]=(d[u]+cost[u][w]);
}
}
cout<<"\n";
}
}
void path::display()
{
cout<<"\n";
cout<<"\nThe shortest path with source vertex"<<v<<"is\
n"; cout<<"\n\t\t"<<b[1];
for(i=2;i<k;i++)
cout<<"----->"<<b[i];
}

void main()
{
clrscr();
path p;
p.getdata();
p.sp();
p.display();
getch();
}
OUTPUT
/*MULTISTAGE GRAPH USING DYNAMIC PROGRAMMING*/

#include<iostream.h>
#include<conio.h>
struct fwd
{
int l;
int a[20];
};
struct fwd s1[10];
void main()
{
int k,i,j,n,p[10],m,z,cost[50],v,c[20][20];
clrscr();
cout<<"Enter the no. of stages
:"; cin>>k;
n=0;
//Get the input for vertices
for(i=1;i<=k;i++)
{
cout<<"Enter no. of vertices in stage "<<i<<"
:"; cin>>s1[i].l;
n+=s1[i].l;
for(j=1;j<=s1[i].l;j++)
{
cout<<"Enter the value of vertex "<<j<<"
:"; cin>>s1[i].a[j];
}
}
//Get the input for cost matrix
for(i=1;i<k;i++)
{
for(j=s1[i].a[1];j<=s1[i].a[s1[i].l];j++)
{
for(z=s1[i+1].a[1];z<=s1[i+1].a[s1[i+1].l];z++)
{
cout<<"Enter the cost of c["<<j<<"]
["<<z<<"] :"; cin>>c[j][z];
}
}
}
//fwd approach
cost[n]=0;
int min,d[50],t;
for(i=k-1;i>=1;i--)
{
for(j=s1[i].a[1];j<=s1[i].a[s1[i].l];j++)
{
min=999;
for(z=s1[i+1].a[1];z<=s1[i+1].a[s1[i].l];z++)
{
if(cost[z]+c[j][z]<min)
min=cost[z]+c[j][z];
t=z;
}
cost[j]=min;
d[j]=t;
}
}
//To display the path
p[1]=1;
p[k]=n;
for(i=2;i<k;i++)
{
p[i]=d[p[i-1]];
}
for(i=1;i<k;i++)
{
cout<<p[i]<<"-->";
}
cout<<p[k];
getch();
}
OUTPUT
/ *0/1 KANPSACK USING DYNAMIC PROGRAMMING*/

#include<iostream.h>
#include<conio.h>
int c,z[20],t[50][50],s[20],i,j,pt[20],wt[20],x[20],m,n;
class knp
{
public:
void get();
void table();
void select();
void display();
}k;
void knp:: get()
{
cout<<"\n\t0/1KNAPSACK PROBLEM"; cout<<"\
nENTER THE CAPACITY OF THE BAG";
cin>>m;
cout<<"\nENTER THE NO OF OBJECTS";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\nENTER THE WEIGHT OF OBJECT"<<i;
cin>>wt[i];
cout<<"\nENTER THE PROFIT OF OBJECT"<<i;
cin>>pt[i];
}
}
void knp:: table()
{
for(i=1;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if((i==1)&&(j<wt[i])) t[i]
[j]=0;
if((i==1)&&(j>=wt[i]))
t[i][j]=pt[i];
if((i>1)&&(j<wt[i]))
t[i][j]=t[i-1][j];
if((i>1)&&(j>=wt[i])) t[i]
[j]=pt[i]+t[i-1][j-wt[i]];
}
}
cout<<"\n\t\tTABLE\n";
for(i=1;i<=n;i++)
{
for(j=0;j<=m;j++)
cout<<t[i][j]<<"\t";
cout<<"\n";
}
}
void knp:: select()
{
int q=0;
c=0;
int a,b,d;
i=n;
for(j=m;j>=0;j--)
{
if(t[i][j]==t[i-1][j])
{
a=i-1;
if(t[a][j]==t[a-1][j-wt[a]]+pt[a])
{
}
else
{
z[c]=t[a][j];
c++;
s[q]=a;
q++;
}
b=a-1;
d=j-wt[a];
if(t[a-1][d]==t[b-1][d-wt[b]])
{
}
else
{
z[c]=t[a-1][j-wt[a]];
c++;
s[q]=a-1;
q++;
}
}
else
{
a=i-1;
if(t[a][j]==t[a-1][j-wt[a]]+pt[a])
{
}
else
{
z[c]=t[a][j];
s[q]=a; q+
+;
}
b=a-1;
d=j-wt[a];
if(t[a-1][d]==t[b-1][d-wt[b]])
{
}
else
{
z[c]=t[a-1][j-wt[a]];
c++;
s[q]=a-1;
q++;
}
}
j=d+1;
}
}
void knp:: display()
{
cout<<"\nTHE OBJECTS SELECTED ARE";
for(int x=0;x<c;x++)
{
cout<<"\t"<<s[x];
}
}
void main()
{
char ch='y';
while(ch=='y')
{
clrscr();
k.get();
k.table();
k.select();
k.display();
cout<<"\ndo u want to
continue(y/n)"; cin>>ch;
}
getch();
}
OUTPUT
0/1 KNAPSACK PROBLEM
/*ALL PAIR SHORTEST PATH USING DYNAMMIC PROGRAMMING */

#include<iostream.h>
#include<conio.h>
int a[20][20],cost[20][20],i,j,n,k;
class path
{
public:
void get();
void allpair(int a[20][20],int n);
void put();
};
void path:: get()
{
cout<<"\n\t\tALL PAIR SHORTEST PATH\
n"; cout<<"\nenter the no of vertices";
cin>>n;
cout<<"\nenter the cost";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
cost[i][j]=0; a[i]
[j]=cost[i][j];
}
else
{
cout<<"\ncost["<<i<<"]["<<j<<"]=";
cin>>cost[i][j];
a[i][j]=cost[i][j];
}
}
cout<<"\n";
}
/* cout<<"enter the cost matrix\n";
for(i=1;i<=n;i++)
{
cout<<i;
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
a[i][j]=cost[i][j];
}
}*/
}
void path:: allpair(int a[20][20],int n)
{
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((a[i][k]+a[k][j])<a[i][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
}
void path:: put()
{
cout<<"\nOUTPUT\n";
cout<<"\nALL PAIR SHORTEST PATH\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
void main()
{
path p;
clrscr();
p.get();
p.allpair(a,n);
p.put();
getch();
}
OUTPUT
/*N QUEEN PROBLEM USING BACKTRACKING METHOD*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void addqueen();
int col[8],upfree[15],dnfree[15],coln[15];
void main(void)
{
int i;
for(i=0;i<=7;i++)
col[i]=1;
for(i=0;i<=14;i++)
upfree[i]=dnfree[i]=1;
clrscr();
addqueen();
getch();
}
void addqueen()
{
int i,c,r;
static int comb,row=-1;
row++; for(i=0;i<=7;i+
+)
{
if(col[i]&&upfree[i+row]&&dnfree[row-i+7])
{
coln[row]=i;
col[i]=0;
upfree[i+row]=0;
dnfree[row-i+7]=0;
if(row>=7)
{
comb++;
cout<<"\n\n\t8QUEENS PROBLEM";
cout<<"\nCombimation no:"<<comb;
for(r=0;r<=7;r++)
{
cout<<endl;
for(c=0;c<=7;c++)
{
if(c==coln[r])
cout<<"\tx";
else
cout<<"\t-";
}
}
getch();
}
else
addqueen();
col[coln[row]]=1;
upfree[row+coln[row]]=1;
dnfree[row-coln[row]+7]=1;
}
}
row--;
}
OUTPUT
/*SUM OF SUBSET PROBLEM USING BACKTRACKING METHOD*/

#include<iostream.h>
#include<conio.h>
int m,n,w[10],x[10]; //Global declaration of variables
void sum_subset(int s,int k,int r)
{
int i;
x[k]=1;
if(s+w[k]==m)
{
for(i=1;i<=n;i++)
{
cout<<"x["<<i<<"]:"<<x[i]<<"\t";
}
cout<<"\n";
}
els
e
{ if((s+w[k]+w[k+1])<=m)
{
sum_subset(s+w[k],k+1,r-w[k]); //Recursive call
}
}
if(((s+r-w[k])>=m)&&((s+w[k+1])<=m))
{
x[k]=0;
sum_subset(s,k+1,r-w[k]);
}
}
void main()
{
clrscr();
//User input
cout<<"Enter the no. of values
:"; cin>>n;
cout<<"Enter the sum :
"; cin>>m;
cout<<"Enter the values
:"; int i;
for(i=1;i<=n;i++)
{
cin>>w[i];
}
int r;
r=0;
for(i=1;i<=n;i++)
{
r+=w[i];
}
sum_subset(0,1,r); //call the function to solve the sum of subsets problem
getch();
}
OUTPUT
/*TRAVELLING SALESMAN PROBLEM USING BRANCH AND BOUND
TECHNIQUE*/

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void astar();
struct tst
{
struct tst*parent;
int city,level,g,h,f;
};
typedef struct tst NODE;
NODE *OPEN[20],*CLOSED[200];
int openc=0,closedc=0;
NODE *start;
int n,scity;
float dist[15][15];
class travebb
{
private:
int i,j;
public:
void get();
void astar();
void addopen(NODE *temp);
int checkopen(NODE *temp);
void removeopen(NODE *temp);
void addclosed(NODE *temp);
int checkclosed(NODE *temp);
void removeclosed(NODE *temp);
void compute_huer(NODE *parent, NODE *succ);
int checkgoal(NODE *temp);
NODE *getmin();
NODE *createnode(int city, int level);
void print(NODE *temp);
};
void travebb::get()
{
cout<<"\n Enter the no. of city:";
cin>>n;
cout<<"\nEnter the cost matrix: \n\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"\nEnter the cost of "<<i<<"-->"<<j<<": ";
cin>>dist[i][j];
dist[j][i]=dist[i][j];
if(i==j)
dist[i][j]=999;
}
cout<<"\nEnter the start city:";
cin>>scity;
cout<<"\nThe shortest path is\n";
astar();
}
void travebb::astar()
{
int
i,closedin,openin;
NODE *bestnode;
NODE *succ;
start=(NODE *)malloc(sizeof
(NODE)); start->city=scity;
start->level=0;
start->h=n;
start->g=0;
start->f=n+0;
start->parent=NULL;
addopen(start);
while(openc)
{
bestnode=getmin();
if(checkgoal(bestnode))
{
print(bestnode);
getch();
exit(0);
}
removeopen(bestnode);
addclosed(bestnode);
for(i=0;i<n;i++)
{
if(bestnode->level>=n)
goto y;
succ=createnode(i,bestnode->level+1);
compute_huer(bestnode,succ);
succ->parent=bestnode;
openin=checkopen(succ);
if(openin!=-1)
{
if(succ->f<OPEN[openin]->f)
{
removeopen(OPEN[openin]);
addopen(succ);
goto x;
}
}
closedin=checkclosed(succ);
if(closedin!=-1)
{
if(succ->f<CLOSED[closedin]->f)
{
removeclosed(CLOSED[closedin]);
addopen(succ);
}
goto x;
}
addopen(succ);
x:;
}
y:;
}
}
void travebb::addopen(NODE *temp)
{
int list[15],i=0,j,k,cnt;
NODE *temp1;
temp1=temp;
while(temp1!=NULL)
{
list[i]=temp1->city;
temp1=temp1->parent;
i++;
}
k=i;
for(j=0;j<n;j++)
{
cnt=0;
for(i=0;i<k;i++)
if(j==list[i])
cnt++;
if(cnt>1&&j!=scity)
return;
if(j==scity&&cnt>1&&temp->level!=n)
return;
if(j==scity&&cnt!=2&&temp->level==n)
return;
}
OPEN[openc]=temp;
openc++;
}
int travebb::checkopen(NODE *temp)
{
int i,j;
for(i=0;i<openc;i++)
if(OPEN[i]->city==temp->city&&OPEN[i]->level==temp->level)
return i;
return -1;
}
void travebb::removeopen(NODE *temp)
{
int i;
for(i=0;i<openc;i++)
if(OPEN[i]==temp)
break;
for(;i<openc;i++)
OPEN[i]=OPEN[i+1];
openc--;
}
void travebb::addclosed(NODE *temp)
{
CLOSED[closedc]=temp;
closedc++;
}
void travebb::removeclosed(NODE *temp)
{
int i;
for(i=0;i<closedc;i++)
if(CLOSED[i]==temp)
break; for(;i<closedc;i+
+)
CLOSED[i]=CLOSED[i+1];
closedc--;
}
int travebb::checkclosed(NODE *temp)
{
int i;
for(i=0;i<closedc;i++)
if(CLOSED[i]->city==temp->city&&CLOSED[i]->level==temp->level)
return i;
return -1;
}
NODE *travebb::createnode(int city, int level)
{
NODE *temp;
temp=(NODE *)malloc(sizeof (NODE));
temp->city=city;
temp->level=level;
temp->h=0;
temp->g=0;
temp->f=0;
temp->parent=NULL;
return temp;
}
void travebb::compute_huer(NODE *parent, NODE *succ)
{
succ->g=parent->g+dist[parent->city][succ->city];
succ->h=n-succ->level;
if(succ->h<0)
succ->h=0;
succ->f=succ->g+succ->h;
}
int travebb::checkgoal(NODE *temp)
{
int list[15],i=0,j,cnt;
if(temp->city==scity&&temp->level==n)
{
while(temp!=NULL)
{
list[i]=temp->city;
temp=temp->parent;
i++;
}
for(j=0;j<n;j++)
{
cnt=0;
for(i=0;i<n;i++)
if(j==list[i])
cnt++;
if(cnt==0)
return 0;
}
return 1;
}
return 0;
}
NODE *travebb::getmin()
{
int minual=9999,min,i;
for(i=0;i<openc;i++)
if(OPEN[i]->f<minual)
{
minual=OPEN[i]->f;
min=i;
}
return OPEN[min];
}
void travebb::print(NODE *temp)
{
if(temp==NULL)
return;
print(temp->parent); cout<<"\
t"<<temp->city<<"\t";
}
void main()
{
travebb t;
clrscr();
cout<<"\nTRAVELLING SALESMAN PROBLEM\n";
t.get();
getch();
}
OUTPUT
/*BREADTH FIRST SEARCH*/

#include<iostream.h>
#include<conio.h>
int i,j,n,a[50],mark[20],adj[50][50],top=0;
int del();
void add(int temp)
{
a[++top]=temp;
}
int del()
{
int temp;
temp=a[1];
for(i=1;i<top;i++)
a[i]=a[i+1];
top--;
return(temp);
}
void bfs(int v)
{
int w,u;
mark[v]=1;
add(v);
while(top!=0)
{
u=del();
cout<<"\n\n VISIT NODE="<<u;
for(w=1;w<n;w++)
if((mark[w]==0)&&(adj[v][w]==1))
{
mark[w]=1;
add(w);
}
}
}
void main()
{
clrscr();
cout<<"\n \n BREADTH FIRST SEARCH:";
cout<<"\n Enter the no of nodes:";
cin>>n;
cout<<"\n Enter 1 if there is an edge between nodes else type
0"; for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
cout<<"\n\n Node "<<i<<" to node "<<j<<"=";
cin>>adj[i][j];
}
}
for(i=1;i<=n;i++)
mark[i]=0;
for(i=1;i<=n;i++)
if(mark[i]==0)
bfs(i);
getch();
}
OUTPUT
/*DEPTH FIRST SEARCH*/

#include<iostream.h>
#include<conio.h>
int n,mark[20],adj[50][50],i,j;
class dfs
{
public:
void getdata(void);
void src(int);
};
void dfs::src(int v)
{
int w;
mark[v]=1;
cout<<"\n visited node:"<<v;
for(w=1;w<=n;w++)
if((mark[w]==0)&&(adj[v][w]==1))src(w);
}
void dfs::getdata(void)
{
cout<<"\n Enter the number of vertices:";
cin>>n;
cout<<"\n Enter 1 if there is direct path else enter 0:\
n"; for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
cout<<"\n vertex"<<i<<"to"<<j<<":";
cin>>adj[i][j];
}
}
}
int main()
{
clrscr();
dfs obj;
obj.getdata();
for(i=1;i<=n;i++)
mark[i]=0;
obj.src(1);
getch();
return 0;
}

OUTPUT
/*LINEAR SEARCH*/

#include <iostream>
using namespace std;
int linearSearch(int a[], int n, int val) {
// Going through array
linearly for (int i = 0; i < n; i+
+)
{
if (a[i] ==
val) return
i+1;
}
return -1;
}
int main() {
int a[] = {69, 39, 29, 10, 56, 40, 24, 13, 51}; // given
array int val = 56; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store
result cout<<"The elements of the array are -
";
for (int i = 0; i < n; i++)
cout<<a[i]<<" ";
cout<<"\nElement to be searched is -
"<<val; if (res == -1)
cout<<"\nElement is not present in the
array"; else
cout<<"\nElement is present at "<<res<<" position of array";
return 0;
}
OUTPUT
/* FIBONACCI SEARCH*/

#include <iostream>
using namespace std;

int fibonacciSearch(int arr[], int n, int x) {


if (n == 0) {
return -1;
}
int fib1 = 0, fib2 = 1, fib3 = fib1 + fib2;

while (fib3 < n) {


fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}

int offset = -1;


while (fib3 > 1) {
int i = min(offset + fib2, n-1);

if (arr[i] < x)
{ fib3 =
fib2; fib2 =
fib1;
fib1 = fib3 - fib2;
offset = i;
}

else if (arr[i] > x)


{ fib3 = fib1;
fib2 = fib2 - fib1;
fib1 = fib3 - fib2;
}

else {
return i;
}
}

if (fib2 == 1 && arr[offset+1] == x) {


return offset + 1;
} else {
return -1;
}
}
int main() {
int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100, 235};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 235;
int ind = fibonacciSearch(arr, n, x);
if (ind >= 0) {
cout << "Found at index: " << ind << endl;
} else {
cout << x << " isn't present in the array" << endl;
}
return 0;
}
OUTPUT

Found at index: 11

You might also like