邻接矩阵的深度优先和广度优先如下:
对邻接矩阵进行定义
#include<stdio.h>
typedef int ElementType;
typedef char VertexType;
#define MAXVEX 100
#define INFINITY 65535
typedef struct { //对邻接矩阵进行定义
VertexType vexs [MAXVEX];
ElementType arc [MAXVEX][MAXVEX];
int numEdge, numvertex;
}Graph;
void CreatGraph(Graph *G) {
int i, j, k;
printf("输入顶点数和边数: ");
scanf("%d,%d", &G->numvertex, &G->numEdge);
for (i = 0; i < G->numvertex; i++) { //初始化顶点
scanf("%d", G->vexs[i]);
}
for (i = 0; i < G->numEdge; i++) {
for (j = 0; j < G->numEdge; j++) { //对边进行初始化
G->arc[i][j] = 0;
}
}
for (int k = 0; k < G->numEdge; k++) {
scanf("%d,%d,%d", &i, &j, &k);
G->arc[i][j] = k;
G->arc[j][i] = G->arc[i][j];
}
}
对邻接矩阵进行深度遍历
typedef int Boolean;
Boolean visited[MAXVEX];
void DFS(Graph *G, int i) { //邻接矩阵的深度优先递归算法
visited[i] = true;
printf("%d", G->vexs[i]);//也可以输出其他信息
for (int j = 0; j < G->numEdge; j++) {
if (G->arc[i][j] !=0 &&visited[j] == false)
DFS(G, j);
}
}
void DFSTraverse(Graph* G) { //邻接矩阵的深度遍历操作
for (int i = 0; i < G->numvertex; i++) {
visited[i] = false;
}
for (int i = 0; i < G->numvertex; i++) {
if (!visited[i])
DFS(G, i);
}
}
对邻接矩阵进行广度优先
void BFSTraverse(Graph *G) {
Queue Q; //队列需要自己额外定义
for (int i = 0; i < G->numvertex; i++) {
visited[i] = false;
}
InitQueue(&Q);
for (int i = 0; i < G->numvertex; i++) {
if (!visited[i]) {
visited[i] = true;
printf("%d", G->vexs[i]);//也可以输入其他信息
EnQueue(&Q, i); //进行入队操作
if (!QueueEmpty(Q)) {
DeQueue(&Q, &i); //出队操作
for (int j = 0; j < G->numvertex; j++) {
if (G->arc[i][j] != 0 && !visited[j]) {
visited[j] = true;
printf("%d", G->vexs[j]);//也可以输出其他信息
EnQueue(&Q, j);
}
}
}
}
}
}
对邻接表进行定义
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100
typedef int EdgeType;
typedef char VertexType;
typedef struct EdgeNode {
int adjvex;
EdgeType weight;
struct EdgeNode*next;
}EdgeNode;
typedef struct VertexNode {
VertexType date;
struct EdgeNode*firstEdge;
}VertexNode,AdjList[MAXVEX];
typedef struct GraphAdjList {
AdjList adjlist;
int numEdge, numvertex;
}GraphAdjList;
void GreateAdjlist(GraphAdjList*G) {
int i, j, q;
EdgeNode *e;
scanf("%d,%d", &G->numEdge, G->numvertex); //输入邻接表边和顶点个数
for (i = 0; i < G->numvertex; i++) {
scanf("%d", G->adjlist[i].date); //输入顶点信息
G->adjlist[i].firstEdge = NULL;
}
for (j = 0; j < G->numEdge; j++) { //建立边表
scanf("%d,%d,%d", &i, &j, &q);
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->weight = q;
e->adjvex = j;
e->next = G->adjlist[i].firstEdge;
G->adjlist[i].firstEdge = e;
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->weight = q;
e->adjvex = i;
e->next = G->adjlist[j].firstEdge;
G->adjlist[j].firstEdge = e;
}
}
对邻接表进行深度优先递归
typedef int Boolean;
Boolean visted[MAXVEX];
void DFS(GraphAdjList*G, int i) {
int j;
EdgeNode*P;
visted[i] = true;
printf("%d", G->adjlist[i].date);//输出对应信息
P = G->adjlist[i].firstEdge;
while (P) {
if (!visted[P->adjvex])
DFS(G, P->adjvex);
P = P->next;
}
}
void DFSTraverse(GraphAdjList*G) {
int i;
for (i = 0; i < G->numvertex; i++) {
visted[i] = false;
}
for (i = 0; i < G->numvertex; i++) {
if (!visted[i])
DFS(G, i);
}
}
对邻接表进行广度优先算法
void BFSTraverse(GraphAdjList*G) {
Queue Q;
EdgeNode *p;
for (int i = 0; i < G->numvertex; i++) {
visted[i] = false;
}
for (int i = 0; i < G->numvertex; i++) {
if (!visted[i]) {
visted[i] = true;
printf("%d", G->adjlist[i].date);
EnQueue(&Q, i);
if (!QueueEmpty(Q)) {
DeQueue(&Q, &i);
p = G->adjlist[i].firstEdge;
while (p) {
if (!visted[p->adjvex]) {
visted[p->adjvex] = true;
printf("%d", G->adjlist[p->adjvex].date);
EnQueue(&Q, p->adjvex);
}
p = p->next;
}
}
}
}
}