@@ -245,12 +245,18 @@ export const makeWeightsOld = (len, min, max, symmetric, unweighted) => {
245
245
// for a large number - could make some adjustments. Could be nice for
246
246
// this to have the X-Y coordinates passed in; currently they are done
247
247
// independently, which limits things somewhat.
248
- export const makeWeights = ( len , min , max , symmetric , unweighted ) => {
248
+ export const makeWeights = ( len , min , max , symmetric , unweighted , circular = false ) => {
249
249
const rows = [ ] ;
250
250
251
251
// get pseudo-random len*len edge matrix
252
252
// Try to get average degree around 3, edges being more likely between
253
- // nodes with similar numbers
253
+ // nodes with similar numbers; lower degree for circular graphs
254
+ let diffMult = 4 ; // magic number for random edge generation
255
+ let sub = 6 ; // magic number for random edge generation
256
+ if ( circular ) {
257
+ diffMult = 3 ; // larger means fewer edges between close nodes
258
+ sub = 0 ; // smaller means fewer edges in total
259
+ }
254
260
for ( let i = 0 ; i < len ; i += 1 ) {
255
261
let row = [ ] ;
256
262
let edgeCount = 0 ;
@@ -269,20 +275,19 @@ export const makeWeights = (len, min, max, symmetric, unweighted) => {
269
275
// code did...
270
276
val = ( symmetric ? 0 : 1 ) ;
271
277
// always have an edge between i and i+1 to make sure graph is
272
- // connected (may not always want this but you can edit graphs
273
- // if desired)
274
- } else if ( j == i + 1 ) {
278
+ // connected, unless its circular
279
+ } else if ( j == i + 1 && ! circular ) {
275
280
val = ( unweighted ? 1 : getRandomInt ( min , max ) ) ;
276
281
// else determine if we want an edge between i and j
277
282
// - if i&j differ more we reduce likelihood
278
- } else if ( Math . random ( ) < 0.75 ** ( Math . abs ( i - j ) * 3 - 6 ) ) {
283
+ } else if ( Math . random ( ) < 0.75 ** ( Math . abs ( i - j ) * diffMult - sub ) ) {
279
284
val = ( unweighted ? 1 : getRandomInt ( min , max ) ) ;
280
285
}
281
286
if ( val > 0 ) edgeCount ++ ;
282
287
row . push ( val ) ;
283
288
}
284
289
// console.log('try ' + tries + 'edgeCount = ' + edgeCount);
285
- } while ( tries < 40 && ( edgeCount === 0 || edgeCount > 4 ) && len > 1 )
290
+ } while ( tries < 40 && ( edgeCount === 0 && ! circular || edgeCount > 4 ) && len > 1 )
286
291
// console.log('row' + i + ': ' + row);
287
292
rows . push ( row ) ;
288
293
}
@@ -300,7 +305,10 @@ export const makeWeights = (len, min, max, symmetric, unweighted) => {
300
305
301
306
// Create len random-ish XY coordinates in range min to max for
302
307
// Euclidean graphs
303
- export const makeXYCoords = ( len , min , max ) => {
308
+ export const makeXYCoords = ( len , min , max , circular = false ) => {
309
+ // added circular layout for Warshall's
310
+ if ( circular )
311
+ return makeXYCoordsCircular ( len , min , max ) ;
304
312
const rows = [ ] ;
305
313
let arr = [ ] ;
306
314
let prevX = 0 ; // keep track of previous 2 X,Y values to reduce close nodes
@@ -348,6 +356,25 @@ export const makeXYCoords = (len, min, max) => {
348
356
return arr ;
349
357
}
350
358
359
+ // Create len circular-ish XY coordinates for graph in range min to max for
360
+ // Warshall's
361
+ export const makeXYCoordsCircular = ( len , min , max ) => {
362
+ const unitAngle = ( 2 * Math . PI ) / len ;
363
+ const radius = ( max - min ) / 6 ; // height on screen is limited
364
+ const midX = ( max - min ) / 2 ;
365
+ const midY = 1 + ( max - min ) / 6 ; // avoid X axis
366
+ let arr = [ ] ;
367
+ for ( let i = 0 ; i < len ; i += 1 ) {
368
+ let data = { } ;
369
+ let x = Math . round ( midX + ( Math . cos ( Math . PI + unitAngle * i ) * radius ) ) ;
370
+ let y = Math . round ( midY + ( Math . sin ( Math . PI + unitAngle * i ) * radius ) ) ;
371
+ data [ `col0` ] = `${ x } ` ;
372
+ data [ `col1` ] = `${ y } ` ;
373
+ arr . push ( data ) ;
374
+ }
375
+ return arr ;
376
+ }
377
+
351
378
// Euclidean distance between two points (rounded up)
352
379
// We round up so we can have the choice of admissible and inadmissible
353
380
// heuristics in A* more easily (and avoid floating point)
0 commit comments