Passed
Push — main ( ec83d6...e6f198 )
by Greg
01:56
created

DijkstraTest::testNullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
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
    private $graph1 = array(
43
        'A' => array('B' => 9, 'D' => 14, 'F' => 7),
44
        'B' => array('A' => 9, 'C' => 11, 'D' => 2, 'F' => 10),
45
        'C' => array('B' => 11, 'E' => 6, 'F' => 15),
46
        'D' => array('A' => 14, 'B' => 2, 'E' => 9),
47
        'E' => array('C' => 6, 'D' => 9),
48
        'F' => array('A' => 7, 'B' => 10, 'C' => 15),
49
    );
50
51
    /**
52
     * Test that there are no paths to/from 'G'.
53
     *
54
     * @return void
55
     */
56
    public function testNoPath()
57
    {
58
        $dijkstra = new Dijkstra($this->graph1);
59
60
        $this->assertSame(array(), $dijkstra->shortestPaths('A', 'G'));
61
        $this->assertSame(array(), $dijkstra->shortestPaths('G', 'A'));
62
    }
63
64
    /**
65
     * Test that there is a null paths to/from the same node.
66
     *
67
     * @return void
68
     */
69
    public function testNullPath()
70
    {
71
        $dijkstra = new Dijkstra($this->graph1);
72
73
        $this->assertSame(array(array('A')), $dijkstra->shortestPaths('A', 'A'));
74
    }
75
76
    /**
77
     * Test there is a unique shortest path from 'A' to every other node.
78
     *
79
     * @return void
80
     */
81
    public function testUniqueShortestPath()
82
    {
83
        $dijkstra = new Dijkstra($this->graph1);
84
85
        $this->assertSame(array(array('A', 'B')), $dijkstra->shortestPaths('A', 'B'));
86
        $this->assertSame(array(array('B', 'A')), $dijkstra->shortestPaths('B', 'A'));
87
88
        $this->assertSame(array(array('A', 'B', 'C')), $dijkstra->shortestPaths('A', 'C'));
89
        $this->assertSame(array(array('C', 'B', 'A')), $dijkstra->shortestPaths('C', 'A'));
90
91
        $this->assertSame(array(array('A', 'B', 'D')), $dijkstra->shortestPaths('A', 'D'));
92
        $this->assertSame(array(array('D', 'B', 'A')), $dijkstra->shortestPaths('D', 'A'));
93
94
        $this->assertSame(array(array('A', 'B', 'D', 'E')), $dijkstra->shortestPaths('A', 'E'));
95
        $this->assertSame(array(array('E', 'D', 'B', 'A')), $dijkstra->shortestPaths('E', 'A'));
96
97
        $this->assertSame(array(array('A', 'F')), $dijkstra->shortestPaths('A', 'F'));
98
        $this->assertSame(array(array('F', 'A')), $dijkstra->shortestPaths('F', 'A'));
99
    }
100
101
    /**
102
     * Test the multiple shortest paths between 'E' and 'F'.
103
     *
104
     * @return void
105
     */
106
    public function testMultipleShortestPaths()
107
    {
108
        $dijkstra = new Dijkstra($this->graph1);
109
110
        $this->assertSame(array(array('E', 'C', 'F'), array('E', 'D', 'B', 'F')), $dijkstra->shortestPaths('E', 'F'));
111
        $this->assertSame(array(array('F', 'C', 'E'), array('F', 'B', 'D', 'E')), $dijkstra->shortestPaths('F', 'E'));
112
    }
113
114
    /**
115
     * Test the exclusion list, for next-shortest paths.
116
     *
117
     * @return void
118
     */
119
    public function testExclusionList()
120
    {
121
        $dijkstra = new Dijkstra($this->graph1);
122
123
        $this->assertSame(array(array('E', 'D', 'B', 'F')), $dijkstra->shortestPaths('E', 'F', array('C')));
124
        $this->assertSame(array(array('F', 'B', 'D', 'E')), $dijkstra->shortestPaths('F', 'E', array('C')));
125
    }
126
}
127