-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraAlgorithm.php
57 lines (49 loc) · 1.4 KB
/
DijkstraAlgorithm.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class DijkstraAlgorithm{
public $arcs;
public $vers = [];
public $visited = [];
public $dis = [];
public function __construct($vers,$arcs){
$this->vers = $vers;
$this->arcs = $arcs;
for ($i = 0; $i < count($vers); $i++) {
$this->visited[$i] = 0;
$this->dis[$i] = 999999;
}
}
public function dsj($i){
$this->dis[$i] = 0;
$this->update($i);
for ($i = 1; $i < count($this->vers); $i++) {
$index = $this->getNext();
$this->update($index);
}
}
public function update($i){
$this->visited[$i] = 1;
for ($j = 0; $j < count($this->arcs[$i]); $j++) {
if(($this->visited[$j] == 0) && ($this->dis[$j] > $this->arcs[$i][$j] + $this->dis[$i])){
$this->dis[$j] = $this->dis[$i] + $this->arcs[$i][$j];
}
}
}
public function getNext(){
$index = 0;
$min = 999999;
for ($i = 0; $i < count($this->vers); $i++) {
if(($this->visited[$i] == 0) && ($this->dis[$i] < $min)){
$min = $this->dis[$i];
$index = $i;
}
}
$this->visited[$i] = 1;
return $index;
}
public function show(){
var_dump($this->dis);
}
}
$prim = new DijkstraAlgorithm(['A','B','C','D','E','F','G'],[[999999,5,7,999999,999999,999999,2],[5,999999,999999,9,999999,999999,3],[7,999999,999999,999999,8,999999,999999],[999999,9,999999,999999,999999,4,999999],[999999,999999,8,999999,999999,5,4],[999999,999999,999999,4,5,999999,6],[2,3,999999,999999,4,6,999999]]);
$prim->dsj(0);
$prim->show();