@keevitaja
I was just trying to establish how frequent? Every second, min, or hour, day, wk, month, or year?
If you can cache the results then it'll be fine.
I would go about turning the records into something like this, each time you work out from the current node to the target you cache which node took you there the quickest and how many steps it took. Then it just needs to check does this particular node have the path to 7 already, yes, well then how many steps is it, if not find out and cache for future checks.
$graph = [
2 => [
'nodes' => [5,12,8],
'paths' => [
7 => ['node' => 12, 'steps' => 2]
]
],
5 => [
'nodes' => [2,4],
'paths' => [
7 => ['node' => 4, 'steps' => 2]
]
],
4 => [
'nodes' => [3],
'paths' => [
7 => ['node' => 3, 'steps' => 1]
]
],
3 => [
'nodes' => [7],
'paths' => [
7 => ['node' => 7, 'steps' => 0]
]
],
12 => [
'nodes' => [3,6],
'paths' => [
7 => ['node' => 3, 'steps' => 1]
]
],
6 => [
'nodes' => [5],
'paths' => [
7 => ['node' => 5, 'steps' => 3]
]
],
8 => [
'nodes' => [9,1,5],
'paths' => [
7 => ['node' => 5, 'steps' => 3]
]
],
9 => [
'nodes' => [4],
'paths' => [
7 => ['node' => 4, 'steps' => 2]
]
],
1 => [
'nodes' => [2],
'paths' => [
7 => ['node' => 2, 'steps' => 3]
]
],
7 => [
'nodes' => [23,47],
'paths' => []
],
];
eg so going from 5 to 7 take route 4 (2 steps away)
5 => [
'nodes' => [2,4],
'paths' => [
7 => ['node' => 4, 'steps' => 2]
]
],
4 => [
'nodes' => [3],
'paths' => [
7 => ['node' => 3, 'steps' => 1]
]
3 => [
'nodes' => [7],
'paths' => [
7 => ['node' => 7, 'steps' => 0]
]
],
then you find say 2 to 6, looks like 12 is quickest.
2 => [
'nodes' => [5,12,8],
'paths' => [
6 => ['node' => 12, 'steps' => 1],
7 => ['node' => 12, 'steps' => 2]
]
],
12 => [
'nodes' => [3,6],
'paths' => [
6 => ['node' => 6, 'steps' => 0],
7 => ['node' => 3, 'steps' => 1]
]
],