# Detailed PHP implementation of depth-first search algorithm

Source: Internet
Author: User
This article mainly introduced the PHP implementation of the depth-first search algorithm (dfs,depth, first search), simple analysis of the depth-priority search algorithm principle and combined with specific examples of the implementation of PHP depth-first search for the specific steps and related operating skills, the need for friends can refer to the next

In this paper, the implementation of depth-first search algorithm in PHP is described. Share to everyone for your reference, as follows:

The implementation principle of depth-first search:

Implementation code:

`<?phpclass search_method{  //non-graph array description  private \$dfs _save;  Global record array  private \$arr;  Control Branch-  private \$k = 0;  Public function __construct ()  {    \$this->dfs_save = Array (Array (      0,1,1,1,0,0,0,0,0),      Array ( 1,0,0,0,1,0,0,0,0),      Array (1,0,0,0,0,1,0,0,0),      Array (1,0,0,0,0,0,1,0,0),      Array (0,1,0,0,0,1,0,0,1 ),      Array (0,0,1,0,1,0,0,1,0),      Array (0,0,0,1,0,0,0,0,0),      Array (0,0,0,0,0,1,0,0,0),      Array ( 0,0,0,0,1,0,0,0,0),    );    \$this->arr = Array ();  }  Recursive implementation of depth-first search public  function dfs (\$v)  {    //Do some operations on vertices    echo str_repeat ("-", \$this->k);    Echo ' V '. (\$v + 1). ' <br> ';    Record the visited vertex    \$this->arr[]= \$v;    Find vertices connected to vertices, continue depth first search for    (\$i =0; \$i <9; \$i + +)    {if      (!in_array (\$i, \$this->arr) &&\$ if present this->dfs_save[\$v] [\$i]==1)      {        \$this->k++;        \$this->dfs (\$i);      }    }    \$this->k--;    return;}  }? >`

To achieve the output result:

`V1-V2--V5---V6----V3----V8---v9-v4--v7`
Related Keywords:
Related Article

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.