forked from DonVictor/PHP-Dijkstra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
66 lines (47 loc) · 1.26 KB
/
test.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
58
59
60
61
62
63
64
65
<?php
/*
* @author: [email protected]
*/
require(__DIR__ . '/src/Dijkstra/Graph.php');
require(__DIR__ . '/src/Dijkstra/Edge.php');
require(__DIR__ . '/src/Dijkstra/QueueItem.php');
require(__DIR__ . '/src/Dijkstra/PriorityQueue.php');
// better use some autoloader
function runTest() {
$g = new \Dijkstra\Graph();
$g->addEdge("a", "b", 4);
$g->addEdge("a", "d", 1);
$g->addEdge("b", "a", 74);
$g->addEdge("b", "a", 4);
// $g->addEdge("b", "a", -4); // throws exception
$g->addEdge("b", "c", 2);
$g->addEdge("b", "e", 12);
$g->addEdge("c", "b", 12);
$g->addEdge("c", "j", 12);
$g->addEdge("c", "f", 74);
$g->addEdge("d", "g", 22);
$g->addEdge("d", "e", 32);
$g->addEdge("e", "h", 33);
$g->addEdge("e", "d", 66);
$g->addEdge("e", "f", 76);
$g->addEdge("f", "j", 21);
$g->addEdge("f", "i", 11);
$g->addEdge("g", "c", 12);
$g->addEdge("g", "h", 10);
$g->addEdge("h", "g", 2);
$g->addEdge("h", "i", 72);
$g->addEdge("i", "j", 7);
$g->addEdge("i", "f", 31);
$g->addEdge("i", "h", 18);
$g->addEdge("j", "f", 8);
try {
list($distances, $prev) = $g->getPathsFrom('a');
$path = $g->getPathsTo($prev, 'i');
print_r($distances);
print_r($path);
echo $g;
} catch (\Exception $e) {
printf('%s%s', $e->getMessage(), PHP_EOL);
}
}
runTest();