Ads 1
Ads 1
Sample Programs:
4. Implement BFT and DFT for given graph, when graph is represented by
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
class GraphMatrix {
private:
int vertices;
vector<vector<int>> adjMatrix;
public:
GraphMatrix(int v) : vertices(v) {
adjMatrix[src][dest] = 1;
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
q.pop();
for (int i = 0; i < vertices; i++) {
visited[i] = true;
q.push(i);
stack<int> s;
s.push(start);
while (!s.empty()) {
s.pop();
if (!visited[vertex]) {
visited[vertex] = true;
s.push(i);
};
int main() {
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.BFT(0);
g.DFT(0);
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
class GraphList {
private:
int vertices;
vector<vector<int>> adjList;
public:
GraphList(int v) : vertices(v) {
adjList.resize(v);
adjList[src].push_back(dest);
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
q.pop();
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
stack<int> s;
s.push(start);
while (!s.empty()) {
s.pop();
if (!visited[vertex]) {
visited[vertex] = true;
if (!visited[*it]) {
s.push(*it);
};
int main() {
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.BFT(0);
g.DFT(0);
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <stack>
#include <utility>
class Graph {
int vertices;
vector<vector<int>> adjList;
vector<bool> visited;
int time;
void BCCUtil(int u) {
visited[u] = true;
int children = 0;
if (!visited[v]) {
children++;
parent[v] = u;
st.push({u, v});
BCCUtil(v);
cout << "(" << st.top().first << ", " << st.top().second << ") ";
st.pop();
cout << "(" << st.top().first << ", " << st.top().second << ")\n";
st.pop();
}
st.push({u, v});
public:
Graph(int v) : vertices(v) {
adjList.resize(v);
disc.resize(v, -1);
low.resize(v, -1);
parent.resize(v, -1);
visited.resize(v, false);
adjList[u].push_back(v);
adjList[v].push_back(u);
void findBCC() {
if (!visited[i]) {
BCCUtil(i);
while (!st.empty()) {
isComponent = true;
cout << "(" << st.top().first << ", " << st.top().second << ") ";
st.pop();
};
int main() {
Graph g(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(3, 4);
g.findBCC();
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
int i = low - 1;
i++;
swap(arr[i], arr[j]);
return i + 1;
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
cout << "Quick Sort Execution Time: " << duration << " microseconds" << endl;
cout << "Merge Sort Execution Time: " << duration << " microseconds" << endl;
int main() {
vector<int> sizes = {1000, 5000, 10000, 50000, 100000}; // Various input sizes for testing
vector<int> arr(size);
measureQuickSort(arr);
measureMergeSort(arr);
return 0;
OUTPUT :
...
7. Compare the performance of Single Source Shortest Paths using Greedy method when
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <chrono>
int V = graph.size();
dist[src] = 0;
int u = -1;
u = i;
visited[u] = true;
if (graph[u][v] && !visited[v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v])
int V = adjList.size();
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
pq.push({dist[v], v});
int main() {
graph[i][j] = weight;
graph[j][i] = weight;
adjList[i].push_back({j, graph[i][j]});
dijkstraMatrix(graph, src);
cout << "Adjacency Matrix Execution Time: " << durationMatrix << " ms" << endl;
dijkstraList(adjList, src);
cout << "Adjacency List Execution Time: " << durationList << " ms" << endl;
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <algorithm>
struct Job {
};
int totalProfit = 0;
vector<int> jobSequence;
// Find a free slot for this job (starting from the last possible slot)
int main() {
vector<Job> jobs = {
{1, 2, 100},
{2, 1, 19},
{3, 2, 27},
{4, 1, 25},
{5, 3, 15}
};
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <algorithm>
// Create a 2D vector to store the maximum value at each (item, weight) combination
if (weights[i - 1] <= w) {
} else {
// Return the maximum value that can be achieved with the given capacity
return dp[n][W];
int main() {
cout << "Maximum value that can be achieved: " << maxValue << endl;
return 0;
OUTPUT :
#include <iostream>
#include <vector>
if (board[i][col] == 1)
return false;
if (board[i][j] == 1)
return false;
if (board[i][j] == 1)
return false;
return true;
if (row >= N)
return true;
board[row][col] = 1;
return true;
board[row][col] = 0;
void solveNQueens(int N) {
if (solveNQueensUtil(board, 0, N))
printSolution(board);
else
int main() {
int N;
cin >> N;
return 0;
OUTPUT:
Q . . .
. . Q .
. . . Q
. Q . .
11. Use Backtracking strategy to solve 0/1 Knapsack problem.
#include <iostream>
#include <vector>
if (n == 0 || W == 0) {
return 0;
if (weights[n - 1] > W) {
} else {
int main() {
cout << "Maximum value that can be achieved: " << maxValue << endl;
return 0;
OUTPUT :
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
// Number of cities
#define N 4
int tsp(int graph[N][N], int path[], int pos, int n, int count, int cost, int ans) {
// Mark as visited
path[i] = count;
// Backtrack
path[i] = -1;
return ans;
int main() {
// Example graph represented as an adjacency matrix
int graph[N][N] = {
};
int path[N];
return 0;
OUTPUT :
Minimum cost: 80