代码如下
*************************************************************************************************************************
#include <stdio.h>
#include <vector>
#include <map>
using namespace std;
class MSTNode {
public:
char key;
int distance;
MSTNode(char arg) {
key = arg;
distance = 0x7fffffff;
}
int operator < (const MSTNode& node) const {
return distance < node.distance;
}
};
template<class T>
class CMinHeap {
private:
vector<T> data;
int size;
public:
CMinHeap(vector<T>& data) {
this->data = data;
size = data.size();
}
void Swap(T& t1, T& t2) {
T temp = t1 ;
t1 = t2;
t2 = temp;
}
void MinHeapify(int index) {
int left = 2*index+1;
int right = 2*(index+1);
int smallest = index;
if (left < size && data[left] < data[index])
smallest = left;
if (right < size && data[right] < data[smallest])
smallest = right;
if (smallest != index) {
Swap(data[smallest], data[index]);
MinHeapify(smallest);
}
}
void BuildMinHeap() {
for (int i = size/2-1; i >= 0; --i) {
MinHeapify(i);
}
}
T HeapExtractMin() {
T ret = data[0];
Swap(data[0], data[size-1]);
size--;
MinHeapify(0);
return ret;
}
int HeapMin() {
return data[0];
}
void MinHeapDecreaseKey (int index, int key){
data[index] = key;
int parent = (index - 1) / 2;
while (parent>=0 && data[index] < data[parent] ) {
Swap(data[parent], data[index]);
index = parent;
parent = (index - 1) / 2;
}
}
int isEmpty() {
return size == 0;
}
int index(char key) {
for (int i = 0; i < size; ++i) {
if (data[i].key == key)
return i;
}
return -1;
}
T get(int index) {
return data[index];
}
};
class MST_PRIM_Graph {
private:
map<char, map<char, int> > adjacency;
public:
MST_PRIM_Graph(map<char, map<char, int> >& adjacency) {
this->adjacency = adjacency;
}
vector<MSTNode> MST_PRIM(char source) {
vector<MSTNode> ret;
vector<MSTNode> nodes;
for (map<char, map<char, int> >::iterator iter = adjacency.begin();
iter != adjacency.end(); ++iter) {
MSTNode tmp(iter->first);
if (iter->first == source)
tmp.distance = 0;
else
tmp.distance = 0x7fffffff;
nodes.push_back(tmp);
}
CMinHeap<MSTNode> Q(nodes);
while (!Q.isEmpty()) {
MSTNode minN = Q.HeapExtractMin();
ret.push_back(minN);
map<char, int> neighbors = adjacency[minN.key];
for (map<char, int>::iterator iter = neighbors.begin(); iter != neighbors.end(); ++iter) {
int index = Q.index(iter->first);
if (index != -1 && iter->second < Q.get(index).distance)
Q.MinHeapDecreaseKey(index, iter->second);
}
}
return ret;
}
};
void testMST_Prim() {
map<char, map<char,int> > adjacency;
map<char,int> aNeighbors;
aNeighbors['b'] = 4;
aNeighbors['h'] = 8;
adjacency['a'] = aNeighbors;
map<char,int> bNeighbors;
bNeighbors['a'] = 4;
bNeighbors['h'] = 11;
bNeighbors['c'] = 8;
adjacency['b'] = bNeighbors;
map<char,int> hNeighbors;
hNeighbors['i'] = 7;
hNeighbors['g'] = 1;
hNeighbors['a'] = 8;
adjacency['h'] = hNeighbors;
map<char,int> iNeighbors;
iNeighbors['h'] = 7;
iNeighbors['g'] = 6;
iNeighbors['c'] = 2;
adjacency['i'] = iNeighbors;
map<char,int> gNeighbors;
gNeighbors['i'] = 6;
gNeighbors['h'] = 1;
gNeighbors['f'] = 2;
adjacency['g'] = gNeighbors;
map<char,int> cNeighbors;
cNeighbors['i'] = 2;
cNeighbors['b'] = 8;
cNeighbors['d'] = 7;
cNeighbors['f'] = 4;
adjacency['c'] = cNeighbors;
map<char,int> dNeighbors;
dNeighbors['c'] = 7;
dNeighbors['f'] = 14;
dNeighbors['e'] = 9;
adjacency['d'] = dNeighbors;
map<char,int> fNeighbors;
fNeighbors['g'] = 2;
fNeighbors['c'] = 4;
fNeighbors['d'] = 14;
fNeighbors['e'] = 10;
adjacency['f'] = fNeighbors;
map<char,int> eNeighbors;
eNeighbors['d'] = 9;
eNeighbors['f'] = 10;
adjacency['e'] = eNeighbors;
// Print out the adjacency map
/* for (map<char, map<char,int> >::iterator iter = adjacency.begin();
iter != adjacency.end(); ++iter) {
printf("%c\n", iter->first);
for (map<char,int>::iterator jter = iter->second.begin(); jter != iter->second.end(); ++jter)
printf("%c - %d ", jter->first, jter->second);
printf("\n");
}
*/
MST_PRIM_Graph mstobj(adjacency);
vector<MSTNode> mst = mstobj.MST_PRIM('a');
for (int i = 0; i < mst.size(); ++i)
printf("%c - %d\n", mst[i].key, mst[i].distance);
}
int main() {
testMST_Prim();
return 0;
}
*************************************************************************************************************************
#include <stdio.h>
#include <vector>
#include <map>
using namespace std;
class MSTNode {
public:
char key;
int distance;
MSTNode(char arg) {
key = arg;
distance = 0x7fffffff;
}
int operator < (const MSTNode& node) const {
return distance < node.distance;
}
};
template<class T>
class CMinHeap {
private:
vector<T> data;
int size;
public:
CMinHeap(vector<T>& data) {
this->data = data;
size = data.size();
}
void Swap(T& t1, T& t2) {
T temp = t1 ;
t1 = t2;
t2 = temp;
}
void MinHeapify(int index) {
int left = 2*index+1;
int right = 2*(index+1);
int smallest = index;
if (left < size && data[left] < data[index])
smallest = left;
if (right < size && data[right] < data[smallest])
smallest = right;
if (smallest != index) {
Swap(data[smallest], data[index]);
MinHeapify(smallest);
}
}
void BuildMinHeap() {
for (int i = size/2-1; i >= 0; --i) {
MinHeapify(i);
}
}
T HeapExtractMin() {
T ret = data[0];
Swap(data[0], data[size-1]);
size--;
MinHeapify(0);
return ret;
}
int HeapMin() {
return data[0];
}
void MinHeapDecreaseKey (int index, int key){
data[index] = key;
int parent = (index - 1) / 2;
while (parent>=0 && data[index] < data[parent] ) {
Swap(data[parent], data[index]);
index = parent;
parent = (index - 1) / 2;
}
}
int isEmpty() {
return size == 0;
}
int index(char key) {
for (int i = 0; i < size; ++i) {
if (data[i].key == key)
return i;
}
return -1;
}
T get(int index) {
return data[index];
}
};
class MST_PRIM_Graph {
private:
map<char, map<char, int> > adjacency;
public:
MST_PRIM_Graph(map<char, map<char, int> >& adjacency) {
this->adjacency = adjacency;
}
vector<MSTNode> MST_PRIM(char source) {
vector<MSTNode> ret;
vector<MSTNode> nodes;
for (map<char, map<char, int> >::iterator iter = adjacency.begin();
iter != adjacency.end(); ++iter) {
MSTNode tmp(iter->first);
if (iter->first == source)
tmp.distance = 0;
else
tmp.distance = 0x7fffffff;
nodes.push_back(tmp);
}
CMinHeap<MSTNode> Q(nodes);
while (!Q.isEmpty()) {
MSTNode minN = Q.HeapExtractMin();
ret.push_back(minN);
map<char, int> neighbors = adjacency[minN.key];
for (map<char, int>::iterator iter = neighbors.begin(); iter != neighbors.end(); ++iter) {
int index = Q.index(iter->first);
if (index != -1 && iter->second < Q.get(index).distance)
Q.MinHeapDecreaseKey(index, iter->second);
}
}
return ret;
}
};
void testMST_Prim() {
map<char, map<char,int> > adjacency;
map<char,int> aNeighbors;
aNeighbors['b'] = 4;
aNeighbors['h'] = 8;
adjacency['a'] = aNeighbors;
map<char,int> bNeighbors;
bNeighbors['a'] = 4;
bNeighbors['h'] = 11;
bNeighbors['c'] = 8;
adjacency['b'] = bNeighbors;
map<char,int> hNeighbors;
hNeighbors['i'] = 7;
hNeighbors['g'] = 1;
hNeighbors['a'] = 8;
adjacency['h'] = hNeighbors;
map<char,int> iNeighbors;
iNeighbors['h'] = 7;
iNeighbors['g'] = 6;
iNeighbors['c'] = 2;
adjacency['i'] = iNeighbors;
map<char,int> gNeighbors;
gNeighbors['i'] = 6;
gNeighbors['h'] = 1;
gNeighbors['f'] = 2;
adjacency['g'] = gNeighbors;
map<char,int> cNeighbors;
cNeighbors['i'] = 2;
cNeighbors['b'] = 8;
cNeighbors['d'] = 7;
cNeighbors['f'] = 4;
adjacency['c'] = cNeighbors;
map<char,int> dNeighbors;
dNeighbors['c'] = 7;
dNeighbors['f'] = 14;
dNeighbors['e'] = 9;
adjacency['d'] = dNeighbors;
map<char,int> fNeighbors;
fNeighbors['g'] = 2;
fNeighbors['c'] = 4;
fNeighbors['d'] = 14;
fNeighbors['e'] = 10;
adjacency['f'] = fNeighbors;
map<char,int> eNeighbors;
eNeighbors['d'] = 9;
eNeighbors['f'] = 10;
adjacency['e'] = eNeighbors;
// Print out the adjacency map
/* for (map<char, map<char,int> >::iterator iter = adjacency.begin();
iter != adjacency.end(); ++iter) {
printf("%c\n", iter->first);
for (map<char,int>::iterator jter = iter->second.begin(); jter != iter->second.end(); ++jter)
printf("%c - %d ", jter->first, jter->second);
printf("\n");
}
*/
MST_PRIM_Graph mstobj(adjacency);
vector<MSTNode> mst = mstobj.MST_PRIM('a');
for (int i = 0; i < mst.size(); ++i)
printf("%c - %d\n", mst[i].key, mst[i].distance);
}
int main() {
testMST_Prim();
return 0;
}