A detailed introduction to the method of implementing breadth-first search algorithm in PHP

Source: Internet
Author: User
This article mainly introduces the implementation of the breadth-first search algorithm of PHP, Bfs,broad first Search, briefly describes the principle of the breadth-priority searching algorithm and analyzes the steps of the implementation of the breadth-first search algorithm with specific examples, and the related operation skills, the need for friends can refer to the following

In this paper, we describe the breadth-first search algorithm for PHP. Share to everyone for your reference, as follows:

The idea of breadth-first search algorithm breadth-firsttraversal

Breadth-first traversal is a traversal strategy for connected graphs. Because its idea is to start from a vertex V0, radiating the priority to traverse its wider area around, so named.

Breadth-First search traversal is similar to a tree-by-hierarchy traversal. For undirected connected graphs, the breadth-first search is v0 from a vertex of the graph, and after accessing V0, it searches for each of the inaccessible adjacency points of the V0 w1,w2, .... Then the sequential search accesses the w1 neighboring points of the W2, each of which has not been visited, .... That is, from the beginning of the V0, from near to far, according to the level of access to the V0 has a path and the length of the path is 1, 2, ... Vertex, until all vertices in the connected graph are accessed once.

As long as a certain order of access to each layer vertex, convenient program implementation, breadth first search of the overall level of order must be, the level of access order is not unique.

Described below:

The initial state of the graph G is that all vertices are not accessed, and the basic idea of a breadth-first search is to select vertex i as the starting point in G:

(1) A vertex of V is accessed and logged.
(2) Access all adjacent vertices of V in turn;
(3) From these adjacency points, and then access their inaccessible neighboring points, until all of the vertices visited in the graph adjacency points are accessed.
(4) Step (3).

And so on until all the vertices in the diagram have been accessed.

Breadth-First search when you access a layer of search, you need to remember the vertices that have been accessed so that when you access the underlying vertices, a search from the vertex that has been visited accesses its adjacency point. So in the breadth-first search, you need to set up a queue of queues, so that the visited vertex order is entered into the queue by the tail. When a search accesses an underlying vertex, a top vertex is fetched from the first of the team, and then a search is made from that vertex to access each of its adjacency points.

searchinterface.php:


<?phpabstract class searchinterface{  protected $G;//graph  protected $s;//Graph's first node  function __construct ($_g, $_s) {$this->g = $_g; $this->s = $_s;}  Public abstract function Search ();}? >

bfs.php:


<?phpinclude_once (' searchinterface.php '), class BFS extends searchinterface{private $d = Array ();//distance between source point s and vertex u Private $tt = Array ();//Node U's parents are stored in the variable private $visit = Array ();//accessed Nodes function __construct ($_g,$_s) {Parent::__constr    UCT ($_g,$_s);      Initializes the $d/$tt with an initial value of 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);              Gets and s directly connected to the vertex u foreach ($link _s as $j + = $u) {if ($this->visit[$u]==0) {              Array_push ($queue, $u);            $this->visit[$u] = 2;        }          }}}}}}?> 

How to use:


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