1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fisharebest\Tests\Algorithm; |
4
|
|
|
|
5
|
|
|
use Fisharebest\Algorithm\Dijkstra; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Greg Roach <[email protected]> |
9
|
|
|
* @copyright (c) 2021 Greg Roach <[email protected]> |
10
|
|
|
* @license GPL-3.0+ |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* You should have received a copy of the GNU General Public License |
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses>. |
22
|
|
|
*/ |
23
|
|
|
class DijkstraTest extends BaseTestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* An undirected graph, with non-negative edge values. |
27
|
|
|
* No two shortest paths exist with the same length. |
28
|
|
|
* |
29
|
|
|
* D---9---E |
30
|
|
|
* / \ \ |
31
|
|
|
* 14 2 6 |
32
|
|
|
* / \ \ |
33
|
|
|
* A---9---B--11--C |
34
|
|
|
* \ / / |
35
|
|
|
* 7 10 / |
36
|
|
|
* \ / / |
37
|
|
|
* F-----15 |
38
|
|
|
* |
39
|
|
|
* |
40
|
|
|
* @var integer[][] Test graph |
41
|
|
|
* |
42
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
43
|
|
|
*/ |
44
|
|
|
private $graph1 = array( |
45
|
|
|
'A' => array('B' => 9, 'D' => 14, 'F' => 7), |
46
|
|
|
'B' => array('A' => 9, 'C' => 11, 'D' => 2, 'F' => 10), |
47
|
|
|
'C' => array('B' => 11, 'E' => 6, 'F' => 15), |
48
|
|
|
'D' => array('A' => 14, 'B' => 2, 'E' => 9), |
49
|
|
|
'E' => array('C' => 6, 'D' => 9), |
50
|
|
|
'F' => array('A' => 7, 'B' => 10, 'C' => 15), |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test that there are no paths to/from 'G'. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* |
58
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
59
|
|
|
*/ |
60
|
|
|
public function testNoPath() |
61
|
|
|
{ |
62
|
|
|
$dijkstra = new Dijkstra($this->graph1); |
63
|
|
|
|
64
|
|
|
$this->assertSame(array(), $dijkstra->shortestPaths('A', 'G')); |
65
|
|
|
$this->assertSame(array(), $dijkstra->shortestPaths('G', 'A')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test that there is a null paths to/from the same node. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
* |
73
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
74
|
|
|
*/ |
75
|
|
|
public function testNullPath() |
76
|
|
|
{ |
77
|
|
|
$dijkstra = new Dijkstra($this->graph1); |
78
|
|
|
|
79
|
|
|
$this->assertSame(array(array('A')), $dijkstra->shortestPaths('A', 'A')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test there is a unique shortest path from 'A' to every other node. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* |
87
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
88
|
|
|
*/ |
89
|
|
|
public function testUniqueShortestPath() |
90
|
|
|
{ |
91
|
|
|
$dijkstra = new Dijkstra($this->graph1); |
92
|
|
|
|
93
|
|
|
$this->assertSame(array(array('A', 'B')), $dijkstra->shortestPaths('A', 'B')); |
94
|
|
|
$this->assertSame(array(array('B', 'A')), $dijkstra->shortestPaths('B', 'A')); |
95
|
|
|
|
96
|
|
|
$this->assertSame(array(array('A', 'B', 'C')), $dijkstra->shortestPaths('A', 'C')); |
97
|
|
|
$this->assertSame(array(array('C', 'B', 'A')), $dijkstra->shortestPaths('C', 'A')); |
98
|
|
|
|
99
|
|
|
$this->assertSame(array(array('A', 'B', 'D')), $dijkstra->shortestPaths('A', 'D')); |
100
|
|
|
$this->assertSame(array(array('D', 'B', 'A')), $dijkstra->shortestPaths('D', 'A')); |
101
|
|
|
|
102
|
|
|
$this->assertSame(array(array('A', 'B', 'D', 'E')), $dijkstra->shortestPaths('A', 'E')); |
103
|
|
|
$this->assertSame(array(array('E', 'D', 'B', 'A')), $dijkstra->shortestPaths('E', 'A')); |
104
|
|
|
|
105
|
|
|
$this->assertSame(array(array('A', 'F')), $dijkstra->shortestPaths('A', 'F')); |
106
|
|
|
$this->assertSame(array(array('F', 'A')), $dijkstra->shortestPaths('F', 'A')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test the multiple shortest paths between 'E' and 'F'. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* |
114
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
115
|
|
|
*/ |
116
|
|
|
public function testMultipleShortestPaths() |
117
|
|
|
{ |
118
|
|
|
$dijkstra = new Dijkstra($this->graph1); |
119
|
|
|
|
120
|
|
|
$this->assertSame(array(array('E', 'C', 'F'), array('E', 'D', 'B', 'F')), $dijkstra->shortestPaths('E', 'F')); |
121
|
|
|
$this->assertSame(array(array('F', 'C', 'E'), array('F', 'B', 'D', 'E')), $dijkstra->shortestPaths('F', 'E')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test the exclusion list, for next-shortest paths. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
* |
129
|
|
|
* @covers \Fisharebest\Algorithm\Dijkstra |
130
|
|
|
*/ |
131
|
|
|
public function testExclusionList() |
132
|
|
|
{ |
133
|
|
|
$dijkstra = new Dijkstra($this->graph1); |
134
|
|
|
|
135
|
|
|
$this->assertSame(array(array('E', 'D', 'B', 'F')), $dijkstra->shortestPaths('E', 'F', array('C'))); |
136
|
|
|
$this->assertSame(array(array('F', 'B', 'D', 'E')), $dijkstra->shortestPaths('F', 'E', array('C'))); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|