请编写代码,要求如下:建立图的邻接表,邻接矩阵 Create_Graph( LGraph lg. MGraph mg ) ②邻接表表示的图的递归深度优先遍历 LDFS( LGraph g, int i ) ③邻接矩阵表示的图的递归深度优先遍历MDFS( MGraph g,int i, int vn ) ④邻接表表示的图的广度优先遍历 LBFS( LGraph g, int s, int n ) ⑤邻接矩阵表示的图的广度优先遍历 MBFS(MGraph g, int s, int n )
时间: 2024-03-26 07:34:31 浏览: 117
以下是 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;
}
```
注意:以上代码只是一种实现方法,不一定是最优的。在实际应用中,还需要考虑图的具体特征和需求,选择合适的存储结构和算法。
阅读全文