design and analysis of algorithm lab file
design and analysis of algorithm lab file
#include <iostream>
using namespace std;
int linearSearch(const int arr[], int size, int key) {
if (size == 0)
return -1;
if (arr[size - 1] == key)
return size - 1;
return linearSearch(arr, size - 1, key);
}
int main() {
int arr[] = {1, 3, 5, 7, 9, 11, 13};
int size = sizeof(arr) / sizeof(arr[0]);
int key;
cout << "Enter the key to search: ";
cin >> key;
int result = linearSearch(arr, size, key);
if (result != -1)
cout << "Key found at index: " << result << endl;
else
cout << "Key not found." << endl;
return 0;
}
2) ) program for rEcursivE binary sEarch:
#include <iostream>
using namespace std;
int binarySearch(const int arr[], int left, int right, int key) {
if (left > right)
return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] > key)
return binarySearch(arr, left, mid - 1, key);
return binarySearch(arr, mid + 1, right, key);
}
int main() {
int arr[] = {1, 3, 5, 7, 9, 11, 13};
int size = sizeof(arr) / sizeof(arr[0]);
int key;
return 0;
}
output :
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
heapSort(arr, n);
return 0;
}
output :
return 0;
}
output :
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Item {
int value;
int weight;
};
double knapsackGreedy(vector<Item>& items, int capacity) {
auto compare = [](Item a, Item b) {
return (double)a.value / a.weight > (double)b.value / b.weight;
};
sort(items.begin(), items.end(), compare);
double totalValue = 0.0;
for (auto& item : items) {
if (capacity >= item.weight) {
capacity -= item.weight;
totalValue += item.value;
} else {
totalValue += item.value * ((double)capacity / item.weight);
break;
}
}
return totalValue;
}
int main() {
int n, capacity;
cout << "Enter number of items and knapsack capacity: ";
cin >> n >> capacity;
vector<Item> items(n);
cout << "Enter value and weight for each item:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Item " << i+1 << ": ";
cin >> items[i].value >> items[i].weight;
}
cout << "Maximum value achievable: " << knapsackGreedy(items, capacity) << endl;
return 0;
}
output :
struct Edge {
int src, dest, weight;
};
int findParent(int node, vector<int>& parent) {
if (parent[node] == node)
return node;
return parent[node] = findParent(parent[node], parent); // Path compression
}
void unionSets(int u, int v, vector<int>& parent, vector<int>& rank) {
int rootU = findParent(u, parent);
int rootV = findParent(v, parent);
if (rootU != rootV) {
if (rank[rootU] > rank[rootV])
parent[rootV] = rootU;
else if (rank[rootU] < rank[rootV])
parent[rootU] = rootV;
else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
}
vector<Edge> kruskalMST(int V, vector<Edge>& edges) {
// Sort edges by weight
sort(edges.begin(), edges.end(), [](Edge a, Edge b) {
return a.weight < b.weight;
});
vector<int> parent(V);
vector<int> rank(V, 0);
for (int i = 0; i < V; ++i)
parent[i] = i;
int main() {
int V, E;
cout << "Enter the number of vertices and edges: ";
cin >> V >> E;
vector<Edge> edges(E);
cout << "Enter each edge as source, destination, and weight:\n";
for (int i = 0; i < E; ++i) {
cout << "Edge " << i + 1 << ": ";
cin >> edges[i].src >> edges[i].dest >> edges[i].weight;
}
vector<Edge> mst = kruskalMST(V, edges);
cout << "\nEdges in the Minimum Spanning Tree:\n";
int mstWeight = 0;
for (auto& edge : mst) {
cout << "Source: " << edge.src << ", Destination: " << edge.dest
<< ", Weight: " << edge.weight << endl;
mstWeight += edge.weight;
}
cout << "Total weight of MST: " << mstWeight << endl;
return 0;
output :
int main() {
int n, W;
cout << "Enter the number of items and knapsack capacity: ";
cin >> n >> W;
return 0;
}
output :
while (!pq.empty()) {
int currDist = pq.top().first;
int currVertex = pq.top().second;
pq.pop();
int main() {
int V, E;
cout << "Enter the number of vertices and edges: ";
cin >> V >> E;
vector<vector<Edge>> graph(V);
int src;
cout << "Enter the source vertex: ";
cin >> src;
vector<int> distances = dijkstra(src, graph);
cout << "\nShortest distances from vertex " << src << ":\n";
for (int i = 0; i < V; ++i) {
cout << "Vertex " << i << ": ";
if (distances[i] == INT_MAX)
cout << "Not reachable";
else
cout << distances[i];
cout << endl;
}
return 0;
}
output :
key[0] = 0;
pq.push({0, 0});
while (!pq.empty()) {
int currVertex = pq.top().second;
pq.pop();
if (inMST[currVertex])
continue;
inMST[currVertex] = true;
totalCost += key[currVertex];
return totalCost;
}
int main() {
int V, E;
cout << "Enter the number of vertices and edges: ";
cin >> V >> E;
output :