数据结构与算法(五)图
- 邻接矩阵存储实现、深度优先及广度优先遍历
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define MAX 100
#define MAXNUM 32767//正无穷
typedef struct
{
char vexs[MAX];//顶点表 (顶点类型是char)
int arcs[MAX][MAX];//邻接矩阵 (边的类型是int)
int vexnum, arcnum;//图当前的点数和边数
}AMGraph;
typedef struct
{
char data[MAX];
int front, rear;
}SeQueue;
int* Dvisited;
int* Bvisited;
int LocateVex(AMGraph* pG, char* pV)
{
int i;
for (i = 0; i < pG->vexnum; ++i)
{
if ( *pV == pG->vexs[i])
break;
}
return i;
}
void initUDN(AMGraph* pG)//初始化无向网
{
char V1, V2;
int i,j,w;
printf("请输入顶点数: ");
scanf_s("%d",&pG->vexnum);
printf("请输入边数: ");
scanf_s("%d",&pG->arcnum);
for (int k = 0; k < pG->vexnum; ++k)
{
printf("请输入第%d个顶点的数据: ",k+1);
scanf_s(" %c",&pG->vexs[k]);
}
for (int k = 0; k < pG->vexnum; ++k)
{
for (int m = 0; m < pG->vexnum; ++m)
{
pG->arcs[k][m] = MAXNUM;
if (k == m)
pG->arcs[k][m] = 0;
}
}
for (int k = 0; k < pG->arcnum; ++k)
{
printf("请输入第一个顶点元素: ");
scanf_s(" %c",&V1);
printf("请输入第二个顶点元素: ");
scanf_s(" %c", &V2);
printf("请输入两个顶点的权值: ");
scanf_s("%d", &w);
i = LocateVex(pG,&V1);
j = LocateVex(pG,&V2);
pG->arcs[i][j] = w;
pG->arcs[j][i] = pG->arcs[i][j];
}
Dvisited = (int*)malloc(sizeof(int) * (pG->vexnum));
for (int m = 0; m < pG->vexnum; ++m)
{
Dvisited[m] = 0;
}
Bvisited = (int*)malloc(sizeof(int) * (pG->vexnum));
for (int m = 0; m < pG->vexnum; ++m)
{
Bvisited[m] = 0;
}
}
void DFS(AMGraph* pG, char V)
{
int i,j;
i = LocateVex(pG,&V);
Dvisited[i] = 1;
if(Dvisited[i]=1)
printf("访问的元素是: %c\n", V);
for (j = 0; j < pG->vexnum; j++)
{
if ((pG->arcs[i][j] == 1) && (Dvisited[j] == 0))
{
DFS(pG, pG->vexs[j]);
}
}
}
void initQueue(SeQueue* Sq)
{
Sq->front = -1;
Sq->rear = Sq->front;
}
bool is_empty(SeQueue* Sq)
{
if (Sq->rear == Sq->front)
return true;
else
return false;
}
bool is_full(SeQueue* Sq)
{
if ((Sq->rear + 1) % MAX == Sq->front)
return true;
else
return false;
}
bool InQueue(SeQueue* Sq, char C)
{
if (is_full(Sq))
return false;
else
{
Sq->rear = (Sq->rear + 1) % MAX;
Sq->data[Sq->rear] = C;
return true;
}
}
bool OutQueue(SeQueue* Sq, char* pC)
{
if (is_empty(Sq))
return false;
else
{
Sq->front = (Sq->front + 1) % MAX;
*pC = Sq->data[Sq->front];
Sq->data[Sq->front] = NULL;
return true;
}
}
void BFS(AMGraph* pG, char C)
{
SeQueue Q;
int i, j,k;
char w;
i = LocateVex(pG,&C);
Bvisited[i]=1;
InQueue(&Q,C);
while (!is_empty(&Q))
{
OutQueue(&Q,&w);
k = LocateVex(pG,&w);
printf("%c\t",w);
for (j = 0; j < pG->vexnum; ++j)
{
if (Bvisited[j] == 0 && pG->arcs[k][j] == 1)
{
Bvisited[j] = 1;
InQueue(&Q,pG->vexs[j]);
}
}
}
}
int main()
{
int Dflag = 1;
int Bflag = 1;
int n;
AMGraph G;
initUDN(&G);
printf("请输入这个图含有的子图个数:");
scanf_s("%d",&n);
for (int i = 0; i < G.vexnum; ++i)
{
for (int j = 0; j < G.vexnum; ++j)
{
printf("%d\t",G.arcs[i][j]);
if (j == (G.vexnum - 1))
printf("\n");
}
}
printf("============深度优先遍历==============");
DFS(&G,'A');
while (Dflag <= n) //这样可以适应非连通图
{
for (int i = 0; i < G.vexnum; ++i)
{
if (Dvisited[i] == 0)
{
DFS(&G, G.vexs[i]);
}
}
Dflag++;
}
printf("======================================");
printf("============广度优先遍历==============");
BFS(&G,'A');
while (Bflag <= n)
{
for (int i = 0; i < G.vexnum; ++i)
{
if (Bvisited[i] == 0)
{
BFS(&G, G.vexs[i]);
}
}
Bflag++;
}
printf("======================================");
}
- 邻接表存储实现、深度优先及广度优先遍历
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef struct arc
{
int adjvex;
int weight;
struct arc* next;
}ArcNode;//边表节点
typedef struct
{
char data;
ArcNode* firstarc;
}VertexNode;
typedef struct
{
VertexNode vertex[MAX];
int vernum, arcnum;
}ALGraph;
typedef struct node
{
char data[MAX];
int front, rear;
}SeQueue;
int* Dvisited;
int* Bvisited;
int LocateVex(ALGraph* pG, char* pV)
{
int i;
for (i = 0; i < pG->vernum; ++i)
{
if (*pV == pG->vertex[i].data)
break;
}
return i;
}
void initGraph(ALGraph* pG)
{
int i, j, w;
printf("请你输入顶点数: ");
scanf_s("%d",&(pG->vernum));
printf("请输入你的边数: ");
scanf_s("%d",&(pG->arcnum));
for (int k = 0; k < pG->vernum; ++k)
{
printf("请输入第%d个节点的元素: ",k+1);
scanf_s(" %c",&(pG->vertex[k].data));
pG->vertex[k].firstarc = NULL;
}
for (int k = 0; k < pG->arcnum; ++k)
{
char V1, V2;
printf("请输入两个互相连接的元素:");
printf("第一个元素是: ");
scanf_s(" %c",&V1);
printf("第二个元素是: ");
scanf_s(" %c", &V2);
i = LocateVex(pG,&V1);
j = LocateVex(pG,&V2);
ArcNode* pNew1 = (ArcNode*)malloc(sizeof(ArcNode));
if (pNew1 == NULL)
{
exit(-1);
return;
}
pNew1->adjvex = j;
pNew1->next = pG->vertex[i].firstarc;
pG->vertex[i].firstarc = pNew1;
ArcNode* pNew2 = (ArcNode*)malloc(sizeof(ArcNode));
if (pNew2 == NULL)
{
exit(-1);
return;
}
pNew2->adjvex = i;
pNew2->next = pG->vertex[j].firstarc;
pG->vertex[j].firstarc = pNew2;
}
Dvisited = (int*)malloc(sizeof(int)*pG->vernum);
for (int i = 0; i < pG->vernum; ++i)
Dvisited[i] = 0;
Bvisited = (int*)malloc(sizeof(int) * pG->vernum);
for (int i = 0; i < pG->vernum; ++i)
Bvisited[i] = 0;
}
void DFS(ALGraph* pG, char V)
{
int i;
ArcNode* p;
i = LocateVex(pG,&V);
Dvisited[i] = 1;
if (Dvisited[i] == 1)
printf("访问的元素是:%c\n",pG->vertex[i].data);
p = pG->vertex[i].firstarc;
while (p != NULL)
{
if (Dvisited[p->adjvex] == 0)
{
DFS(pG,pG->vertex[p->adjvex].data);
}
p = p->next;
}
}
void initQueue(SeQueue* Sq)
{
Sq->front = -1;
Sq->rear = Sq->front;
}
bool is_empty(SeQueue* Sq)
{
if (Sq->front == Sq->rear)
return true;
else
return false;
}
bool is_full(SeQueue* Sq)
{
if ((Sq->rear + 1) % MAX == Sq->front)
return true;
else
return false;
}
bool InQueue(SeQueue* Sq,char c)
{
if (is_full(Sq))
return false;
else
{
Sq->rear = (Sq->rear + 1) % MAX;
Sq->data[Sq->rear] = c;
}
return true;
}
bool OutQueue(SeQueue* Sq, char* pC)
{
if (is_empty(Sq))
return false;
else
{
Sq->front = (Sq->front + 1) % MAX;
*pC = Sq->data[Sq->front];
Sq->data[Sq->front] = NULL;
return true;
}
}
void BFS(ALGraph* pG, char C)
{
int i,j,w;
char V;
ArcNode* p;
SeQueue Q;
//printf("%c\t",C);
i = LocateVex(pG,&C);
Bvisited[i] = 1;
initQueue(&Q);
InQueue(&Q,C);
while (!is_empty(&Q))
{
OutQueue(&Q,&V);
j=LocateVex(pG,&V);
p = pG->vertex[j].firstarc;
printf("访问的元素是:%c\n",V);
while (p != NULL)
{
w = p->adjvex;
if (Bvisited[w] == 0)
{
//printf("%c\t",pG->vertex[w].data);
Bvisited[w]=1;
InQueue(&Q,pG->vertex[w].data);
}
p = p->next;
}
}
}
int main()
{
int Dflag = 1;
int Bflag = 1;
int n;
ALGraph G;
initGraph(&G);
printf("请您输入这个图有几个子图:");
scanf_s("%d", &n);
for (int i = 0; i < G.vernum; ++i)
{
ArcNode* p = G.vertex[i].firstarc;
printf("%c: ",G.vertex[i]);
while (p != NULL)
{
printf("%c", G.vertex[p->adjvex].data);
p = p->next;
}
printf("\n");
}
printf("============深度优先遍历==============\n");
DFS(&G,'A');
while (Dflag <= n)
{
for (int i = 0; i < G.vernum; ++i)
{
if (Dvisited[i] == 0)
DFS(&G, G.vertex[i].data);
}
Dflag++;
}
printf("======================================\n");
printf("============广度优先遍历==============\n");
BFS(&G,'A');
while(Bflag<=n)
{
for (int i = 0; i < G.vernum; ++i)
{
if (Bvisited[i] == 0)
BFS(&G, G.vertex[i].data);
}
Bflag++;
}
printf("======================================\n");
return 0;
}
如有问题请多指教