最近学习了图的存储和遍历,DFS借助递归(或者循环)进行,BFS的重要特点就是要存储节点数据,而且先入者先出,要借助队列进行。
6.1
#include <stdio.h>
typedef enum {false, true} bool;
#define MaxVertexNum 10 /* 最大顶点数设为10 */
#define INFINITY 65535 /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex; /* 用顶点下标表示顶点,为整型 */
typedef int WeightType; /* 边的权值设为整型 */
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv; /* 顶点数 */
int Ne; /* 边数 */
WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
};
typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
bool Visited[MaxVertexNum]; /* 顶点的访问标记 */
MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */
void Visit( Vertex V )
{
printf(" %d", V);
}
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );
int main()
{
MGraph G;
Vertex V;
G = CreateGraph();
scanf("%d", &V);
printf("DFS from %d:", V);
DFS(G, V, Visit);
return 0;
}
/* 你的代码将被嵌在这里 */
void DFS(MGraph Graph, Vertex V) {
Vertex W;
Visit(V);
Visited[V] = true;
for (W = 0; W < Graph->Nv; W++) {
if (!Visited[W] && (Graph->G[V][W] < INFINITY))
DFS(Graph, W);
}
}
6.2
#include <stdio.h>
#include <malloc.h>
#define MaxVertexNum 10 /* 最大顶点数设为10 */
typedef int Vertex; /* 用顶点下标表示顶点,为整型 */
/* 邻接点的定义 */
typedef struct AdjVNode* PtrToAdjVNode;
struct AdjVNode {
Vertex AdjV; /* 邻接点下标 */
PtrToAdjVNode Next; /* 指向下一个邻接点的指针 */
};
/* 顶点表头结点的定义 */
typedef struct Vnode {
PtrToAdjVNode FirstEdge; /* 边表头指针 */
} AdjList[MaxVertexNum]; /* AdjList是邻接表类型 */
/* 图结点的定义 */
typedef struct GNode* PtrToGNode;
struct GNode {
int Nv; /* 顶点数 */
int Ne; /* 边数 */
AdjList G; /* 邻接表 */
};
typedef PtrToGNode LGraph; /* 以邻接表方式存储的图类型 */
bool Visited[MaxVertexNum]; /* 顶点的访问标记 */
LGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */
void Visit(Vertex V)
{
printf(" %d", V);
}
void BFS(LGraph Graph, Vertex S, void (*Visit)(Vertex));
int main()
{
LGraph G;
Vertex S;
G = CreateGraph();
scanf("%d", &S);
printf("BFS from %d:", S);
BFS(G, S, Visit);
return 0;
}
/* 你的代码将被嵌在这里 */
typedef Vertex ElementType;
typedef int Position;
#define ERROR -1
typedef struct QNode* PtrToNode;
struct QNode {
ElementType* data;
Position Front, rear;
int Maxsize;
};
typedef PtrToNode Queue;
Queue CreatQueue(int maxsize){
Queue Q = (Queue) malloc (sizeof(struct QNode));
Q->data = (ElementType*)malloc(maxsize * sizeof(ElementType));
Q->Front = Q->rear = 0;
Q->Maxsize = maxsize;
return Q;
}
bool IsEmpty(Queue Q) {
return (Q->Front == Q->rear);
}
bool IsFull(Queue Q) {
return((Q->rear + 1) % Q->Maxsize == Q->Front);
}
bool Add(Queue Q, ElementType X) {
if (IsFull(Q))
return false;
else {
Q->rear = (Q->rear + 1) % Q->Maxsize;
Q->data[Q->rear] = X;
return true;
}
}
ElementType Delete(Queue Q) {
if (IsEmpty(Q))
return ERROR;
else {
Q->Front = (Q->Front + 1) % Q->Maxsize;
return Q->data[Q->Front];
}
}
void BFS(LGraph Graph, Vertex S, void (*Visit)(Vertex)) {
Queue Q;
Vertex V;
PtrToAdjVNode W;
Q = CreatQueue(MaxVertexNum);
Visit(S);
Visited[S] = true;
Add(Q, S);
while (!IsEmpty(Q)) {
V = Delete(Q);
for (W = Graph->G[V].FirstEdge; W; W = W->Next)
if (!Visited[W->AdjV]) {
Visit(W->AdjV);
Visited[W->AdjV] = true;
Add(Q, W->AdjV);
}
}
}
1.邻接表是目前学过的最复杂的结构,要多加练习
2.队列创建的时候要注意,rear和front都是+1之后赋值或者返回值的
本文深入探讨了图的两种基本存储结构:邻接矩阵和邻接表,并通过具体的C语言代码示例,详细讲解了深度优先搜索(DFS)和广度优先搜索(BFS)的实现过程。同时,强调了邻接表的复杂性和队列在BFS中的关键作用。
734

被折叠的 条评论
为什么被折叠?



