Blame view

addons/fastflow/model/graph.php 1.17 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  <?php

  

  namespace addons\fastflow\model;

  

  class graph

  {

      private $found_path = [];

      private $node_stack = [];

      private $pathIndex = 0;

      private $visited = [];

  

      private function findAllPath($matrix, $start_node, $end_node)

      {

          $this->node_stack[] = $start_node;

          $this->visited[] = $start_node;

          $temp_node = end($this->node_stack);

          if ($temp_node == $end_node) {

              foreach ($this->node_stack as $node) {

                  $this->found_path[$this->pathIndex][] = $node;

              }

              array_pop($this->node_stack);

              array_pop($this->visited);

              $this->pathIndex += 1;

              return;

          }

          $targets = $matrix[$temp_node];

  

          foreach ($targets as $target) {

              if (!in_array($target,$this->visited)) {

                  self::findAllPath($matrix, $target, $end_node);

              }

          }

          array_pop($this->node_stack);

          array_pop($this->visited);

      }

  

      public function getAllPath($matrix, $start_node, $end_node)

      {

          self::findAllPath($matrix, $start_node, $end_node);

          return $this->found_path;

      }

  

  }