您的位置:首页 > 其它

Implement a undirected graph,represent by a adjacent matrix

2008-11-21 12:53 246 查看
and this a undirected graph represent by adjacent list..
http://blog.csdn.net/Non_Recursive/archive/2008/11/26/3377728.aspx

//MatrixGraph.h

#include<iostream>

using namespace std;

const int maxWeight = 10000; //infinity number

const int defaultVertices = 30; //default the biggest number of vertices (=n)

/*

template<typename T,typename E>

struct Graph

{

int maxVertices;

int numEdges,numVertices; //current number of edge and vertices

//T GetVertexPos(T vertex); //get the position of the current vertex in graph

};

*/

//use Adjacent matrix represent graph's class definition

template<typename T,typename E>

class MatrixGraph

{

int maxVertices;

int numEdges,numVertices; //current number of edge and vertex

T *VerticesList; //vertex table

E **Edge; //Adjacent matrix

int GetVertexPos(T vertex) //get the position of the current vertex in graph

{

for(int i=0;i<numVertices;i++)

if (VerticesList[i] == vertex)

return i;

return -1;

}

friend istream& operator >>(istream& in,MatrixGraph<T,E>& G);

friend ostream& operator <<(ostream& out,MatrixGraph<T,E>& G);

public:

MatrixGraph(int sz=defaultVertices);

~MatrixGraph()

{

delete [] VerticesList;

delete [] Edge;

}

T GetValue(int i) //get the vertex's value.

{

return i>=0 && i<=numVertices ? VerticesList[i] : NULL;

}

E GetWeight(int v1,int v2)

{

return v1 != -1 && v2 != -1 ? Edge[v1][v2] : 0;

}

int NumberOfVertices() { return numVertices;}

int NumberOfEdges() { return numEdges;}

int GetFirstNeighbor(int v);

//get adjacent vertex of w and w is the neighbor of v

int GetNextNeightbor(int v,int w);

bool InsertVertex(const T& vertex);

bool InsertEdge(int v1,int v2,E cost);

bool RemoveVertex(int v);

bool RemoveEdge(int v1,int v2);

};

/MatrixGraph.cpp

//implement of the MatrixGraph

#include "MatrixGraph.h"

template<typename T,typename E>

MatrixGraph<T,E>::MatrixGraph(int sz)

{

maxVertices = sz;

numVertices = numEdges = 0;

VerticesList = new T[maxVertices]; //create array of vertex table

Edge = new int *[maxVertices]; //create array of AdjacentMatrix

for (int i=0;i<maxVertices;i++)

Edge[i] = new int[maxVertices];

for (int i=0;i<maxVertices;i++)

for (int j=0;j<maxVertices;j++)

Edge[i][j] = (i==j) ? 0 : maxWeight;

}

template<typename T,typename E>

int MatrixGraph<T,E>::GetFirstNeighbor(int v)

{

if (v != -1)

for (int col=0;col<numVertices;col++)

if (Edge[v][col]>0 && Edge[v][col]<maxWeight) return col;

return -1;

}

template<typename T,typename E>

int MatrixGraph<T,E>::GetNextNeightbor(int v,int w)

{

//get adjacent vertex of 'w' and which 'w' if the adjacent vertex of' v'

if ((v != -1) && (w != -1))

for(int col=w+1;col<numVertices;col++)

if (Edge[v][col]>0 && Edge[v][col]<maxWeight) return col;

return -1;

}

template<typename T,typename E>

bool MatrixGraph<T,E>::InsertVertex(const T& vertex)

{

if (numVertices == maxVertices) return false;

VerticesList[numVertices++] = vertex;

return true;

}

template<typename T,typename E>

bool MatrixGraph<T,E>::RemoveVertex(int v)

{

//remove vertex and edge which is related with this vertex

if (v<0 && v>=numVertices) return false;

if (numVertices == 1) return false; //only one vertex leave,don't delete it

//remove this vertex from vertex table

VerticesList[v] = VerticesList[numVertices-1];

for (int i=0;i<numVertices;i++) //delete edge which is related with 'v'

if (Edge[i][v]>0 && Edge[i][v]<maxWeight)

numEdges--;

for (int j=0;j<numVertices;j++)

Edge[j][v] = Edge[j][numVertices-1]; //use the last column to fill with the 'v' column

numVertices--;

for (int k=0;k<numVertices;k++) //use the last row to fill with the 'v' row

Edge[v][k] = Edge[numVertices][k];

return true;

}

template<typename T,typename E>

bool MatrixGraph<T,E>::InsertEdge(int v1,int v2,E cost)

{

if ((v1>-1 && v1<numVertices) &&

(v2>-1 && v2<numVertices) && Edge[v1][v2] == maxWeight)

{

Edge[v1][v2] = Edge[v2][v1] = cost;

numEdges++;

return true;

}

return false;

}

template<typename T,typename E>

bool MatrixGraph<T,E>::RemoveEdge(int v1,int v2)

{

if((v1>-1 && v1<numVertices) && (v2>-1 && v2<numVertices) &&

(Edge[v1][v2]>0 && Edge[v1][v2]<maxWeight))

{

Edge[v1][v2] = Edge[v2][v1] = maxWeight;

numEdges--;

return true;

}

return false;

}

template<typename T,typename E>

istream& operator >>(istream& in,MatrixGraph<T,E>& G)

{

int n,m;

T v1,v2;

E weight;

cout<<"please the numbers of vertex and edge:";

cin>>n>>m; //input vertices number,edges number;

for (int i=0;i<n;i++) //create data of vertex table

{

in>>v1;

G.insertVertex(v1);

}

int j=0;

while (j<m)

{

in>>v1>>v2>>weight; //input information

int pos1 = G.GetVertexPos(v1);

int pos2 = G.GetVertexPos(v2);

if (pos1==-1 || pos2==-1) cout<<"Error,please input again"<<endl;

else j++,G.InsertEdge(pos1,pos2,weight);

}

return in;

}

template<typename T,typename E>

ostream& operator <<(ostream& out,MatrixGraph<T,E>& G)

{

//out all the information of graph's vertices and edges

int n = G.NumberOfVertices(),m = G.NumberOfEdges();

cout<<"number of vertices is "<<n<<", number of edges if "<<m<<endl;

for (int i=0;i<n;i++)

for (int j=i+1;j<n;j++)

{

int w = G.GetWeight(i,j);

if (w>0 && w<maxWeight)

{

int e1 = G.GetValues(i),e2 = G.GetValue(j);

out<<"("<<e1<<","<<e2<<","<<w<<")"<<endl;

}

}

return out;

}

//a test for class MatrixGraph

#include "MatrixGraph.cpp"

int main(void)

{

MatrixGraph<int,int> AdjMatG(10);

int n,m;

int v,v1,v2;

int weight;

cout<<"please the numbers of vertex and edge:";

cin>>n>>m; //input vertices number,edges number;

for(int i=0;i<n;i++)

{

cin>>v;

AdjMatG.InsertVertex(v); //insert vertex

}

for(int j=0;j<m;j++)

{

cin>>v1>>v2>>weight;

AdjMatG.InsertEdge(v1,v2,weight);

}

AdjMatG.Display();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: