github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/testgraph/testcases.go (about) 1 // Copyright ©2018 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package testgraph 6 7 import ( 8 "math" 9 10 "github.com/jingcheng-WU/gonum/graph" 11 ) 12 13 // node is a graph.Node implementation that is not exported 14 // so that other packages will not be aware of its implementation. 15 type node int64 16 17 func (n node) ID() int64 { return int64(n) } 18 19 // line is an extended graph.Edge implementation that is not exported 20 // so that other packages will not be aware of its implementation. It 21 // covers all the edge types exported by graph. 22 type line struct { 23 F, T graph.Node 24 UID int64 25 W float64 26 } 27 28 func (e line) From() graph.Node { return e.F } 29 func (e line) To() graph.Node { return e.T } 30 func (e line) ReversedEdge() graph.Edge { e.F, e.T = e.T, e.F; return e } 31 func (e line) ID() int64 { return e.UID } 32 func (e line) Weight() float64 { return e.W } 33 34 var testCases = []struct { 35 // name is the name of the test. 36 name string 37 38 // nodes is the set of nodes that should be used 39 // to construct the graph. 40 nodes []graph.Node 41 42 // edges is the set of edges that should be used 43 // to construct the graph. 44 edges []WeightedLine 45 46 // nonexist is a set of nodes that should not be 47 // found within the graph. 48 nonexist []graph.Node 49 50 // self is the weight value associated with 51 // a self edge for simple graphs that do not 52 // store individual self edges. 53 self float64 54 55 // absent is the weight value associated 56 // with absent edges. 57 absent float64 58 }{ 59 { 60 name: "empty", 61 nonexist: []graph.Node{node(-1), node(0), node(1)}, 62 self: 0, 63 absent: math.Inf(1), 64 }, 65 { 66 name: "one - negative", 67 nodes: []graph.Node{node(-1)}, 68 nonexist: []graph.Node{node(0), node(1)}, 69 self: 0, 70 absent: math.Inf(1), 71 }, 72 { 73 name: "one - zero", 74 nodes: []graph.Node{node(0)}, 75 nonexist: []graph.Node{node(-1), node(1)}, 76 self: 0, 77 absent: math.Inf(1), 78 }, 79 { 80 name: "one - positive", 81 nodes: []graph.Node{node(1)}, 82 nonexist: []graph.Node{node(-1), node(0)}, 83 self: 0, 84 absent: math.Inf(1), 85 }, 86 87 { 88 name: "one - self loop", 89 nodes: []graph.Node{node(0)}, 90 edges: []WeightedLine{line{F: node(0), T: node(0), UID: 0, W: 0.5}}, 91 nonexist: []graph.Node{node(-1), node(1)}, 92 self: 0, 93 absent: math.Inf(1), 94 }, 95 96 { 97 name: "two - positive", 98 nodes: []graph.Node{node(1), node(2)}, 99 edges: []WeightedLine{line{F: node(1), T: node(2), UID: 0, W: 0.5}}, 100 nonexist: []graph.Node{node(-1), node(0)}, 101 self: 0, 102 absent: math.Inf(1), 103 }, 104 { 105 name: "two - negative", 106 nodes: []graph.Node{node(-1), node(-2)}, 107 edges: []WeightedLine{line{F: node(-1), T: node(-2), UID: 0, W: 0.5}}, 108 nonexist: []graph.Node{node(0), node(-3)}, 109 self: 0, 110 absent: math.Inf(1), 111 }, 112 { 113 name: "two - zero spanning", 114 nodes: []graph.Node{node(-1), node(1)}, 115 edges: []WeightedLine{line{F: node(-1), T: node(1), UID: 0, W: 0.5}}, 116 nonexist: []graph.Node{node(0), node(2)}, 117 self: 0, 118 absent: math.Inf(1), 119 }, 120 { 121 name: "two - zero contiguous", 122 nodes: []graph.Node{node(0), node(1)}, 123 edges: []WeightedLine{line{F: node(0), T: node(1), UID: 0, W: 0.5}}, 124 nonexist: []graph.Node{node(-1), node(2)}, 125 self: 0, 126 absent: math.Inf(1), 127 }, 128 129 { 130 name: "three - positive", 131 nodes: []graph.Node{node(1), node(2), node(3)}, 132 edges: []WeightedLine{line{F: node(1), T: node(2), UID: 0, W: 0.5}}, 133 nonexist: []graph.Node{node(-1), node(0)}, 134 self: 0, 135 absent: math.Inf(1), 136 }, 137 { 138 name: "three - negative", 139 nodes: []graph.Node{node(-1), node(-2), node(-3)}, 140 edges: []WeightedLine{line{F: node(-1), T: node(-2), UID: 0, W: 0.5}}, 141 nonexist: []graph.Node{node(0), node(1)}, 142 self: 0, 143 absent: math.Inf(1), 144 }, 145 { 146 name: "three - zero spanning", 147 nodes: []graph.Node{node(-1), node(0), node(1)}, 148 edges: []WeightedLine{line{F: node(-1), T: node(1), UID: 0, W: 0.5}}, 149 nonexist: []graph.Node{node(-2), node(2)}, 150 self: 0, 151 absent: math.Inf(1), 152 }, 153 { 154 name: "three - zero contiguous", 155 nodes: []graph.Node{node(0), node(1), node(2)}, 156 edges: []WeightedLine{line{F: node(0), T: node(1), UID: 0, W: 0.5}}, 157 nonexist: []graph.Node{node(-1), node(3)}, 158 self: 0, 159 absent: math.Inf(1), 160 }, 161 162 { 163 name: "three in only", 164 nodes: []graph.Node{node(0), node(1), node(2), node(3)}, 165 edges: []WeightedLine{ 166 line{F: node(1), T: node(0), UID: 0, W: 0.5}, 167 line{F: node(2), T: node(0), UID: 1, W: 0.5}, 168 line{F: node(3), T: node(0), UID: 2, W: 0.5}, 169 }, 170 nonexist: []graph.Node{node(-1), node(4)}, 171 self: 0, 172 absent: math.Inf(1), 173 }, 174 { 175 name: "three out only", 176 nodes: []graph.Node{node(0), node(1), node(2), node(3)}, 177 edges: []WeightedLine{ 178 line{F: node(0), T: node(1), UID: 0, W: 0.5}, 179 line{F: node(0), T: node(2), UID: 1, W: 0.5}, 180 line{F: node(0), T: node(3), UID: 2, W: 0.5}, 181 }, 182 nonexist: []graph.Node{node(-1), node(4)}, 183 self: 0, 184 absent: math.Inf(1), 185 }, 186 187 { 188 name: "4-clique - single(non-prepared)", 189 edges: func() []WeightedLine { 190 const n = 4 191 var uid int64 192 var edges []WeightedLine 193 for i := 0; i < n; i++ { 194 for j := i + 1; j < 4; j++ { 195 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 196 uid++ 197 } 198 } 199 return edges 200 }(), 201 nonexist: []graph.Node{node(-1), node(4)}, 202 self: 0, 203 absent: math.Inf(1), 204 }, 205 { 206 name: "4-clique+ - single(non-prepared)", 207 edges: func() []WeightedLine { 208 const n = 4 209 var uid int64 210 var edges []WeightedLine 211 for i := 0; i < n; i++ { 212 for j := i; j < 4; j++ { 213 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 214 uid++ 215 } 216 } 217 return edges 218 }(), 219 nonexist: []graph.Node{node(-1), node(4)}, 220 self: 0, 221 absent: math.Inf(1), 222 }, 223 { 224 name: "4-clique - single(prepared)", 225 nodes: func() []graph.Node { 226 const n = 4 227 nodes := make([]graph.Node, n) 228 for i := range nodes { 229 nodes[i] = node(i) 230 } 231 return nodes 232 }(), 233 edges: func() []WeightedLine { 234 const n = 4 235 var uid int64 236 var edges []WeightedLine 237 for i := 0; i < n; i++ { 238 for j := i + 1; j < n; j++ { 239 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 240 uid++ 241 } 242 } 243 return edges 244 }(), 245 nonexist: []graph.Node{node(-1), node(4)}, 246 self: 0, 247 absent: math.Inf(1), 248 }, 249 { 250 name: "4-clique+ - single(prepared)", 251 nodes: func() []graph.Node { 252 const n = 4 253 nodes := make([]graph.Node, n) 254 for i := range nodes { 255 nodes[i] = node(i) 256 } 257 return nodes 258 }(), 259 edges: func() []WeightedLine { 260 const n = 4 261 var uid int64 262 var edges []WeightedLine 263 for i := 0; i < n; i++ { 264 for j := i; j < n; j++ { 265 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 266 uid++ 267 } 268 } 269 return edges 270 }(), 271 nonexist: []graph.Node{node(-1), node(4)}, 272 self: 0, 273 absent: math.Inf(1), 274 }, 275 276 { 277 name: "4-clique - double(non-prepared)", 278 edges: func() []WeightedLine { 279 const n = 4 280 var uid int64 281 var edges []WeightedLine 282 for i := 0; i < n; i++ { 283 for j := i + 1; j < n; j++ { 284 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 285 uid++ 286 edges = append(edges, line{F: node(j), T: node(i), UID: uid, W: 0.5}) 287 uid++ 288 } 289 } 290 return edges 291 }(), 292 nonexist: []graph.Node{node(-1), node(4)}, 293 self: 0, 294 absent: math.Inf(1), 295 }, 296 { 297 name: "4-clique+ - double(non-prepared)", 298 edges: func() []WeightedLine { 299 const n = 4 300 var uid int64 301 var edges []WeightedLine 302 for i := 0; i < n; i++ { 303 for j := i; j < n; j++ { 304 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 305 uid++ 306 edges = append(edges, line{F: node(j), T: node(i), UID: uid, W: 0.5}) 307 uid++ 308 } 309 } 310 return edges 311 }(), 312 nonexist: []graph.Node{node(-1), node(4)}, 313 self: 0, 314 absent: math.Inf(1), 315 }, 316 { 317 name: "4-clique - double(prepared)", 318 nodes: func() []graph.Node { 319 const n = 4 320 nodes := make([]graph.Node, n) 321 for i := range nodes { 322 nodes[i] = node(i) 323 } 324 return nodes 325 }(), 326 edges: func() []WeightedLine { 327 const n = 4 328 var uid int64 329 var edges []WeightedLine 330 for i := 0; i < n; i++ { 331 for j := i + 1; j < n; j++ { 332 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 333 uid++ 334 edges = append(edges, line{F: node(j), T: node(i), UID: uid, W: 0.5}) 335 uid++ 336 } 337 } 338 return edges 339 }(), 340 nonexist: []graph.Node{node(-1), node(4)}, 341 self: 0, 342 absent: math.Inf(1), 343 }, 344 { 345 name: "4-clique+ - double(prepared)", 346 nodes: func() []graph.Node { 347 const n = 4 348 nodes := make([]graph.Node, n) 349 for i := range nodes { 350 nodes[i] = node(i) 351 } 352 return nodes 353 }(), 354 edges: func() []WeightedLine { 355 const n = 4 356 var uid int64 357 var edges []WeightedLine 358 for i := 0; i < n; i++ { 359 for j := i; j < n; j++ { 360 edges = append(edges, line{F: node(i), T: node(j), UID: uid, W: 0.5}) 361 uid++ 362 edges = append(edges, line{F: node(j), T: node(i), UID: uid, W: 0.5}) 363 uid++ 364 } 365 } 366 return edges 367 }(), 368 nonexist: []graph.Node{node(-1), node(4)}, 369 self: 0, 370 absent: math.Inf(1), 371 }, 372 }