Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- private $INT_MAX = 0x7FFFFFFF;
- private $known = [];
- private $distance = [];
- private $path = [];
- private $paths = [];
- function ShortestViaNode() {
- $min = $this->INT_MAX;
- $minIndex = 0;
- for ($v = 0; $v < $this->siz; $v++) { // if v unknown and the distance to there (from start) <= min, this is the new min way node.
- if ($this->known[$v] == false && $this->distance[$v] <= $min) {
- $min = $this->distance[$v];
- $minIndex = $v;
- }
- }
- return $minIndex;
- }
- function Dijkstra($graph, $start) {
- $this->siz = sizeof($graph);
- for ($i = 0; $i < $this->siz; $i++) {
- $this->distance[$i] = $this->INT_MAX;
- $this->known[$i] = false;
- $this->path[$i] = -1;
- }
- $this->paths[$i-1][] = -1;
- $this->distance[$start] = 0; // startpoint (0) is done. Let's iterate 1..all
- for ($i = 0; $i < $this->siz; $i++) { // this $i is else then the before one! It just counts...
- $u = $this->ShortestViaNode(); // u is the node through the shortest node, when $start->$i
- $this->known[$u] = true; // we set u as known
- for ($v = 0; $v < $this->siz; $v++)
- // let's iterate all the nodes. Maybe there is a shortest way than we know until now...
- // if v is unknown & there is edge u->v & distance]u] is finite & distance[u]+ u->v < distance[v] then it is the new distance.
- // changed last ... < ...dist... to <=, allowing equation also for ALL paths
- if (!$this->known[$v] && $graph[$u][$v] && $this->distance[$u] != $this->INT_MAX && $this->distance[$u] + $graph[$u][$v] <= $this->distance[$v]) {
- $this->distance[$v] = $this->distance[$u] + $graph[$u][$v];
- //ยค print $v.":".$u.";".$this->distance[$v].n;
- $this->path[$v] = $u;
- $this->paths[$v][] = $u;
- }
- }
- }
Add Comment
Please, Sign In to add comment