邻接矩阵图类实现(C++)

/* * AdjMWGraph.h * * Created on: Jul 15, 2010 * Author: kevin */ #ifndef ADJMWGRAPH_H_ #define ADJMWGRAPH_H_ #include "SeqList.h" #include "SeqQueue.h" #include <stdlib.h> #include <limits> #include <iostream> using namespace std; //typedef int T; const int MaxVertices = 100; const int MaxWeight = numeric_limits <int> ::max() ; template <class T> class AdjMWGraph { private: SeqList<T>* Vertices; int **Edge; int numOfEdges; void DepthFirstSearch(const int v, int visted[], void Visit(T item)); void BroadFirstSearch(const int v, int visted[], void Visit(T item)); void my_assert(bool val, char* err_info); int** delete_row_and_col(int** edges,const int size, const int v); void delete_sqr_mtrx(int **arr, int sz); public: AdjMWGraph(const int sz = MaxVertices); ~AdjMWGraph(); int NumOfEdges() {return numOfEdges;}; int NumOfVertices(); T GetValue(const int v); int GetWeight(const int v1, const int v2); void InsertVertex(const T & vertex); void InsertEdge(const int v1, const int v2, int weight); void DeleteVertex(const int v); void DeleteEdge(const int v1, const int v2); int GetFirstNeighbor(const int v); int GetNextNeighbor(const int v1, const int v2); void DepthFirstSearch(void Visit(T item)); void BroadFirstSearch(void Visit(T item)); }; template<class T> AdjMWGraph<T>::AdjMWGraph(const int sz) { this->Vertices = new SeqList<T>(sz); Edge =new int* [sz]; for(int i=0; i < sz; ++i) Edge[i] = new int[sz]; for(int i=0; i < sz; ++i) for(int j=0; j < sz; ++j) { if(i==j) Edge[i][j] = 0; else Edge[i][j] = MaxWeight; } this->numOfEdges = 0; } template<class T> AdjMWGraph<T>::~AdjMWGraph() { // TODO Auto-generated destructor stub int sz = Vertices->Size(); delete_sqr_mtrx(Edge,sz); delete Vertices; } template<class T> T AdjMWGraph<T>::GetValue(const int v) { my_assert(v >= 0 && v <= Vertices->Size(), "Index out of boundary in T AdjMWGraph<T>::GetValue(...)"); return Vertices->GetData(v); } template<class T> void AdjMWGraph<T>::my_assert(bool val, char* err_info) { if(!val) { cout<<err_info<<endl; exit(0); } } template<class T> int AdjMWGraph<T>::GetWeight(const int v1, const int v2) { my_assert(v1 >= 0 && v1 <= Vertices->Size(), "Index v1 out of boundary in int AdjMWGraph<T>::GetWeight(...)"); my_assert(v2 >= 0 && v2 <= Vertices->Size(), "Index v2 out of boundary in int AdjMWGraph<T>::GetWeight(...)"); return Edge[v1][v2]; } template<class T> void AdjMWGraph<T>::InsertVertex(const T & vertex) { this->Vertices->Insert(vertex, Vertices->Size()); } template<class T> void AdjMWGraph<T>::InsertEdge(const int v1, const int v2, int weight) { my_assert(v1 >= 0 && v1 <= Vertices->Size(), "Index v1 out of boundary in void AdjMWGraph<T>::InsertEdge(...)"); my_assert(v2 >= 0 && v2 <= Vertices->Size(), "Index v2 out of boundary in void AdjMWGraph<T>::InsertEdge(...)"); Edge[v1][v2] = weight ; this->numOfEdges++; } template<class T> void AdjMWGraph<T>::DeleteVertex(const int v) { int i,j, n = Vertices->Size(); for(i=0;i<n;i++) for(j=0;j<n;j++) { if((i==v||j==v) && Edge[i][j]>0 && Edge[i][j] < MaxWeight) { numOfEdges--; } } int** tmp = delete_row_and_col(Edge,n,v); delete_sqr_mtrx(Edge,n); Edge = tmp; Vertices->Delete(v); } template<class T> void AdjMWGraph<T>::DeleteEdge(const int v1,const int v2) { my_assert(v1 >= 0 && v1 <= Vertices->Size(), "Index v1 out of boundary in void AdjMWGraph<T>::DeleteEdge(...)"); my_assert(v2 >= 0 && v2 <= Vertices->Size(), "Index v2 out of boundary in void AdjMWGraph<T>::DeleteEdge(...)"); Edge[v1][v2]=MaxWeight; numOfEdges--; } template<class T> int AdjMWGraph<T>::GetFirstNeighbor(const int v) { my_assert(v >= 0 && v <= Vertices->Size(), "Index v1 out of boundary in int AdjMWGraph<T>::GetFirstNeighbor(...)"); int col; for(col=0;col<Vertices->Size();col++) if(Edge[v][col]>0&&Edge[v][col]<MaxWeight) return col; return -1; } template<class T> int AdjMWGraph<T>::GetNextNeighbor(const int v1,const int v2) { my_assert(v1 >= 0 && v1 <= Vertices->Size(), "Index v1 out of boundary in int AdjMWGraph<T>::GetNextNeighbor(...)"); my_assert(v2 >= 0 && v2 <= Vertices->Size(), "Index v2 out of boundary in int AdjMWGraph<T>::GetNextNeighbor(...)"); int col; for(col=v2+1;col<Vertices->Size();col++) if(Edge[v1][col] > 0 && Edge[v1][col] < MaxWeight) return col; return -1; } template<class T> void AdjMWGraph<T>::DepthFirstSearch(const int v, int visited[], void Visit(T item)) { Visit(GetValue(v)); visited[v] = 1; int w = GetFirstNeighbor(v); while( w!=-1 ) { if(!visited[w]) DepthFirstSearch(w,visited,Visit); w = GetNextNeighbor(v,w); } } template<class T> void AdjMWGraph<T>::DepthFirstSearch(void Visit(T item)) { int * visited = new int[this->NumOfVertices()]; for(int i=0; i < this->NumOfVertices(); ++i) { visited[i] = 0; } for(int i=0; i < this->NumOfVertices(); ++i) { if(!visited[i]) { DepthFirstSearch(i,visited,Visit); } } delete []visited; } template<class T> void AdjMWGraph<T>::BroadFirstSearch(void Visit(T item)) { int * visited = new int[this->NumOfVertices()]; for(int i=0; i < this->NumOfVertices(); ++i) { visited[i] = 0; } for(int i=0; i < this->NumOfVertices(); ++i) { if(!visited[i]) { BroadFirstSearch(i,visited,Visit); } } delete []visited; } template<class T> int AdjMWGraph<T>::NumOfVertices() { return Vertices->Size(); }; template<class T> void AdjMWGraph<T>::BroadFirstSearch(const int v, int visited[], void Visit(T item)) { T u,w; SeqQueue<int>* queue =new SeqQueue<int>(); Visit(GetValue(v)); visited[v] = 1; queue->in(v); while( !queue->isEmpty() ) { u = queue->out(); w = this->GetFirstNeighbor(u); while(w!=-1) { if(!visited[w]) { Visit(GetValue(w)); visited[w] = 1; queue->in(w); } w = this->GetNextNeighbor(u,w); } } } template<class T> int** AdjMWGraph<T>::delete_row_and_col(int** edges,const int size, const int v) { int **tmp =new int*[size-1]; for(int i=0;i<size-1;++i) { tmp[i] = new int[size-1]; } int i,j; //Delete the v th row for(i=0; i<v; i++) for(j=0;j<v;j++) tmp[i][j] = Edge[i][j]; //Delete the v th column for(i=v; i<size-1; i++) for(j=0;j<v;j++) tmp[i][j] = Edge[i+1][j]; for(i=0; i<v; i++) for(j=v;j<size-1;j++) tmp[i][j] = Edge[i][j+1]; for(i=v; i<size-1; i++) for(j=v;j<size-1;j++) tmp[i][j] = Edge[i+1][j+1]; return tmp; } template<class T> void AdjMWGraph<T>::delete_sqr_mtrx(int** arr, int sz) { for(int i=0; i < sz; ++i) delete[] arr[i]; delete[] arr; } //typedef char DataType; #endif /* ADJMWGRAPH_H_ */

你可能感兴趣的:(c,delete,Class,ini,2010,destructor)