|
| 1 | +Class { |
| 2 | + #name : 'AIFloydWarshallTest', |
| 3 | + #superclass : 'TestCase', |
| 4 | + #instVars : [ |
| 5 | + 'floydWarshall' |
| 6 | + ], |
| 7 | + #category : 'AI-Algorithms-Graph-Tests-Shortest path', |
| 8 | + #package : 'AI-Algorithms-Graph-Tests', |
| 9 | + #tag : 'Shortest path' |
| 10 | +} |
| 11 | + |
| 12 | +{ #category : 'running' } |
| 13 | +AIFloydWarshallTest >> setUp [ |
| 14 | + |
| 15 | + super setUp. |
| 16 | + floydWarshall := AIFloydWarshall new |
| 17 | +] |
| 18 | + |
| 19 | +{ #category : 'tests' } |
| 20 | +AIFloydWarshallTest >> testAllPairsDistances [ |
| 21 | + |
| 22 | + | graphType graph result | |
| 23 | + graphType := AICyclicWeightedSimpleFixture new. |
| 24 | + graph := graphType simpleWeightedGraph2. |
| 25 | + |
| 26 | + floydWarshall nodes: graph nodes. |
| 27 | + floydWarshall |
| 28 | + edges: graph edges |
| 29 | + from: #first |
| 30 | + to: #second |
| 31 | + weight: #third. |
| 32 | + |
| 33 | + floydWarshall run. |
| 34 | + result := floydWarshall getAllPairsDistances. |
| 35 | + |
| 36 | + self assert: (result at: { 1. 2 }) equals: 3. |
| 37 | + self assert: (result at: { 1. 5 }) equals: 8. |
| 38 | + self assert: (result at: { 3. 5 }) equals: 5 |
| 39 | +] |
| 40 | + |
| 41 | +{ #category : 'tests' } |
| 42 | +AIFloydWarshallTest >> testAllPairsPaths [ |
| 43 | + |
| 44 | + | graphType graph result | |
| 45 | + graphType := AICyclicWeightedSimpleFixture new. |
| 46 | + graph := graphType simpleWeightedGraph2. |
| 47 | + |
| 48 | + floydWarshall nodes: graph nodes. |
| 49 | + floydWarshall |
| 50 | + edges: graph edges |
| 51 | + from: #first |
| 52 | + to: #second |
| 53 | + weight: #third. |
| 54 | + |
| 55 | + floydWarshall run. |
| 56 | + result := floydWarshall getAllPairsPaths. |
| 57 | + |
| 58 | + self assert: (result at: { 1. 2 }) equals: #( 1 2 ). |
| 59 | + self assert: (result at: { 1. 5 }) equals: #( 1 5 ). |
| 60 | + self assert: (result at: { 3. 5 }) equals: #( 3 4 5 ) |
| 61 | +] |
| 62 | + |
| 63 | +{ #category : 'tests' } |
| 64 | +AIFloydWarshallTest >> testComplexWeightedGraph2 [ |
| 65 | + |
| 66 | + | graphType graph | |
| 67 | + graphType := AICyclicWeightedComplexFixture new. |
| 68 | + graph := graphType complexWeightedGraph2. |
| 69 | + |
| 70 | + floydWarshall nodes: graph nodes. |
| 71 | + floydWarshall |
| 72 | + edges: graph edges |
| 73 | + from: #first |
| 74 | + to: #second |
| 75 | + weight: #third. |
| 76 | + |
| 77 | + floydWarshall run. |
| 78 | + |
| 79 | + self assert: (floydWarshall distanceFrom: 0 to: 5) equals: 10. |
| 80 | + |
| 81 | + self assert: (floydWarshall distanceFrom: 1 to: 5) equals: 6. |
| 82 | + |
| 83 | + self assert: (floydWarshall distanceFrom: 3 to: 5) equals: 3. |
| 84 | + |
| 85 | + self |
| 86 | + assert: (floydWarshall pathFrom: 0 to: 5) |
| 87 | + equals: #( 0 2 1 3 4 5 ). |
| 88 | + self assert: (floydWarshall pathFrom: 1 to: 5) equals: #( 1 3 4 5 ). |
| 89 | + self assert: (floydWarshall pathFrom: 3 to: 5) equals: #( 3 4 5 ). |
| 90 | + |
| 91 | + self assert: floydWarshall hasNegativeCycle equals: false |
| 92 | +] |
| 93 | + |
| 94 | +{ #category : 'tests' } |
| 95 | +AIFloydWarshallTest >> testHasPathFromTo [ |
| 96 | + |
| 97 | + | graphType graph | |
| 98 | + graphType := AICyclicWeightedSimpleFixture new. |
| 99 | + graph := graphType negativeUnconnectedWeightedGraph. |
| 100 | + |
| 101 | + floydWarshall nodes: graph nodes. |
| 102 | + floydWarshall |
| 103 | + edges: graph edges |
| 104 | + from: #first |
| 105 | + to: #second |
| 106 | + weight: #third. |
| 107 | + |
| 108 | + floydWarshall run. |
| 109 | + |
| 110 | + self assert: (floydWarshall hasPathFrom: 0 to: 1). |
| 111 | + self deny: (floydWarshall hasPathFrom: 0 to: 8). |
| 112 | + self deny: (floydWarshall hasPathFrom: 0 to: 9). |
| 113 | +] |
| 114 | + |
| 115 | +{ #category : 'tests' } |
| 116 | +AIFloydWarshallTest >> testNegativeUnconnectedWeightedGraph [ |
| 117 | + |
| 118 | + | graphType graph | |
| 119 | + graphType := AICyclicWeightedSimpleFixture new. |
| 120 | + graph := graphType negativeUnconnectedWeightedGraph. |
| 121 | + |
| 122 | + floydWarshall nodes: graph nodes. |
| 123 | + floydWarshall |
| 124 | + edges: graph edges |
| 125 | + from: #first |
| 126 | + to: #second |
| 127 | + weight: #third. |
| 128 | + |
| 129 | + floydWarshall run. |
| 130 | + |
| 131 | + self assert: (floydWarshall distanceFrom: 0 to: 1) equals: 5. |
| 132 | + self |
| 133 | + assert: (floydWarshall distanceFrom: 0 to: 2) |
| 134 | + equals: Float negativeInfinity. |
| 135 | + self assert: (floydWarshall distanceFrom: 0 to: 7) equals: -10. |
| 136 | + self |
| 137 | + assert: (floydWarshall distanceFrom: 0 to: 8) |
| 138 | + equals: Float infinity. |
| 139 | + |
| 140 | + self assert: (floydWarshall pathFrom: 0 to: 1) equals: #( 0 1 ). |
| 141 | + self assert: (floydWarshall pathFrom: 0 to: 7) equals: #( 0 1 5 6 7 ). |
| 142 | + self assert: (floydWarshall pathFrom: 0 to: 8) equals: #( ). |
| 143 | + |
| 144 | + self assert: floydWarshall hasNegativeCycle equals: true. |
| 145 | + |
| 146 | + self |
| 147 | + assert: (floydWarshall distanceFrom: 0 to: 9) |
| 148 | + equals: Float negativeInfinity. |
| 149 | + self |
| 150 | + assert: (floydWarshall pathFrom: 0 to: 9) |
| 151 | + equals: #( 'Path affected by negative cycle' ). |
| 152 | + |
| 153 | + self |
| 154 | + assert: (floydWarshall distanceFrom: 2 to: 3) |
| 155 | + equals: Float negativeInfinity. |
| 156 | + self |
| 157 | + assert: (floydWarshall pathFrom: 2 to: 3) |
| 158 | + equals: #( 'Path affected by negative cycle' ) |
| 159 | +] |
| 160 | + |
| 161 | +{ #category : 'tests' } |
| 162 | +AIFloydWarshallTest >> testNegativeWeightedGraph [ |
| 163 | + |
| 164 | + | graphType graph | |
| 165 | + graphType := AICyclicWeightedSimpleFixture new. |
| 166 | + graph := graphType negativeWeightedGraph. |
| 167 | + |
| 168 | + floydWarshall nodes: graph nodes. |
| 169 | + floydWarshall |
| 170 | + edges: graph edges |
| 171 | + from: #first |
| 172 | + to: #second |
| 173 | + weight: #third. |
| 174 | + |
| 175 | + floydWarshall run. |
| 176 | + |
| 177 | + self assert: (floydWarshall distanceFrom: 0 to: 8) equals: -20. |
| 178 | + self |
| 179 | + assert: (floydWarshall distanceFrom: 0 to: 9) |
| 180 | + equals: Float negativeInfinity. |
| 181 | + |
| 182 | + self |
| 183 | + assert: (floydWarshall pathFrom: 0 to: 8) |
| 184 | + equals: #( 0 1 5 6 7 8 ). |
| 185 | + self |
| 186 | + assert: (floydWarshall pathFrom: 0 to: 9) |
| 187 | + equals: #( 'Path affected by negative cycle' ). |
| 188 | + |
| 189 | + self assert: floydWarshall hasNegativeCycle equals: true |
| 190 | +] |
| 191 | + |
| 192 | +{ #category : 'tests' } |
| 193 | +AIFloydWarshallTest >> testSimpleWeightedGraph [ |
| 194 | + |
| 195 | + | graphType graph | |
| 196 | + graphType := AICyclicWeightedSimpleFixture new. |
| 197 | + graph := graphType simpleWeightedGraph. |
| 198 | + |
| 199 | + floydWarshall nodes: graph nodes. |
| 200 | + floydWarshall |
| 201 | + edges: graph edges |
| 202 | + from: #first |
| 203 | + to: #second |
| 204 | + weight: #third. |
| 205 | + |
| 206 | + floydWarshall run. |
| 207 | + |
| 208 | + self assert: (floydWarshall distanceFrom: 1 to: 5) equals: 3. |
| 209 | + self assert: (floydWarshall distanceFrom: 2 to: 5) equals: 4. |
| 210 | + self assert: (floydWarshall distanceFrom: 3 to: 5) equals: 6. |
| 211 | + |
| 212 | + self assert: (floydWarshall pathFrom: 1 to: 5) equals: #( 1 5 ). |
| 213 | + self assert: (floydWarshall pathFrom: 2 to: 5) equals: #( 2 4 5 ). |
| 214 | + self assert: (floydWarshall pathFrom: 3 to: 5) equals: #( 3 4 5 ). |
| 215 | + |
| 216 | + self assert: floydWarshall hasNegativeCycle equals: false |
| 217 | +] |
| 218 | + |
| 219 | +{ #category : 'tests' } |
| 220 | +AIFloydWarshallTest >> testWeightedDAG [ |
| 221 | + |
| 222 | + | graphType graph | |
| 223 | + graphType := AIWeightedDAGFixture new. |
| 224 | + graph := graphType weightedDAG. |
| 225 | + |
| 226 | + floydWarshall nodes: graph nodes. |
| 227 | + floydWarshall |
| 228 | + edges: graph edges |
| 229 | + from: #first |
| 230 | + to: #second |
| 231 | + weight: #third. |
| 232 | + |
| 233 | + floydWarshall run. |
| 234 | + |
| 235 | + self assert: (floydWarshall distanceFrom: $A to: $F) equals: 19. |
| 236 | + self assert: (floydWarshall distanceFrom: $B to: $F) equals: 18. |
| 237 | + self assert: (floydWarshall distanceFrom: $G to: $F) equals: 17. |
| 238 | + |
| 239 | + self |
| 240 | + assert: (floydWarshall pathFrom: $A to: $F) |
| 241 | + equals: #( $A $B $E $F ). |
| 242 | + self |
| 243 | + assert: (floydWarshall pathFrom: $G to: $F) |
| 244 | + equals: #( $G $D $E $F ). |
| 245 | + |
| 246 | + self assert: floydWarshall hasNegativeCycle equals: false |
| 247 | +] |
0 commit comments