用索引堆代替堆
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>
#include <vector>
#include <ctime>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
template<typename Weight>
class Edge {
private:
int a, b;
Weight weight;
public:
Edge(int a, int b, Weight weight) {
this->a = a;
this->b = b;
this->weight = weight;
}
Edge() {}
~Edge() {}
int v() { return a; }
int w() { return b; }
Weight wt() { return weight; }
int other(int x) {
assert(x == a || x == b);
return x == a ? b : a;
}
bool operator<(Edge<Weight>& e) {
return weight < e.wt();
}
bool operator<=(Edge<Weight>& e) {
return weight <= e.wt();
}
bool operator>(Edge<Weight>& e) {
return weight > e.wt();
}
bool operator>=(Edge<Weight>& e) {
return weight >= e.wt();
}
bool operator==(Edge<Weight>& e) {
return weight == e.wt();
}
friend ostream& operator<<(ostream &os, const Edge &e) {
os << e.a << "-" << e.b << ": " << e.weight;
return os;
}
};
template<typename Item>
class MinHeap {
private:
Item *data;
int count;
int capacity;
void shiftUp(int k) {
while (k > 1 && data[k / 2] > data[k]) {
swap(data[k / 2], data[k]);
k /= 2;
}
}
void shiftDown(int k) {
while (2 * k <= count) {
int j = 2 * k;
if (j + 1 <= count && data[j + 1] < data[j]) j++;
if (data[k] <= data[j]) break;
swap(data[k], data[j]);
k = j;
}
}
public:
MinHeap(int capacity) {
data = new Item[capacity + 1];
count = 0;
this->capacity = capacity;
}
MinHeap(Item arr[], int n) {
data = new Item[n + 1];
capacity = n;
for (int i = 0; i < n; i++)
data[i + 1] = arr[i];
count = n;
for (int i = count / 2; i >= 1; i--)
shiftDown(i);
}
~MinHeap() {
delete[] data;
}
int size() {
return count;
}
bool isEmpty() {
return count == 0;
}
void insert(Item item) {
assert(count + 1 <= capacity);
data[count + 1] = item;
shiftUp(count + 1);
count++;
}
Item extractMin() {
assert(count > 0);
Item ret = data[1];
swap(data[1], data[count]);
count--;
shiftDown(1);
return ret;
}
Item getMin() {
assert(count > 0);
return data[1];
}
void show() {
cout << "| ";
for (int i = 1; i <= count; i++)
cout << data[i]->wt() << " | ";
cout << endl;
}
};
template<typename Item>
class IndexMinHeap {
private:
Item *data;
int *indexes;
int *reverse;
int count;
int capacity;
void shiftUp(int k) {
while (k > 1 && data[indexes[k / 2]] > data[indexes[k]]) {
swap(indexes[k / 2], indexes[k]);
reverse[indexes[k / 2]] = k / 2;
reverse[indexes[k]] = k;
k /= 2;
}
}
void shiftDown(int k) {
while (2 * k <= count) {
int j = 2 * k;
if (j + 1 <= count && data[indexes[j]] > data[indexes[j + 1]])
j += 1;
if (data[indexes[k]] <= data[indexes[j]])
break;
swap(indexes[k], indexes[j]);
reverse[indexes[k]] = k;
reverse[indexes[j]] = j;
k = j;
}
}
public:
IndexMinHeap(int capacity) {
data = new Item[capacity + 1];
indexes = new int[capacity + 1];
reverse = new int[capacity + 1];
for (int i = 0; i <= capacity; i++)
reverse[i] = 0;
count = 0;
this->capacity = capacity;
}
~IndexMinHeap() {
delete[] data;
delete[] indexes;
delete[] reverse;
}
int size() {
return count;
}
bool isEmpty() {
return count == 0;
}
void insert(int index, Item item) {
assert(count + 1 <= capacity);
assert(index + 1 >= 1 && index + 1 <= capacity);
index += 1;
data[index] = item;
indexes[count + 1] = index;
reverse[index] = count + 1;
count++;
shiftUp(count);
}
Item extractMin() {
assert(count > 0);
Item ret = data[indexes[1]];
swap(indexes[1], indexes[count]);
reverse[indexes[count]] = 0;
reverse[indexes[1]] = 1;
count--;
shiftDown(1);
return ret;
}
int extractMinIndex() {
assert(count > 0);
int ret = indexes[1] - 1;
swap(indexes[1], indexes[count]);
reverse[indexes[count]] = 0;
reverse[indexes[1]] = 1;
count--;
shiftDown(1);
return ret;
}
Item getMin() {
assert(count > 0);
return data[indexes[1]];
}
int getMinIndex() {
assert(count > 0);
return indexes[1] - 1;
}
bool contain(int index) {
return reverse[index + 1] != 0;
}
Item getItem(int index) {
assert(contain(index));
return data[index + 1];
}
void change(int index, Item newItem) {
assert(contain(index));
index += 1;
data[index] = newItem;
shiftUp(reverse[index]);
shiftDown(reverse[index]);
}
};
template<typename Weight>
class DenseGraph {
private:
int n, m;
bool directed;
vector<vector<Edge<Weight> *>> g;
public:
DenseGraph(int n, bool directed) {
this->n = n;
this->m = 0;
this->directed = directed;
for (int i = 0; i < n; i++) {
g.push_back(vector<Edge<Weight> *>(n, NULL));
}
}
~DenseGraph() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (g[i][j] != NULL)
delete g[i][j];
}
int V() { return n; }
int E() { return m; }
void addEdge(int v, int w, Weight weight) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
if (hasEdge(v, w)) {
delete g[v][w];
if (!directed)
delete g[w][v];
m--;
}
g[v][w] = new Edge<Weight>(v, w, weight);
if (!directed)
g[w][v] = new Edge<Weight>(w, v, weight);
m++;
}
bool hasEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
return g[v][w] != NULL;
}
void show() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (g[i][j])
cout << g[i][j]->wt() << "\t";
else
cout << "NULL\t";
cout << endl;
}
}
class adjIterator {
private:
DenseGraph &G;
int v;
int index;
public:
adjIterator(DenseGraph &graph, int v) : G(graph) {
this->v = v;
this->index = -1;
}
Edge<Weight>* begin() {
index = -1;
return next();
}
Edge<Weight>* next() {
for (index += 1; index < G.V(); index++)
if (G.g[v][index])
return G.g[v][index];
return NULL;
}
bool end() {
return index >= G.V();
}
};
};
template<typename Weight>
class SparseGraph {
private:
int n, m;
bool directed;
vector<vector<Edge<Weight> *>> g;
public:
SparseGraph(int n, bool directed) {
this->n = n;
this->m = 0;
this->directed = directed;
for (int i = 0; i < n; i++)
g.push_back(vector<Edge<Weight> *>());
}
~SparseGraph() {
for (int i = 0; i < n; i++)
for (int j = 0; j < g[i].size(); j++)
delete g[i][j];
}
int V() { return n; }
int E() { return m; }
void addEdge(int v, int w, Weight weight) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
g[v].push_back(new Edge<Weight>(v, w, weight));
if (v != w && !directed)
g[w].push_back(new Edge<Weight>(w, v, weight));
m++;
}
bool hasEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
for (int i = 0; i < g[v].size(); i++)
if (g[v][i]->other(v) == w)
return true;
return false;
}
void show() {
for (int i = 0; i < n; i++) {
cout << "vertex " << i << ":\t";
for (int j = 0; j < g[i].size(); j++)
cout << "( to:" << g[i][j]->w() << ",wt:" << g[i][j]->wt() << ")\t";
cout << endl;
}
}
class adjIterator {
private:
SparseGraph &G;
int v;
int index;
public:
adjIterator(SparseGraph &graph, int v) : G(graph) {
this->v = v;
this->index = 0;
}
Edge<Weight>* begin() {
index = 0;
if (G.g[v].size())
return G.g[v][index];
return NULL;
}
Edge<Weight>* next() {
index += 1;
if (index < G.g[v].size())
return G.g[v][index];
return NULL;
}
bool end() {
return index >= G.g[v].size();
}
};
};
template<typename Graph, typename Weight>
class LazyPrimMST {
private:
Graph &G;
MinHeap<Edge<Weight>> pq;
bool *marked;
vector<Edge<Weight>> mst;
Weight mstWeight;
void visit(int v) {
assert(!marked[v]);
marked[v] = true;
typename Graph::adjIterator adj(G, v);
for (Edge<Weight>* e = adj.begin(); !adj.end(); e = adj.next())
if (!marked[e->other(v)])
pq.insert(*e);
}
public:
LazyPrimMST(Graph &graph) :G(graph), pq(MinHeap<Edge<Weight>>(graph.E())) {
marked = new bool[G.V()];
for (int i = 0; i < G.V(); i++)
marked[i] = false;
mst.clear();
visit(0);
while (!pq.isEmpty()) {
Edge<Weight> e = pq.extractMin();
if (marked[e.v()] == marked[e.w()])
continue;
mst.push_back(e);
if (!marked[e.v()])
visit(e.v());
else
visit(e.w());
}
mstWeight = mst[0].wt();
for (int i = 1; i < (int)mst.size(); i++)
mstWeight += mst[i].wt();
}
~LazyPrimMST() {
delete[] marked;
}
vector<Edge<Weight>> mstEdges() {
return mst;
};
Weight result() {
return mstWeight;
};
};
template<typename Graph, typename Weight>
class PrimMST {
private:
Graph &G;
vector<Edge<Weight>> mst;
bool* marked;
IndexMinHeap<Weight> ipq;
vector<Edge<Weight>*> edgeTo;
Weight mstWeight;
void visit(int v) {
assert(!marked[v]);
marked[v] = true;
typename Graph::adjIterator adj(G, v);
for (Edge<Weight>* e = adj.begin(); !adj.end(); e = adj.next()) {
int w = e->other(v);
if (!marked[w]) {
if (!edgeTo[w]) {
edgeTo[w] = e;
ipq.insert(w, e->wt());
}
else if (e->wt() < edgeTo[w]->wt()) {
edgeTo[w] = e;
ipq.change(w, e->wt());
}
}
}
}
public:
PrimMST(Graph &graph) :G(graph), ipq(IndexMinHeap<double>(graph.V())) {
assert(graph.E() >= 1);
marked = new bool[G.V()];
for (int i = 0; i < G.V(); i++) {
marked[i] = false;
edgeTo.push_back(NULL);
}
mst.clear();
visit(0);
while (!ipq.isEmpty()) {
int v = ipq.extractMinIndex();
assert(edgeTo[v]);
mst.push_back(*edgeTo[v]);
visit(v);
}
mstWeight = mst[0].wt();
for (int i = 1; i < (int)mst.size(); i++)
mstWeight += mst[i].wt();
}
~PrimMST() {
delete[] marked;
}
vector<Edge<Weight>> mstEdges() {
return mst;
};
Weight result() {
return mstWeight;
};
};
template <typename Graph, typename Weight>
class ReadGraph {
public:
ReadGraph(Graph &graph, const string &filename) {
ifstream file(filename);
string line;
int V, E;
assert(file.is_open());
assert(getline(file, line));
stringstream ss(line);
ss >> V >> E;
assert(graph.V() == V);
for (int i = 0; i < E; i++) {
assert(getline(file, line));
stringstream ss(line);
int a, b;
Weight w;
ss >> a >> b >> w;
assert(a >= 0 && a < V);
assert(b >= 0 && b < V);
graph.addEdge(a, b, w);
}
}
};
int main(void)
{
string filename = "testG2.txt";
int V = 250;
SparseGraph<double> g = SparseGraph<double>(V, false);
ReadGraph<SparseGraph<double>, double> readGraph(g, filename);
cout << "Test Lazy Prim MST:" << endl;
LazyPrimMST<SparseGraph<double>, double> lazyPrimMST(g);
vector<Edge<double>> mst = lazyPrimMST.mstEdges();
for (int i = 0; i < (int)mst.size(); i++)
cout << mst[i] << endl;
cout << "The MST weight is: " << lazyPrimMST.result() << endl;
cout << endl;
cout << "Test Prim MST:" << endl;
PrimMST<SparseGraph<double>, double> primMST(g);
mst = primMST.mstEdges();
for (int i = 0; i < (int)mst.size(); i++)
cout << mst[i] << endl;
cout << "The MST weight is: " << primMST.result() << endl;
cout << endl;
system("pause");
return 0;
}