


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



#include
void Graph::initializeState(){ for(int i=0; i<this->vertexCount; i++){ this->key[i] = 0; // 0=not in MST, 1=yes in MST this->distance[i]= INT_MAX; //initially distance is Max int this->parent[i] = -1; // -1=no parent, else parent info // } } void Graph::addEdgeUndirectedWeight(int i, int j, int cost) { if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { adjMatrix[i][j] = cost; adjMatrix[j][i] = cost; } } void Graph::removeEdgeUndirected(int i, int j) { if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { adjMatrix[i][j] = 0; adjMatrix[j][i] = 0; } } int Graph::isEdge(int i, int j) { if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) return adjMatrix[i][j]; else{ cout<<"Invalid vertex number.\n"; return 0; } } void Graph::display(){ int u,v; //vertex cout<<"\t "; for(u=0; u<vertexCount; u++){ cout<<u<<" "; } for(u=0; u<vertexCount; u++) { cout << "\nadj[" << (char) (u+48) << "] -> "; for(v=0; v<vertexCount; ++v) { cout << " " << adjMatrix[u][v]; } } cout << "\n\n"; } void Graph::showBasicInfo(){ for(int i=0; i<vertexCount; i++){ cout<<"node: "<<i<<" Key: "<<key[i] <<" distance: "<<distance[i]<<" parent: "<<parent[i]<<"\n"; } } int Graph::isAllKeyTrue(){ for(int i=0; i<this->vertexCount; i++){ if(this->key[i]==0) return 0; // not MST yet
Graph g(10); g.addEdgeUndirectedWeight(0, 1, 4); g.addEdgeUndirectedWeight(0, 3, 1); g.addEdgeUndirectedWeight(1, 2, 4); g.addEdgeUndirectedWeight(1, 9, 10); g.addEdgeUndirectedWeight(2, 4, 2); g.addEdgeUndirectedWeight(2, 5, 1); g.addEdgeUndirectedWeight(3, 1, 4); g.addEdgeUndirectedWeight(3, 7, 5); g.addEdgeUndirectedWeight(3, 9, 6); g.addEdgeUndirectedWeight(4, 6, 2); g.addEdgeUndirectedWeight(5, 6, 3); g.addEdgeUndirectedWeight(5, 8, 5); g.addEdgeUndirectedWeight(6, 8, 3); g.addEdgeUndirectedWeight(6, 9, 4); g.addEdgeUndirectedWeight(7, 9, 2); g.addEdgeUndirectedWeight(8, 9, 3); g.initializeState(); g.display(); int startNode = 0; g.showBasicInfo(); g.primsMST(startNode); //g.showBasicInfo();