You are on page 1of 10

PROGRAM NO : 09

Program Objective : Write a program to Compute a Longest Common Subsequence


for Multiple Sequences and analyze it’s time complexity
Program in C++ :
#include <iostream>
using namespace std;
void lcsAlgo(string S1, string S2, int m, int n)
{
int LCS_table[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
LCS_table[i][j] = 0;
else if (S1[i - 1] == S2[j - 1])
LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;
else
LCS_table[i][j] = max(LCS_table[i - 1][j], LCS_table[i][j - 1]);
}
}
int ind = LCS_table[m][n];
char lcsAlgo[ind + 1];
lcsAlgo[ind] = '\0';

int i = m, j = n;
while (i > 0 && j > 0)
{
if (S1[i - 1] == S2[j - 1])
{
lcsAlgo[ind - 1] = S1[i - 1];
i--;
j--;
ind--;
}

else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])


i--;
else
j--;
}
Nikhil Kumar 08215603120 T-7
cout <<"Longest Common Subsequence For Given Sequences is : " << lcsAlgo <<
"\n";
}

int main()
{
string S1,S2;
cout<<"\n!!! Computing a Longest Common Subsequence for Two Sequences
!!!\n";
cout<<"\t Enter First Sequence : ";
cin>>S1;
cout<<"\t Enter Second Sequence : ";
cin>>S2;
int m = S1.length();
int n = S2.length();
lcsAlgo(S1, S2, m, n);
}
Output :

Time Complexity:
1. Best Case: O(n*m)

2. Worst Case: O(n*m)

3. Average Case : O(n*m)

Since we are using two for loops for both the strings ,therefore the time complexity of
finding the longest common subsequence using dynamic programming approach
is O(n * m) where n and m are the lengths of the strings.

Nikhil Kumar 08215603120 T-7


PROGRAM NO : 10
Program Objective : Write a program to implement Radix Sort using array as a data
structure and analyze it’s time complexity.
Program in C++ :
#include <iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[10] = {0};
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
int main()
{
int n ;
cout <<"Enter the Number Of Elements : ";
cin>>n;
Nikhil Kumar 08215603120 T-7
int arr[n];
for( int i = 0 ; i < n; i++){
cout<<"Enter Element "<<i+1<<" : ";
cin>> arr[i];
}
cout<<"Sorting the Array using Radix Sort Method !!!\n";
cout<<"The Unsorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
radixsort(arr, n);
cout<<"\nThe Sorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
return 0;
}
Output :

Time Complexity:
1. Best Case: O(n+k)

2. Worst Case: O(n+k)

3. Average Case : O(n+k)


Since radix sort is a non-comparative algorithm, it has advantages over comparative
sorting algorithms . For the radix sort that uses counting sort as an intermediate stable
sort, the time complexity is O(d( n +k )).Here , d is the number cycle and isO( n + k )
the time complexity of counting sort .Thus, radix sort has linear time complexity
which is better than O(n logn) of comparative sorting algorithms.

Nikhil Kumar 08215603120 T-7


PROGRAM NO : 11
Program Objective : Write a program to Perform Floyed warshall's Algorithm
Program and analyze it’s time complexity.
Program in C++ :
#include<iostream>u
singnamespa std;
#define nV 4
#define INF 999
void printMatrix(int matrix[][nV])
{
for (int i = 0; i < nV; i++)
{
for (int j = 0; j < nV; j++)
{
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}
void floydWarshall(int graph[][nV])
{
int matrix[nV][nV], i, j, k;

for (i = 0; i < nV; i++)


for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];
for (k = 0; k < nV; k++)
{
for (i = 0; i < nV; i++)
{
for (j = 0; j < nV; j++)
{
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printMatrix(matrix);
Nikhil Kumar 08215603120 T-7
}

Nikhil Kumar 08215603120 T-7


int main()
{
cout<<"Performing Floyed warshall's algorithm Program on : \n";
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
printMatrix(graph);
cout<<"Shortest Path Between all the pairs of Vertices are : \n";
floydWarshall(graph);
}
Output :

Time Complexity:

There are three loops. Each loop has constant complexities. So, the time complexity of
the Floyd-Warshall algorithm is O(n3).
SO,
1. Best Case: O(n3)

2. Worst Case: O(n3)

3. Average Case : O(n3)

Nikhil Kumar 08215603120 T-7


PROGRAM NO : 12
Program Objective : Write a program to Perform Dijkstra’s Algorithm Program and
analyze it’s time complexity.
Program in C++ :
#include <iostream>
using namespace std;
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printSolution(int dist[])
{
cout << "Vertex \t Distance from Source" << endl;
for (int i = 0; i < V; i++)
cout << i << " \t\t" << dist[i] << endl;
}
void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}

Nikhil Kumar 08215603120 T-7


int main()
{

int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},


{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0);
return 0;
}
Output :

Time Complexity:

1. Best Case: Based On the Graph the time complexity for best case is calculated.

2. Worst Case: Time Complexity of Dijkstra's Algorithm is O(V2) but with min-
priority queue it drops down to O(V+ElogV).

3. Average Case : O(V2)

Nikhil Kumar 08215603120 T-7


Nikhil Kumar 08215603120 T-7

You might also like