#include <stdio.h>
#define INFINITY 100000 //最大值
#define MAX_VERTEX_NUM 20 //最大顶点数
#define TRUE 1
#define FALSE 0
typedef char VertexType;
typedef int EdgeType;
typedef struct{
VertexType vexs[MAX_VERTEX_NUM]; //顶点变量
EdgeType arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵
int numVer,numEdg; //图的结点数和边数
}MGraph;
typedef struct {
int data[MAX_VERTEX_NUM];
int front, rear;
}Queue;
int visited[MAX_VERTEX_NUM];
void InitQueue(Queue *q){
q->front=q->rear=0;
}
//入队
void EnQueue(Queue *Q, int e)
{
if ((Q->rear+1)%MAX_VERTEX_NUM == Q->front)//判断队列是否已满
printf("队列已满!\n");
Q->data[Q->rear] = e;
Q->rear = (Q->rear+1)%MAX_VERTEX_NUM;
}
//出队
void DeQueue(Queue *Q, int *e)
{
if (Q->front == Q->rear)
printf("队列为空!\n");
*e = Q->data[Q->front];
Q->front = (Q->front+1)%MAX_VERTEX_NUM;
}
//判空
int QueueEmpty(Queue *Q)
{
if (Q->front == Q->rear)
return TRUE;
else
return FALSE;
}
//建立图的邻接矩阵
void CreateMGraph(MGraph *G)
{
int i, j, k, w;
printf("输入顶点数和边数: ");
scanf("%d%d", &G->numVer,&G->numEdg);
fflush(stdin);
printf("==============================\n");
printf("输入各个顶点:\n");
for (i=0; i<G->numVer; ++i)
{
printf("顶点%d: ",i+1);
scanf("%c", &G->vexs[i]);
fflush(stdin);
}
for (i=0; i<G->numVer; ++i)
{
for (j=0; j<G->numVer; ++j)
G->arc[i][j] = INFINITY;
}
printf("==============================\n");
for (k=0; k<G->numEdg; ++k)
{
printf("输入边(vi, vj)中的下标i和j和权W: ");
scanf("%d%d%d", &i,&j,&w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];
}
}
//输出
void DisMGraph(MGraph *G)
{
int i, j, k;
k = G->numVer;
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.numVer; ++j)
{
if (G.arc[i][j]!=INFINITY && !visited[j])
DFS(G, j);
}
}
void DFSTraverse(MGraph G)
{
int i;
for (i=0; i<G.numVer; ++i)
visited[i] = FALSE;
for (i=0; i<G.numVer; ++i)
{
if (!visited[i])
DFS(G, i);
}
}
//图的广度优先遍历
void BFSTraverse(MGraph *G)
{
int i, j;
Queue Q;
for (i=0; i<G->numVer; ++i)
visited[i] = FALSE;
InitQueue(&Q);
for (i=0; i<G->numVer; ++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->numVer; ++j)
{
if (!visited[j] && G->arc[i][j]!=INFINITY)
{
visited[j] = TRUE;
printf("%c ", G->vexs[j]);
EnQueue(&Q, j);
}
}
}
}
}
}
//程序入口
int main(){
MGraph G;
CreateMGraph(&G);
printf("\n图的深度优先遍历为: ");
DFSTraverse(G);
printf("\n图的广度优先遍历为: ");
BFSTraverse(&G);
printf("\n");
return 0;
}
#define INFINITY 100000 //最大值
#define MAX_VERTEX_NUM 20 //最大顶点数
#define TRUE 1
#define FALSE 0
typedef char VertexType;
typedef int EdgeType;
typedef struct{
VertexType vexs[MAX_VERTEX_NUM]; //顶点变量
EdgeType arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵
int numVer,numEdg; //图的结点数和边数
}MGraph;
typedef struct {
int data[MAX_VERTEX_NUM];
int front, rear;
}Queue;
int visited[MAX_VERTEX_NUM];
void InitQueue(Queue *q){
q->front=q->rear=0;
}
//入队
void EnQueue(Queue *Q, int e)
{
if ((Q->rear+1)%MAX_VERTEX_NUM == Q->front)//判断队列是否已满
printf("队列已满!\n");
Q->data[Q->rear] = e;
Q->rear = (Q->rear+1)%MAX_VERTEX_NUM;
}
//出队
void DeQueue(Queue *Q, int *e)
{
if (Q->front == Q->rear)
printf("队列为空!\n");
*e = Q->data[Q->front];
Q->front = (Q->front+1)%MAX_VERTEX_NUM;
}
//判空
int QueueEmpty(Queue *Q)
{
if (Q->front == Q->rear)
return TRUE;
else
return FALSE;
}
//建立图的邻接矩阵
void CreateMGraph(MGraph *G)
{
int i, j, k, w;
printf("输入顶点数和边数: ");
scanf("%d%d", &G->numVer,&G->numEdg);
fflush(stdin);
printf("==============================\n");
printf("输入各个顶点:\n");
for (i=0; i<G->numVer; ++i)
{
printf("顶点%d: ",i+1);
scanf("%c", &G->vexs[i]);
fflush(stdin);
}
for (i=0; i<G->numVer; ++i)
{
for (j=0; j<G->numVer; ++j)
G->arc[i][j] = INFINITY;
}
printf("==============================\n");
for (k=0; k<G->numEdg; ++k)
{
printf("输入边(vi, vj)中的下标i和j和权W: ");
scanf("%d%d%d", &i,&j,&w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];
}
}
//输出
void DisMGraph(MGraph *G)
{
int i, j, k;
k = G->numVer;
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.numVer; ++j)
{
if (G.arc[i][j]!=INFINITY && !visited[j])
DFS(G, j);
}
}
void DFSTraverse(MGraph G)
{
int i;
for (i=0; i<G.numVer; ++i)
visited[i] = FALSE;
for (i=0; i<G.numVer; ++i)
{
if (!visited[i])
DFS(G, i);
}
}
//图的广度优先遍历
void BFSTraverse(MGraph *G)
{
int i, j;
Queue Q;
for (i=0; i<G->numVer; ++i)
visited[i] = FALSE;
InitQueue(&Q);
for (i=0; i<G->numVer; ++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->numVer; ++j)
{
if (!visited[j] && G->arc[i][j]!=INFINITY)
{
visited[j] = TRUE;
printf("%c ", G->vexs[j]);
EnQueue(&Q, j);
}
}
}
}
}
}
//程序入口
int main(){
MGraph G;
CreateMGraph(&G);
printf("\n图的深度优先遍历为: ");
DFSTraverse(G);
printf("\n图的广度优先遍历为: ");
BFSTraverse(&G);
printf("\n");
return 0;
}