Figure of the algorithm series--BFS

Source: Internet
Author: User

The breadth-first search takes the source node s as the starting point, and the algorithm always extends the boundary between the discovered and the uncovered nodes, extending outward along its breadth direction. That is, the algorithm needs to discover all the nodes that are from the source node S K to find other nodes that are k+1 from the source node.

Talk is cheap,show me the code! on the specific code, the easiest to explain it all.

This example is based on an no-map, and the comments in the code are specified and are not explained in each step:

1#include <iostream>2#include <list>3#include <queue>4 using namespacestd;5 6 //graph without Direction7 classgraph{8 Private:9     intV//number of nodes in the graphTenlist<int> *adj;//point to an array that contains a list of pro links One  Public: AGraph (intv); -     voidAddedge (intStartintEnd);//add a pro edge -     voidBFS (ints);//Breadth First Search the }; -  -Graph::graph (intv) { -      This->v =v; +Adj =Newlist<int>[v]; - } +  A //add an edge to the No map add two items to the Pro table at voidGraph::addedge (intStartintend) { - adj[start].push_back (end); - adj[end].push_back (start); - } -  - voidGRAPH::BFS (ints) { in     //mark all nodes first without being accessed. -     BOOL* visited =New BOOL[v]; to      for(intI=0; i<v;i++) +Visited[i] =false; -     //Create a queue thequeue<int> q = queue<int>(); *     //set source node first $Visited[s] =true;Panax Notoginseng Q.push (s); -  the      while(!Q.empty ()) { +         intnode =Q.front (); Acout<<node<<" "; the Q.pop (); +         //node node's pro table -list<int> nlist =Adj[node]; $list<int>::iterator Beg =Nlist.begin (); $          for(; Beg! = Nlist.end (); beg++){             -             if(!visited[*Beg]) { -Visited[*beg] =true; theQ.push (*beg); -             }Wuyi         } the     } -cout<<"\ n"; Wu } -  About intMain () { $Graph G = Graph (6); -G.addedge (0,1); -G.addedge (0,2); -G.addedge (0,5); AG.addedge (1,3); +G.addedge (2,3); theG.addedge (2,5); -G.addedge (2,4); $G.addedge (4,5); theG.BFS (0); the  the     return 0; the}

Each node in this example is represented by an integer and, more complicated, each node can be organized into a node structure, which can add a distance attribute to each node node.

The results of the operation are as follows:

Figure of the algorithm series--BFS

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.