Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #include "AdjacencyMatrix.h"
- void Prim(Matrix, int, int);
- int main() {
- Matrix M(6);
- int weight = 0;
- Prim(M,weight,0);
- system("pause");
- return 0;
- }
- void Prim(Matrix*G, int*D, int s) { // Primβs MST algorithm
- int VISITED = 1;
- int UNVISITED = 0;
- int V[6]; // Store closest vertex
- int i, w;
- for (int i=0; i<G->n(); i++) // Initialize
- D[i] = INFINITY;
- D[s] = 0;
- for (i=0; i<G->n(); i++) { // Process the vertices
- int v = minVertex(G, D);
- G->setMark(v, VISITED);
- //if (v != s)
- // AddEdgetoMST(V[v], v); // Add edge to MST
- if (D[v] == INFINITY) return; // Unreachable vertices
- for (w=G->first(v); w<G->n(); w = G->next(v,w))
- if (D[w] > G->weight(v,w)) {
- D[w] = G->weight(v,w); // Update distance
- V[w] = v; // Where it came from
- }
- }
- }
- class Matrix : public Graph {
- public:
- Matrix (int numVert) { Init(numVert); }
- ~Matrix() { // Destructor
- delete [] mark; // Return dynamically allocated memory
- for (int i=0; i<numVertex; i++)
- delete [] matrix[i];
- delete [] matrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement