图的邻接矩阵实现_MGraph

邻接矩阵有两种, 不带权图和网的邻接矩阵. 不带权图的邻接矩阵元素为0或1, 网的邻接矩阵中包含0, INF, 和边上的权值, 权值类型T可

为整型, 实型. 三元组(u, v, w)代表一条边, u, v是边的两个定点, w表示u v的关系: 

a[u][u] = 0, 两种邻接矩阵的主对角元素都是0. a[u][v] = w, 若<u, v> 在E中, 则w = 1(不带权图)或w = w(i, j)(网). 若<u, v>不在E中, 

则w = noEdge, noEdge = 0(不带权图)或noEdge = INF(网).

保护数据成员T **a指向动态生成的二维数组, 用来存储邻接矩阵.

包含的函数Exist(): 若输入参数u, v无效或a[u][v] == noEdge, 则不存在边<u, v>, 返回false, 否则返回true.

函数Insert(): 若输入参数u, v无效返回Failure. 若a[u][v] != noEdge, 表示边<u, v>已经存在, 函数返回Duplicate. 否则添加边<u, v>, 返回

Success, 具体做法: a[u][v] = w, e++.

函数Remove(): 若输入参数u, v无效, 不能执行删除运算, 返回Failure. 若a[u][v] == noEdge, 表示图中不存在边<u, v>, 函数返回

Notpresent. 否则从邻接矩阵中删除边<u, v>, 返回Success, 具体做法: a[u][v] = noEdge, e--.

实现代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
enum ResultCode { Underflow, Overflow, Success, Duplicate, NotPresent, Failure };
template <class T>
class Graph
{
public:
	virtual	~Graph() {};
	virtual ResultCode Insert(int u, int v, T &w) = 0;
	virtual ResultCode Remove(int u, int v) = 0;
	virtual bool Exist(int u, int v) const = 0;
	/* data */
};
template <class T>
class MGraph: public Graph<T>
{
public:
	MGraph(int mSize, const T& noedg);
	~MGraph();
	ResultCode Insert(int u, int v, T &w);
	ResultCode Remove(int u, int v);
	bool Exist(int u, int v) const;
	int Vertices() const { return n; }
	void Output();
protected:
	T **a;
	T noEdge;
	int n, e;
	/* data */
};
template <class T>
void MGraph<T>::Output()
{
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j)
			if(a[i][j] == noEdge) cout << "NE\t";
			else cout << a[i][j] << "\t";
		cout << endl;
	}
	cout << endl << endl << endl;
}
template <class T>
MGraph<T>::MGraph(int mSize, const T &noedg)
{
	n = mSize, e = 0, noEdge = noedg;
	a = new T *[n];
	for(int i = 0; i < n; ++i) {
		a[i] = new T[n];
		for(int j = 0; j < n; ++j)
			a[i][j] = noEdge;
		a[i][i] = 0;
	}
}
template <class T>
MGraph<T>::~MGraph()
{
	for(int i = 0; i < n; ++i)
		delete []a[i];
	delete []a;
}
template <class T>
bool MGraph<T>::Exist(int u, int v) const
{
	if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v || a[u][v] == noEdge) return false;
	return true;
}
template <class T>
ResultCode MGraph<T>::Insert(int u, int v, T &w)
{
	if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v) return Failure;
	if(a[u][v] != noEdge) return Duplicate;
	a[u][v] = w;
	e++;
	return Success; 
}
template <class T>
ResultCode MGraph<T>::Remove(int u, int v)
{
	if(u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v) return Failure;
	if(a[u][v] == noEdge) return NotPresent;
	a[u][v] = noEdge;
	e--;
	return Success;
}
int main(int argc, char const *argv[])
{
	MGraph<int> mg(4, 99);
	int w = 4; mg.Insert(1, 0, w); mg.Output();
	w = 5; mg.Insert(1, 2, w); mg.Output();
	w = 3; mg.Insert(2, 3, w); mg.Output();
	w = 1; mg.Insert(3, 0, w); mg.Output();
	w = 1; mg.Insert(3, 1, w); mg.Output();
	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值