#include <stdio.h>
#include <stdlib.h>
#define QUEUE_MAXSIZE 30
#define VERTEX_MAX 26
#define MAXVALUE 99999
typedef struct
{
char Vertex[VERTEX_MAX];//保存顶点信息,序号或字母
int Edges[VERTEX_MAX][VERTEX_MAX];//保存边的权
int isTrav[VERTEX_MAX];//遍历标志
int VertexNum;//顶点的数量
int EdgeNum;//边的数量
int GraphType;//图的类型,0:无向图;1:有向图
}MatrixGraph;
void CreateMatrixGraph(MatrixGraph *G);//创建邻接矩阵图
void OutMatrix(MatrixGraph *G);//输出邻接矩阵
/************************************************************************
函数名称:CreateMatrixGraph
函数功能:创建邻接矩阵图
函数参数:G是定义的图结构的指针
函数返回:无
************************************************************************/
void CreateMatrixGraph(MatrixGraph *G)
{
int i, j, k, weight;
char start, end;
printf("输入各个顶点的信息\n");
for (i = 0; i < G->VertexNum; i++)
{
getchar();
printf("第%d个顶点:", i+1);
scanf("%c", &(G->Vertex[i]));
}
printf("输入构成各边的两个顶点及权值(用逗号分隔):\n");
for (k = 0; k < G->EdgeNum; k++)
{
getchar();
printf("第%d条边:", k+1);
scanf("%c,%c,%d", &start, &end, &weight);
for (i = 0; start != G->Vertex[i]; i++);//在已有顶点中查找起始点
for (j = 0; end != G->Vertex[j]; j++);//在已有顶点中查找终点
G->Edges[i][j] = weight;
if (G->GraphType == 0)
G->Edges[j][i] = weight;
}
}
/************************************************************************
函数名称:OutMatrix
函数功能:输出邻接矩阵
函数参数:G是定义的图结构的指针
函数返回:无
************************************************************************/
void OutMatrix(MatrixGraph *G)
{
int i, j;
for (j = 0; j < G->VertexNum; j++)
{
printf("\t%c", G->Vertex[j]);
}
printf("\n");
printf(" ------------------------------------\n");
for (i = 0; i < G->VertexNum; i++)
{
printf(" %c |", G->Vertex[i]);
for (j = 0; j < G->VertexNum; j++)
{
if (G->Edges[i][j] == MAXVALUE)
printf("\t∞");
else
printf("\t%d", G->Edges[i][j]);
}
printf("\n");
}
printf("\n");
}
/**********************************************************
*******上面是图的代码
*******下面是实现广度优先遍历的代码
***********************************************************/
typedef struct
{
int Data[QUEUE_MAXSIZE];
int head;
int tail;
}SeqQueue;
void QueueInit(SeqQueue *Q)//队列初始化
{
Q->head = Q->tail = 0;
}
int QueueIsEmpty(SeqQueue Q)//判断队列是否已空
{
return Q.head == Q.tail;
}
int QueueIn(SeqQueue *Q, int ch)//入队列,成功返回1,失败返回0
{
if ((Q->tail + 1) % QUEUE_MAXSIZE == Q->head)
return 0;
Q->Data[Q->tail] = ch;
Q->tail = (Q->tail + 1) % QUEUE_MAXSIZE;
return 1;
}
int QueueOut(SeqQueue *Q, int *ch)//出队列,成功返回1,失败返回0
{
if (Q->head == Q->tail)
return 0;
*ch = Q->Data[Q->head];
Q->head = (Q->head + 1) % QUEUE_MAXSIZE;
return 1;
}
//广度优先遍历
void BFSM(MatrixGraph *G, int k)
{
int i, j;
SeqQueue Q;
QueueInit(&Q);
G->isTrav[k] = 1;
printf("->%c", G->Vertex[k]);
QueueIn(&Q, k);
while(!QueueIsEmpty(Q))
{
QueueOut(&Q, &i);
for (j = 0; j < G->VertexNum; j++)
{
if (G->Edges[i][j] != MAXVALUE && !G->isTrav[j])
{
printf("->%c", G->Vertex[j]);
G->isTrav[j] = 1;
QueueIn(&Q, j);
}
}
}
}
void BFSTraverse(MatrixGraph *G)
{
int i;
for (i = 0; i < G->VertexNum; i++)
G->isTrav[i] = 0;
printf("广度优先遍历结点:");
for (i = 0; i < G->VertexNum; i++)
if (!G->isTrav[i])
BFSM(G, i);
printf("\n");
}
//深度优先遍历
void DFSM(MatrixGraph *G, int i)
{
int j;
G->isTrav[i] = 1;
printf("->%c", G->Vertex[i]);
for (j = 0; j < G->VertexNum; j++)
if (G->Edges[i][j] != MAXVALUE && !G->isTrav[j])
DFSM(G, j);
}
void DFSTraverse(MatrixGraph *G)
{
int i;
for (i = 0; i < G->VertexNum; i++)
G->isTrav[i] = 0;
printf("深度优先遍历结点:");
for (i = 0; i < G->VertexNum; i++)
if (!G->isTrav[i])
DFSM(G, i);
printf("\n");
}
/************************************************************************
函数名称:main
函数功能:测试图
函数参数:无
函数返回:0表示成功
************************************************************************/
int main()
{
MatrixGraph G;
int i, j;
char select;
do
{
printf("输入生成图的类型(0:无向图,1:有向图):");
scanf("%d", &G.GraphType);
printf("输入图的顶点数量和边数量:");
scanf("%d,%d", &G.VertexNum, &G.EdgeNum);
//清空矩阵
for (i = 0; i < G.VertexNum; i++)
for (j = 0; j < G.VertexNum; j++)
G.Edges[i][j] = MAXVALUE;
CreateMatrixGraph(&G);
printf("邻接矩阵数据如下:\n");
OutMatrix(&G);
DFSTraverse(&G);//深度优先搜索遍历图
BFSTraverse(&G);//广度优先搜索遍历图
printf("图遍历完,继续进行吗?(Y/N)");
scanf("%c", &select);
getchar();
}while (select != 'N' && select != 'n');
getchar();
getchar();
return 0;
}