-
Notifications
You must be signed in to change notification settings - Fork 0
/
005.GraphBFS.cpp
46 lines (45 loc) · 1.14 KB
/
005.GraphBFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "graph.h"
#include <iostream>
#include <queue>//for BFS queue
//广度优先搜索
void GraphBFS(Graph *graph,int s)
{
//args init
int i,n=graph->nodeNum,u,v;
for(i=0;i<n;++i)
{
graph->G[i].color = WHITE;
graph->G[i].d = INFINITE;
graph->G[i].pi = NIL;
}
//循环不变式:队列Q中存储着访问到却还没有遍历其邻接链表的节点
//访问源点s
graph->G[s].color = GRAY;
std::cout << "find: " << s << " ";
graph->G[s].d = 0;
graph->G[s].pi = NIL;
std::queue<int> Q;
Q.push(s);
while(!Q.empty())
{
u = Q.front();
Q.pop();
//访问节点u的所有相邻白色节点
LNode *lnptr = graph->G[u].next;
while(lnptr)
{
v = lnptr->n;
if(graph->G[v].color == WHITE)
{
graph->G[v].color = GRAY;
std::cout << v << " ";
graph->G[v].d = graph->G[u].d + 1;
graph->G[v].pi = u;
Q.push(v);
}
lnptr = lnptr->next;
}
graph->G[u].color = BLACK;
}
std::cout << "\n";
}