3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Graph { private $graph = []; private $nodes = []; public function __construct(array $edges) { foreach ($edges as $edge => $weight) { $start = $edge[0]; $end = $edge[1]; $this->graph[$start][$end] = $weight; $this->graph[$end][$start] = $weight; $this->nodes[$start] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; $this->nodes[$end] = [ 'L' => INF, 'Q' => null, 'final' => false, ]; } } public function find($start, $end) { $this->initStartNode($start); $nodes = $this->getNextNodes($start); $startLength = $this->getLength($start); foreach($nodes as $node => $weight) { $currentLength = $this->getLength($node); if ($startLength + $weight < $currentLength) { $this->setLength($node, $startLength + $weight); $this->setFromNode($node, $start); } } // var_dump($this->getGraph()); var_dump($this->getNodes()); die(); } protected function initStartNode($node) { $this->setLength($node, 0); $this->setFromNode($node, '-'); $this->setFinal($node); } protected function getNextNodes($node) { return $this->graph[$node]; } protected function setLength($node, $value) { $this->nodes[$node]['L'] = $value; } protected function getLength($node) { return $this->nodes[$node]['L']; } protected function setFromNode($node, $value) { $this->nodes[$node]['Q'] = $value; } protected function setFinal($node) { $this->nodes[$node]['final'] = true; } public function getGraph() { return $this->graph; } public function getNodes() { return $this->nodes; } } $g = new Graph([ 'AB' => 5, 'AC' => 2, 'CB' => 2, 'CD' => 8, 'BE' => 7, 'DE' => 1, ]); $result = $g->find('A', 'E'); // var_dump($result); // var_dump($g->getGraph()); var_dump($g->getNodes());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  (null)
number of ops:  15
compiled vars:  !0 = $g, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   NEW                                              $2      'Graph'
   91     1        SEND_VAL_EX                                              <array>
          2        DO_FCALL                                      0          
   90     3        ASSIGN                                                   !0, $2
   99     4        INIT_METHOD_CALL                                         !0, 'find'
          5        SEND_VAL_EX                                              'A'
          6        SEND_VAL_EX                                              'E'
          7        DO_FCALL                                      0  $5      
          8        ASSIGN                                                   !1, $5
  102     9        INIT_FCALL                                               'var_dump'
         10        INIT_METHOD_CALL                                         !0, 'getNodes'
         11        DO_FCALL                                      0  $7      
         12        SEND_VAR                                                 $7
         13        DO_ICALL                                                 
         14      > RETURN                                                   1

Class Graph:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 23
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/7UaXZ
function name:  __construct
number of ops:  25
compiled vars:  !0 = $edges, !1 = $weight, !2 = $edge, !3 = $start, !4 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    9     1      > FE_RESET_R                                       $5      !0, ->23
          2    > > FE_FETCH_R                                       ~6      $5, !1, ->23
          3    >   ASSIGN                                                   !2, ~6
   10     4        FETCH_DIM_R                                      ~8      !2, 0
          5        ASSIGN                                                   !3, ~8
   11     6        FETCH_DIM_R                                      ~10     !2, 1
          7        ASSIGN                                                   !4, ~10
   12     8        FETCH_OBJ_W                                      $12     'graph'
          9        FETCH_DIM_W                                      $13     $12, !3
         10        ASSIGN_DIM                                               $13, !4
         11        OP_DATA                                                  !1
   13    12        FETCH_OBJ_W                                      $15     'graph'
         13        FETCH_DIM_W                                      $16     $15, !4
         14        ASSIGN_DIM                                               $16, !3
         15        OP_DATA                                                  !1
   15    16        FETCH_OBJ_W                                      $18     'nodes'
         17        ASSIGN_DIM                                               $18, !3
   16    18        OP_DATA                                                  <array>
   20    19        FETCH_OBJ_W                                      $20     'nodes'
         20        ASSIGN_DIM                                               $20, !4
   21    21        OP_DATA                                                  <array>
    9    22      > JMP                                                      ->2
         23    >   FE_FREE                                                  $5
   26    24      > RETURN                                                   null

End of function __construct

Function find:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 33
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 33
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 32
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 32
Branch analysis from position: 33
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 33
filename:       /in/7UaXZ
function name:  find
number of ops:  41
compiled vars:  !0 = $start, !1 = $end, !2 = $nodes, !3 = $startLength, !4 = $weight, !5 = $node, !6 = $currentLength
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   30     2        INIT_METHOD_CALL                                         'initStartNode'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   31     5        INIT_METHOD_CALL                                         'getNextNodes'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $8      
          8        ASSIGN                                                   !2, $8
   33     9        INIT_METHOD_CALL                                         'getLength'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $10     
         12        ASSIGN                                                   !3, $10
   34    13      > FE_RESET_R                                       $12     !2, ->33
         14    > > FE_FETCH_R                                       ~13     $12, !4, ->33
         15    >   ASSIGN                                                   !5, ~13
   36    16        INIT_METHOD_CALL                                         'getLength'
         17        SEND_VAR_EX                                              !5
         18        DO_FCALL                                      0  $15     
         19        ASSIGN                                                   !6, $15
   37    20        ADD                                              ~17     !3, !4
         21        IS_SMALLER                                               ~17, !6
         22      > JMPZ                                                     ~18, ->32
   38    23    >   INIT_METHOD_CALL                                         'setLength'
         24        SEND_VAR_EX                                              !5
         25        ADD                                              ~19     !3, !4
         26        SEND_VAL_EX                                              ~19
         27        DO_FCALL                                      0          
   39    28        INIT_METHOD_CALL                                         'setFromNode'
         29        SEND_VAR_EX                                              !5
         30        SEND_VAR_EX                                              !0
         31        DO_FCALL                                      0          
   34    32    > > JMP                                                      ->14
         33    >   FE_FREE                                                  $12
   44    34        INIT_FCALL                                               'var_dump'
         35        INIT_METHOD_CALL                                         'getNodes'
         36        DO_FCALL                                      0  $22     
         37        SEND_VAR                                                 $22
         38        DO_ICALL                                                 
   45    39      > EXIT                                                     
   46    40*     > RETURN                                                   null

End of function find

Function initstartnode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  initStartNode
number of ops:  13
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1        INIT_METHOD_CALL                                         'setLength'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              0
          4        DO_FCALL                                      0          
   51     5        INIT_METHOD_CALL                                         'setFromNode'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAL_EX                                              '-'
          8        DO_FCALL                                      0          
   52     9        INIT_METHOD_CALL                                         'setFinal'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0          
   53    12      > RETURN                                                   null

End of function initstartnode

Function getnextnodes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  getNextNodes
number of ops:  5
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
   57     1        FETCH_OBJ_R                                      ~1      'graph'
          2        FETCH_DIM_R                                      ~2      ~1, !0
          3      > RETURN                                                   ~2
   58     4*     > RETURN                                                   null

End of function getnextnodes

Function setlength:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  setLength
number of ops:  7
compiled vars:  !0 = $node, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   62     2        FETCH_OBJ_W                                      $2      'nodes'
          3        FETCH_DIM_W                                      $3      $2, !0
          4        ASSIGN_DIM                                               $3, 'L'
          5        OP_DATA                                                  !1
   63     6      > RETURN                                                   null

End of function setlength

Function getlength:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  getLength
number of ops:  6
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   67     1        FETCH_OBJ_R                                      ~1      'nodes'
          2        FETCH_DIM_R                                      ~2      ~1, !0
          3        FETCH_DIM_R                                      ~3      ~2, 'L'
          4      > RETURN                                                   ~3
   68     5*     > RETURN                                                   null

End of function getlength

Function setfromnode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  setFromNode
number of ops:  7
compiled vars:  !0 = $node, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   72     2        FETCH_OBJ_W                                      $2      'nodes'
          3        FETCH_DIM_W                                      $3      $2, !0
          4        ASSIGN_DIM                                               $3, 'Q'
          5        OP_DATA                                                  !1
   73     6      > RETURN                                                   null

End of function setfromnode

Function setfinal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  setFinal
number of ops:  6
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
   77     1        FETCH_OBJ_W                                      $1      'nodes'
          2        FETCH_DIM_W                                      $2      $1, !0
          3        ASSIGN_DIM                                               $2, 'final'
          4        OP_DATA                                                  <true>
   78     5      > RETURN                                                   null

End of function setfinal

Function getgraph:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  getGraph
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   FETCH_OBJ_R                                      ~0      'graph'
          1      > RETURN                                                   ~0
   83     2*     > RETURN                                                   null

End of function getgraph

Function getnodes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7UaXZ
function name:  getNodes
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   FETCH_OBJ_R                                      ~0      'nodes'
          1      > RETURN                                                   ~0
   87     2*     > RETURN                                                   null

End of function getnodes

End of class Graph.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162 ms | 1408 KiB | 15 Q