Hussan Bano22
Hussan Bano22
Hussan Bano22
Assignment#4
22F-8802
Hussan Bano
Task 1:
#include <iostream>
#include <vector>
#include <algorithm>
heap.push_back(task);
return top;
vector<int> top3;
top3.push_back(extractMin(heap));
return top3;
int main() {
system("color F5");
buildMinHeap(tasks);
printHeap(tasks);
insertTask(tasks, 5);
printHeap(tasks);
printHeap(tasks);
cout << "Heap after extracting the top 3 priority tasks: ";
printHeap(tasks);
system("pause");
return 0;
Output:
Task 2:
#include <iostream>
#include <climits>
#include <vector>
minDistance = graph[currentCity][i];
nearestCity = i;
}
return nearestCity;
int main() {
int graph[4][4] = {
};
int numCities = 4;
int currentCity = 0;
int totalCost = 0;
visited[currentCity] = true;
route[0] = currentCity;
numCities);
totalCost += graph[currentCity][nextCity];
currentCity = nextCity;
visited[currentCity] = true;
route[i] = currentCity;
}
totalCost += graph[currentCity][route[0]];
route[numCities] = route[0];
cout << "Approximate total cost: " << totalCost << endl;
system("pause");
return 0;
Output:
Task 3:
#include <iostream>
#include <algorithm>
#include <cstring>
struct Node {
char neighbors[MAX_NODES];
int count;
Node() : count(0) {}
};
Node graph[MAX_NODES];
if (nodes[i] == node) {
return i;
return -1;
graph[uIndex].neighbors[graph[uIndex].count++] = v;
graph[vIndex].neighbors[graph[vIndex].count++] = u;
char queue[MAX_NODES];
queue[rear++] = start;
visited[findIndex(start)] = true;
if (!visited[neighborIndex]) {
queue[rear++] = neighbor;
visited[neighborIndex] = true;
char stack[MAX_NODES];
stack[++top] = start;
if (!visited[currentIndex]) {
visited[currentIndex] = true;
if (!visited[neighborIndex]) {
stack[++top] = neighbor;
char queue[MAX_NODES];
int parent[MAX_NODES];
queue[rear++] = start;
visited[findIndex(start)] = true;
if (current == end) {
break;
if (!visited[neighborIndex]) {
queue[rear++] = neighbor;
visited[neighborIndex] = true;
parent[neighborIndex] = currentIndex;
cout << "Shortest path from " << start << " to " << end << ":
";
if (parent[endIndex] == -1) {
return;
char path[MAX_NODES];
int pathSize = 0;
path[pathSize++] = nodes[i];
}
cout << endl;
int main() {
system("color F4");
addEdge('A', 'B');
addEdge('A', 'C');
addEdge('B', 'D');
addEdge('B', 'E');
addEdge('C', 'F');
BFS(start);
DFS(start);
shortestPathBFS(start, end);
system("pause");
return 0;
Output:
Task 4:
#include <iostream>
#include <vector>
#include <limits>
#define V 5
min = key[v];
min_index = v;
return min_index;
graph[V][V]) {
int totalCost = 0;
cout << parent[i] << " - " << i << " \t" <<
totalCost += graph[i][parent[i]];
cout << "Total cost of the MST: " << totalCost << endl;
vector<int> parent(V);
parent[0] = -1;
mstSet[u] = true;
key[v]) {
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, graph);
int main() {
system("color F3");
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph);
system("pause");
return 0;
Output: