Name:Rohan
Name:Rohan
implement it using a program to sort an array of elements by dividing the array into two
subarrays and combining these subarrays after sorting each one of them. Your program should
also find number of comparisons and inversions during sorting the array.
SOURCE CODE:
#include <iostream>
using namespace std;
int i = 0, j = 0, k = left;
while(i < n1 && j < n2) {
comparisons++;
if(L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
inversions += (n1 - i);
}
}
while(i < n1) arr[k++] = L[i++];
while(j < n2) arr[k++] = R[j++];
}
int main() {
int T;
cout << "Enter number of test cases: ";
cin >> T;
while(T--) {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
cout << "Enter array elements: ";
for(int i = 0; i < n; i++) cin >> arr[i];
comparisons = 0; inversions = 0;
mergeSort(arr, 0, n - 1);
OUTPUT:
SOURCE CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int T;
cout << "Enter number of test cases: ";
cin >> T;
while(T--) {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
Name:Rohan Section:L1 Roll No. 44
cout << "Enter array elements: ";
for(int i = 0; i < n; i++) cin >> arr[i];
comparisons = swaps = 0;
quickSort(arr, 0, n - 1);
OUTPUT:
SOURCE CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int T;
cout << "Enter number of test cases: ";
cin >> T;
while(T--) {
int n, k;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
cout << "Enter array elements: ";
for(int i = 0; i < n; i++) cin >> arr[i];
OUTPUT:
SOURCE CODE:
#include <iostream>
#include <vector>
using namespace std;
if (maxCount > 1)
cout << maxChar << " - " << maxCount << endl;
else
cout << "No Duplicates Present" << endl;
}
int main() {
int T;
cout << "Enter number of test cases: ";
cin >> T;
while (T--) {
int n;
cout << "Enter size of array: ";
cin >> n;
vector<char> arr(n);
cout << "Enter array elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
findMaxOccurrence(arr, n);
}
return 0;
OUTPUT:
SOURCE CODE: -
#include <iostream>
#include <vector>
using namespace std;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (aleft[i] <= aright[j]) {
a[k++] = aleft[i++];
} else {
a[k++] = aright[j++];
}
}
while (i < n1) a[k++] = aleft[i++];
while (j < n2) a[k++] = aright[j++];
}
int main() {
int T;
cout << "Enter number of test cases: ";
cin >> T;
while (T--) {
int n;
cout << "Enter size of array: ";
cin >> n;
vector<int> a(n);
cout << "Enter array elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
mergeSort(a, 0, n - 1);
int target;
cout << "Enter target value: ";
cin >> target;
if (!found) {
cout << "No such elements exist" << endl;
}
}
return 0;
}
OUTPUT:
#include <iostream>
int main() {
int m, n;
OUTPUT:
if (visited[dest]) {
cout << "Yes Path Exists\n";
} else {
cout << "No Such Path Exists\n";
}}
OUTPUT:
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v = 0; v < n; v++) {
if (graph[u][v]) {
if (color[v] == -1) {
color[v] = 1 - color[u];
q.push(v);
} else if (color[v] == color[u]) {
return false;
}
}
}
}
}
}
return true;
}
int main() {
int n;
cout << "Enter number of nodes: ";
cin >> n;
if (isBipartite(graph, n)) {
cout << "Yes Bipartite\n";
} else {
cout << "Not Bipartite\n";
}
return 0;
}
recStack[node] = false;
return false;
}
int main() {
int n;
cout << "Enter number of vertices: ";
cin >> n;
if (isCyclic(adj, n))
cout << "Yes Cycle Exists\n";
else
cout << "No Cycle Exists\n";
return 0;
}
SOURCE CODE:
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
dist[src] = 0;
int main() {
int V;
cout << "Enter number of vertices: ";
cin >> V;
int source;
cout << "Enter source vertex (Akshay's house, 1-based): ";
cin >> source;
cout << "Shortest paths to source " << source << ":\n";
dijkstra(graph, source - 1, V); // Convert to 0-based indexing
return 0;
}
OUTPUT:
struct Edge {
int u, v, weight;
};
dist[src] = 0;
// Print result
Name:Rohan Section:L1 Roll No. 44
for (int i = 0; i < V; i++) {
cout << "Node " << i + 1 << ": ";
if (dist[i] == INT_MAX) {
cout << "No Path" << endl;
} else {
printPath(parent, i);
cout << " : " << dist[i] << endl;
}
}
}
int main() {
int V;
cout << "Enter number of vertices: ";
cin >> V;
int src;
cout << "Enter source vertex (Akshay's house, 1-based): ";
cin >> src;
cout << "Shortest paths to source " << src << " using Bellman-Ford:\n";
bellmanFord(graph, V, src - 1); // Convert to 0-based index
return 0;
}
OUTPUT:
int main() {
int n;
cout << "Enter number of vertices: ";
cin >> n;
return 0;
}
OUTPUT:
int totalCost = 0;
for (int i = 1; i < n; i++) {
cout << parent[i] + 1 << " - " << i + 1 << " : " << graph[i][parent[i]] << endl;
totalCost += graph[i][parent[i]];
}
cout << "Minimum cost to connect all cities: " << totalCost << endl;
}
int main() {
int n;
cin >> n;
vector<vector<int>> graph(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
Name:Rohan Section:L1 Roll No. 44
cin >> graph[i][j];
primMST(graph, n);
return 0;
}
OUTPUT:
int totalCost = 0;
for (int i = 1; i < n; i++) {
cout << parent[i] + 1 << " - " << i + 1 << " : " << graph[i][parent[i]] << endl;
totalCost += graph[i][parent[i]];
}
cout << "Minimum cost to connect all cities: " << totalCost << endl;
}
int main() {
int n;
cin >> n;
vector<vector<int>> graph(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> graph[i][j];
primMST(graph, n);
return 0;
}
OUTPUT:
Name:Rohan Section:L1 Roll No. 44
Name:Rohan Section:L1 Roll No. 44
PROBLEM STATEMENT 3: Assume that same road construction project is given to another
person. The amount he will earn from this project is directly proportional to the budget of the
project. This person is greedy, so he decided to maximize the budget by constructing those roads
who have highest construction cost. Design an algorithm and implement it using a program to
find the maximum budget required for the project.
SOURCE CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int u, v, weight;
};
if (parentU != parentV) {
if (rank[parentU] > rank[parentV]) {
parent[parentV] = parentU;
} else if (rank[parentU] < rank[parentV]) {
parent[parentU] = parentV;
} else {
parent[parentV] = parentU;
rank[parentU]++;
}
}
}
vector<Edge> mst;
int totalCost = 0;
int main() {
int n, m;
cin >> n >> m;
vector<Edge> edges(m);
kruskalMaximizingBudget(n, edges);
return 0;
}
OUTPUT: