Id - 466
Id - 466
Id - 466
Submitted By :
Submitted To
Code :
#include <bits/stdc++.h>
#include <vector>
vector<int> sieveOfEratosthenes(int N)
{
vector<bool> isPrime(N + 1, true);
isPrime[0] = isPrime[1] = false;
vector<int> primes;
for (int i = 2; i <= N; i++)
{
if (isPrime[i])
{
primes.push_back(i);
}
}
return primes;
}
int main()
{
int N;
cin >> N;
return 0;
}
OUTPUT :
LAB : 07 (B)
Code :
#include <bits/stdc++.h>
#include <climits>
#include <vector>
int main()
{
// Example
int N = 4;
vector<int>dimensions = {1, 2, 3 ,4};
int min_cost = matrix_chain_multiplication(dimensions);
return 0;
}
OUTPUT :
LAB : 07 (C)
Code :
#include <bits/stdc++.h>
#include <algorithm>
int main() {
int n;
cin >> n;
int result = 0;
for (int i = 0; i < n; i++) {
int originalDigit = original[i] - '0';
int targetDigit = target[i] - '0';
return 0;
}
OUTPUT :
LAB : 07 (D)
Code :
#include <iostream>
bool buy(int x) {
if (x % 3 == 0) {
return true;
}
if (x % 7 == 0) {
return true;
}
return false;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
if (buy(x)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
OUTPUT :
LAB : 08 (A)
Code :
#include <bits/stdc++.h>
#include <vector>
#include <stack>
while (!nodeStack.empty()) {
int current = nodeStack.top();
cout << current << " ";
nodeStack.pop();
int main() {
int numVertices, numEdges;
cin >> numVertices >> numEdges;
vector<vector<int>> graph(numVertices);
int startNode;
cin >> startNode;
return 0;
}
LAB : 08 (B)
Code :
#include <bits/stdc++.h>
#include <vector>
#include <queue>
visited[startNode] = true;
nodeQueue.push(startNode);
while (!nodeQueue.empty()) {
int current = nodeQueue.front();
cout << current << " ";
nodeQueue.pop();
int main() {
int numVertices, numEdges;
cin >> numVertices >> numEdges;
vector<vector<int>> graph(numVertices);
return 0;
}
LAB : 08 (C)
Code :
#include <bits/stdc++.h>
color[pos] = c;
bool ans = true;
for(int i=0;i<V;i++){
if(G[pos][i]){
if(color[i] == -1)
ans &= colorGraph(G,color,i,1-c);
int pos = 0;
return colorGraph(G,color,pos,1);
int main()
{
int G[][V] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
};
LAB : 08 (D)
Code :
#include <iostream>
#include <vector>
#include <queue>
int ni = i + di;
int nj = j + dj;
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == '1' && !visited[i][j]) {
DFS(i, j, n, mat, visited);
count++;
}
}
}
return count;
}
int main() {
int n;
cin >> n;
char mat[MAX_N][MAX_N];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> mat[i][j];
}
}
return 0;
}
OUTPUT :
LAB : 09 (A)
Code :
#include <iostream>
#include <vector>
#include <climits>
key[0] = 0;
int cost = 0;
mstSet[u] = true;
return cost;
}
int main() {
int V, E;
cin >> V >> E;
return 0;
}
OUTPUT :
LAB : 09 (B)
Code :
#include <iostream>
#include <vector>
#include <algorithm>
struct Edge {
int u, v, weight;
};
if (rootX != rootY) {
if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else {
parent[rootX] = rootY;
rank[rootY]++;
}
}
}
int mstWeight = 0;
for (const Edge& edge : edges) {
int rootU = findParent(edge.u, parent);
int rootV = findParent(edge.v, parent);
if (rootU != rootV) {
cout << edge.u << " - " << edge.v << " Weight: " << edge.weight << endl;
mstWeight += edge.weight;
unionSets(rootU, rootV, parent, rank);
}
}
cout << "Total MST Weight: " << mstWeight << endl;
}
int main() {
int V, E;
cin >> V >> E;
vector<Edge> edges(E);
for (int i = 0; i < E; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].weight;
}
kruskalMST(edges, V);
return 0;
}
OUTPUT :
LAB : 09 (C)
Code :
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
if (rootX != rootY) {
if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else {
parent[rootX] = rootY;
rank[rootY]++;
}
}
}
return false;
}
int main() {
int V, E;
cin >> V >> E;
vector<Edge> edges(E);
for (int i = 0; i < E; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].weight;
}
if (hasCycle(edges, V)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
LAB : 10 (A)
Code :
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
class Graph {
public:
int vertices;
vector<vector<pair<int, int>>> adjacencyList;
pq.push(make_pair(0, source));
distance[source] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
cout << "Shortest distances from source " << source << " to all other vertices:\n";
for (int i = 0; i < vertices; ++i) {
cout << "Vertex " << i << ": ";
if (distance[i] == INF)
cout << "INF\n";
else
cout << distance[i] << "\n";
}
}
};
int main() {
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
Graph g(vertices);
cout << "Enter the edges with weights (format: source destination weight):\n";
for (int i = 0; i < edges; ++i) {
int u, v, weight;
cin >> u >> v >> weight;
g.addEdge(u, v, weight);
}
int source;
cout << "Enter the source vertex: ";
cin >> source;
g.dijkstra(source);
return 0;
}
OUTPUT :
LAB : 10 (B)
Code :
#include <bits/stdc++.h>
#include <vector>
#include <climits>
struct Edge {
int src, dest, weight;
};
class Graph {
public:
int vertices, edges;
vector<Edge> edgeList;
cout << "Shortest distances from source " << source << " to all other vertices:\n";
for (int i = 0; i < vertices; ++i) {
cout << "Vertex " << i << ": ";
if (distance[i] == INF)
cout << "INF\n";
else
cout << distance[i] << "\n";
}
}
};
int main() {
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
cout << "Enter the edges with weights (format: source destination weight):\n";
for (int i = 0; i < edges; ++i) {
int u, v, weight;
cin >> u >> v >> weight;
g.addEdge(u, v, weight);
}
int source;
cout << "Enter the source vertex: ";
cin >> source;
g.bellmanFord(source);
return 0;
}