0% found this document useful (0 votes)
14 views12 pages

Daa 5-10

The document contains multiple C++ experiments demonstrating various algorithms, including Dijkstra's shortest path algorithm, matrix chain multiplication, longest common subsequence, naive string matching, Strassen's matrix multiplication, and bitonic sort. Each experiment includes code snippets, input prompts, and example outputs. The algorithms cover topics in graph theory, dynamic programming, string processing, and matrix operations.

Uploaded by

riddhiybansal04
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)
14 views12 pages

Daa 5-10

The document contains multiple C++ experiments demonstrating various algorithms, including Dijkstra's shortest path algorithm, matrix chain multiplication, longest common subsequence, naive string matching, Strassen's matrix multiplication, and bitonic sort. Each experiment includes code snippets, input prompts, and example outputs. The algorithms cover topics in graph theory, dynamic programming, string processing, and matrix operations.

Uploaded by

riddhiybansal04
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/ 12

Experiment-5

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

// Function to nd the vertex with the minimum distance value


int minDistance(const vector<int>& dist, const vector<bool>& sptSet, int V) {
int min = INT_MAX, min_index;

for (int v = 0; v < V; v++) {


if (!sptSet[v] && dist[v] <= min) {
min = dist[v], min_index = v;
}
}
return min_index;
}

// Dijkstra's algorithm
void dijkstra(const vector<vector<int>>& graph, int src, int V) {
vector<int> dist(V, INT_MAX); // Distance from source to each vertex
vector<bool> sptSet(V, false); // Shortest path tree set (True if vertex is included)
dist[src] = 0; // Distance from source to itself is 0

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, sptSet, V); // Pick the vertex with the minimum
distance
sptSet[u] = true; // Mark the picked vertex as processed

// Update the dist value of the adjacent vertices of the picked vertex
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];
}
}
}

// Print the calculated shortest distances


cout << "Vertex\tDistance from Source\n";
for (int i = 0; i < V; i++) {
cout << i << "\t\t" << dist[i] << "\n";
}
}

int main() {
int V, E;
fi
cout << "Enter the number of vertices: ";
cin >> V;

vector<vector<int>> graph(V, vector<int>(V, 0));

cout << "Enter the number of edges: ";


cin >> E;

cout << "Enter the edges (format: u v weight), where u and v are the vertices (0-
based indexing):\n";
for (int i = 0; i < E; i++) {
int u, v, weight;
cin >> u >> v >> weight;
graph[u][v] = weight;
graph[v][u] = weight; // Assuming undirected graph
}

int source;
cout << "Enter the source vertex: ";
cin >> source;

dijkstra(graph, source, V);

return 0;
}

Input

Enter the number of vertices: 9


Enter the number of edges: 14
Enter the edges (format: u v weight), where u and v are the vertices (0-based
indexing):
014
078
128
1 7 11
237
254
282
349
3 5 14
4 5 10
562
671
686
787
Enter the source vertex: 0
Output

Vertex Distance from Source


0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
Experiment-6

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

// Function to nd the minimum number of multiplications needed for matrix chain


multiplication
int matrixChainMultiplication(const vector<int>& dims) {
int n = dims.size() - 1; // Number of matrices
vector<vector<int>> dp(n, vector<int>(n, 0)); // dp[i][j] will store the minimum number of
multiplications for matrices i to j

// L is the chain length


for (int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1; // The ending matrix index for the current chain

dp[i][j] = INT_MAX;

// Find the minimum cost for multiplying matrices i to j


for (int k = i; k < j; k++) {
int q = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * dims[j + 1];
if (q < dp[i][j]) {
dp[i][j] = q;
}
}
}
}

return dp[0][n - 1]; // Minimum cost for multiplying matrices from 1 to n


}

int main() {
int n;
cout << "Enter the number of matrices: ";
cin >> n;

vector<int> dims(n + 1);

cout << "Enter the dimensions of the matrices (size of matrix i is dims[i-1] x dims[i]):\n";
for (int i = 0; i <= n; i++) {
cin >> dims[i];
}

int result = matrixChainMultiplication(dims);

cout << "Minimum number of multiplications: " << result << endl;
return 0;
}
fi
Input

Enter the number of matrices: 4


Enter the dimensions of the matrices (size of matrix i is dims[i-1] x dims[i]):
10 20 30 40 30

Output

Minimum number of multiplications: 18000


Experiment-7

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// Function to nd the length of the longest common subsequence


int LCS(const string& str1, const string& str2) {
int m = str1.length();
int n = str2.length();

// Create a 2D DP table to store lengths of LCS of substrings


vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

// Build the DP table


for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; // If characters match, LCS length increases
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // Otherwise, take the max from the
previous values
}
}
}

// Return the length of the LCS


return dp[m][n];
}

int main() {
string str1, str2;

cout << "Enter the rst string: ";


cin >> str1;
cout << "Enter the second string: ";
cin >> str2;

int result = LCS(str1, str2);

cout << "Length of Longest Common Subsequence: " << result << endl;

return 0;
}

Output
Enter the rst string: ABCDGH
Enter the second string: AEDFHR

Length of Longest Common Subsequence: 3


fi
fi
fi
Experiment-8

#include <iostream>
#include <string>
using namespace std;

// Function to implement Naive String Matching


void naiveStringMatching(const string& text, const string& pattern) {
int n = text.length();
int m = pattern.length();

// Loop to slide the pattern over the text


for (int i = 0; i <= n - m; i++) {
int j = 0;
while (j < m && text[i + j] == pattern[j]) {
j++;
}
// If full pattern is found, print the index
if (j == m) {
cout << "Pattern found at index " << i << endl;
}
}
}

int main() {
string text, pattern;

// Input the text and pattern from the user


cout << "Enter text: ";
cin >> text;
cout << "Enter pattern: ";
cin >> pattern;

// Call the naive string matching function


naiveStringMatching(text, pattern);

return 0;
}

Output

Enter text: AABAACAADAABAABA


Enter pattern: AABA

Pattern found at index 0


Pattern found at index 9
Pattern found at index 12
Pattern found at index 17
Experiment-9

#include <iostream>
#include <vector>

using namespace std;

typedef vector<vector<int>> Matrix;

// Function to add two matrices


Matrix add(const Matrix& A, const Matrix& B) {
int n = A.size();
Matrix C(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}

// Function to subtract two matrices


Matrix subtract(const Matrix& A, const Matrix& B) {
int n = A.size();
Matrix C(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
C[i][j] = A[i][j] - B[i][j];
}
}
return C;
}

// Function to multiply matrices using Strassen's algorithm


Matrix strassen(const Matrix& A, const Matrix& B) {
int n = A.size();

// Base case for recursion


if (n == 1) {
Matrix result(1, vector<int>(1));
result[0][0] = A[0][0] * B[0][0];
return result;
}

int newSize = n / 2;
Matrix A11(newSize, vector<int>(newSize)), A12(newSize, vector<int>(newSize)),
A21(newSize, vector<int>(newSize)), A22(newSize, vector<int>(newSize));
Matrix B11(newSize, vector<int>(newSize)), B12(newSize, vector<int>(newSize)),
B21(newSize, vector<int>(newSize)), B22(newSize, vector<int>(newSize));

// Dividing matrices into sub-matrices


for (int i = 0; i < newSize; ++i) {
for (int j = 0; j < newSize; ++j) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + newSize];
A21[i][j] = A[i + newSize][j];
A22[i][j] = A[i + newSize][j + newSize];

B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
}
}

// Calculating the 7 products using Strassen's formula


Matrix M1 = strassen(add(A11, A22), add(B11, B22));
Matrix M2 = strassen(add(A21, A22), B11);
Matrix M3 = strassen(A11, subtract(B12, B22));
Matrix M4 = strassen(A22, subtract(B21, B11));
Matrix M5 = strassen(add(A11, A12), B22);
Matrix M6 = strassen(subtract(A21, A11), add(B11, B12));
Matrix M7 = strassen(subtract(A12, A22), add(B21, B22));

// Computing the nal result matrix


Matrix C(n, vector<int>(n));
Matrix C11 = add(subtract(add(M1, M4), M5), M7);
Matrix C12 = add(M3, M5);
Matrix C21 = add(M2, M4);
Matrix C22 = add(subtract(add(M1, M3), M2), M6);

// Combine the sub-matrices to form the result


for (int i = 0; i < newSize; ++i) {
for (int j = 0; j < newSize; ++j) {
C[i][j] = C11[i][j];
C[i][j + newSize] = C12[i][j];
C[i + newSize][j] = C21[i][j];
C[i + newSize][j + newSize] = C22[i][j];
}
}

return C;
}

// Function to print a matrix


void printMatrix(const Matrix& M) {
for (const auto& row : M) {
for (int elem : row) {
cout << elem << " ";
}
cout << endl;
}
}
fi
int main() {
int n;
cout << "Enter the size of the matrix (n x n): ";
cin >> n;

Matrix A(n, vector<int>(n)), B(n, vector<int>(n));

cout << "Enter elements of matrix A:\n";


for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> A[i][j];
}
}

cout << "Enter elements of matrix B:\n";


for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> B[i][j];
}
}

// Perform Strassen's matrix multiplication


Matrix C = strassen(A, B);

cout << "Resulting matrix C (A * B):\n";


printMatrix(C);

return 0;
}

Input
Enter the size of the matrix (n x n): 4
Enter elements of matrix A:
1234
5678
9 10 11 12
13 14 15 16

Enter elements of matrix B:


16 15 14 13
12 11 10 9
8765
4321

Output
Resulting matrix C (A * B):
80 70 60 50
240 214 188 162
400 358 316 274
560 502 444 386
Experiment-10

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Function to perform a swap if the elements are in the wrong order


void bitonicCompare(vector<int>& arr, int i, int j, bool ascending) {
if ((arr[i] > arr[j] && ascending) || (arr[i] < arr[j] && !ascending)) {
swap(arr[i], arr[j]);
}
}

// Bitonic merge function that recursively merges two bitonic sequences


void bitonicMerge(vector<int>& arr, int low, int cnt, bool ascending) {
if (cnt > 1) {
int mid = cnt / 2;
for (int i = low; i < low + mid; ++i) {
bitonicCompare(arr, i, i + mid, ascending);
}
bitonicMerge(arr, low, mid, ascending);
bitonicMerge(arr, low + mid, mid, ascending);
}
}

// Function to sort the entire sequence using the bitonic sort network
void bitonicSort(vector<int>& arr, int low, int cnt, bool ascending) {
if (cnt > 1) {
int mid = cnt / 2;

// Sorting the rst half in ascending order


bitonicSort(arr, low, mid, true);

// Sorting the second half in descending order


bitonicSort(arr, low + mid, mid, false);

// Merging both halves


bitonicMerge(arr, low, cnt, ascending);
}
}

// Function to print the array


void printArray(const vector<int>& arr) {
for (int i = 0; i < arr.size(); ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
fi
int main() {
int n;

cout << "Enter the number of elements: ";


cin >> n;

vector<int> arr(n);

cout << "Enter the elements: ";


for (int i = 0; i < n; ++i) {
cin >> arr[i];
}

cout << "Original Array: ";


printArray(arr);

// Perform bitonic sort


bitonicSort(arr, 0, n, true); // True indicates sorting in ascending order

cout << "Sorted Array: ";


printArray(arr);

return 0;
}

Input

Enter the number of elements: 8


Enter the elements: 3 7 4 8 2 5 1 6

Output

Original Array: 3 7 4 8 2 5 1 6
Sorted Array: 1 2 3 4 5 6 7 8

You might also like