图的邻接矩阵实现_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
    评论
以下是 C++ 代码实现: ```cpp #include<iostream> #include<queue> using namespace std; // 邻接表存储结构 #define MAX_VERTEX_NUM 100 typedef struct ArcNode { int adjvex; // 该弧指向的顶点的位置 struct ArcNode *nextarc; // 指向下一条弧的指针 //InfoType info; // 网的边权值 } ArcNode; typedef struct VNode { char data; // 顶点信息 ArcNode *firstarc; // 指向第一条依附该顶点的弧的指针 } VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum, arcnum; // 的当前顶点数和弧数 } LGraph; // 邻接矩阵存储结构 #define INFINITY INT_MAX typedef struct { char vexs[MAX_VERTEX_NUM]; // 顶点向量 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵,可看作边表 int vexnum, arcnum; // 的当前顶点数和弧数 } MGraph; // 邻接表表示的的递归深度优先遍历 void DFS(LGraph g, int i, int *visited) { visited[i] = 1; cout << g.vertices[i].data << " "; ArcNode *p = g.vertices[i].firstarc; while (p != NULL) { if (visited[p->adjvex] == 0) { DFS(g, p->adjvex, visited); } p = p->nextarc; } } void LDFS(LGraph g, int i) { int visited[MAX_VERTEX_NUM] = { 0 }; DFS(g, i, visited); } // 邻接矩阵表示的的递归深度优先遍历 void MDFS(MGraph g, int i, int *visited) { visited[i] = 1; cout << g.vexs[i] << " "; for (int j = 0; j < g.vexnum; j++) { if (g.arcs[i][j] != 0 && visited[j] == 0) { MDFS(g, j, visited); } } } void MDFS(MGraph g, int i) { int visited[MAX_VERTEX_NUM] = { 0 }; MDFS(g, i, visited); } // 邻接表表示的的广度优先遍历 void LBFS(LGraph g, int s) { queue<int> q; int visited[MAX_VERTEX_NUM] = { 0 }; visited[s] = 1; cout << g.vertices[s].data << " "; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); ArcNode *p = g.vertices[v].firstarc; while (p != NULL) { if (visited[p->adjvex] == 0) { visited[p->adjvex] = 1; cout << g.vertices[p->adjvex].data << " "; q.push(p->adjvex); } p = p->nextarc; } } } // 邻接矩阵表示的的广度优先遍历 void MBFS(MGraph g, int s) { queue<int> q; int visited[MAX_VERTEX_NUM] = { 0 }; visited[s] = 1; cout << g.vexs[s] << " "; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int j = 0; j < g.vexnum; j++) { if (g.arcs[v][j] != 0 && visited[j] == 0) { visited[j] = 1; cout << g.vexs[j] << " "; q.push(j); } } } } // 建立的邻接表 void Create_Graph(LGraph &g, MGraph &mg) { // 邻接表 g.vexnum = mg.vexnum; g.arcnum = mg.arcnum; for (int i = 0; i < mg.vexnum; i++) { g.vertices[i].data = mg.vexs[i]; g.vertices[i].firstarc = NULL; } for (int i = 0; i < mg.vexnum; i++) { for (int j = 0; j < mg.vexnum; j++) { if (mg.arcs[i][j] != 0) { ArcNode *p = new ArcNode; p->adjvex = j; p->nextarc = g.vertices[i].firstarc; g.vertices[i].firstarc = p; } } } // 邻接矩阵 for (int i = 0; i < mg.vexnum; i++) { for (int j = 0; j < mg.vexnum; j++) { if (mg.arcs[i][j] == INFINITY) { mg.arcs[i][j] = 0; } } } } int main() { LGraph lg; MGraph mg; // 初始化邻接矩阵 mg.vexnum = 5; mg.arcnum = 7; for (int i = 0; i < mg.vexnum; i++) { for (int j = 0; j < mg.vexnum; j++) { mg.arcs[i][j] = INFINITY; } } mg.vexs[0] = 'A'; mg.vexs[1] = 'B'; mg.vexs[2] = 'C'; mg.vexs[3] = 'D'; mg.vexs[4] = 'E'; mg.arcs[0][1] = 1; mg.arcs[0][2] = 1; mg.arcs[1][2] = 1; mg.arcs[1][3] = 1; mg.arcs[2][0] = 1; mg.arcs[2][3] = 1; mg.arcs[3][4] = 1; // 建立邻接表 Create_Graph(lg, mg); // 邻接表表示的的递归深度优先遍历 LDFS(lg, 0); cout << endl; // 邻接矩阵表示的的递归深度优先遍历 MDFS(mg, 0); cout << endl; // 邻接表表示的的广度优先遍历 LBFS(lg, 0); cout << endl; // 邻接矩阵表示的的广度优先遍历 MBFS(mg, 0); cout << endl; return 0; } ``` 注意:以上代码只是一种实现方法,不一定是最优的。在实际应用中,还需要考虑的具体特征和需求,选择合适的存储结构和算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值