DFS BFS Sample and BFS part algorithm code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.