采用邻接矩阵表示法,创建无向图graph。实现功能:1、打印无向图void printUDN(AMGraph graph) 2、增加顶点Status InsertVex(AMGraph &G, int v)3、增加边Status DeleteVex(AMGraph &G, int v) 4、删除顶点Status InsertArc(AMGraph &G, int v, int w)5、删除边Status DeleteArc( AMGraph &G, int v, int w)
时间: 2024-03-07 12:47:14 浏览: 57
无向图的建立和邻接矩阵的输出
5星 · 资源好评率100%
以下是基于邻接矩阵表示法的无向图的实现代码:
```c++
#include <iostream>
using namespace std;
const int MAX_VERTEX_NUM = 20;
const int INF = INT_MAX;
typedef char VertexType;
typedef int EdgeType;
typedef enum { UNVISITED, VISITED } VisitIf;
typedef struct {
VertexType vexs[MAX_VERTEX_NUM]; // 存储顶点元素
EdgeType arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边的权值
int vexnum, arcnum; // 顶点数和边数
} AMGraph;
// 打印无向图
void printUDN(AMGraph graph) {
for (int i = 0; i < graph.vexnum; i++) {
for (int j = 0; j < graph.vexnum; j++) {
if (graph.arc[i][j] == INF) {
cout << "∞" << " ";
} else {
cout << graph.arc[i][j] << " ";
}
}
cout << endl;
}
}
// 增加顶点
int LocateVex(AMGraph graph, VertexType v) {
for (int i = 0; i < graph.vexnum; i++) {
if (v == graph.vexs[i]) {
return i;
}
}
return -1;
}
bool InsertVex(AMGraph &G, VertexType v) {
if (G.vexnum == MAX_VERTEX_NUM) {
return false;
}
G.vexs[G.vexnum] = v;
for (int i = 0; i < G.vexnum; i++) {
G.arc[i][G.vexnum] = INF;
G.arc[G.vexnum][i] = INF;
}
G.vexnum++;
return true;
}
// 删除顶点
bool DeleteVex(AMGraph &G, VertexType v) {
int k = LocateVex(G, v);
if (k == -1) {
return false;
}
for (int i = 0; i < G.vexnum; i++) {
if (i == k) {
continue;
}
if (G.arc[i][k] != INF) {
G.arc[i][k] = INF;
G.arc[k][i] = INF;
G.arcnum--;
}
}
for (int i = k + 1; i < G.vexnum; i++) {
G.vexs[i - 1] = G.vexs[i];
for (int j = 0; j < G.vexnum; j++) {
G.arc[j][i - 1] = G.arc[j][i];
}
}
for (int i = 0; i < G.vexnum - 1; i++) {
for (int j = k + 1; j < G.vexnum; j++) {
G.arc[i][j - 1] = G.arc[i][j];
}
}
G.vexnum--;
return true;
}
// 增加边
bool InsertArc(AMGraph &G, VertexType v, VertexType w, EdgeType weight) {
int i = LocateVex(G, v);
int j = LocateVex(G, w);
if (i == -1 || j == -1) {
return false;
}
if (G.arc[i][j] != INF) {
return false;
}
G.arc[i][j] = weight;
G.arc[j][i] = weight;
G.arcnum++;
return true;
}
// 删除边
bool DeleteArc(AMGraph &G, VertexType v, VertexType w) {
int i = LocateVex(G, v);
int j = LocateVex(G, w);
if (i == -1 || j == -1) {
return false;
}
if (G.arc[i][j] == INF) {
return false;
}
G.arc[i][j] = INF;
G.arc[j][i] = INF;
G.arcnum--;
return true;
}
int main() {
AMGraph graph;
graph.vexnum = 0;
graph.arcnum = 0;
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
for (int j = 0; j < MAX_VERTEX_NUM; j++) {
graph.arc[i][j] = INF;
}
}
// 测试代码
InsertVex(graph, 'A');
InsertVex(graph, 'B');
InsertVex(graph, 'C');
InsertVex(graph, 'D');
InsertArc(graph, 'A', 'B', 3);
InsertArc(graph, 'A', 'C', 4);
InsertArc(graph, 'B', 'C', 5);
InsertArc(graph, 'B', 'D', 6);
InsertArc(graph, 'C', 'D', 7);
printUDN(graph);
DeleteVex(graph, 'C');
printUDN(graph);
DeleteArc(graph, 'B', 'D');
printUDN(graph);
return 0;
}
```
该代码中包含了打印无向图、增加顶点、删除顶点、增加边、删除边等操作。在测试代码中,我们先增加了4个顶点,然后增加了5条边,接着删除了一个顶点和一条边,最后打印了无向图。
阅读全文