0% found this document useful (0 votes)
3 views12 pages

Dsa 3

The document contains multiple programming problems, each demonstrating different data structures and algorithms in C++. Problem 1 and Problem 6 focus on implementing Kruskal's algorithm for finding the Minimum Spanning Tree using a Disjoint Set. Other problems include implementing a priority queue for emergency incidents, managing trade routes, representing a royal family tree, and finding the shortest path in a courier map using Dijkstra's algorithm.

Uploaded by

anumsaeed095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Dsa 3

The document contains multiple programming problems, each demonstrating different data structures and algorithms in C++. Problem 1 and Problem 6 focus on implementing Kruskal's algorithm for finding the Minimum Spanning Tree using a Disjoint Set. Other problems include implementing a priority queue for emergency incidents, managing trade routes, representing a royal family tree, and finding the shortest path in a courier map using Dijkstra's algorithm.

Uploaded by

anumsaeed095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Page 1 of 12

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];
}

bool unionSets(int x, int y) {


int xr = find(x);
int yr = find(y);
if (xr == yr)
return false;
if (rank[xr] < rank[yr])
parent[xr] = yr;
else if (rank[xr] > rank[yr])
parent[yr] = xr;
else {
parent[yr] = xr;
rank[xr]++;
}
return true;
}
};

class Network {
private:
vector<Edge> edges;

Page 2 of 12
int villages;

public:
Network(int v) : villages(v) {}

void addConnection(int u, int v, int cost) {


Edge e;
e.u = u;
e.v = v;
e.cost = cost;
edges.push_back(e);
}

void kruskalMST() {
sort(edges.begin(), edges.end());
DisjointSet ds(villages);
int totalCost = 0;

cout << "Edges in the Minimum Spanning Tree:\n";


for (size_t i = 0; i < edges.size(); ++i) {
Edge e = edges[i];
if (ds.unionSets(e.u, e.v)) {
cout << e.u << " - " << e.v << " : " << e.cost << "\n";
totalCost += e.cost;
}
}
cout << "Total minimum cost: " << totalCost << endl;
}
};

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;

bool operator<(const Incident& other) const {


return priority < other.priority;
}
};

class EmergencyHeap {
private:
vector<Incident> heap;

void heapifyUp(int index) {


while (index > 0 && heap[(index - 1) / 2] < heap[index]) {
swap(heap[(index - 1) / 2], heap[index]);
index = (index - 1) / 2;
}
}

void heapifyDown(int index) {


int size = heap.size();
while (true) {
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < size && heap[largest] < heap[left]) largest = left;


if (right < size && heap[largest] < heap[right]) largest =
right;
if (largest == index) break;
swap(heap[index], heap[largest]);
index = largest;
}
}

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 updatePriority(int id, int newPriority) {


for (int i = 0; i < heap.size(); ++i) {
if (heap[i].id == id) {
heap[i].priority = newPriority;
heapifyUp(i);
heapifyDown(i);
break;
}
}
}

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();

cout << "Dispatching highest priority: " << system.dispatch().id <<


endl;
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 addRoute(const string& from, const string& to) {


graph[from].push_back(to);
graph[to].push_back(from);
}

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;
}
}

void addHub(const string& name, const vector<string>& connectsTo) {


graph[name] = connectsTo;
for (size_t i = 0; i < connectsTo.size(); ++i) {
graph[connectsTo[i]].push_back(name);
}
}
};

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;
}

bool dfsFindAncestor(const string& current, const string& target, int


depth) {
if (current == target) return true;
if (depth == 0) return false;

map<string, vector<string> >::iterator it;


for (it = graph.begin(); it != graph.end(); ++it) {
vector<string>::iterator childIt;
for (childIt = it->second.begin(); childIt != it->second.end();
++childIt) {
if (*childIt == current) {
if (dfsFindAncestor(it->first, target, depth - 1))
return true;
}
}
}
return false;
}
};

int main() {
RoyalFamily rf;
rf.addRelation("King", "Prince");
rf.addRelation("King", "Princess");
rf.addRelation("Prince", "Grandson");
rf.addRelation("Princess", "Granddaughter");

rf.bfsDescendants("King");

cout << "Is King ancestor of Grandson (depth ≤ 2)? ";

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;

typedef pair<int, int> pii;

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));
}

void dijkstra(int source, int destination) {


map<int, int> dist;
map<int, vector<pii> >::iterator it;
for (it = graph.begin(); it != graph.end(); ++it)
dist[it->first] = INT_MAX;
dist[source] = 0;

priority_queue<pii, vector<pii>, greater<pii> > pq;


pq.push(make_pair(0, source));

while (!pq.empty()) {
int u = pq.top().second;
int d = pq.top().first;
pq.pop();

if (d > dist[u]) continue;

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];
}

bool unionSets(int x, int y) {


int xr = find(x);
int yr = find(y);
if (xr == yr)
return false;
if (rank[xr] < rank[yr])
parent[xr] = yr;
else if (rank[xr] > rank[yr])
parent[yr] = xr;
else {
parent[yr] = xr;
rank[xr]++;
}
return true;
}
};

class Network {
private:
vector<Edge> edges;
int villages;

public:
Network(int v) {
villages = v;
}

void addConnection(int u, int v, int cost) {


Edge e;
e.u = u;
e.v = v;
e.cost = cost;
edges.push_back(e);
}

Page 11 of 12
void kruskalMST() {
sort(edges.begin(), edges.end());
DisjointSet ds(villages);
int totalCost = 0;

cout << "Edges in the Minimum Spanning Tree:\n";


for (size_t i = 0; i < edges.size(); ++i) {
Edge e = edges[i];
if (ds.unionSets(e.u, e.v)) {
cout << e.u << " - " << e.v << " : " << e.cost << "\n";
totalCost += e.cost;
}
}
cout << "Total minimum cost: " << totalCost << endl;
}
};

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

You might also like