// file PrimMST.h  -bds 2010Apr
// This is a small modification of DijkstraSSSP.h

#ifndef __PRIMMST_H
#define __PRIMMST_H

#include <vector>
#include <cmath>
using namespace std;
#include "Graph.h"
#include "BinaryHeap.h"

typedef int Vertex;
typedef double Dist;
const Dist INFINITY = 1.8e308;

// Single source shortest paths
void Prim(	Graph<>& G,
		vector<Graph<>::Vertex> & prev // the output indicating the shortest paths.
	)
{
	typedef Graph<>::Vertex Vertex;
	int n = G.nbr.size(); // number of vertices.
	Vertex s = 0; // arbitrary choice
	vector<Dist> dist(n, INFINITY);
	dist[s] = 0;

	prev.resize(n);
	for (Vertex u = 0; u < n; ++u) {
		prev[u]=u; //i.e. no path to s yet. 
	}

	Heap<Dist> H( dist );
	for (Vertex u = 0; u < n; ++u) H.insert( u );

	while ( ! H.isEmpty() ) {
		Vertex v = H.extractMin();
		for (int i = 0; i < G.nbr[v].size(); ++i){
			Vertex u = G.nbr[v][i];
			Dist l = G.edgeWeight[v][i];
			if (dist[u] > l) {
				prev[u] = v;
				dist[u] = l;
				H.decreaseKey(u);
			}
		}
	}
};
#endif // __PRIMMST_H
