void DFSTraverse(Graph graph) { for (int i = 1; i <= graph->vexnum; i++) visited[i] = false; for (int i = 1; i <= graph->vexnum; i++) { if (!visited[i]) DFS(graph, i); } } void DFS(Graph graph, int v) { visited[v] = true; cout << graph->list[v].data << " "; Node* header = graph->list[v].head; while (header) { if (!visited[header->vex]) { DFS(graph, header->vex); } header = header->next; } } void BFSTraverse(Graph graph) { queue<int> MyQueue; for (int i = 1; i <= graph->vexnum; i++) visited[i] = false; for (int i = 1; i <= graph->vexnum; i++) { if (!visited[i]) { visited[i] = true; cout << graph->list[i].data << " "; MyQueue.push(i); while (!MyQueue.empty()) { int front = MyQueue.front(); MyQueue.pop(); Node* header = graph->list[front].head; while (header) { if (!visited[header->vex]) { visited[header->vex] = true; cout << graph->list[header->vex].data << " "; MyQueue.push(header->vex); } header = header->next; } } } } } void printGraph(Graph graph) { int** arr = new int* [graph->vexnum + 1]; for (int i = 1; i <= graph->vexnum; i++) { arr[i] = new int[graph->vexnum + 1]; } for (int i = 1; i <= graph->vexnum; i++) { for (int j = 1; j <= graph->vexnum; j++) { arr[i][j] = 0; } } for (int i = 1; i <= graph->vexnum; i++) { Node* header = graph->list[i].head; while (header) { arr[i][header->vex] = header->weight; header = header->next; } } for (int i = 1; i <= graph->vexnum; i++) { for (int j = 1; j <= graph->vexnum; j++) { cout << arr[i][j] << " "; } cout << endl; } }代码讲解
时间: 2024-04-05 22:34:23 浏览: 117
DFSTraverse函数和DFS函数实现了图的深度优先遍历。首先将visited数组初始化为false,然后对每个顶点进行深度优先遍历。在DFS函数中,首先将当前顶点标记为已访问,并输出其数据。然后遍历当前顶点的邻接表,对于每个未被访问过的邻接点,递归调用DFS函数进行遍历。
BFSTraverse函数实现了图的广度优先遍历。同样先将visited数组初始化为false,然后对每个未被访问过的顶点进行广度优先遍历。在遍历每个顶点时,将其标记为已访问,输出其数据,并将其加入队列中。然后对队列中的元素依次出队,并遍历其邻接表,对于每个未被访问过的邻接点,将其标记为已访问,输出其数据,并将其加入队列中。
printGraph函数用于输出图的邻接矩阵。首先动态创建一个二维数组arr来存储邻接矩阵。然后将其初始化为0。接着遍历每个顶点的邻接表,将对应的邻接矩阵元素赋值为边的权重。最后输出邻接矩阵。
相关问题
#include <iostream> #include <queue> using namespace std; const int VEX_MAX = 100; bool visited[VEX_MAX]; struct Node { int vex; int weight; struct Node* next; }; typedef struct ENode { char data; Node* head; }*List; typedef struct GNode { List list; int vexnum; int arcnum; }*Graph; Graph createGraph(); void DFSTraverse(Graph graph); void DFS(Graph graph, int v); void BFSTraverse(Graph graph); void printGraph(Graph graph); int main() { Graph graph = createGraph(); printGraph(graph); cout << "深度优先遍历:" << endl; DFSTraverse(graph); cout << endl; cout << "广度优先遍历:" << endl; BFSTraverse(graph); return 0; } Graph createGraph() { Graph graph = new GNode; cout << "请输入顶点个数和边个数:" << endl; cin >> graph->vexnum >> graph->arcnum; graph->list = new ENode[graph->vexnum + 1]; cout << "请输入顶点数据:" << endl; for (int i = 1; i <= graph->vexnum; i++) { cin >> graph->list[i].data; graph->list[i].head = NULL; } int begin, end, weight; cout << "请输入边的起始点及其权重:" << endl; for (int i = 1; i <= graph->arcnum; i++) { cin >> begin >> end >> weight; Node* node = new Node; node->weight = weight; node->vex = end; node->next = graph->list[begin].head; graph->list[begin].head = node; node = new Node; node->weight = weight; node->vex = begin; node->next = graph->list[end].head; graph->list[end].head = node; } return graph; }讲解
这段代码是一个图的创建和遍历的程序,使用的是邻接表来存储图的信息。具体来说,这里定义了三个结构体:Node、ENode和GNode。其中,Node表示邻接表中的一个节点,ENode表示一个顶点和其邻接表组成的一个节点,GNode表示整个图。程序中先通过createGraph()函数创建了一个图,并将其存储在GNode中。在createGraph()函数中,先读入了顶点个数和边的个数,然后再读入每个顶点的数据,并将其存储在ENode中。接着,读入每条边的起始点、终止点和权重,并将其存储在邻接表中。最后,程序分别调用了DFSTraverse()和BFSTraverse()函数对图进行深度优先遍历和广度优先遍历,并输出结果。
优化这段代码int BFS(ALGraph G, int startVertex) { int visited[MVNum] = {0}; // 初始化为0 visited[startVertex] = 1; printf("广度优先搜索遍历结果:"); printf("%c", G.vertices[startVertex].data); Queue queue; initQueue(&queue); enqueue(&queue, startVertex); while (!isEmpty(queue)) { int vertex = dequeue(&queue); for (ArcNode* p = G.vertices[vertex].finstarc; p != NULL; p = p->nextarc) { if (!visited[p->adjvex]) { visited[p->adjvex] = 1; printf("%c", G.vertices[p->adjvex].data); enqueue(&queue, p->adjvex); } } } } void DFS(ALGraph G, int v, int* visited) { visited[v] = 1; printf("%c", G.vertices[v].data); for (ArcNode* p = G.vertices[v].finstarc; p != NULL; p = p->nextarc) { if (!visited[p->adjvex]) { DFS(G, p->adjvex, visited); } } } void DFSTraverse(ALGraph G) { int visited[MVNum] = { 0 }; // 初始化为0 printf("深度优先遍历结果:"); for (int i = 0; i < G.vexnum; i++) { if (!visited[i]) { DFS(G, i, visited); } } } int main() { int v; ALGraph G; CreateGraph(&G); ShowGraph(G); printf("请输入开始节点中所在的位置:"); scanf("%d", &v); DFSTraverse(G, v); BFSTraverse(G, v); return 0; }
好的,下面是对代码的优化:
```c
int BFS(ALGraph G, int startVertex) {
int visited[MVNum] = {0}; // 初始化为0
visited[startVertex] = 1;
printf("广度优先搜索遍历结果:");
printf("%c", G.vertices[startVertex].data);
Queue queue;
initQueue(&queue);
enqueue(&queue, startVertex);
while (!isEmpty(queue)) {
int vertex = dequeue(&queue);
for (ArcNode* p = G.vertices[vertex].finstarc; p != NULL; p = p->nextarc) {
if (!visited[p->adjvex]) {
visited[p->adjvex] = 1;
printf("%c", G.vertices[p->adjvex].data);
enqueue(&queue, p->adjvex);
}
}
}
}
void DFS(ALGraph G, int v, int* visited) {
visited[v] = 1;
printf("%c", G.vertices[v].data);
for (ArcNode* p = G.vertices[v].finstarc; p != NULL; p = p->nextarc) {
if (!visited[p->adjvex]) {
DFS(G, p->adjvex, visited);
}
}
}
void DFSTraverse(ALGraph G, int startVertex) {
int visited[MVNum] = {0}; // 初始化为0
printf("深度优先遍历结果:");
DFS(G, startVertex, visited);
}
int main() {
int v;
ALGraph G;
CreateGraph(&G);
ShowGraph(G);
printf("请输入开始节点中所在的位置:");
scanf("%d", &v);
DFSTraverse(G, v);
BFS(G, v);
return 0;
}
```
优化的内容主要包括:
1. 将 `DFSTraverse` 函数的参数修改为了 `ALGraph G, int startVertex`,以便在函数内部调用 `DFS` 函数时指定起始节点。
2. 将 `DFS` 函数和 `DFSTraverse` 函数的输出语句提前,以便代码更加清晰。
3. 在 `main` 函数中先调用 `DFSTraverse` 函数,再调用 `BFS` 函数,以便程序输出结果的顺序更加合理。
4. 修改了一些变量名和注释,以便代码更加易读。
阅读全文