#ifndef GRAPH_H #define GRAPH_H #include #include #include using namespace std; #include "point.h" #include "tplane.h" #include "binheap.h" template class Graph; template struct Vertex; template class vCompare; template ostream& operator <<(ostream& s, const Vertex& rhs); template struct Vertex{ T p; int edges; double dist; bool visited; bool onqueue; Vertex* path; int graphpos; int bheappos; bool processed; vector adj; vector child; Vertex(int gpos=1, T inp=NULL, int n=20000) : \ p(inp), edges(0), adj(), visited(false), processed(false), dist(INFINITY), path(NULL), graphpos(gpos), bheappos(0) {adj.reserve(n);} Vertex(T inp=NULL) : \ p(inp), edges(0), adj(), visited(false), processed(false), onqueue(false), dist(INFINITY), path(NULL), graphpos(0), bheappos(0) { adj.reserve(20000); } const Vertex& operator=(const Vertex &rhs) { if (this != &rhs) { p = rhs.p; edges = rhs.adj.size(); // adj.clear(); adj.resize(rhs.adj.size()); for(int i=0; i<(int)rhs.adj.size(); ++i) adj[i] = rhs.adj[i]; visited = rhs.visited; processed = rhs.processed; onqueue = rhs.onqueue; dist = rhs.dist; path = rhs.path; // child.clear(); for(int i=0; i<(int)rhs.child.size(); ++i) child.push_back(rhs.child[i]); graphpos = rhs.graphpos; bheappos = rhs.bheappos; } return *this; } bool operator==(Vertex& rhs) { return( dist == rhs.dist); } bool operator<(Vertex& rhs) { return( dist < rhs.dist); } bool operator<(Vertex* rhs) { return( dist < rhs->dist); } bool operator>(Vertex& rhs) { return( dist > rhs.dist); } bool operator>(Vertex* rhs) { return( dist > rhs->dist); } friend ostream& operator<< <>(ostream& s, const Vertex& rhs); friend ostream& operator<<(ostream& s, Vertex* rhs) { return(s << (*rhs)); } // Distance functions for arbitrary types. double distance(Vertex& rhs) {return p->distance(rhs.p);} double distance(Vertex* rhs) {return p->distance(rhs->p);} }; template ostream& operator<<(ostream& s, const struct Vertex& rhs) { s << rhs.p; if(rhs.processed) s << " T "; else s << " F "; if(rhs.onqueue) s << " on "; else s << " off "; s << "d_v = " << (float)rhs.dist; s << std::endl; return s; } template class vCompare{ public: bool operator()(Vertex* v1, Vertex* v2) {return (*v1) < (*v2);} }; template class Graph; template class Graph { public: Graph() : vertexMap() {}; void addVertex(int i, T p, int n); void addEdge(int i, T p1, int j, T p2); void addEdge(int i, int j); int prims(int isp, double rho); void path(int ip, T dp, vector *p); // void consistency(); // void rconsistency(Vertex *v); void dfs(vector *p); void rdfs(Vertex *v, vector *p); void edges(vector *p); void bfsedges(Vertex *v, vector *p); void resize(int n) {vertexMap.resize(n,0);} void clear(); void clearMSF(); bool empty() {return vertexMap.empty();} private: vector* > vertexMap; vector* > mstRoot; Vertex* getVertex(T p); binheap bh; }; #endif