#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int arcs[100][100];
int vexnum, arcnum;
//图结构
}AMGraph;
//深度优先搜索遍历
int visited[100];
void DFS(AMGraph G, int v)
{
printf("%d", v);
visited[v] = 1;//访问第v个顶点
for (int w = 0; w < G.vexnum; w++)
{
if (G.arcs[v][w] != 0 && !(visited[w]))
{
DFS(G, w);//递归调用DFS
}
}
}
void BFS(Graph G, int v)
{
printf("%d", v);
visited[v] = 1;
InitQueue(Q);//辅助队列初始化置空
EnQueue(Q, v);//v进队
while (!QueueEmpty(Q))//队列非空
{
DeQueue(Q, u);
for (int w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w))
{
if (!visited[w])
{
printf("%d", w);
visited[w] = 1;
EnQueue(Q, w);
}
}
}
}
int main()
{
for (int i = 0; i < 100; i++)
visited[i] = 0;
return 0;
}
BFS和DFS笔记
最新推荐文章于 2024-08-30 15:57:36 发布