Dsa 3
Dsa 3
Problem1:
Input:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int u, v, cost;
bool operator<(const Edge& other) const {
return cost < other.cost;
}
};
class DisjointSet {
private:
vector<int> parent, rank;
public:
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; ++i)
parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
class Network {
private:
vector<Edge> edges;
Page 2 of 12
int villages;
public:
Network(int v) : villages(v) {}
void kruskalMST() {
sort(edges.begin(), edges.end());
DisjointSet ds(villages);
int totalCost = 0;
int main() {
Network telecom(5);
telecom.addConnection(0, 1, 10);
telecom.addConnection(0, 2, 6);
telecom.addConnection(0, 3, 5);
telecom.addConnection(1, 3, 15);
telecom.addConnection(2, 3, 4);
telecom.addConnection(1, 2, 25);
telecom.addConnection(3, 4, 2);
telecom.kruskalMST();
return 0;
}
Output:
Page 3 of 12
Problem2:
Input:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Incident {
int id;
int priority;
class EmergencyHeap {
private:
vector<Incident> heap;
public:
void addIncident(int id, int priority) {
Incident i;
i.id = id;
i.priority = priority;
heap.push_back(i);
heapifyUp(heap.size() - 1);
}
Page 4 of 12
Incident dispatch() {
Incident top = heap[0];
heap[0] = heap.back();
heap.pop_back();
heapifyDown(0);
return top;
}
void display() {
for (int i = 0; i < heap.size(); ++i)
cout << "(" << heap[i].id << ", " << heap[i].priority << ") ";
cout << endl;
}
};
int main() {
EmergencyHeap system;
system.addIncident(1, 10);
system.addIncident(2, 30);
system.addIncident(3, 20);
system.display();
system.updatePriority(1, 50);
system.display();
return 0;
}
Output:
Problem3:
Input:
Page 5 of 12
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
class TradeRoutes {
private:
map<string, vector<string> > graph;
public:
void addSettlement(const string& name) {
graph[name] = vector<string>();
}
void displayNetwork() {
map<string, vector<string> >::iterator it;
for (it = graph.begin(); it != graph.end(); ++it) {
cout << it->first << " -> ";
vector<string>::iterator vit;
for (vit = it->second.begin(); vit != it->second.end(); ++vit)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main() {
TradeRoutes network;
network.addSettlement("A");
network.addSettlement("B");
network.addSettlement("C");
network.addSettlement("D");
network.addSettlement("E");
network.addRoute("A", "B");
network.addRoute("A", "C");
network.addRoute("B", "D");
network.addRoute("C", "D");
Page 6 of 12
network.addRoute("D", "E");
network.addRoute("C", "E");
network.displayNetwork();
vector<string> hubConnections;
hubConnections.push_back("A");
hubConnections.push_back("C");
hubConnections.push_back("E");
network.addHub("F", hubConnections);
cout << "\nAfter adding new trade hub F:\n";
network.displayNetwork();
return 0;
}
Output:
Problem4:
Input:
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
class RoyalFamily {
private:
map<string, vector<string> > graph;
public:
void addRelation(const string& parent, const string& child) {
graph[parent].push_back(child);
}
Page 7 of 12
void bfsDescendants(const string& royal) {
queue<string> q;
set<string> visited;
q.push(royal);
visited.insert(royal);
cout << "Descendants (by generation): ";
while (!q.empty()) {
string current = q.front(); q.pop();
vector<string>::iterator it;
for (it = graph[current].begin(); it != graph[current].end();
++it) {
if (visited.find(*it) == visited.end()) {
cout << *it << " ";
q.push(*it);
visited.insert(*it);
}
}
}
cout << endl;
}
int main() {
RoyalFamily rf;
rf.addRelation("King", "Prince");
rf.addRelation("King", "Princess");
rf.addRelation("Prince", "Grandson");
rf.addRelation("Princess", "Granddaughter");
rf.bfsDescendants("King");
Page 8 of 12
cout << (rf.dfsFindAncestor("Grandson", "King", 2) ? "Yes" : "No") <<
endl;
return 0;
}
Output:
Problem5:
Input:
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <climits>
using namespace std;
class CourierMap {
private:
map<int, vector<pii> > graph;
public:
void addRoad(int u, int v, int time, bool directed = false) {
graph[u].push_back(make_pair(v, time));
if (!directed)
graph[v].push_back(make_pair(u, time));
}
while (!pq.empty()) {
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
vector<pii>::iterator edgeIt;
Page 9 of 12
for (edgeIt = graph[u].begin(); edgeIt != graph[u].end();
++edgeIt) {
int v = edgeIt->first;
int w = edgeIt->second;
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push(make_pair(dist[v], v));
}
}
}
cout << "Shortest time from " << source << " to " << destination <<
": ";
if (dist.find(destination) == dist.end() || dist[destination] ==
INT_MAX)
cout << "No path" << endl;
else
cout << dist[destination] << " minutes" << endl;
}
};
int main() {
CourierMap city;
city.addRoad(1, 2, 4);
city.addRoad(1, 3, 1);
city.addRoad(3, 2, 2);
city.addRoad(2, 4, 1);
city.addRoad(3, 4, 5);
city.dijkstra(1, 4);
return 0;
}
Output:
Problem6:
Input:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int u, v, cost;
bool operator<(const Edge& other) const {
return cost < other.cost;
}
};
Page 10 of 12
class DisjointSet {
private:
vector<int> parent, rank;
public:
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; ++i)
parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
class Network {
private:
vector<Edge> edges;
int villages;
public:
Network(int v) {
villages = v;
}
Page 11 of 12
void kruskalMST() {
sort(edges.begin(), edges.end());
DisjointSet ds(villages);
int totalCost = 0;
int main() {
Network telecom(5);
telecom.addConnection(0, 1, 10);
telecom.addConnection(0, 2, 6);
telecom.addConnection(0, 3, 5);
telecom.addConnection(1, 3, 15);
telecom.addConnection(2, 3, 4);
telecom.addConnection(1, 2, 25);
telecom.addConnection(3, 4, 2);
telecom.kruskalMST();
return 0;
}
Output:
Page 12 of 12