The previous article wrote the Dfs adjacency matrix and adjacency table, and this one writes about the BFS
First about BFS, full name Breadth First search, no backtracking and probing, step by step, using queue implementation
Accesses the current vertex v first, then accesses each of the inaccessible adjacency vertices of V, and then accesses the inaccessible adjacency vertices of each of the adjacent vertices of V at a time
The code is implemented as follows:
void BFS (Graph &g, int v) {int i, W, n = g.number ();////////////////////////////////////////// +) {visit[i] =false; int loc = G.getvertexpos (v); cout << g.getvalue (Loc) <<endl; Visit[loc] = true;//first vertex access After the queue Q; Q.enqueue (loc);//Enter queue while (! Q.isempty ()) {q.dequeue (loc), w = G.getfirstneig (loc), while (w! =-1) {if (Visit[w] ==false) {cout << g.getvalue (w) << ""; visit[w] = true; Q.enqueue (w);} w = G.getnextneig (Loc, w);}} delete []visit;} }
The code is written in text, probably not very canonical, probably the same as in the book, the title is: Data structure (using object-oriented method and C + + language description)
DFS BFS Sample and BFS part algorithm code