23536 DSA Assignment 7
23536 DSA Assignment 7
23536 DSA Assignment 7
#include <iostream>
struct Edge
};
struct AdjListNode
AdjListNode *next;
};
class MyString
private:
char str[MAX_NAME_LENGTH];
public:
MyString()
str[0] = '\0';
}
void set(const char *s)
int i = 0;
str[i] = s[i];
i++;
str[i] = '\0';
return str;
MyString result;
int i;
result.str[i] = '\0';
return result;
};
class CampusGraph
{
private:
int V;
MyString departments[MAX_VERTICES];
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
AdjListNode *adjList[MAX_VERTICES];
int edgeCount;
if (parent[i] != i)
return parent[i];
parent[xroot] = yroot;
parent[yroot] = xroot;
}
else
parent[yroot] = xroot;
rank[xroot]++;
min = key[v];
min_index = v;
return min_index;
void sortEdges()
{
Edge temp = edges[j];
edges[j + 1] = temp;
public:
adjMatrix[i][j] = 0;
adjList[i] = nullptr;
departments[index].set(name);
}
}
adjMatrix[src][dest] = weight;
adjMatrix[dest][src] = weight;
newNode->dest = dest;
newNode->weight = weight;
newNode->next = adjList[src];
adjList[src] = newNode;
newNode->dest = src;
newNode->weight = weight;
newNode->next = adjList[dest];
adjList[dest] = newNode;
edges[edgeCount].src = src;
edges[edgeCount].dest = dest;
edges[edgeCount].weight = weight;
edgeCount++;
void printAdjMatrix()
{
cout << departments[i].substring(0, 3).get() << " ";
void printAdjList()
cout << departments[temp->dest].get() << "(" << temp->weight << ") -> ";
temp = temp->next;
}
void kruskalMST()
sortEdges();
int parent[MAX_VERTICES];
int rank[MAX_VERTICES];
parent[i] = i;
rank[i] = 0;
int totalWeight = 0;
if (x != y)
totalWeight += edges[i].weight;
}
cout << "Total distance of MST: " << totalWeight << "m\n";
void primMST()
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
bool mstSet[MAX_VERTICES];
key[i] = INT_MAX;
mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
parent[v] = u;
key[v] = adjMatrix[u][v];
}
}
int totalWeight = 0;
totalWeight += adjMatrix[i][parent[i]];
cout << "Total distance of MST: " << totalWeight << "m\n";
~CampusGraph()
current = current->next;
delete temp;
};
int main()
CampusGraph campus(6);
campus.setDepartmentName(0, "CSE");
campus.setDepartmentName(1, "ECE");
campus.setDepartmentName(2, "Library");
campus.setDepartmentName(3, "Canteen");
campus.setDepartmentName(4, "Admin");
campus.setDepartmentName(5, "Mech");
campus.addEdge(0, 1, 100);
campus.addEdge(0, 2, 150);
campus.addEdge(1, 2, 80);
campus.addEdge(1, 3, 120);
campus.addEdge(2, 3, 70);
campus.addEdge(2, 4, 180);
campus.addEdge(3, 4, 90);
campus.addEdge(3, 5, 200);
campus.addEdge(4, 5, 140);
int choice;
do
switch (choice)
{
case 1:
campus.printAdjMatrix();
break;
case 2:
campus.printAdjList();
break;
case 3:
campus.kruskalMST();
break;
case 4:
campus.primMST();
break;
case 5:
break;
default:
return 0;
GitHub link:-
https://github.com/VedantKaulgekar/CollegeDSAL/blob/main/Assignment_7.cpp