Completed
Push — master ( a2e1dc...e269fd )
by Greg
02:20
created

DijkstraTest::testMultipleShortestPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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