邻接矩阵-C++

原创 2015年11月18日 19:57:47
#include <iostream>
using namespace std;

struct Edge
{
	int start;
	int dest;
	int weight;
	friend ostream &operator<<(ostream &out,Edge &e);
};

ostream &operator<<(ostream &out,Edge &e)
{
	out<<"("<<e.start<<","<<e .dest<<","<<e .weight<<")";
	return out;
}

#include "Edge.h"
#include "SeqList.h"
#include <iostream>
using namespace std;

const int MAX_WEIGHT=9999;

template<class T>
class AdjMatrixGraph
{
public:
	AdjMatrixGraph(int size=10);
	AdjMatrixGraph(T vertice[],int vertCount,Edge edges[],int edgeCount);
	~AdjMatrixGraph();

	int vertexCount();
	T Get(int i);
	void insertVertex(T vertex);
	bool insertEdge(int i,int j,int weight);
	bool insertEdge(Edge edge);
	friend ostream &operator<<<T>(ostream &out,AdjMatrixGraph<T> &graph);
	bool removeEdge(int i,int j);
	bool removeVertex(int v,T &old);

private:
	SeqList<T> vertexList;
	int **adjmatrix;
	int size;
	int vertCount;
	void init(int size);
};

template<class T>
AdjMatrixGraph<T>::AdjMatrixGraph(int size)
{
	init(size);
}

template<class T>
void AdjMatrixGraph<T>::init(int size)
{
	this->size=size<10?10:size;
	this->adjmatrix=new int *[this->size];
	for(int i=0;i <this->size;i++)
	{
		this->adjmatrix[i]=new int[this->size];
		for (int j=0;j<this->size;j++)
		{
			this->adjmatrix[i][j]=(i==j)?0:MAX_WEIGHT;
		}
		this->vertCount=0;
	}
}

template<class T>
AdjMatrixGraph<T>::AdjMatrixGraph(T vertice[],int vertCount,Edge edges[],int edgeCount)
{
	init(vertCount);
	for (int i=0;i<vertCount;i++)
	{
		insertVertex(vertice[i]);
	}
	for (int j=0;j<edgeCount;j++)
	{
		insertEdge(edges[j]);
	}
}

template<class T>
AdjMatrixGraph<T>::~AdjMatrixGraph()
{
	for (int i=0;i<size;i++)
	{
		delete(adjmatrix[i]);
	}
	delete(adjmatrix);
}

template<class T>
int AdjMatrixGraph<T>::vertexCount()
{
	return vertCount;
}

template<class T>
T AdjMatrixGraph<T>::Get(int i)
{
	return vertexList.get(i);
}

template<class T>
ostream &operator<<(ostream &out,AdjMatrixGraph<T> &graph)
{
	out<<"顶点集合:"<<graph.vertexList<<"邻接矩阵:\n";
	int n=graph.vertCount;
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			if (graph.adjmatrix[i][j]==MAX_WEIGHT)
			{
				out<<"∞ ";
			}
			else
			{
				out<<" "<<graph.adjmatrix[i][j]<<" ";
			}
		}
		out<<"\n";
	}
	return out;
}

template<class T>
void AdjMatrixGraph<T>::insertVertex(T vertex)
{
	vertexList.insert(vertex);
	vertCount++;
	if (vertCount>size)
	{
		int **temp=adjmatrix;
		adjmatrix=new int *[size*2];
		int i,j;
		for (i=0;i<size;i++)
		{
			adjmatrix[i]=new int[size*2];
			for (j=0;j<size;j++)
			{
				adjmatrix[i][j]=temp[i][j];
			}
			for (j=size;j<size*2;j++)
			{
				adjmatrix[i][j]=MAX_WEIGHT;
			}
		}
		for (i=size;i<size*2;i++)
		{
			adjmatrix[i]=new int[size*2];
			for (j=0;j<size*2;)
			{
				adjmatrix[i][j]=(i==j)?0:MAX_WEIGHT;
			}
		}
		size*=2;
	}
}

template<class T>
bool AdjMatrixGraph<T>::insertEdge(int i,int j,int weight)
{
	if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j&&adjmatrix[i][j]==MAX_WEIGHT)
	{
		adjmatrix[i][j]=weight;
		return true;
	}
	return false;
}

template<class T>
bool AdjMatrixGraph<T>::insertEdge(Edge edge)
{
	return insertEdge(edge.start,edge.dest,edge.weight);
}

template<class T>
bool AdjMatrixGraph<T>::removeEdge(int i,int j)
{
	if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j&&adjmatrix[i][j]!=MAX_WEIGHT)
	{
		adjmatrix[i][j]=MAX_WEIGHT;
		return true;
	}
	return false;
}

template<class T>
bool AdjMatrixGraph<T>::removeVertex(int v,T &old)
{
	if (v>=0&&v<vertCount&&vertexList.remove(v,old))
	{
		for (int i=v;i<vertCount-1;i++)
		{
			for (int j=0;j<vertCount;j++)
			{
				adjmatrix[i][j]=adjmatrix[i+1][j];
			}
		}

		for (int j=v;j<vertCount-1;j++)
		{
			for (int i=0;i<vertCount-1;i++)
			{
				adjmatrix[i][j]=adjmatrix[i][j+1];
			}
		}
		vertCount--;
		return true;
	}
	return false;
}

#include <iostream>
using namespace std;

template<class T>
class SeqList
{
public:
	SeqList(int size=64);
	SeqList(T value[],int n);
	~SeqList();

	bool isEmpty();
	int length();
	T get(int i);
	bool set(int i,T x);
	friend ostream &operator<<<T>(ostream &out,SeqList<T> &list);
	void insert(int i,T x);
	void insert(T x);
	bool remove(int i,T &old);
	void clear();

private:
	T *element;
	int size;
	int len;
};

template<class T>
SeqList<T>::SeqList(int size)
{
	this->size=size<64?64:size;
	this->element=new T[this->size];
	this->len=0;
}

template<class T>
SeqList<T>::SeqList(T value[],int n)
{
	if (n>0)
	{
		this->element=new T[2*n];
		this->size=2*n;
		this->len=n;

		for(int i=0;i <n;i ++)
		{
			this->element[i]=value[i];
		}
	}
}

template<class T>
SeqList<T>::~SeqList()
{
	delete []this->element;
}

template<class T>
bool SeqList<T>::isEmpty()
{
	return len==0;
}

template<class T>
int SeqList<T>::length()
{
	return len;
}

template<class T>
T SeqList<T>::get(int i)
{
	return element[i];
	/*if (i >=0&&i<len)
	{
	return element[i];
	}
	return "false";*/
}

template<class T>
bool SeqList<T>::set(int i,T x)
{
	if (i>=0&&i<len)
	{
		element[i]=x;
		return true;
	}
	return false;
}

template<class T>
ostream &operator<<(ostream &out,SeqList<T> &list)
{
	out<<"(";
	if (list.len>0)
	{
		out<<list.element[0];
		for (int i=1;i<list.len;i++)
		{
			out<<","<<list.element[i];
		}
	}
	out<<")\n";
	return out;
}

template<class T>
void SeqList<T>::insert(int i,T x)
{
	if (len==size)
	{
		T *temp=element;
		element=new T[size*2];
		for (int i=0;i<size;i ++)
		{
			element[i]=temp[i];
		}
		size*=2;
	}
	if (i<0)
	{
		i=0;
	}
	if (i>len)
	{
		i=len;
	}
	for (int j=len-1;j>=i;j--)
	{
		element[j+1]=element[j];
	}
	element[i]=x;
	len++;
}

template<class T>
void SeqList<T>::insert(T x)
{
	insert(len,x);
}

template<class T>
bool SeqList<T>::remove(int i,T &old)
{
	if (len>0&&i>=0&&i<len)
	{
		old=element[i];
		for (int j=i;j<len;j++)
		{
			element[j]=element[j+1];
		}
		len--;
		return true;
	}
	return false;
}

template<class T>
void SeqList<T>::clear()
{
	len=0;
}
#include "AdjMatrixGraph.h"
#include <iostream>
using namespace std;

int main()
{
	char *vertices="ABCDE";
	Edge edges[]={{0,1,5},{0,3,2},{1,0,5},{1,2,7},{1,3,6},{2,1,7},{2,3,8},{2,4,3},{3,0,2},{3,1,6},{3,2,8},{3,4,9},{4,2,3},{4,3,9}};
	AdjMatrixGraph<char> graph(vertices,5,edges,14);
	cout<<"带权无向图G3,\n"<<graph<<endl;
	graph.insertVertex('F');
	cout<<"插入顶点F,插入边(A,F,20)?"<<graph.insertEdge(0,5,20)<<"\n";
	char old=' ';
	if (graph.removeVertex(2,old))
	{
		cout<<"删除顶点"<<old<<"\n";
	}
	cout<<"删除边(v2,v3),"<<(graph.removeEdge(2,3)&&graph.removeEdge(3,2))<<"\n";
	cout<<graph<<endl;
	system("pause");
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C++ 邻接矩阵

  • 2011-12-13 15:38
  • 20KB
  • 下载

C++邻接矩阵

邻接矩阵图的应用c++

注:若有错误或要改善的地方请告之,谢谢,新手上路,有些函数还是很冗余的,慢慢改进。 头文件: #define MAXSIZE 100 typedef enum { NN, NY, ...

C++ 数据结构 邻接矩阵

C++ 邻接矩阵

  • 2014-10-06 13:43
  • 124KB
  • 下载

算法与数据结构基础9:C++实现有向图——邻接矩阵存储

邻接矩阵的存储比邻接表实现起来更加方便,也更加容易理解。 邻接矩阵就是用一个二维数组matrix来存储每两个点的关系。如果两个点m,n之间有边,将数组matrix[]m[m]设为1,否则设为0。 如果...

c++邻接矩阵源码

  • 2013-04-10 09:10
  • 2.22MB
  • 下载

图的邻接矩阵的C++实现

该程序实现的主要是无向图。实现图这样的数据结构,透彻理解图的性质是很重要的。在弄明白了图的性质后再结合一些常见的编程思想,完成无向图的邻接矩阵应该不是很难。 // Graph1.h: interfac...

邻接矩阵C++代码

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)