3v4l.org

run code in 500+ PHP versions simultaneously
<?php function findWayPoints(array $lookup, $start, $end, array $path = []): ?array { if (isset($lookup[$start][$end])) { return $path; // full path found; no need to further recurse } foreach ($lookup[$start] ?? [] as $conn => $irrelevant) { $result = findWayPoints( array_diff_key($lookup, [$start => null]), // eliminate circular/infinite recursion $conn, // change starting node in next call $end, array_merge($path, [$conn]) // append current match to path ); if ($result !== null) { return $result; } } return null; // dead end; kill path } function buildLookup($nodes) { $lookup = []; foreach ($nodes as $k => [$connections]) { foreach (explode(' ', $connections) as $conn) { $lookup[$k][$conn] = true; $lookup[$conn][$k] = true; } } return $lookup; } $nodes = [ 'f' => ['d g'], 'b' => ['a d'], 'g' => ['i'], 'd' => ['c e'], 'i' => ['h'] ]; $lookup = buildLookup($nodes); echo json_encode(findWayPoints($lookup, 'h', 'f')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'f', 'h')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'c')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'b')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'h')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'i', 'j'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9WaiB
function name:  (null)
number of ops:  65
compiled vars:  !0 = $nodes, !1 = $lookup
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                       !0, <array>
   40     1        INIT_FCALL                                                   'buildlookup'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !1, $3
   43     5        INIT_FCALL                                                   'json_encode'
          6        INIT_FCALL                                                   'findwaypoints'
          7        SEND_VAR                                                     !1
          8        SEND_VAL                                                     'h'
          9        SEND_VAL                                                     'f'
         10        DO_FCALL                                          0  $5      
         11        SEND_VAR                                                     $5
         12        DO_ICALL                                             $6      
         13        ECHO                                                         $6
   44    14        ECHO                                                         '%0A---%0A'
   45    15        INIT_FCALL                                                   'json_encode'
         16        INIT_FCALL                                                   'findwaypoints'
         17        SEND_VAR                                                     !1
         18        SEND_VAL                                                     'f'
         19        SEND_VAL                                                     'h'
         20        DO_FCALL                                          0  $7      
         21        SEND_VAR                                                     $7
         22        DO_ICALL                                             $8      
         23        ECHO                                                         $8
   46    24        ECHO                                                         '%0A---%0A'
   47    25        INIT_FCALL                                                   'json_encode'
         26        INIT_FCALL                                                   'findwaypoints'
         27        SEND_VAR                                                     !1
         28        SEND_VAL                                                     'a'
         29        SEND_VAL                                                     'c'
         30        DO_FCALL                                          0  $9      
         31        SEND_VAR                                                     $9
         32        DO_ICALL                                             $10     
         33        ECHO                                                         $10
   48    34        ECHO                                                         '%0A---%0A'
   49    35        INIT_FCALL                                                   'json_encode'
         36        INIT_FCALL                                                   'findwaypoints'
         37        SEND_VAR                                                     !1
         38        SEND_VAL                                                     'a'
         39        SEND_VAL                                                     'b'
         40        DO_FCALL                                          0  $11     
         41        SEND_VAR                                                     $11
         42        DO_ICALL                                             $12     
         43        ECHO                                                         $12
   50    44        ECHO                                                         '%0A---%0A'
   51    45        INIT_FCALL                                                   'json_encode'
         46        INIT_FCALL                                                   'findwaypoints'
         47        SEND_VAR                                                     !1
         48        SEND_VAL                                                     'a'
         49        SEND_VAL                                                     'h'
         50        DO_FCALL                                          0  $13     
         51        SEND_VAR                                                     $13
         52        DO_ICALL                                             $14     
         53        ECHO                                                         $14
   52    54        ECHO                                                         '%0A---%0A'
   53    55        INIT_FCALL                                                   'json_encode'
         56        INIT_FCALL                                                   'findwaypoints'
         57        SEND_VAR                                                     !1
         58        SEND_VAL                                                     'i'
         59        SEND_VAL                                                     'j'
         60        DO_FCALL                                          0  $15     
         61        SEND_VAR                                                     $15
         62        DO_ICALL                                             $16     
         63        ECHO                                                         $16
         64      > RETURN                                                       1

Function findwaypoints:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 38
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 38
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 37
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/9WaiB
function name:  findWayPoints
number of ops:  42
compiled vars:  !0 = $lookup, !1 = $start, !2 = $end, !3 = $path, !4 = $irrelevant, !5 = $conn, !6 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
          3        RECV_INIT                                            !3      <array>
    5     4        FETCH_DIM_IS                                         ~7      !0, !1
          5        ISSET_ISEMPTY_DIM_OBJ                             0          ~7, !2
          6      > JMPZ                                                         ~8, ->9
    6     7    >   VERIFY_RETURN_TYPE                                           !3
          8      > RETURN                                                       !3
    8     9    >   FETCH_DIM_IS                                         ~9      !0, !1
         10        COALESCE                                             ~10     ~9
         11        QM_ASSIGN                                            ~10     <array>
         12      > FE_RESET_R                                           $11     ~10, ->38
         13    > > FE_FETCH_R                                           ~12     $11, !4, ->38
         14    >   ASSIGN                                                       !5, ~12
    9    15        INIT_FCALL_BY_NAME                                           'findWayPoints'
   10    16        INIT_FCALL                                                   'array_diff_key'
         17        SEND_VAR                                                     !0
         18        INIT_ARRAY                                           ~14     null, !1
         19        SEND_VAL                                                     ~14
         20        DO_ICALL                                             $15     
         21        SEND_VAR_NO_REF_EX                                           $15
         22        SEND_VAR_EX                                                  !5
         23        SEND_VAR_EX                                                  !2
   13    24        INIT_FCALL                                                   'array_merge'
         25        SEND_VAR                                                     !3
         26        INIT_ARRAY                                           ~16     !5
         27        SEND_VAL                                                     ~16
         28        DO_ICALL                                             $17     
         29        SEND_VAR_NO_REF_EX                                           $17
    9    30        DO_FCALL                                          0  $18     
         31        ASSIGN                                                       !6, $18
   15    32        TYPE_CHECK                                      1020          !6
         33      > JMPZ                                                         ~20, ->37
   16    34    >   VERIFY_RETURN_TYPE                                           !6
         35        FE_FREE                                                      $11
         36      > RETURN                                                       !6
    8    37    > > JMP                                                          ->13
         38    >   FE_FREE                                                      $11
   19    39      > RETURN                                                       null
   20    40*       VERIFY_RETURN_TYPE                                           
         41*     > RETURN                                                       null

End of function findwaypoints

Function buildlookup:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 21
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/9WaiB
function name:  buildLookup
number of ops:  26
compiled vars:  !0 = $nodes, !1 = $lookup, !2 = $connections, !3 = $k, !4 = $conn
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
   23     1        ASSIGN                                                       !1, <array>
   24     2      > FE_RESET_R                                           $6      !0, ->23
          3    > > FE_FETCH_R                                           ~10     $6, $7, ->23
          4    >   FETCH_LIST_R                                         $8      $7, 0
          5        ASSIGN                                                       !2, $8
          6        FREE                                                         $7
          7        ASSIGN                                                       !3, ~10
   25     8        INIT_FCALL                                                   'explode'
          9        SEND_VAL                                                     '+'
         10        SEND_VAR                                                     !2
         11        DO_ICALL                                             $12     
         12      > FE_RESET_R                                           $13     $12, ->21
         13    > > FE_FETCH_R                                                   $13, !4, ->21
   26    14    >   FETCH_DIM_W                                          $14     !1, !3
         15        ASSIGN_DIM                                                   $14, !4
         16        OP_DATA                                                      <true>
   27    17        FETCH_DIM_W                                          $16     !1, !4
         18        ASSIGN_DIM                                                   $16, !3
         19        OP_DATA                                                      <true>
   25    20      > JMP                                                          ->13
         21    >   FE_FREE                                                      $13
   24    22      > JMP                                                          ->3
         23    >   FE_FREE                                                      $6
   30    24      > RETURN                                                       !1
   31    25*     > RETURN                                                       null

End of function buildlookup

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
164.87 ms | 1612 KiB | 24 Q