Graph storage and traversal
content
Using the adjacency matrix to describe the example graph, write a program to output the traversal sequence of depth-first search and breadth-first search of the example graph.
Specific steps are as follows:
- Describe the adjacency matrix of the graph as a two-dimensional array, and define the array as a global variable for data transfer;
- Define a queue that stores vertices with path lengths 1, 2, ... that have been visited during breadth-first search;
- Define the access function visit(), the depth-first search function DFS() and the breadth-first search function BFS();
- The main function realizes the call of each function.
experimental code
Some are shown below 内联代码片
.
#include <stdio.h>
#define MAX 100
#define TRUE 1
#define FALSE 0
typedef char VerTexType; //顶点类型
typedef int ArcType; //权值类型
typedef int Bool;
Bool visited[MAX];
//图的邻接矩阵存储表示
typedef struct {
VerTexType vexs[MAX];
ArcType arc[MAX][MAX];
int vexnum, arcnum;
}MGraph;
//队列的顺序存储结构
typedef struct {
int data[MAX];
int front, rear;
}Queue;
//========================================
void InitQueue(Queue *Q);
void EnQueue(Queue *Q, int e);
Bool QueueEmpty(Queue *Q);
void DeQueue(Queue *Q, int *e);
void CreateMGraph(MGraph *G);
void DisMGraph(MGraph *G);
void DFS(MGraph G, int i);
void DFSTraverse(MGraph G);
void BFS(MGraph *G);
void InitQueue(Queue *Q)
{
Q->front = Q->rear = 0;
}
void EnQueue(Queue *Q, int e)
{
if ((Q->rear+1)%MAX == Q->front)
return ;
Q->data[Q->rear] = e;
Q->rear = (Q->rear+1)%MAX;
}
Bool QueueEmpty(Queue *Q)
{
if (Q->front == Q->rear)
return TRUE;
else
return FALSE;
}
void DeQueue(Queue *Q, int *e)
{
if (Q->front == Q->rear)
return ;
*e = Q->data[Q->front];
Q->front = (Q->front+1)%MAX;
}
//=============================================
void CreateMGraph(MGraph *G) //建¨立ⅰ?图?的?邻ⅷ?接ó矩?阵ó
{
int i, j, k, w;
printf("输入顶点数和边数: ");
scanf("%d%d", &G->vexnum,&G->arcnum);
fflush(stdin);
//printf("==============================\n");
printf("输入各个顶点信息:\n");
for (i=0; i<G->vexnum; ++i)
{
printf("顶点%d: ",i+1);
scanf("%c", &G->vexs[i]);
fflush(stdin);
}
for (i=0; i<G->vexnum; ++i)
{
for (j=0; j<G->vexnum; ++j)
G->arc[i][j] = 0;
}
printf("==============================\n");
for (k=1; k<=G->arcnum; ++k)
{
printf("输入当前边两端点的下标值: ");
scanf("%d%d", &i,&j);
G->arc[i][j] = 1;
G->arc[j][i] = G->arc[i][j];
}
}
void DisMGraph(MGraph *G) //输出
{
int i, j, k;
k = G->vexnum;
for (i=0; i<k; ++i)
{
for (j=0; j<k; ++j)
{
printf("%5d ", G->arc[i][j]);
}
putchar('\n');
}
}
//==============================================
void DFS(MGraph G, int i) //图的深度优先遍历
{
int j;
visited[i] = TRUE;
printf("%c ", G.vexs[i]);
for (j=0; j<G.vexnum; ++j)
{
if (G.arc[i][j]!=0 && !visited[j])
DFS(G, j);
}
}
void DFSTraverse(MGraph G)
{
int i;
for (i=0; i<G.vexnum; ++i)
visited[i] = FALSE;
for (i=0; i<G.vexnum; ++i)
{
if (!visited[i])
DFS(G, i);
}
}
void BFS(MGraph *G) //图的广度优先遍历
{
int i, j;
Queue Q;
for (i=0; i<G->vexnum; ++i)
visited[i] = FALSE;
InitQueue(&Q);
for (i=0; i<G->vexnum; ++i)
{
if (!visited[i])
{
visited[i] = TRUE;
printf("%c ", G->vexs[i]);
EnQueue(&Q, i);
while (!QueueEmpty(&Q))
{
DeQueue(&Q, &i);
for (j=0; j<G->vexnum; ++j)
{
if (!visited[j] && G->arc[i][j]!=0)
{
visited[j] = TRUE;
printf("%c ", G->vexs[j]);
EnQueue(&Q, j);
}
}
}
}
}
}
//========================================
int main(){
MGraph G;
CreateMGraph(&G);
printf("\n图的深度优先遍历为: ");
DFSTraverse(G);
printf("\n图的广度优先遍历为: ");
BFS(&G);
printf("\n\n\n");
return 0;
}