邻接矩阵存储图的深度优先遍历
试实现邻接矩阵存储图的深度优先遍历。
函数接口定义:
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );
其中MGraph是邻接矩阵存储的图,定义如下:
typedef struct GNode PtrToGNode;
struct GNode{
int Nv; / 顶点数 /
int Ne; / 边数 /
WeightType G[MaxVertexNum][MaxVertexNum]; / 邻接矩阵 /
};
typedef PtrToGNode MGraph; / 以邻接矩阵存储的图类型 */
函数DFS应从第V个顶点出发递归地深度优先遍历图Graph,遍历时用裁判定义的函数Visit访问每个顶点。当访问邻接点时,要求按序号递增的顺序。题目保证V是图中的合法顶点。
裁判测试程序样例:
#include
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;
}
/* 你的代码将被嵌在这里 */
输入样例:给定图如下
5
输出样例:
DFS from 5: 5 1 3 0 2 4 6
dfs都是用递归遍历,bfs都是用队列遍历。
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
{
int w;
// printf(" %d",V);
Visit(V);
Visited[V]=true;
for(w=0;w<Graph->Nv;w++){
if(!Visited[w]&& Graph->G[V][w]<INFINITY){
DFS(Graph,w,Visit);
}
}
}
邻接表存储图的广度优先遍历
试实现邻接表存储图的广度优先遍历。
函数接口定义:
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) );
其中LGraph是邻接表存储的图,定义如下:
/* 邻接点的定义 */
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; / 以邻接表方式存储的图类型 */
函数BFS应从第S个顶点出发对邻接表存储的图Graph进行广度优先搜索,遍历时用裁判定义的函数Visit访问每个顶点。当访问邻接点时,要求按邻接表顺序访问。题目保证S是图中的合法顶点。
裁判测试程序样例:
#include
typedef enum {false, true} bool;
#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;
}
2
输出样例:
BFS from 2: 2 0 3 5 4 1 6
题中应该自带有队列的函数,名字和书上的一样,不过我想复习一下队列,就自己写了函数了。
#include
int maxsize=100;
typedef struct node *queue;
struct node{
int *data,front,rear,maxsize;
};
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
queue q=(queue)malloc(sizeof(struct node));
q->data=(int *)malloc(sizeof(int)*maxsize);
q->front=q->rear=0;
Visit(S);
// add[q , S]; q->rear=(q->rear+1)%q->maxsize;
q->data[++q->rear]=S;
Visited[S]=true;
PtrToAdjVNode w;
while(q->front!=q->rear){
int v=q->data[++q->front];
for(w=Graph->G[v].FirstEdge;w;w=w->Next){
if(!Visited[w->AdjV]){
Visit(w->AdjV);
q->data[++q->rear]=w->AdjV;
Visited[w->AdjV]=true;
}
}
}
}
学的书上的代码的话用c吧,有点麻烦,做题用c++吧,该学学了。