Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Graph
- {
- protected $_len = 0;
- protected $_i = 0;
- protected $_g = array();
- protected $_visited = array();
- public function __construct()
- {
- // $this->_g = array(
- // array(0, 1, 1, 0, 0, 0),
- // array(1, 0, 0, 1, 0, 0),
- // array(1, 0, 0, 1, 1, 1),
- // array(0, 1, 1, 0, 1, 0),
- // array(0, 0, 1, 1, 0, 1),
- // array(0, 0, 1, 0, 1, 0),
- // );
- $this->_g = array(
- 'A' => array('B', 'F'),
- 'B' => array('A', 'D', 'E'),
- 'C' => array('F'),
- 'D' => array('B', 'E'),
- 'E' => array('B', 'D', 'F'),
- 'F' => array('A', 'C', 'E'),
- );
- $this->_len = count($this->_g);
- $this->_initVisited();
- // print_r($this->_visited);exit;
- }
- protected function _initVisited()
- {
- // for ($i = 0; $i < $this->_len; $i++) {
- // $this->_visited[$i] = 0;
- // }
- foreach ($this->_g as $key => $value) {
- $this->_visited[$key] = 0;
- }
- }
- public function depthFirst($ori, $desti)
- {
- $this->_i++;
- $this->_visited[$ori] = 1;
- foreach($this->_g[$ori] as $val){
- if($val==$desti){
- echo $val;
- exit;
- }else{
- if($this->_visited[$val] == 0){
- echo $val . "->";
- $this->depthFirst($val, $desti);
- }
- }
- }
- // echo $ori . "\n";
- // for ($i = 0; $i < $this->_len; $i++) {
- // if ($this->_g[$vertex][$i] == 1 && !$this->_visited[$i]) {
- // $this->depthFirst($i);
- // }
- // }
- // foreach ($this->_g[] as $key => $value) {
- // if($this->_g[$ori][$key] == ){
- // }
- // }
- }
- }
- $g = new Graph();
- // 2 0 1 3 4 5
- $g->depthFirst('D', 'C');
- // echo $this->_i;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement