#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);
}
LGraph CreateGraph(int VertexNum)
{ /* 初始化一个有VertexNum个顶点但没有边的图 */
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode)); /* 建立图 */
Graph->Nv = VertexNum;
Graph->Ne = 0;
/* 初始化邻接表头指针 */
/* 注意:这里默认顶点编号从0开始,到(Graph->Nv - 1) */
for (V = 0; V<Graph->Nv; V++)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void DFS(LGraph Graph, Vertex V, void(*Visit)(Vertex))
{ /* 以V为出发点对邻接表存储的图Graph进行DFS搜索 */
PtrToAdjVNode W;
Visit(V); /* 访问第V个顶点 */
Visited[V] = true; /* 标记V已访问 */
for (W = Graph->G[V].FirstEdge; W; W = W->Next) /* 对V的每个邻接点W->AdjV */
if(!Visited[W->AdjV]) /* 若W->AdjV未被访问 */
DFS(Graph, W->AdjV, Visit); /* 则递归访问之 */
}
void DFSTraverse(Graph G,Status(*Visit)(int v))
{
VisitFunc =Visit;
for(v=0; v<G.vexnum; ++) visited[v] =FALSE;
for(v=0; v<G.vexnum; ++v)
if(!visited[v]) DFS(G,v);
}
void DFS(Graph G, int v)
{ //从顶点v出发,采用递归,深度遍历
visit(v); // 访问顶点v
visited[v]=TRUE; //标记访问
for(w=FirstNeighbor(G,v); w>=0;w=NextNeighor(G,v,w))
//FirstNeighbor()=图G中顶点x的第一个领接点,有则返回序列号
//NextNeighor()=如果y是x的一个领接点,返回除y之外的顶点x的下一个领接点号
if(!visited[w])
DFS(G,w);
}
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
int q[1005];
int head=0,rear=0;
q[rear++]=S;
Visited[S]=false;
while(head!=rear)
{
PtrToAdjVNode p= Graph->G[q[head]].FirstEdge;
if(p==NULL)(*Visit)(q[head++]);
else
{
while(p!=NULL)
{
if(Visited[p->AdjV]==true)
{
q[rear++]=p->AdjV;
Visited[p->AdjV]=false;
}
p=p->Next;
}
(*Visit)(q[head++]);
}
}
}
void BFS(MGraph Graph, Vertex S, void(*Visit)(Vertex))
{ /* 以S为出发点对邻接矩阵存储的图Graph进行BFS搜索 */
Queue Q;
Vertex V, W;
Q = CreateQueue(); /* 创建空队列, MaxSize为外部定义的常数 */
/* 访问顶点S:此处可根据具体访问需要改写 */
Visit(S);
Visited[S] = true; /* 标记S已访问 */
InsertQ(S, Q); /* S入队列 */
while (!IsEmpty(Q)) {
V = DeleteQ(Q); /* 弹出V */
for (W = 0; W<Graph->Nv; W++) /* 对图中的每个顶点W */
/* 若W是V的邻接点并且未访问过 */
if (!Visited[W] && IsEdge(Graph, V, W)) {
/* 访问顶点W */
Visit(W);
Visited[W] = true; /* 标记W已访问 */
InsertQ(W, Q); /* W入队列 */
}
} /* while结束*/
}
void print_graph(Graph G)
{
int i,j;
printf("matrix Graph:\n");
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
printf("%d ",G.matrix[i][j]);
printf("\n");
}
}int first_vertex(Graph G,int v)
{
int i;
for(i=0;i<G.vexnum;i++)
if(G.matrix[v][i]==1)
return i;
return -1;
}
int next_vertex(Graph G,int v,int w)
{
int i;
for(i=w+1;i<G.vexnum;i++)
if(G.matrix[v][i]==1)
return i;
return -1;
}
void BFSTraverse(Graph G, Status (*Visit)(int v))
{
// 按广度优先非递归遍历图G。使用辅助队列Q和访问标志数组visited。
for (v=0; v<G.vexnum; ++v) visited[v] = FALSE;
InitQueue(Q); // 置空的辅助队列Q
for ( v=0; v<G.vexnum; ++v )
if (!visited[v])
{
EnQueue(Q, v); //入队
visited[u] = TRUE;
Visit(u); //访问
while (!QueueEmpty(Q))
{
DeQueue(Q, u); //出队
for ( w=FirstAdjVex(G, u); w!=0; w=NextAdjVex(G, u, w) )
if ( ! visited[w])
{
visited[u] = TRUE;
Visit(u); //访问
EnQueue(Q, w); //入队
}
} //while
} //if
}
图的遍历
最新推荐文章于 2023-07-19 21:01:33 发布