Algorithm Design and Analysis: Algorithm Design Is A Specific Method To Create A Mathematical
Algorithm Design and Analysis: Algorithm Design Is A Specific Method To Create A Mathematical
Page | 2
DAV COLLEGE JALANDHAR
PROGRAMS:
/* PROGRAM TO PERFORM QUICK SORT USING DIVIDE &
CONQUER TECHNIQUE*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
#include<string.h>
#define NULL 0
char lower[10];
char upper[10];
int top=-1;
void display(int s[],int n);
int main()
{
void quicksort(int s[],int n);
int n=10;
clrscr();
int s[]={41,19,65,35,21,73,28,87,52,59};
cout<<"Before Sorting\n\n";
display(s,n);
quicksort(s,n);
cout<<"Afetr Sorting\n\n";
display(s,n);
Page | 3
DAV COLLEGE JALANDHAR
top=top+1;
lower[top]=beg; upper[top]=loc-1;
}
if(loc+2<=end)
{
top=top+1;
lower[top]=loc+1;upper[top]=end;
}
}
}
int partition(int s[],int n,int beg,int end)
{
int left,right,loc,temp;
left=beg; right=end; loc=beg;
while(n>=1)
{
while((s[loc]<s[right]) && (loc!=right))
right=right-1;
if(loc==right)
return(loc);
if(s[loc]>s[right])
{
temp=s[loc];
s[loc]=s[right];
Page | 5
DAV COLLEGE JALANDHAR
s[right]=temp;
loc=right;
}
while(s[loc]>=s[left] && loc!=left)
left=left+1;
if(loc==left)
return(loc);
if(s[loc]<s[left])
{
temp=s[loc];
s[loc]=s[left];
s[left]=temp;
loc=left;
}
n=n-1;
}
return 0;
}
Page | 6
DAV COLLEGE JALANDHAR
Page | 7
DAV COLLEGE JALANDHAR
min=a[k];
loc=k;
}
}
temp=a[p];
a[p]=a[loc];
a[loc]=temp;
}
cout<<"\nAfter Sorting : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
Page | 9
DAV COLLEGE JALANDHAR
Page | 10
DAV COLLEGE JALANDHAR
high=n-1;
flag=binser(a,item,low,high);
cout<<"elements is at["<<flag<<"]location";
getch();
}
int binser(int a[],int item,int low,int high)
{
int m;
m=(low+high)/2;
if(item==a[m])
{
return m;
}
else if(item<a[m])
{
binser(a,item,low,m-1);
}
else
{
binser(a,item,m+1,high);
}
}
Page | 12
DAV COLLEGE JALANDHAR
#include<iostream.h>
#include<conio.h>
rec_fibo(int);
void main()
{
clrscr();
int n,i;
cout<<"Enter the total elements in the series : ";
cin>>n;
cout<<"\nThe Fibonacci series is:\n";
for(i=0;i<n;i++)
{
cout<<rec_fibo(i)<<" ";
}
getch();
}
rec_fibo(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return rec_fibo(n-1)+rec_fibo(n-2);
Page | 14
DAV COLLEGE JALANDHAR
Page | 15
DAV COLLEGE JALANDHAR
Page | 16
DAV COLLEGE JALANDHAR
for(i=0;i<=n;i++)
w[i][i]=q[i];
c[i][i]=r[i][i]=0;
w[i][i+1]=q[i]+q[i+1]+p[i+1];
r[i][i+1]=i+1;
c[i][i+1]=q[i]+q[i+1]+p[i+1];
}
w[n][n]=q[n];
r[n][n]=c[n][n]=0;
for(m=2;m<=n;m++)
for(i=0;i<=n-m;i++)
{
j=i+m;
w[i][j]=w[i][j-1]+p[j]+q[j];
k=find(i,j);
r[i][j]=k;
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
}
cout <<"\n";
print(0,n);
getch();
}
int find(int i,int j)
int min=2000,m,l;
Page | 18
DAV COLLEGE JALANDHAR
for(m=i+1;m<=j;m++)
if(c[i][m-1]+c[m][j]<min)
{
min=c[i][m-1]+c[m][j];
l=m;
}
return l;
}
void print(int i,int j)
if(i<j)
puts(idnt[r[i][j]]);
else
return;
print(i,r[i][j]-1);
print(r[i][j],j);
}
Page | 19
DAV COLLEGE JALANDHAR
Page | 20
DAV COLLEGE JALANDHAR
cout<<"\n";
getch();
}
int tsp_dp(int c[][max],int tour[],int start,int n)
{
int i,j,k,temp[max],min_tour[max],min_cost,cost;
if(start==n-2)
return c[tour[n-1]][tour[n-1]]+c[tour[n-1]][0];
min_cost=infinity;
for(i=start+1;i<n;i++)
{
for(j=0;j<n;j++)
temp[j]=tour[j];
temp[start+1]=tour[i];
temp[i]=tour[start+1];
if(c[tour[start]][tour[i]]+(cost=tsp_dp(c,temp,start+1,n))<min_cost)
{
min_cost=c[tour[start]][tour[i]]+cost;
for(k=0;k<n;k++)
min_tour[k]=temp[k];
} }
for(i=0;i<n;i++)
tour[i]=min_tour[i];
return (min_cost);
Page | 22
DAV COLLEGE JALANDHAR
Page | 23
DAV COLLEGE JALANDHAR
Page | 24
DAV COLLEGE JALANDHAR
for(int i=0;i<n;i++)
cout<<p_no[i]<<"\t\t"<<w[i]<<"\t\t"<<p[i]<<"\t\t"<<pwr[i]<<"\n";
}
void knapsack()
{
int u,i,z[100];
u=m;
float x[10];
for(i=0;i<n;i++)
{
if(w[i]>0)
break;
else
{
x[i]=1;
u=u-w[i];
}
}
if(i<=n)
x[i]=(float)u/w[i];
for(i=0;i<n;i++)
{
for(int j=1;j<n;j++)
{
Page | 26
DAV COLLEGE JALANDHAR
if(p_no[i]>p_no[j])
{
swap(p_no,i,j);
swap(x,i,j);
swap(p,i,j);
}
}
}
float opt_sol=0;
cout<<"\nthe order is\n";
cout<<"/";
for(i=0;i<n;i++)
cout<<p_no[i]<<"";
cout<<")";
cout<<"\t(";
for(i=0;i<n;i++)
{
cout<<x[i]<<"";
opt_sol=opt_sol+(p[i]*z[i]);
}
cout<<")";
cout<<"\noptimal solution is:\t"<<opt_sol;
}
int main()
Page | 27
DAV COLLEGE JALANDHAR
{
int i;
cout<<"nter the bag capacity";
cin>>m;
cout<<"nter the no. of items";
cin>>n;
swap(p_no,i,j);
swap(w,i,j);
swap(p,i,j);
}
}
}
display();
knapsack();
getch();
}
Page | 29
DAV COLLEGE JALANDHAR
Page | 30
DAV COLLEGE JALANDHAR
{
if(g[i][j]<md)
{
md=g[i][j];
v1=i;
v2=j;
}
}
}
}
cout<<"\'n edge("<<v1<<","<<v2<<")"<<"weight="<<md<<endl;
q[v1]=q[v2]=1;
total=total+md;
}
cout<<"\n total path length:"<<total;
}
void main()
{
int g[s][s],node,v1,v2,len,i,j,n;
cout<<"\n prim algorithm";
cout<<"\nenter the no of nodes in graph";
cin>>node;
cout<<"nenter the no of edges in graph";
cin>>n;
Page | 32
DAV COLLEGE JALANDHAR
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
g[i][j]=0;
}}
cout<<"\n edge & weight";
for(i=0;i<n;i++)
{
cout<<"\n enter the edges v1 & v2";
cin>>v1>>v2;
cout<<"enter the corresponding weight";
cin>>len;
g[v1][v2]=g[v1][v2]=len;
}
prim(g,node);
getch();
}
Page | 33
DAV COLLEGE JALANDHAR
Page | 34
DAV COLLEGE JALANDHAR
int main()
{
cout<<"\n enter the no. of nodes:";
cin>>n;
cout<<"\n enter the adjancy matrix:";
for(i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[i]=1;
cout<<"\n";
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
Page | 35
DAV COLLEGE JALANDHAR
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0||visited[v]==0)
{
cout<<"\n edge:"<<ne++<<"cost:",a,b,min;
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n minimum cost:"<<mincost;
getch();
return 0;
}
Page | 36
DAV COLLEGE JALANDHAR
Page | 37
DAV COLLEGE JALANDHAR
noe=0;
cout<<"\n enter the weights for the following edges:\n";
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<"<"<<i<<","<<j<<">:";
int w;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}
cout<<"\n The edges in the graph are:\n";
for(i=1;i<=noe;i++)
{
cout<<"<"<<graph_edge[i][1]<<","<<graph_edge[i][2]<<">:"<<graph_edge[i]
[3]<<endl;
}}
Page | 39
DAV COLLEGE JALANDHAR
void kruskal::sort_edges()
{
for(int i=1;i<=noe-1;i++)
{
for(int j=1;j<=noe-1;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
cout<<"\n After sorting the edges in the graph are:\n";
for(i=1;i<=noe;i++)
{
Page | 40
DAV COLLEGE JALANDHAR
cout<<"<"<<graph_edge[i][1]<<","<<graph_edge[i][2]<<">"<<graph_edge[i]
[3]<<endl;
}
}
void kruskal::algorithm()
{
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
cout<<"\n The algorithm starts:\n";
for(i=1;i<=noe;i++)
{
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
if(p1!=p2)
{
cout<<"\n The edge included in tree is"<<"<"<<graph_edge[i]
[1]<<","<<graph_edge[i][2]<<","<<endl;
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
for(int j=1;j<=top[p2];j++)
{
top[p1]++;
Page | 41
DAV COLLEGE JALANDHAR
sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
else
{
cout<<"\n Inclusion of the edge"<<graph_edge[i][1]<<"<"<<graph_edge[i]
[2]<<">"<<"forms a cycle so it is removed\n";
}
}
}
int kruskal::find_node(int n)
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}
void main()
Page | 42
DAV COLLEGE JALANDHAR
{
clrscr();
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
getch();
}
Page | 43
DAV COLLEGE JALANDHAR
Page | 44
DAV COLLEGE JALANDHAR
{
for(j=1;j<=n;j++)
cout<<wadj[i][j]<<"\t";
cout<<endl;
}
static int cost[10],path[10],sh[10],s;
cout<<"\n enter the source vertex:";
cin>>s;
for(i=1;i<=n;i++)
sh[i]=0;
for(i=1;i<=n;i++)
if(wadj[s][i]==999)
{
cost[i]=999;
path[i]=0;
}
else
{
cost[i]=wadj[s][i];
path[i]=s;
sh[s]=1;
cost[s]=0;
for(i=1;i<=n;i++)
cout<<cost[i]<<"\t";
Page | 46
DAV COLLEGE JALANDHAR
cout<<endl;
int it_flag=0;
while(it_flag==0)
{
j=min(cost,sh,n);
sh[j]=1;
for(i=1;i<=n;i++)
{
if(sh[i]==0)
{
if(wadj[j][i]!=999)
{
if(cost[j]+wadj[i][j]<cost[i])
{
cost[i]=cost[j]+wadj[j][i];
path[i]=j;
}
}
}
}
it_flag=1;
for(i=1;i<=n;i++)
if(sh[i]==0)
{
Page | 47
DAV COLLEGE JALANDHAR
it_flag=0;
break;
}
}
for(i=1;i<=n;i++)
cout<<"\n Cost of shorted path from source vertex:"<<s<<"to destination
vertex:"<<i<<"is:"<<cost[i];
cout<<"\n enter destination vertex:";
cin>>dx;
cout<<"\n cost of reaching from"<<s<<"to"<<dx<<"="<<cost[dx];
int short_path[10];
int m=path[dx];
i=0;
while(m!=s)
{
short_path[i]=m;
i++;
m=path[m];
}
cout<<s<<"-->";
for(j=i-1;j>=0;j--)
cout<<"\n"<<short_path[j]<<"-->";
cout<<dx;
}
Page | 48
DAV COLLEGE JALANDHAR
getch();
}
int min(int cost[],int sh[],int n)
{
int v,i,min1=999;
for(i=1;i<=n;i++)
{
if(sh[i]==0)
{
if(cost[i]<min1)
{
min1=cost[i];
v=i;
}
}
}
return(v);
}
Page | 49
DAV COLLEGE JALANDHAR