0% found this document useful (0 votes)
2 views

PROGRAM 01_ (1)

The document contains two C++ programs. The first program implements Dijkstra's algorithm to find the shortest paths from a source vertex in a weighted graph, while the second program computes the longest common subsequence (LCS) between two strings. Both programs measure and output their execution time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PROGRAM 01_ (1)

The document contains two C++ programs. The first program implements Dijkstra's algorithm to find the shortest paths from a source vertex in a weighted graph, while the second program computes the longest common subsequence (LCS) between two strings. Both programs measure and output their execution time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM 01:

#include <bits/stdc++.h>
using namespace std;

struct Edge {
int vertex;
int weight;
};

struct Compare {
bool operator()(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
};

void dijkstra(int source, const vector<vector<Edge>>& graph) {


int n = graph.size();
vector<int> dist(n, INT_MAX);
dist[source] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq;


pq.push({source, 0});

int totalMinCost = 0;

while (!pq.empty()) {
int u = pq.top().first;
int d = pq.top().second;
pq.pop();
for (const auto& edge : graph[u]) {
int v = edge.vertex;
int weight = edge.weight;

if (dist[v] > d + weight) {


dist[v] = d + weight;
pq.push({v, dist[v]});
}
}
}

for (int i = 0; i < n; ++i) {


if (dist[i] != INT_MAX) {
totalMinCost += dist[i];
}
}

cout << "Vertex\tDistance from Source\n";


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

cout << "Total Minimum Cost: " << totalMinCost << "\n";
}

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

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


cin >> m;

vector<vector<Edge>> graph(n);

cout << "Enter the edges in the format: u v w\n";


for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}

int source;
cout << "Enter the source vertex: ";
cin >> source;
auto startTime = chrono::high_resolution_clock::now();

dijkstra(source, graph);

auto endTime = chrono::high_resolution_clock::now();

auto duration = chrono::duration_cast<chrono::microseconds>(endTime - startTime);

cout << "Execution Time: " << duration.count() << " microseconds\n";

return 0;
}

OUTPUT:
PROGRAM 02:
#include <bits/stdc++.h>
using namespace std;

pair<int, string> LCSub(const string& A, const string& B) {


int m = A.size();
int n = B.size();
vector<vector<int>> LCS(m + 1, vector<int>(n + 1, 0));

for (int i = 1; i <= m; ++i) {


for (int j = 1; j <= n; ++j) {
if (A[i - 1] == B[j - 1]) {
LCS[i][j] = 1 + LCS[i - 1][j - 1];
} else {
LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
}

string lcsSequence = "";


int i = m, j = n;
while (i > 0 && j > 0) {
if (A[i - 1] == B[j - 1]) {
lcsSequence = A[i - 1] + lcsSequence;
i--;
j--;
} else if (LCS[i - 1][j] > LCS[i][j - 1]) {
i--;
} else {
j--;
}
}

return {LCS[m][n], lcsSequence};


}
int main() {
string A = "lullabybabies";
string B = "skullandbones";

auto startTime = chrono::high_resolution_clock::now();

pair<int, string> result = LCSub(A, B);

auto endTime = chrono::high_resolution_clock::now();


auto duration = chrono::duration_cast<chrono::microseconds>(endTime - startTime);

cout << "Length of LCS: " << result.first << endl;


cout << "LCS: " << result.second << endl;
cout << "Execution Time: " << duration.count() << " ms\n";

return 0;
}

OUTPUT:

You might also like