Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

ANALYSIS OF DESIGN AND ALGORITHM

ASSIGNMENT

Submitted to Submitted By:


Mr. G.L Saini Aman Abhishek
Assistant Professor Section-A1
Semester-7th
Enrol. No.-A20405216119

1
INDEX

S. No. Title Page No. Signature


1. WAP to implement Queue data 3
structure using Linked List.
2. WAP to implement Stack data structure 6
using Linked List.
3. WAP to implement Binary 9
Search(Divide and Conquer method)
4. WAP to implement Quick Sort(Divide 10
and Conquer method)
5. WAP to implement Merge Sort(Divide 12
and Conquer method)
6. WAP to implement Multiplication of 14
two matrices using strassen’s
method(Divide and Conquer method)
7. WAP to implement DFS(using 16
Adjacenecy Matrix)
8. WAP to implement BFS(using 18
Adjacenecy Matrix)
9. WAP to implement Prims Algorithm to 19
find minimum spanning tree
10. WAP to implement Kruskals Algorithm 21
to find minimum spanning tree.
11. WAP to implement Fractional Knapsack 23
(Dynamic Approach).
12. WAP to implement 0/1 Knapsack 25
(Dynamic Approach).
Q1. WAP to implement queue data structure using linked list.
Code:
#include <iostream>
using namespace std;
struct node
{ int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert()
{ int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else
{ temp=(struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp; }
}
void Delete()
{ temp = front;
if (front == NULL)
{ cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL)
{ temp = temp->next;
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL; }
}
void Display()
{ temp = front;
if ((front == NULL) && (rear == NULL))
{ cout<<"Queue is empty"<<endl;
return; }
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next; }
cout<<endl;
}
int main()
{ int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch)
{ case 1:
Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0; }
Output:-
Q2. WAP to implement Stack Data Structure using Linked List.
Code:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
void push(int val) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
newnode->next = top;
top = newnode;
}
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
}
}
void display()
{ struct Node*
ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL)
{ cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}
int main()
{ int ch,
val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4:
{ cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

Output:-
Q3. WAP to implement Binary Search ( Divide & Conquer method).
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{ int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{ cin>>a[i]; }
sort(a,a+n);
int k;
cin>>k;
int s=0,ans=-1;
int e=n-1;int mid;
while(s<=e)
{ mid=(s+e)/2;
if(a[mid]==k)
{ ans=mid;
break; }
if(a[mid]>k)
{ e=mid-1; }
if(a[mid]<k)
{ s=mid+1; }
} cout<<ans;
return 0; }
Output:-
Q4. WAP to implement Quick Sorting (Divide & Conquer methods)
Code:
#include<iostream>
using namespace std;
int partition(int *a,int s,int e)
{ int piviot=a[e];
int pind=s;
int i,t;
for(i=s;i<e;i++)
{ if(a[i]<=piviot)
{ t=a[i];
a[i]=a[pind];
a[pind]=t; pind+
+;
}
}
t=a[e];
a[e]=a[pind];
a[pind]=t;
return pind;
}
void quicksort(int *a,int s,int e)
{ if(s<e){
int pind=partition(a,s,e);
quicksort(a,s,pind-1);
quicksort(a,pind+1,e);
}
}
int main()
{

10
int n;
cout<<"Enter number of elements"<<endl;
cin>>n;
int a[n];
cout<<"Enter the array :- ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quicksort(a,0,n-1);
cout<<"Sorted array is :- ";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
Output:-

11
Q5. WAP to implement Merge Sort(Divide & Conquer methods).
Code:
#include<iostream>
using namespace std;
void Merge(int A[],int p, int q,int r) {
int n1=q-p+1,i,j,k;
int n2=r-q;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=A[p+i];
}
for(j=0;j<n2;j++)
{
R[j]=A[q+j+1];
}
i=0,j=0;
for(k=p;i<n1&&j<n2;k++)
{
if(L[i]<R[j])
{
A[k]=L[i++];
}
else
{
A[k]=R[j++];
}
}
while(i<n1) {
A[k++]=L[i++];
}
while(j<n2) {
A[k++]=R[j++];
}
}
void MergeSort(int A[],int p,int r)
{ int q;
if(p<r){ q=(p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q,r); }
}
int main(){
int A_Size;
cout<<"Enter number of elements :";
cin>>A_Size;
int A[A_Size];
cout<<"Enter the array elements :";
for(int i=0;i<A_Size;i++){
cin>>A[i];
}
MergeSort(A,0,A_Size-1);
for(int i=0;i<A_Size;i++){
cout<<A[i]<<" "; }
cout<<endl; }
Output:-
Q6. WAP to implement Multiplication of two matrices using strasses’s method.
(Divide and Conquer method).
Code:
#include <iostream>
using namespace std;
int main()
{ int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
cout<<"Enter the 4 elements of first matrix: ";
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>a[i][j];
cout<<"Enter the 4 elements of second matrix: ";
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>b[i][j];
cout<<endl;
cout<<"The first matrix is";
for(i=0;i<2;i++)
{ cout<<endl;
for(j=0;j<2;j++)
cout<<a[i][j]<<” “; }
cout<<"The second matrix is";
for(i=0;i<2;i++)
{ cout<<endl;
for(j=0;j<2;j++)
cout<<b[i][j]<<” “;
}

m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);


m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7; c[0]
[1]=m3+m5;
c[1][0]=m2+m4; c[1]
[1]=m1-m2+m3+m6;

cout<<"After multiplication using ";


for(i=0;i<2;i++)
{
cout<<endl;
for(j=0;j<2;j++)
cout<<c[i][j]<<” “;
}

return 0;
}

Output:-
Q7.WAP to implement BFS (Using Adjacency Matrix).
Code:-
#include<iostream>
using namespace std;
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r)
{ visited[q[f]] = 1;
bfs(q[f++]); }
}
int main()
{ int v;
cout<<"Enter the number of vertices:"<<endl;
cin>>n;
for(i=1; i <= n; i++) {
q[i] = 0;
visited[i] = 0;
}
cout<<" Enter graph data in matrix form:"<<endl;
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++)
{ cin>>a[i][j];
}
}
cout<<endl;
cout<<" Enter the starting vertex:"<<endl;
cin>>v;
bfs(v);
cout<<" Output of BFS:"<<endl;
for(i=1; i <= n; i++)
{ if(visited[i])
cout<<i<<" ";
else {
cout<<"Bfs is not possible. Not all nodes are reachable";
break;
}
}}

Output:-
Q8. WAP too implement DFS (Using Adjacency Matrix).
Code:
#include<iostream>
using namespace std;
int G[10][10],visited[10],n;
void DFS(int i)
{ int j;
cout<<i<<" ";
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j); }
int main()
{ int i,j;
cout<<"Enter number of vertices:"<<endl;
cin>>n;
cout<<"Enter adjecency matrix of the graph:"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>G[i][j];
for(i=0;i<n;i++)
visited[i]=0;
cout<<"Output of DFS is:"<<endl;
DFS(0); }
Output:-
Q9. WAP to implement Prims algorithms to find minimum spanning tree.
Code:
#include<iostream>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;
main()
{ int m,c;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES Cost\n";
for(k=1;k<=m;k++)
{ cin >>i>>j>>c;
cost[i][j]=c; }
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0) cost[i]
[j]=INT_MAX;
cout <<"MST is:";
k=1;
while(k<n)
{ m=INT_MAX;
if(k==1)
{ for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(cost[i][j]<m)
{ m=cost[i][j];
u=i;
} }
else
{ for(j=n;j>=1;j--)
if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
m=cost[v][j];
u=j;
}
}
cost[v][u]=INT_MAX;
v=u;
cout<<v << " "; k+
+;
visit[v]=0; visited[v]=1;
}
}
Output:-

20
Q10. WAP to implement Kruskals algorithms to find minimum spanning tree.
Code:-
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;
main()
{
int dup1,dup2;
cout<<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"EDGE Cost";
for(k=1;k<=m;k++)
{
cin >>i >>j >>c; cost[i]
[j]=c;
cost[j][i]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=INT_MAX;
visit=1;
while(visit<n)
{ v=INT_MA
X;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]!= INT_MAX && cost[i][j]<v && cost[i][j]!=-1 )
{
int count =0;
for(p=1;p<=n;p++)
{
if(visited[p]==i || visited[p]==j)
count++;
}
if(count >= 2)
{
for(p=1;p<=n;p++)
if(cost[i][p]!= INT_MAX && p!=j)
dup1=p;
for(p=1;p<=n;p++)
if(cost[j][p]!= INT_MAX && p!=i)
dup2=p;
if(cost[dup1][dup2]==-1)
continue;

21
}
l=i;
k=j;
v=cost[i][j];
}
cout <<"edge from " <<l <<"-->"<<k;
cost[l][k]=-1;
cost[k][l]=-1;
visit++;
int count=0;
count1=0;
for(i=1;i<=n;i++)
{
if(visited[i]==l)
count++;
if(visited[i]==k)
count1++;
}
if(count==0) visited[+
+vst]=l; if(count1==0)
visited[++vst]=k;
}
}
Output:-
Q11. WAP to implement Fractional Knapsack(Greedy Approach).
Code:-
#include<bits/stdc++.h>

using namespace std;

bool cmp(pair<int, int> a, pair<int,int>b)

//cout<<a<<" "<<b<<endl;

double x=double((double)a.first/(double)a.second);

double y=double((double)b.first/(double)b.second);

return x>y;

int main ()

int n,kn;

cin>>n>>kn;

pair<int, int>knap[10002];

for(int i=0;i<n;i++)

int p,w;

cin>>p>>w;

knap[i].first=p;

knap[i].second=w;

sort(knap,knap+n,cmp);

//sort(wt,wt+n,cmp);

int currwt=0;

double finalwt=0.0;

for(int i=0;i<n;i++)

{
if(knap[i].second+currwt<=kn)

finalwt=finalwt+knap[i].first;

currwt=currwt+knap[i].second;

else

int temp=kn-currwt;

finalwt=finalwt+knap[i].first*((double)temp/knap[i].second);

break;

printf("%0.1lf",finalwt);

return 0;

Output:-
Q12. WAP to implement 0/1 Knapsack(Dynamic Approach).
Code:-
#include<iostream>
using namespace std;
int max(int a, int b) { return (a > b)? a : b; }
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int i, n, val[20], wt[20], W;
cout<<"Enter number of items:"<<endl;
cin>>n;
cout<<"Enter value and weight of items:";
for(i = 0;i < n; ++i){
cin>>val[i]>>wt[i];
}
cout<<"Enter size of knapsack:";
cin>>W;
cout<<knapSack(W, wt, val, n)<<endl;
return 0;
}
Output:-

You might also like