Você está na página 1de 21

ANALYSIS AND DESIGN

OF ALGORITHM
PRACTICAL FILE

Submitted to: Submitted by:


Mr. Rajneesh Kumar Arpita Srivastava
7CS2
497
BTech CS & E
INDEX

S.No. PROGRAM SIGN


1. WAP to obtain GCD of two numbers recursively

2. WAP to implement Binary Search recursively

3. WAP to implement Tower of Hanoi problem

4. WAP to implement Quick Sort algorithm

5. WAP to implement the Merge Sort algorithm

6. WAP to implement Prim’s algorithm

7. WAP to implement Kruskal’s algorithm

8. WAP to implement 8 Queens problem


PROGRAM 1
WAP TO OBTAIN THE GCD OF TWO NUMBERS
RECURSIVELY

#include<stdio.h> 
#include<conio.h> 
int GCD (int a,int b) 

if (a<0) a= -a; 
if (b<0) b= -b; 
if (a==0 || b==1 || a==b) return b; 
if (a==1 || b==0) return a; 
if (a>b) return GCD (b, a%b); 
else return GCD (a, b%a); 

void main() 

int x,y; 
clrscr(); 
printf("Enter 1st number:"); 
scanf("%d",&x); 
printf("Enter 2nd number:"); 
scanf("%d",&y); 

printf("\nGCD is:%d", GCD(x,y)); 


getch(); 
}

OUTPUT
Enter two numbers for GCD
1. 35
2. 75
The GCD is: 5
PROGRAM 2
WAP TO IMPLEMENT BINARY SEARCH ALGORITHM
RECURSIVELY

#include<stdio.h>
int binarysearch(int a[],int n,int low,int high)
{ int mid;
if (low > high)
return -1;
mid = (low + high)/2;
if(n == a[mid])
{ printf("The element is at position %d\n",mid+1);
return 0;
}
if(n < a[mid])
{ high = mid - 1;
binarysearch(a,n,low,high);
}
if(n > a[mid])
{ low = mid + 1;
binarysearch(a,n,low,high);
}
}

int main()
{ int a[50];
int n,no,x,result;
printf("Enter the number of terms : ");
scanf("%d",&no);
printf("Enter the elements :\n");
for(x=0;x<no;x++)
scanf("%d",&a[x]);
printf("Enter the number to be searched : ");
scanf("%d",&n);
result = binarysearch(a,n,0,no-1);
if(result == -1)
printf("Element not found");
return 0;
}
OUTPUT

Enter the number of terms: 5


12
54
89
75
63

Enter the number to be searched: 54


The element is at position 2
PROGRAM 3
WAP TO IMPLEMENT TOWER OF HANOI PROBLEM
#include<stdio.h>
#include<math.h>
void towers(int,char,char,char);

void towers(int n,char frompeg,char topeg,char auxpeg)


{ /* If only 1 disk, make the move and return */
if(n==1)
{ printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg);
return;
}
/* Move top n-1 disks from A to B, using C as auxiliary */
towers(n-1,frompeg,auxpeg,topeg);
/* Move remaining disks from A to C */
printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg);
/* Move n-1 disks from B to C using A as auxiliary */
towers(n-1,auxpeg,topeg,frompeg);
}
int main()
{ int n;
int moves;
printf("Enter the number of disks : ");
scanf("%d",&n);
moves=pow(2,n)-1;
printf("\nThe No of moves required is=%d \n",moves);
printf("The Tower of Hanoi involves the moves :\n\n");
towers(n,'A','C','B');
return 0;
}

OUTPUT
Enter the number of Disks : 3

The No of moves required are : 7


The tower of Hanoi involves the moves:

Move disk from A to C


Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C
PROGRAM 4
WAP TO IMPLEMENT QUICK SORT ALGORITHM

#include <stdio.h>
#include <stdlib.h>
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot(int i,int j )
{
return((i+j) /2);
}
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = choose_pivot(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
// swap two elements
swap(&list[m],&list[j]);
// recursively sort the lesser list
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void main()
{
int MAX_ELEMENTS;
printf("How many numbers you want to sort:");
scanf("%d",&MAX_ELEMENTS);
int list[50];
int i = 0;
// generate random numbers and fill them to the list
printf("Enter the numbers\n");
for(i = 0; i < MAX_ELEMENTS; i++ ){
scanf("%d",&list[i]);
}
printf("The list before sorting is:\n");
printlist(list,MAX_ELEMENTS);
// sort the list using quicksort
quicksort(list,0,MAX_ELEMENTS-1);

// print the result


printf("\nThe list after sorting using quicksort algorithm:\n");
printlist(list,MAX_ELEMENTS);
}

OUTPUT
How many numbers you want to sort: 5
Enter the numbers:
66
11
44
33
22
The list before sorting:
66 11 44 33 22
The list after sorting:
11 22 33 44 66
PROGRAM 5
WAP TO IMPLEMENT THE MERGE SORT ALGORITHM

#include<stdio.h>
#include<conio.h>
int j,a[15],i,temp,n;
void main()
{
clrscr();
void createheap(int);
printf("Enter number of elements(Not more than 15)\n ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>=2;i--)
createheap(i);
for(i=n-1;i>=1;i--)
{
temp=a[1];
a[1]=a[i+1];
a[i+1]=temp;
for(j=i;j>=2;j--)
createheap(j);
}
printf("The sorted elements are :\n");
for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
getch();

}
void createheap(int y)
{
if(y>1)
{
if(a[y]>a[y/2])
{
temp=a[y];
a[y]=a[y/2];
a[y/2]=temp;
createheap(y/2);
}}}

OUTPUT

Enter the number of elements (not more than 15): 5


Enter the numbers:
66
11
44
33
22
The sorted elements are:
11 22 33 44 66
PRACTICAL 6
WAP TO IMPLEMENT PRIM’S ALGORITHM

#include<iostream>
using namespace std;
class prims
{
private:
int n;
int graph_edge[250][4];
int g;
int tree_edge[250][4];
int t;
int s;
int T1[50],t1;
int T2[50],t2;

public:
void input();
int findset(int);
void algorithm();
void output();
};

void prims::input()
{
cout<<"Enter the no. of nodes in the undirected weighted graph ::";
cin>>n;
g=0;
cout<<"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)
{
g++;
graph_edge[g][1]=i;
graph_edge[g][2]=j;
graph_edge[g][3]=w;
}
}
}
// print the graph edges
cout<<"\n\nThe edges in the given graph are::\n";
for(int i=1;i<=g;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" >
::"<<graph_edge[i][3]<<endl;
}

int prims::findset(int x)
{
for(int i=1;i<=t1;i++)
if(x==T1[i])
return 1;
for(int i=1;i<=t2;i++)
if(x==T2[i])
return 2;
return -1;
}

void prims::algorithm()
{
t=0;
t1=1;
T1[1]=1; //The source node
t2=n-1;
for(int i=1;i<=n-1;i++)
T2[i]=i+1; //The reamining nodes
cout<<"\n*****The algorithm starts*****\n\n";
while(g!=0 && t!=n-1)
{
// Find the least cost edge
int min=9999;
int p;
int u,v,w;
for(int i=1;i<=g;i++)
{
bool flag1=false,flag2=false;
//if u and v are in different sets
if(findset(graph_edge[i][1])!=findset(graph_edge[i][2]))
{
if(min>graph_edge[i][3])
{
min=graph_edge[i][3];
u=graph_edge[i][1];
v=graph_edge[i][2];
w=graph_edge[i][3];
p=i;
}
}
}
//break if there is no such edge
cout<<"The edge included in the tree is ::";
cout<<" < "<<u<<" , "<<v<<" > "<<endl;

//delete the edge from graph edges


for(int l=p;l<g;l++)
{
graph_edge[l][1]=graph_edge[l+1][1];
graph_edge[l][2]=graph_edge[l+1][2];
graph_edge[l][3]=graph_edge[l+1][3];
}
g--;
//add the edge to the tree
t++;
tree_edge[t][1]=u;
tree_edge[t][2]=v;
tree_edge[t][3]=w;
//Alter the set partitions
t1++;
int m;
if(findset(v)==2)
{
T1[t1]=v;
m=v;
}
else if(findset(u)==2)
{
T1[t1]=u;
m=u;
}

int x;
for(x=1;T2[x]!=m;x++);
for(;x<t2;x++)
T2[x]=T2[x+1];
t2--;
// Print the sets
int k;
cout<<"NOW\nT1 :: ";
for(k=1;k<=t1;k++)
cout<<T1[k]<<" ";
cout<<endl;
cout<<"T2 :: ";
for(k=1;k<=t2;k++)
cout<<T2[k]<<" ";
cout<<endl;
cout<<"The graph edges are ::\n";
for(int i=1;i<=g;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" >
::"<<graph_edge[i][3]<<endl;
cout<<endl<<endl;
}
}
void prims::output()
{
cout<<"\nThe selected edges are ::\n";
for(int i=1;i<=t;i++)
cout<<" < "<<tree_edge[i][1]<<" , "<<tree_edge[i][2]<<" > ::"<<tree_edge[i]
[3]<<endl;
}
int main()
{
prims obj;
obj.input();
obj.algorithm();
obj.output();
return 0;
}
OUTPUT
Enter the no. of nodes in the undirected weighted graph :: 5
Enter the weights for the following edges ::
< 1 , 2 > ::6
< 1 , 3 > ::5
< 1 , 4 > ::1
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 3 , 5 > ::2
< 4 , 5 > ::6

The edges in the given graph are ::


< 1 , 2 > ::6
< 1 , 3 > ::5
< 1 , 4 > ::1
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 3 , 5 > ::2
< 4 , 5 > ::6

*****The algorithm starts*****

The edge included in the tree is :: < 1 , 4 >


NOW
T1 :: 1 4
T2 :: 2 3 5
The graph edges are ::
< 1 , 2 > ::6
< 1 , 3 > ::5
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 3 , 5 > ::2
< 4 , 5 > ::6

The edge included in the tree is :: < 1 , 3 >


NOW
T1 :: 1 4 3
T2 :: 2 5
The graph edges are ::
< 1 , 2 > ::6
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 3 , 5 > ::2
< 4 , 5 > ::6

The edge included in the tree is :: < 3 , 5 >


NOW
T1 :: 1 4 3 5
T2 :: 2
The graph edges are ::
< 1 , 2 > ::6
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 4 , 5 > ::6

The edge included in the tree is :: < 2 , 3 >


NOW
T1 :: 1 4 3 5 2
T2 ::
The graph edges are ::
< 1 , 2 > ::6
< 1 , 5 > ::5
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 4 , 5 > ::6

The selected edges are ::


< 1 , 4 > ::1
< 1 , 3 > ::5
< 3 , 5 > ::2
< 2 , 3 > ::3
PROGRAM 7
WAP TO IMPLEMENT KRUSHKAL’S ALGORITHM

#include < stdio.h>


#include < conio.h>
typedef struct
{
int node1;
int node2;
int wt;
}edge;

void sortedges(edge a[],int n)


{
int i,j;
edge temp;
for(i=0;i< n-1;++i)
for(j=i+1;j< n;++j)
if(a[i].wt>a[j].wt){temp=a[i];a[i]=a[j];a[j]=temp;}
}

int checkcycle(int p[],int i,int j)


{
int v1,v2;
v1 = i;
v2 = j;
while(p[i]>-1)
i = p[i];
while(p[j]>-1)
j = p[j];
if(i!=j)
{
p[j]=i;
printf("%d %d\n",v1,v2);
return 1;
}
return 0;
}
void main()
{
edge e[100];
int parent[100];
int n,i,j,m,k = 1,cost = 0;
clrscr();
printf("KRUSKAL's ALGORITHM\n");
printf("Enter number of nodes\n");
scanf("%d",&n);
for(i=0;i< n;++i)
parent[i]=-1;
i = 0;
printf("Enter number of edges\n");
scanf("%d",&m);
for(i=0;i< m;++i)
{
printf("enter an edge and wt\n");
scanf("%d %d %d", &e[i].node1,&e[i].node2,&e[i].wt);
}
sortedges(e,m);
printf("\n\nEdges of the tree\n");
i = 0;
while(k< n)
{
if(checkcycle(parent,e[i].node1,e[i].node2))
{
k++;
cost=cost+e[i].wt;
i++;
}
}
printf("cost = %d",cost);
getch();
}
OUTPUT

KRUSKAL's ALGORITHM
Enter number of nodes
4
Enter number of edges
6
enter an edge and wt
124
enter an edge and wt
136
enter an edge and wt
147
enter an edge and wt
4 3 10
enter an edge and wt
425
enter an edge and wt
231

Edges of the tree


23
12
42
cost = 10
PRACTICAL 8
WAP TO IMPLEMENT 8 QUEENS PROBLEM

#include<stdio.h>
#include<math.h>
#define QUEENNO 8

void placeQueen(int,int *);


int canBePlaced(int,int,int *);
void showBoard(int *);

void main()
{
int x[QUEENNO],i;
printf("The 8 Queens problem");
placeQueen(0,x);
printf("end");
}

void placeQueen(int k,int *x)


{
int i,j;
char ch;
for(i=0;i<8;i++)
{
if(canBePlaced(k,i,x))
{
x[k]=i;
if(k==7)
{
showBoard(x);
printf("Want to see more? [n->stop, other->continue] : ");
scanf("%c",&ch);
if(ch=='n' || ch=='N') exit(0);
}
if(k<7) placeQueen(k+1,x);
}
}
}

int canBePlaced(int k,int i,int *x)


{
int j;
for(j=0;j<k;j++)
{
if((abs(j-k)==abs(x[j]-i))||(x[j]==i))
return 0;
}
return 1;
}

void showBoard(int *x)


{
int i,j;
printf("\n");
for(i=0;i<8;i++)
printf("%d ",(i+1));
printf("\n");
for(i=0;i<8;i++)
{
printf("%d ",(i+1));
for(j=0;j<8;j++)
{
if(j==x[i])
printf("Q");
else
printf("-");
printf(" ");
}
printf("\n");
}
}

OUTPUT
The 8 Queens problem
12345678
1 Q-------
2 ----Q---
3 -------Q
4 -----Q--
5 --Q-----
6 ------Q-
7 -Q------
8 ---Q----
Want to see more? [n->stop, other->continue]: n

Você também pode gostar