0% found this document useful (0 votes)
13 views60 pages

Bhumika DAA File

The document outlines several programming algorithms including Linear Search, Binary Search, Merge Sort, Quick Sort, Bubble Sort, Selection Sort, Heap Sort, Huffman Coding, and Minimum Spanning Tree. Each algorithm is accompanied by its implementation in C++, along with a focus on analyzing its time complexity. The document is structured into multiple programs, each demonstrating different algorithms and their respective outputs.

Uploaded by

Bhumika Piplani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views60 pages

Bhumika DAA File

The document outlines several programming algorithms including Linear Search, Binary Search, Merge Sort, Quick Sort, Bubble Sort, Selection Sort, Heap Sort, Huffman Coding, and Minimum Spanning Tree. Each algorithm is accompanied by its implementation in C++, along with a focus on analyzing its time complexity. The document is structured into multiple programs, each demonstrating different algorithms and their respective outputs.

Uploaded by

Bhumika Piplani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

PROGRAM-1

AIM:To implement Linear Search and Binary Search and analyse its time
complexity.

@LINEAR SEARCH:
PROGRAM CODE:
#include<iostream>
using namespace std;
int main()
{
int a[10],i,n,num,index;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_______BHUMIKA_______\n";
cout<<"\nEnter number of elements for an array:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter a number to search:";
cin>>num;
for(i=0;i<n;i++)
{
if(a[i]==num)
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
{
index=i;
break;
}
}
cout<<"\nThe elements is found at index number:"<<index;
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
@BINARY SEARCH:
PROGRAM CODE:
#include<iostream>
using namespace std;
int main()
{
int i,a[10],num,beg=0,end,mid,n;
clock_t t1,t2;
float t;
t1=clock();
cout<<"______BHUMIKA_____\n";
cout<<"\nEnter number of elements for an array:";
cin>>n;
cout<<"\nEnter the elements(in increasing order):";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter a number to search:";
cin>>num;
end=n-1;
mid=(beg+end)/2;
while(beg<=end)
{
if(a[mid]<num)
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
beg=mid+1;
else if(a[mid]==num)
{
cout<<"\nThe element is found at position:"<<mid+1;
break;
}
else
end=mid-1;
mid=(beg+end)/2;
}
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-2
AIM:To implement following algorithms using array as a data structure and
analyse its time complexity.

a. MERGE SORT

PROGRAM CODE:
#include <iostream>
using namespace std;
void Merge(int *a, int low, int high, int mid)
{
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
clock_t t1,t2;
float t;
t1=clock();
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
else
{
temp[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
int main()
{
int n, i;
clock_t t1,t2;
float t;
t1=clock();
cout<<"______BHUMIKA_____\n";
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
cin>>arr[i];
}

MergeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;

return 0;
}

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
: OUTPUT

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
b. QUICK SORT:

PROGRAM CODE:
#include<iostream>
#include<cstdlib>
using namespace std;
// Swapping two values.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
index++;
}
}
swap(&a[pivot], &a[index]);
return index;
}
int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
pvt = low + n%(high-low+1);
swap(&a[high], &a[pvt]);
return Partition(a, low, high);
}
int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
pindex = RandomPivotPartition(a, low, high);
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
}
int main()
{
int n, i;
clock_t t1,t2;
float t;
t1=clock();
cout<<"______BHUMIKA______\n";
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
c. BUBBLE SORT:

PROGRAM CODE:
#include <iostream>
using namespace std;
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
}
}
int main()
{
int n, i;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
clock_t t1,t2;
float t;
t1=clock();
cout<<"______BHUMIKA_____\n";
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

BubbleSort(arr, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
d. SELECTION SORT:

PROGRAM CODE:
#include <iostream>
using namespace std;
void SelectionSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
if (arr[i] > arr[j])
{
arr[i] = arr[i]+arr[j];
arr[j] = arr[i]-arr[j];
arr[i] = arr[i]-arr[j];
}
}
}
}
int main()
{
int n, i;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
clock_t t1,t2;
float t;
t1=clock();
cout<<"______BHUMIKA______\n";
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
SelectionSort(arr, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
e. HEAP SORT:

PROGRAM CODE:
#include <iostream>
using namespace std;
void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
int main()
{
int n, i;
clock_t t1,t2;
float t;
t1=clock();

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
cout<<"_____BHUMIKA_____\n";
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
Build_MaxHeap(arr, n-1);
HeapSort(arr, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
PROGRAM-3
AIM: To implement Huffman Coding and analyse its time complexity.
PROGRAM CODE:
#include <bits/stdc++.h>
using namespace std;
struct MinHeapNode
{
char d;
unsigned frequency;
MinHeapNode *lChild, *rChild;
MinHeapNode(char d, unsigned frequency)
{
lChild = rChild = NULL;
this->d = d;
this->frequency = frequency;
}
};
struct compare
{
bool operator()(MinHeapNode *l, MinHeapNode *r)
{
return (l->frequency > r->frequency);
}
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
};
void printCodes(struct MinHeapNode *root, string str)
{
if (!root)
return;
if (root->d != '$')
cout << root->d << ": " << str << "\n";
printCodes(root->lChild, str + "0");
printCodes(root->rChild, str + "1");
}
void HuffmanCodes(char d[], int frequency[], int size)
{
struct MinHeapNode *lChild, *rChild, *top;
priority_queue<MinHeapNode *, vector<MinHeapNode *>, compare>
minHeap;
for (int i = 0; i < size; ++i)
minHeap.push(new MinHeapNode(d[i], frequency[i]));
while (minHeap.size() != 1)
{
lChild = minHeap.top();
minHeap.pop();
rChild = minHeap.top();
minHeap.pop();

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
top = new MinHeapNode('$', lChild->frequency + rChild->frequency);
top->lChild = lChild;
top->rChild = rChild;
minHeap.push(top);
}
printCodes(minHeap.top(), "");
}
int main()
{
int i,n,freq[10];
char arr[10];;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA____\n";
cout<<"\nEnter number of alphabets for Huffman Coding:";
cin>>n;
cout<<"\nEnter alphabets:";
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"\nEnter frequency for each alphabet:";
for(i=0;i<n;i++)
cin>>freq[i];
HuffmanCodes(arr,freq,n);

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-4
AIM: To implement Minimum Spanning Tree and analyse its time complexity.
PROGRAM CODE:
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
typedef pair<int, int> pii;
vector<vector<pii>> adj;
vector<bool> visited;
int prim(int start) {
int n = adj.size();
int mstWeight = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push(make_pair(0, start));
while (!pq.empty()) {
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if (visited[u]) continue;
visited[u] = true;
mstWeight += w;
for (auto& v : adj[u]) {
if (!visited[v.first]) {
pq.push(make_pair(v.second, v.first));
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
}
}
}
return mstWeight;
}
int main() {
adj.resize(5);
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
adj[0].push_back(make_pair(1, 2));
adj[1].push_back(make_pair(0, 2));
adj[0].push_back(make_pair(3, 6));
adj[3].push_back(make_pair(0, 6));
adj[1].push_back(make_pair(2, 3));
adj[2].push_back(make_pair(1, 3));
adj[1].push_back(make_pair(3, 8));
adj[3].push_back(make_pair(1, 8));
adj[1].push_back(make_pair(4, 5));
adj[4].push_back(make_pair(1, 5));
adj[2].push_back(make_pair(4, 7));
adj[4].push_back(make_pair(2, 7));

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
adj[3].push_back(make_pair(4, 9));
adj[4].push_back(make_pair(3, 9));
visited.resize(5, false);
int mstWeight = prim(0);
cout << "Minimum spanning tree weight: " << mstWeight << endl;
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-5
AIM: To implement Dijkstra’s algorithm and analyse its time complexity.
PROGRAM CODE:
#include<iostream>
#include<climits>
using namespace std;
int minimumDist(int dist[], bool Tset[])
{
int min=INT_MAX,index;
for(int i=0;i<6;i++)
{
if(Tset[i]==false && dist[i]<=min)
{
min=dist[i];
index=i;
}
}
return index;
}
void Dijkstra(int graph[6][6],int src)
{
int dist[6];
bool Tset[6];
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
for(int i = 0; i<6; i++)
{
dist[i] = INT_MAX;
Tset[i] = false;
}
dist[src] = 0;
for(int i = 0; i<6; i++)
{
int m=minimumDist(dist,Tset);
Tset[m]=true;
for(int i = 0; i<6; i++)
{
dist[i]=dist[m]+graph[m][i];
}
}
cout<<"Vertex\t\tDistance from source"<<endl;
for(int i = 0; i<6; i++)
{
char str=65+i;
cout<<str<<"\t\t\t"<<dist[i]<<endl;
}
}
int main()
{

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA______\n";
int graph[6][6]={
{0, 10, 20, 0, 0, 0},
{10, 0, 0, 50, 10, 0},
{20, 0, 0, 20, 33, 0},
{0, 50, 20, 0, 20, 2},
{0, 10, 33, 20, 0, 1},
{0, 0, 0, 2, 1, 0}};
Dijkstra(graph,0);
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
PROGRAM-6
AIM: To implement Bellman Ford algorithm and analyse its time complexity.
PROGRAM CODE:
#include <bits/stdc++.h>
using namespace std;
void BellmanFord(int graph[][3], int V, int E,int src)
{
int dis[V];
for (int i = 0; i < V; i++)
dis[i] = INT_MAX;
dis[src] = 0;
for (int i = 0; i < V - 1; i++) {
for (int j = 0; j < E; j++) {
if (dis[graph[j][0]] != INT_MAX && dis[graph[j][0]]
+ dis[j][2]< dis[graph[j][1]])
dis[graph[j][1]] = dis[graph[j][0]] + graph[j][2];
}
}
for (int i = 0; i < E; i++) {
int x = graph[i][0];
int y = graph[i][1];
int weight = graph[i][2];
if (dis[x] != INT_MAX &&
dis[x] + weight < dis[y])
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
cout << "Graph contains negative"
" weight cycle"
<< endl;
}

cout << "Vertex Distance from Source" << endl;


for (int i = 0; i < V; i++)
cout << i << "\t\t" << dis[i] << endl;
}
int main()
{
int V = 5;
int E = 8;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
int graph[][3] = { { 0, 1, -1 }, { 0, 2, 4 },
{ 1, 2, 3 }, { 1, 3, 2 },
{ 1, 4, 2 }, { 3, 2, 5 },
{ 3, 1, 1 }, { 4, 3, -3 } };

BellmanFord(graph, V, E, 0);
t2=clock();

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-7
AIM: To implement N Queen’s problem using backtracking and analyse its time
complexity.

PROGRAM CODE:
#include <bits/stdc++.h>
#define N 4
using namespace std;
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
if(board[i][j])
cout << "Q ";
else cout<<". ";
printf("\n");
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;

return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0;
}
}
return false;
}
bool solveNQ()

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

if (solveNQUtil(board, 0) == false) {
cout << "Solution does not exist";
return false;
}
printSolution(board);
return true;
}
int main()
{
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
solveNQ();
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM -8
AIM: To implement Matrix Multiplication and analyse its time complexity.
PROGRAM CODE:
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
PROGRAM-9
AIM: To implement Longest Common Subsequence problem and analyse its
time complexity.

PROGRAM CODE:
#include<iostream>
#include<string>
using namespace std;
int longestCommonSubsequece(string str1, string str2, int len1, int len2)
{
int i, j;
int LCS[len1+1][len2+1];
for(i=0;i<=len1;i++)
LCS[i][0]=0;

for(j=0;j<=len2;j++)
LCS[0][j]=0;
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
{
LCS[i][j]=1+LCS[i-1][j-1];
}

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
else
{
LCS[i][j]=max(LCS[i-1][j],LCS[i][j-1]);
}
}
}
return LCS[len1][len2];

}
int main()
{
string str1,str2;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
cout<<"Enter first string ";
getline(cin, str1);
cout<<"Enter second string ";
getline(cin, str2);
int len1=str1.length();
int len2=str2.length();
cout<<"Length of longest common subsequence is
"<<longestCommonSubsequece(str1,str2,len1,len2);

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
cout<<endl;
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-10
AIM: To implement naïve String Matching algorithm,Rabin Karp algorithm and
Knuth Morris Pratt algorithm and analyse its time complexity.

PROGRAM CODE:
@ NAÏVE STRING MATCHING ALGORITHM:
#include <bits/stdc++.h>
using namespace std;
void search(string& pat, string txt)
{
int M = pat.size();
int N = txt.size();
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j
== M)
cout << "Pattern found at index " << i << endl;
}
}

int main()
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
{
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
string txt = "AABAACAADAABAAABAA";
string pat = "AABA";
search(pat, txt);
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
@RABIN KARP ALGORITHM:
#include <string.h>
#include <iostream>
using namespace std;
#define d 10
void rabinKarp(char pattern[], char text[], int q) {
int m = strlen(pattern);
int n = strlen(text);
int i, j;
int p = 0;
int t = 0;
int h = 1;

for (i = 0; i < m - 1; i++)


h = (h * d) % q;
for (i = 0; i < m; i++) {
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}
for (i = 0; i <= n - m; i++) {
if (p == t) {
for (j = 0; j < m; j++) {
if (text[i + j] != pattern[j])
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
break;
}
if (j == m)
cout << "Pattern is found at position: " << i + 1 << endl;
}
if (i < n - m) {
t = (d * (t - text[i] * h) + text[i + m]) % q;
if (t < 0)
t = (t + q);
}
}
}
int main() {
char text[] = "ABCCDDAEFG";
char pattern[] = "CDD";
int q = 13;
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
rabinKarp(pattern, text, q);
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
@KNUTH MORRIS PRATT ALGORITHM:
#include<iostream>
#include<cstring>
using namespace std;
void computeLPSArray(char* pat, int M, int* lps);
void KMPSearch(char* pat, char* txt)
{
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
computeLPSArray(pat, M, lps);

int i = 0;
int j = 0;
while ((N - i) >= (M - j)) {
if (pat[j] == txt[i]) {
j++;
i++;
}

if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(char* pat, int M, int* lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
}
else
{
lps[i] = 0;
i++;
}
}
}
}
int main()
{
clock_t t1,t2;
float t;
t1=clock();
cout<<"_____BHUMIKA_____\n";
char txt[] = "ABABDABACDABABCABAB";
char pat[] = "ABABCABAB";
KMPSearch(pat, txt);
t2=clock();
t=(float)(t2-t1)/CLOCKS_PER_SEC;
cout<<"\n\nTime Complexity:"<<t;
return 0;
}

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida
PROGRAM-11
AIM: To implement Sorting Network.
PROGRAM CODE:
#include<iostream>
using namespace std;
void brickSort(int arr[], int n){
bool flag = false;
while(!flag){
flag = true;
for(int i = 1; i<n-1; i= i+2){
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = false;
}
}
for(int i = 0; i<n-1; i= i+2){
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = false;
}
}
}
}
Department of Computer Science and Engineering
Delhi Technical Campus,Greater Noida
main() {
cout<<"_____BHUMIKA______\n";
int data[] = {54, 74, 98, 154, 98, 32, 20, 13, 35, 40};
int n = sizeof(data)/sizeof(data[0]);
cout << "Sorted Sequence ";
brickSort(data, n);
for(int i = 0; i <n;i++){
cout << data[i] << " ";
}
}

OUTPUT:

Department of Computer Science and Engineering


Delhi Technical Campus,Greater Noida

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy