Adjacency Matrix of Graph(图的邻接矩阵)

//本文件是图的邻接矩阵的头文件,使用C++模板类封装(This file is the header file of adjacency matrix of graph,and packed by C++ template class)
#ifndef MGRAPH_H
#define MGRAPH_H
#include 
#include "SeqList.h"	//“SeqList.h”为我的博客里的一片文章,同在“证明我学过数据结构的痕迹”分类里
using namespace std;

#define MAXINT 1<<32-1
const int MaxGraphSize=1;

template 
class MGraph
{
	public:
    		//Constructor
		MGraph(void);
		
		//Data access functions
		int NumberOfVertices(void) const;
		int GetWeight(const T& vertex1,const T& vertex2);
		int GetFirstNeighbor(const int v);
		int GetNextNeighbor(const int v1,const int v2);
		SeqList 	GetVertex(int v);								
		SeqList& GetNeighbors(const T& vertex);	
		
		//The update funtion of graph
		void InsertVertex(const T& vertex);
		void InsertEdge(const T& vertex1,const T& vertex2,int weight);
		void DeleteVertex(const T& vertex);
		void DeleteEdge(const T& vertex1,const T& vertex2);
		
		// SeqList& BFS(const T& begin_vertex);       		//Not instantiate
		// void DFS();							//Not instantiate
		// void DFS(const int v,int visited[]);				//Not instantiate
      
		// void Components();	//Get connected component		//Not instantiate
		
		// void DfnLow(const int v);	//Get articulation point	//Not instantiate
		// void DfnLow(const int u,const int v); 			//Not instantiate
	private:
		SeqList vertex_list;											
		int edge[MaxGraphSize][MaxGraphSize];
		int num_of_vertices,num_of_arcs;
		// int FindVertex(SeqList &L,const T& vertex);//Not instantiate
		int GetVertexPos(const T& vertex);           
};

//Constructor
template 
MGraph::MGraph(void)
{
	for(int i=0;i
int MGraph::NumberOfVertices(void) const
{
	return num_of_vertices;
}

//Instantiate function GetWeight
template 
int MGraph::GetWeight(const T& vertex1,const T& vertex2)
{
	int pos1=GetVertexPos(vertex1),pos2=GetVertexPos(vertex2);
	if(-1==pos1||-1==pos2)
	{
		cerr<<"GetWeight:avertex is not in the graph."<
int MGraph::GetFirstNeighbor(const int v)
{
	if(v<0||v>num_of_vertices)
	{
		cerr<<"The vertex is not in the graph."<0&&edge[v][i]
int MGraph::GetNextNeighbor(const int v1,const int v2)
{
	if(v1<0||v1>num_of_vertices||v2<0||v2>num_of_vertices)
	{
		cerr<<"The vertex is not in the graph."<0&&edge[v1][i]
void MGraph::InsertVertex(const T& vertex)
{
	if(num_of_vertices+1>MaxGraphSize)
	{
		cerr<<"Grpah is full."<
void MGraph::InsertEdge(const T& vertex1,const T& vertex2,int weight)
{
	int pos1=GetVertexPos(vertex1),pos2=GetVertexPos(vertex2);
	
	if(-1==pos1||-1==pos2)
	{
		cerr<<"InsertEdge:a vertex is not in the graph."<
void MGraph::DeleteVertex(const T& vertex)
{
	int pos=GetVertexPos(vertex);
	int row,col;
	
	if(-1==pos)
	{
		cerr<<"DeleteVertex:a vertex is not in the graph."<
void MGraph::DeleteEdge(const T& vertex1,const T& vertex2)
{
	int pos1=GetVertexPos(vertex1),pos2=GetVertexPos(vertex2);
	
	if(-1==pos1||-1==pos2)
	{
		cerr<<"DeleteEdge:a vertex is not in the graph."<
int MGraph::GetVertexPos(const T& vertex)
{
  return vertex_list.Search(vertex);
}
#endif



你可能感兴趣的:(证明我学过数据结构的痕迹)