In-depth description of the breadth-First Search algorithm (BFS, Broad First Search) implemented by PHP, bfsbroad

Source: Internet
Author: User

In-depth description of the breadth-First Search algorithm (BFS, Broad First Search) implemented by PHP, bfsbroad

This example describes how to implement the breadth-first search algorithm in PHP. We will share this with you for your reference. The details are as follows:


Breadth-first traversal is a traversal policy for connected graphs. Because its idea is from a vertex V0, the radiation first traverses the wide area around it, hence the name.

The breadth-first search traversal is similar to the hierarchy traversal of a tree. For a undirected connected graph, the breadth-first search starts from a vertex v0 of the graph. After accessing v0, search for the neighboring contacts w1, w2,… that have not been accessed by accessing v0 in sequence ,.... Then, you can search for unaccessed adjacent contacts of w1 and those of w2 in sequence ,.... That is to say, access from v0, from near to far, and access from v0 to v0 in hierarchical order has the same path and the path length is 1, 2 ,... Until all vertices in the connected graph are accessed once.

As long as you access the vertices of each layer in a certain order, it is easy to implement the program. The overall level order of the breadth-first search is certain, and the access order of each layer is not unique.

The detailed description is as follows:

If the initial state of graph G is that all vertices are not accessed, and you can select a vertex I in G as the initial vertex, the basic idea of breadth-first search is:

(1) access and record a vertex V.
(2) access all adjacent vertices of V in sequence;
(3) Access the unaccessed adjacent points from these neighboring points in sequence until all the neighboring points of the accessed vertex in the figure are accessed.
(4) Step (3.

And so on until all vertices in the graph are accessed.

When you search for access to a layer, you need to remember the accessed vertex to search for access to the adjacent vertex from the accessed vertex when accessing the lower vertex. Therefore, you need to set a Queue in the breadth-first search so that the accessed vertex sequence enters the Queue from the end of the team. When searching and accessing lower-level vertices, first retrieve an accessed upper-level vertex from the first of the team, and then search for each adjacent vertex to access it from the vertex.

SearchInterface. php:

<? Phpabstract class SearchInterface {protected $ G; // fig protected $ s; // function _ construct ($ _ G, $ _ s) of the first node of the graph) {$ this-> G =$ _ G; $ this-> s =$ _ s;} public abstract function search () ;}?>

Bfs. php:

<? Phpinclude_once ('searchinterface. php '); class bfs extends SearchInterface {private $ d = array (); // The distance between the Source Vertex s and the vertex u is private $ tt = array (); // The parent of node u is saved in the variable private $ visit = array (); // The accessed node function _ construct ($ _ G, $ _ s) {parent :: __construct ($ _ G, $ _ s); // initialize $ d/$ tt. The initial value is infinity/NULL for ($ I = 0; $ I <9; $ I ++) {$ this-> d [$ I] = 20000; $ this-> tt [$ I] = NULL; $ this-> visit [$ I] = 0 ;}} public function search () {// access all nodes $ queue = Array (); for ($ I = 0; $ I <9; $ I ++) {if ($ this-> visit [$ I] = 0) {array_push ($ queue, $ I); while (! Empty ($ queue) {$ _ s = array_shift ($ queue); $ this-> visit [$ _ s] = 1; echo ($ _ s + 1 ). '<br>'; $ link_s = $ this-> G-> get_links ($ _ s ); // obtain the vertex u foreach ($ link_s as $ j => $ u) {if ($ this-> visit [$ u] = 0) that is directly connected to s) {array_push ($ queue, $ u); $ this-> visit [$ u] = 2 ;}}}}}?>


$G = new Graphic;$search = new bfs($G,1);$search->search();

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: 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.