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

Shri Vaishnav Vidyapeeth

Vishwavidyalaya, Indore (M.P.)


Shri Vaishnav Institute of Information Technology
Department of Computer Science Engineering

Lab Manual

Subject: Analysis and Design of Algorithms

Subject Code: BTCS302N

Semester: III

Section: CS-K

Submitted By: Submitted to:


Tanisha Shrivas Prof. Shilpa Agnihotri
Enroll No.: 21100BTCSE10008
Shri Vaishnav Institute of Information
Technology, Indore (M.P.)

VISION

“To be renowned for excellence in Computer Science &


Engineering.”

MISSION

“To impart quality education, meeting the latest industry


requirements, futuristic research & developments in
Computer Science & Engineering.”

Page | 1
INDEX

S.No. Experiments Date of Page


Experiment No
1. Write a program for Iterative and Recursive
Binary Search.
2. Write a program for Merge Sort.
3. Write a program for Quick Sort.
4. Write a program for Strassen Matrix’s
Multiplication
5. Write a program to perform all the
operations on stack and deque.
6. Write a program to perform all the
operations on Linked list.
7. Write a program for Floyd-Warshall
Problem.
8. Write a program for Hamiltonian Cycle
Problem.
9.
10.

Page | 2
EXPERIMENT 1
AIM : Write a program for Iterative and Recursive Binary Search.
Code :
Iterative binary search
#include<iostream>
using namespace std;

int binarysearch(int arr[],int start,int end,int data){


while(start<=end){
int mid=(start+end)/2;
if(arr[mid]==data){
return mid;
}
else if(data<arr[mid]){
end=mid-1;
}
else{
start=mid+1;
}
}
}

int main(){
int n,ans,data;
cout<<"enter size of array :";
cin>>n;

Page | 3
int arr[n];
cout<<"enter elements of array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"enter the data to be searched \n";
cin>>data;
ans= binarysearch(arr,0,n-1,data);
(ans==-1)?cout<<"Element is not present":cout<<"Element is present at
index "<<ans;
return 0;
}

Recursive binary search


#include<iostream>
using namespace std;

int binarysearch(int arr[],int start,int end,int data){


while(start<=end){
int mid=(start+end)/2;
if(arr[mid]==data)
return mid;

else if(data<arr[mid])
return binarysearch(arr,start,mid-1,data);

Page | 4
else
return binarysearch(arr,mid+1,end,data);

}
return -1;
}

int main(){
int n,ans,data;
cout<<"enter size of array :";
cin>>n;
int arr[n];
cout<<"enter elements of array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"enter the data to be searched \n";
cin>>data;
ans= binarysearch(arr,0,n-1,data);
(ans==-1)?cout<<"Element is not present":cout<<"Element is present at
index "<<ans;
return 0;
}

Page | 5
OUTPUT:

Page | 6
EXPERIMENT 2
AIM : Write a program for Merge Sort.
Code :
#include<iostream>
using namespace std;

void print(int arr[],int n){


for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

void merge(int arr[],int p,int m,int q){


int n1=m-p+1,n2=q-m;

int L[n1],M[n2];

for(int i=0;i<n1;i++)
L[i]=arr[p+i];
for(int j=0;j<n2;j++)
M[j]=arr[m+1+j];

int i=0,j=0,k=p;
while(i<n1 && j<n2){
if(L[i]<=M[j]){

Page | 7
arr[k]=L[i];
i++;
}
else{
arr[k]=M[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=L[i];
i++;
k++;
}
while(j<n2){
arr[k]=M[j];
j++;
k++;
}

}
void mergesort(int arr[],int l,int h){
int m;
if(l<h){
// m= (l+(h-l))/2;
m=(l+h)/2;

Page | 8
mergesort(arr,l,m);
mergesort(arr,m+1,h);
merge(arr,l,m,h);
}

int main(){
int arr[]={4,9,8,12,45,3,7,2};
int n= sizeof(arr)/sizeof(arr[0]);

mergesort(arr,0,n-1);
cout<<"Sorted array: "<<endl;
print(arr,n);
return 0;
}

OUTPUT:

Page | 9
EXPERIMENT 3
AIM : Write a program for Quick Sort.
Code :
#include <iostream>
using namespace std;

int partition(int arr[], int start, int end)


{
int pivot = arr[start];
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);

int i = start, j = end;


while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}

while (arr[j] > pivot) {


j--;
}

Page | 10
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
if (start >= end)
return;

int p = partition(arr, start, end);


quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}
int main()
{
int arr[] = { 9, 3, 7,12,1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}

Page | 11
OUTPUT:

Page | 12
EXPERIMENT 4
AIM : Write a program for Strassen Matrix’s Multiplication.
Code :
#include<iostream>
using namespace std;

int SMM(int arr[2][2],int brr[2][2]){


int a,b,c,d,e,f,g,h,p1,p2,p3,p4,p5,p6,p7;
arr[0][0]=a,arr[0][1]=b,arr[1][0]=c,arr[1][1]=d;
brr[0][0]=e,brr[0][1]=f,brr[1][0]=g,brr[1][1]=h;
p1=a*(f-h);
p2=h*(a+b);
p3=(c+d)*e;
p4=(g-e)*d;
p5=(a+d)*(e+h);
p6=(b-d)*(g+h);
p7=(a-c)*(e+f);

cout<<p5+p4-p2+p6<<" "<<p1+p2<<endl;
cout<<p3+p4<<" "<<p1+p5-p3-p7<<endl;

}
void print(int A[2][2]){
for(int i=0;i<2;i++){
cout<<endl;
for(int j=0;j<2;j++)

Page | 13
cout<<A[i][j]<<" ";
}
}

int main(){
int A[2][2],B[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cin>>A[i][j];
}

for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cin>>B[i][j];
}
cout<<"A= ";
print(A);

cout<<"\nB= ";
print(B);
cout<<"\nC= ";
SMM(A,B);

return 0;
}

Page | 14
OUTPUT:

Page | 15
EXPERIMENT 5
AIM : Write a program to perform all the operations on stack and
deque.
Code :
For stack:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {

Page | 16
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
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;

Page | 17
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

For Deque:
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);

Page | 18
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1) {
cout<<"Overflow"<<endl;
} else {
if(f==-1) {
f++;
r++;
} else
r=r+1;

a[r]=i;
cout<<"Inserted item is "<<a[r]<<endl;
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i;

Page | 19
cout<<"inserted element is: "<<i<<endl;
} else if(f!=0) {
a[--f]=i;
cout<<"inserted element is:"<<i<<endl;
} else
cout<<"Overflow";

}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty"<<endl;
return;
}
else {
cout<<"the deleted element is:"<<a[f]<<endl;
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty"<<endl;
return;

Page | 20
}
else {
cout<<"the deleted element is:"<<a[r]<<endl;
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty"<<endl;
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
}
int main() {
int c,i;
dequeue d;
cout<<"1.insert at beginning\n";
cout<<"2.insert at end\n";
cout<<"3.show\n";
cout<<"4.deletion from front\n";

Page | 21
cout<<"5.deletion from rear\n";
cout<<"6.exit\n";
do{
cout<<"enter your choice:";
cin>>c;
switch(c) {
case 1:{
cout<<"enter the element to be inserted"<<endl;
cin>>i;
d.insert_at_beg(i);
break;
}
case 2:{
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_end(i);
break;
}
case 3:{
d.show();
break;
}
case 4:{
d.delete_fr_front();
break;
}

Page | 22
case 5:{
d.delete_fr_rear();
break;
}
case 6:{
cout<<"Exit";
break;
}
default:{
cout<<"invalid choice";
break;
}
}
}while(c!=6);
}

Page | 23
OUTPUT:

Page | 24
EXPERIMENT 6
AIM : Write a program to perform all the operations on linked list.
Code :
#include <stdlib.h>
#include <iostream>
using namespace std;

struct Node {
int data;
struct Node* next;
};

void insertAtBeginning(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void insertAfter(struct Node* prev_node, int new_data) {


if (prev_node == NULL) {
cout << "the given previous node cannot be NULL";
return;
}

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

Page | 25
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}

void insertAtEnd(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

while (last->next != NULL) last = last->next;


last->next = new_node;
return;
}

void deleteNode(struct Node** head_ref, int key) {


struct Node *temp = *head_ref, *prev;

if (temp != NULL && temp->data == key) {


*head_ref = temp->next;
free(temp);

Page | 26
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;

free(temp);
}
void printList(struct Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);

Page | 27
cout << "Linked list: ";
printList(head);

cout << "\nAfter deleting an element: ";


deleteNode(&head, 3);
printList(head);
return 0;
}

OUTPUT:

Page | 28
EXPERIMENT 7
AIM : Write a program for Floyd-Warshall Problem.
Code :
#include <bits/stdc++.h>
using namespace std;
#define V 4
#define INF 99999
void printSolution(int dist[][V]);
void floydWarshall(int dist[][V])
{
int i, j, k;
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] > (dist[i][k] + dist[k][j])
&& (dist[k][j] != INF
&& dist[i][k] != INF))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V]){
cout << "The following matrix shows the shortest distances between
every pair of vertices \n";

Page | 29
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF"
<< " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}
int main(){
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
floydWarshall(graph);
return 0;
}

OUTPUT:

Page | 30
EXPERIMENT 8
AIM : Write a program for Hamiltonian Cycle Problem.
Code :
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define V 5
using namespace std;
void printSolution(int path[]);
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
if (graph [path[pos-1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
bool hamCycleUtil(bool graph[V][V], int path[], int pos){
if (pos == V) {
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}

Page | 31
for (int v = 1; v < V; v++){
if (isSafe(v, graph, path, pos)) {
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
bool hamCycle(bool graph[V][V]){
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false) {
cout<<"\nSolution does not exist"<<endl;
return false;
}
printSolution(path);
return true;
}
void printSolution(int path[]){
cout<<"Solution Exists:";
cout<<" Following is one Hamiltonian Cycle \n"<<endl;
for (int i = 0; i < V; i++)

Page | 32
cout<<path[i]<<" ";
cout<< path[0]<<endl;
}
int main(){
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
hamCycle(graph1);
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}

OUTPUT:

Page | 33

You might also like