#include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 #define FALSE 0 #define TRUE 1 // 邻接表结构体 typedef struct ArcNode{ int adjvex; struct ArcNode *nextarc; }ArcNode; typedef struct VNode{ int data; ArcNode *firstarc; }VNode, AdjList[MAX_VERTEX_NUM]; typedef struct{ AdjList vertices; int vexnum, arcnum; // 顶点数和边数 }ALGraph; // 初始化邻接表 void InitALGraph(ALGraph *G) { int i; G->vexnum = G->arcnum = 0; for(i = 0; i < MAX_VERTEX_NUM; i++){ G->vertices[i].firstarc = NULL; } } // 添加顶点 void AddVertex(ALGraph *G, int v) { if(G->vexnum == MAX_VERTEX_NUM){ printf("Error: Vertex number exceeds maximum.\n"); return; } G->vertices[G->vexnum].data = v; G->vexnum++; } // 添加边 void AddArc(ALGraph *G, int v1, int v2) { if(G->arcnum >= MAX_VERTEX_NUM * (MAX_VERTEX_NUM - 1) / 2){ printf("Error: Arc number exceeds maximum.\n"); return; } ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = v2; p->nextarc = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; G->arcnum++; } // DFS遍历 void DFS(ALGraph *G, int v, int *visited) { printf("%d ", G->vertices[v].data); visited[v] = TRUE; ArcNode *p = G->vertices[v].firstarc; while(p != NULL){ int w = p->adjvex; if(!visited[w]){ DFS(G, w, visited); } p = p->nextarc; } } // BFS遍历 void BFS(ALGraph *G, int v, int *visited) { int queue[MAX_VERTEX_NUM]; int front = -1, rear = -1; printf("%d ", G->vertices[v].data); visited[v] = TRUE; queue[++rear] = v; while(front != rear){ int w = queue[++front]; ArcNode *p = G->vertices[w].firstarc; while(p != NULL){ int u = p->adjvex; if(!visited[u]){ printf("%d ", G->vertices[u].data); visited[u] = TRUE; queue[++rear] = u; } p = p->nextarc; } } } int main() { ALGraph G; InitALGraph(&G); // 添加顶点 AddVertex(&G, 1); AddVertex(&G, 2); AddVertex(&G, 3); AddVertex(&G, 4); AddVertex(&G, 5); // 添加边 AddArc(&G, 0, 1); AddArc(&G, 0, 2); AddArc(&G, 1, 3); AddArc(&G, 1, 4); AddArc(&G, 2, 4); // 输出深度优先序列 int visited[MAX_VERTEX_NUM] = {FALSE}; printf("DFS: "); DFS(&G, 0, visited); printf("\n"); // 输出广度优先序列 int visited2[MAX_VERTEX_NUM] = {FALSE}; printf("BFS: "); BFS(&G, 0, visited2); printf("\n"); return 0; } 修改代码,使其能输出图的可视化输出图
时间: 2023-11-28 11:47:13 浏览: 101
要输出图的可视化输出图,需要使用图形库或工具,例如Graphviz。以下是修改后的代码,可以输出Graphviz所需的DOT格式的图形描述语言,通过复制到Graphviz的在线编辑器或使用本地Graphviz软件处理后,即可生成图形化的图像。
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100 // 最大顶点数
#define FALSE 0
#define TRUE 1
// 邻接表结构体
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct{
AdjList vertices;
int vexnum, arcnum; // 顶点数和边数
}ALGraph;
// 初始化邻接表
void InitALGraph(ALGraph *G) {
int i;
G->vexnum = G->arcnum = 0;
for(i = 0; i < MAX_VERTEX_NUM; i++){
G->vertices[i].firstarc = NULL;
}
}
// 添加顶点
void AddVertex(ALGraph *G, int v) {
if(G->vexnum == MAX_VERTEX_NUM){
printf("Error: Vertex number exceeds maximum.\n");
return;
}
G->vertices[G->vexnum].data = v;
G->vexnum++;
}
// 添加边
void AddArc(ALGraph *G, int v1, int v2) {
if(G->arcnum >= MAX_VERTEX_NUM * (MAX_VERTEX_NUM - 1) / 2){
printf("Error: Arc number exceeds maximum.\n");
return;
}
ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->nextarc = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p;
G->arcnum++;
}
// DFS遍历
void DFS(ALGraph *G, int v, int *visited) {
printf("%d ", G->vertices[v].data);
visited[v] = TRUE;
ArcNode *p = G->vertices[v].firstarc;
while(p != NULL){
int w = p->adjvex;
if(!visited[w]){
DFS(G, w, visited);
}
p = p->nextarc;
}
}
// BFS遍历
void BFS(ALGraph *G, int v, int *visited) {
int queue[MAX_VERTEX_NUM];
int front = -1, rear = -1;
printf("%d ", G->vertices[v].data);
visited[v] = TRUE;
queue[++rear] = v;
while(front != rear){
int w = queue[++front];
ArcNode *p = G->vertices[w].firstarc;
while(p != NULL){
int u = p->adjvex;
if(!visited[u]){
printf("%d ", G->vertices[u].data);
visited[u] = TRUE;
queue[++rear] = u;
}
p = p->nextarc;
}
}
}
// 输出Graphviz所需的DOT格式的图形描述语言
void OutputGraphviz(ALGraph *G) {
printf("digraph G {\n");
int i;
for(i = 0; i < G->vexnum; i++){
printf(" %d;\n", G->vertices[i].data);
ArcNode *p = G->vertices[i].firstarc;
while(p != NULL){
printf(" %d -> %d;\n", G->vertices[i].data, G->vertices[p->adjvex].data);
p = p->nextarc;
}
}
printf("}\n");
}
int main() {
ALGraph G;
InitALGraph(&G);
// 添加顶点
AddVertex(&G, 1);
AddVertex(&G, 2);
AddVertex(&G, 3);
AddVertex(&G, 4);
AddVertex(&G, 5);
// 添加边
AddArc(&G, 0, 1);
AddArc(&G, 0, 2);
AddArc(&G, 1, 3);
AddArc(&G, 1, 4);
AddArc(&G, 2, 4);
// 输出深度优先序列
int visited[MAX_VERTEX_NUM] = {FALSE};
printf("DFS: ");
DFS(&G, 0, visited);
printf("\n");
// 输出广度优先序列
int visited2[MAX_VERTEX_NUM] = {FALSE};
printf("BFS: ");
BFS(&G, 0, visited2);
printf("\n");
// 输出Graphviz所需的DOT格式的图形描述语言
OutputGraphviz(&G);
return 0;
}
```
阅读全文