|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fisharebest\Tests\Algorithm; |
|
4
|
|
|
|
|
5
|
|
|
use Fisharebest\Algorithm\ConnectedComponent; |
|
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 ConnectedComponentTest extends BaseTestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* A graph with no components. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testNoComponents() |
|
29
|
|
|
{ |
|
30
|
|
|
$graph = array(); |
|
31
|
|
|
|
|
32
|
|
|
$components = array(); |
|
33
|
|
|
|
|
34
|
|
|
$algorithm = new ConnectedComponent($graph); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame($components, $algorithm->findConnectedComponents()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* A graph with one component. |
|
41
|
|
|
* |
|
42
|
|
|
* D----E |
|
43
|
|
|
* / \ \ |
|
44
|
|
|
* / \ \ |
|
45
|
|
|
* A-----B---C |
|
46
|
|
|
* \ / / |
|
47
|
|
|
* \ / / |
|
48
|
|
|
* F----/ |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testOneComponent() |
|
51
|
|
|
{ |
|
52
|
|
|
$graph = array( |
|
53
|
|
|
'A' => array('B' => 1, 'D' => 1, 'F' => 1), |
|
54
|
|
|
'B' => array('A' => 1, 'C' => 1, 'D' => 1, 'F' => 1), |
|
55
|
|
|
'C' => array('B' => 1, 'E' => 1, 'F' => 1), |
|
56
|
|
|
'D' => array('A' => 1, 'B' => 1, 'E' => 1), |
|
57
|
|
|
'E' => array('C' => 1, 'D' => 1), |
|
58
|
|
|
'F' => array('A' => 1, 'B' => 1, 'C' => 1), |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$components = array( |
|
62
|
|
|
1 => array('A', 'B', 'C', 'D', 'E', 'F'), |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$algorithm = new ConnectedComponent($graph); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame($components, $algorithm->findConnectedComponents()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* A graph with two component. |
|
72
|
|
|
* |
|
73
|
|
|
* D E |
|
74
|
|
|
* / \ \ |
|
75
|
|
|
* / \ \ |
|
76
|
|
|
* A-----B C |
|
77
|
|
|
* \ / |
|
78
|
|
|
* \ / |
|
79
|
|
|
* F |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testTwoComponent() |
|
82
|
|
|
{ |
|
83
|
|
|
$graph = array( |
|
84
|
|
|
'A' => array('B' => 1, 'D' => 1, 'F' => 1), |
|
85
|
|
|
'B' => array('A' => 1, 'D' => 1, 'F' => 1), |
|
86
|
|
|
'C' => array('E' => 1), |
|
87
|
|
|
'D' => array('A' => 1, 'B' => 1), |
|
88
|
|
|
'E' => array('C' => 1), |
|
89
|
|
|
'F' => array('A' => 1, 'B' => 1), |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$components = array( |
|
93
|
|
|
1 => array('A', 'B', 'D', 'F'), |
|
94
|
|
|
2 => array('C', 'E'), |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$algorithm = new ConnectedComponent($graph); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertSame($components, $algorithm->findConnectedComponents()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* A graph with two component. |
|
104
|
|
|
* |
|
105
|
|
|
* A B |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testUnconnected() |
|
108
|
|
|
{ |
|
109
|
|
|
$graph = array( |
|
110
|
|
|
'A' => array(), |
|
111
|
|
|
'B' => array(), |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$components = array( |
|
115
|
|
|
1 => array('A'), |
|
116
|
|
|
2 => array('B'), |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$algorithm = new ConnectedComponent($graph); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertSame($components, $algorithm->findConnectedComponents()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|