Professional Documents
Culture Documents
PRATICAL FILE
Enrol : 0901IO201051
2
CONTENT
S.NO PAGE NO. REMARK
EXPERIMENT
WAP to implement the following using array as 3-20
1 data structure and analyse its time complexity.
a. Insertion sort b. Selection sort c. Bubble sort
d. Quick sort e. Merge sort f. Bucket sort g.
Shell sort h. Radix sort i. Heap sort
Write a Program to implement Linear and 21-22
2 Binary Search and analyse its time complexity.
Write a Program to implement Matrix Chain 23-24
3 Multiplication and analyses its time complexity.
Write a Program to implement Longest 25-26
4 Common Subsequence Problem and analyse its
time complexity.
Write a Program to implement Optimal Binary 27-28
5 Search Tree Problem and analyse its time
complexity.
Write a Program to implement Huffman Coding 29-33
6 and analyse its time complexity.
Write a Program to implement Dijkstra’s 34-36
7 Algorithm and analyse its time complexity.
Write a Program to implement Bellman Ford 37-40
8 Algorithm and analyses its time complexity.
Write a Program to implement DFS and BFS and 41-45
9 analyse their time complexities.
Write a Program to Implement 0/1 knapsack 46-47
10 using dynamic programming.
3
PROGRAM 1:
Write a Program to implement the following using array as data
structure and analyse its time complexity.
a. Insertion sort
CODE :
// C++ program for insertion sort
#include <bits/stdc++.h>
using namespace std;
/* Driver code */
int main()
{
int arr[] = { 9, 21, 51, 12, 90 };
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"After applying Insertion sort : ";
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT :
b. Selection sort
CODE:
// C++ program for implementation of selection sort
#include <bits/stdc++.h>
using namespace std;
int i, j, min_idx;
OUTPUT:
6
c. Bubble sort
CODE:
// C++ program for implementation of Bubble sort
#include <bits/stdc++.h>
*xp = *yp;
*yp = temp;
int i, j;
swap(&arr[j], &arr[j+1]);
int i;
// Driver code
int main()
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
8
OUTPUT:
Worst and Average Case Time Complexity: O(n*n). Worst case occurs
when array is reverse sorted.
Best Case Time Complexity: O(n). Best case occurs when array is
already sorted.
Auxiliary Space: O(1)
d. Quick sort
CODE:
/* C++ implementation of QuickSort */
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
int arr[] = {10, 97, 38, 79, 12, 35};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "After applying Quick sort then Sorted array: \n";
printArray(arr, n);
return 0;
10
OUTPUT:
Analysis of QuickSort
Time taken by QuickSort, in general, can be written as following.
T(n) = T(k) + T(n-k-1) + (n)
Worst Case:
Best Case:
T(n) = 2T(n/2) + (n)
Average Case:
T(n) = T(n/9) + T(9n/10) + (n)
Solution of above recurrence is also O(nLogn)
e. Merge sort
CODE:
// C++ program for Merge Sort
#include <iostream>
using namespace std;
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
for (auto i = 0; i < size; i++)
cout << A[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
auto arr_size = sizeof(arr) / sizeof(arr[0]);
OUTPUT:
f. Bucket sort
CODE:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
b[bi].push_back(arr[i]);
}
OUTPUT:
The average time complexity for Bucket Sort is O(n + k). The worst time
complexity is O(n²). The space complexity for Bucket Sort is O(n+k).
15
g. Shell sort
CODE:
// C++ implementation of Shell Sort
#include <iostream>
using namespace std;
int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
shellSort(arr, n);
return 0;
}
OUTPUT:
h. Radix sort
CODE:
// C++ implementation of Radix Sort
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"sorted Array :"<<endl;
// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
OUTPUT:
Time complexity best case Ω(N k), Average case Θ(N k),
worst case O(N k), worst spaces O(N + k)
19
i. Heap sort
CODE:
// C++ program for implementation of Heap Sort
#include <iostream>
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
OUTPUT:
PROGRAM 2:
Write a Program to implement Linear and Binary Search and
analyse its time complexity.
CODE:
#include <iostream>
using namespace std;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x ;
cout<<"Enter the NUMBER you want to search : ";
cin>>x;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, n, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "\n here we use linear search Element is present at index " << result;
OUTPUT:
The time complexity of linear search is O(N) while binary search has O(log2N).
The best case time in linear search is for the first element i.e., O(1). As against,
in binary search, it is for the middle element, i.e., O(1).
23
PROGRAM 3:
Write a Program to implement Matrix Chain Multiplication and
analyses its time complexity.
CODE:
#include <bits/stdc++.h>
using namespace std;
int m[n][n];
int i, j, k, L, q;
// L is chain length.
for (L = 2; L < n; L++)
{
for (i = 1; i < n - L + 1; i++)
{
j = i + L - 1;
m[i][j] = INT_MAX;
for (k = i; k <= j - 1; k++)
{
// q = cost/scalar multiplications
q = m[i][k] + m[k + 1][j]
+ p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int size = sizeof(arr) / sizeof(arr[0]);
getchar();
return 0;
}
OUTPUT:
PROGRAM 4:
Write a Program to implement Longest Common Subsequence
Problem and analyse its time complexity.
CODE:
#include <bits/stdc++.h>
using namespace std;
/* Driver code */
int main()
{
char X[] = "RAHUL";
char Y[] = "KUSHWAH";
int m = strlen(X);
int n = strlen(Y);
return 0;
26
OUTPUT:
Time complexity of the above naive recursive approach is O(2^n) in worst case and
worst case happens when all characters of X and Y mismatch i.e., length of LCS is 0.
27
PROGRAM 5:
Write a Program to implement Optimal Binary Search Tree Problem
and analyse its time complexity.
CODE:
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
int keys[] = {19, 9, 10};
int freq[] = {3, 8, 5};
int n = sizeof(keys) / sizeof(keys[0]);
cout << "Cost of Optimal BST is "
<< optimalSearchTree(keys, freq, n);
return 0;
}
OUTPUT:
PROGRAM 6:
Write a Program to implement Huffman Coding and analyse its
time complexity.
CODE:
// Huffman Coding in C++
#include <iostream>
using namespace std;
#define MAX_TREE_HT 50
struct MinHNode {
unsigned freq;
char item;
struct MinHNode *left, *right;
};
struct MinH {
unsigned size;
unsigned capacity;
struct MinHNode **array;
};
return temp;
}
minHeap->capacity = capacity;
minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct
MinHNode *));
return minHeap;
}
// Swap function
void swapMinHNode(struct MinHNode **a, struct MinHNode **b) {
struct MinHNode *t = *a;
*a = *b;
*b = t;
}
// Heapify
void minHeapify(struct MinH *minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (smallest != idx) {
swapMinHNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
31
// Check if size if 1
int checkSizeOne(struct MinH *minHeap) {
return (minHeap->size == 1);
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
// Insertion
void insertMinHeap(struct MinH *minHeap, struct MinHNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
minHeap->array[i] = minHeapNode;
}
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHNode *buildHfTree(char item[], int freq[], int size) {
struct MinHNode *left, *right, *top;
struct MinH *minHeap = createAndBuildMinHeap(item, freq, size);
while (!checkSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printHCodes(struct MinHNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printHCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printHCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
33
// Wrapper function
void HuffmanCodes(char item[], int freq[], int size) {
struct MinHNode *root = buildHfTree(item, freq, size);
OUTPUT:
PROGRAM 7:
Write a Program to implement Dijkstra’s Algorithm and analyse its
time complexity.
CODE:
#include <iostream>
using namespace std;
#include <limits.h>
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
return min_index;
}
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
dijkstra(graph, 0);
return 0;
}
OUTPUT:
PROGRAM 8:
Write a Program to implement Bellman Ford Algorithm and
analyses its time complexity.
CODE:
// A C++ program for Bellman-Ford's single source
// shortest path algorithm.
#include <bits/stdc++.h>
printArr(dist, V);
return;
}
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
BellmanFord(graph, 0);
return 0;
}
OUTPUT:
PROGRAM 9:
Write a Program to implement DFS and BFS and analyse their time
complexities.
CODE:
// C++ program to print DFS traversal from
#include <bits/stdc++.h>
using namespace std;
void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
return 0;
}
AND
// Program to print BFS traversal from a given
// source vertex. BFS(int s) traverses vertices
// reachable from s.
#include<iostream>
#include <list>
list<int> *adj;
public:
Graph(int V); // Constructor
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
while(!queue.empty())
44
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
return 0;
}
OUTPUT:
45
The time complexity of DFS if the entire tree is traversed is O(V) where V is the number of nodes.
The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency
Matrix is used, where V stands for vertices and E stands for edges.
46
PROGRAM 10:
Write a Program to Implement 0/1 knapsack using dynamic
programming.
CODE:
// A dynamic programming based
// solution for 0-1 Knapsack problem
#include <bits/stdc++.h>
using namespace std;
return K[n][W];
}
// Driver Code
int main()
{
int val[] = { 60, 100, 120 };
int wt[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
return 0;
}
OUTPUT:
Complexity Analysis:
Time Complexity: O(N*W).
where ‘N’ is the number of weight element and ‘W’ is capacity. As for
every weight element we traverse through all weight capacities 1<=w<=W.
Auxiliary Space: O(N*W).
The use of 2-D array of size ‘N*W’