Advertisement
tsuedeun

Untitled

Jun 5th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2. class Walker {
  3.     public $graph, $path;
  4.  
  5.     public function __construct($graph) {
  6.         $this->graph = $graph;
  7.         $this->path = [];
  8.     }
  9.  
  10.     public function DepthRec($node) { //РЕКУРРЕНТНЫЙ ОБХОД ВГЛУБИНУ
  11.         $this->path[$node] = true;
  12.         foreach ($this->graph->getEdges($node) as $node2 => $length)
  13.             if (!$this->path[$node2])
  14.                 $this->DepthRec($node2);
  15.     }
  16.  
  17.     public function DepthIter($node) { //ИТЕРАТИВНЫЙ АЛГОРИТМ ОБХОДА ГРАФА ВГЛУБИНУ
  18.         $path = [];
  19.         $stack = new Stack();
  20.         $stack->put($node);
  21.         while ($stack->last != null) {
  22.             $curr = $stack->get();
  23.             $path[$curr] = true;
  24.             foreach ($this->graph->getEdges($curr) as $node2 => $length)
  25.                 if (!$path[$node2] && !$stack->contains($node2))
  26.                     $stack->put($node2);
  27.         }
  28.         return $path;
  29.     }
  30.  
  31.     public function waves($node) { //ИТЕРАТИВНЫЙ АЛГОРИТМ ОБХОДА ГРАФА В ШИРИНУ
  32.         $path = [];
  33.         $queue = new Queue();
  34.         $queue->put($node);
  35.         while ($queue->first != null) {
  36.             $curr = $queue->get();
  37.             $path[$curr] = true;
  38.             foreach ($this->graph->getEdges($curr) as $node2 => $length)
  39.                 if (!$path[$node2] && !$queue->contains($node2))
  40.                     $queue->put($node2);
  41.         }
  42.         return $path;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement