Skip to content

Commit 5b4cb76

Browse files
committed
Random circular graphs for TC
1 parent 2be1f65 commit 5b4cb76

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

src/algorithms/parameters/TCParam.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function TransitiveClosureParam() {
4949
EXAMPLE={TRANSITIVE_CLOSURE_EXAMPLE}
5050
setMessage={setMessage}
5151
symmetric={false}
52+
circular={true}
5253
unweighted
5354
/>
5455

src/algorithms/parameters/helpers/EuclideanMatrixParams.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ function EuclideanMatrixParams({
198198
ALGORITHM_NAME,
199199
EXAMPLE,
200200
EXAMPLE2,
201-
unweighted
201+
unweighted,
202+
circular
202203
}) {
203204

204205
// XXX these get re-evaluated when anything much changes - could
@@ -321,8 +322,8 @@ function EuclideanMatrixParams({
321322
setEndNodes(newEndNodes);
322323
}
323324
if (graphChoice === GRAPHCHOICERAND) {
324-
const edges = makeWeights(newSize, 1, 10, symmetric, unweighted);
325-
const coords = makeXYCoords(newSize, min, max);
325+
const edges = makeWeights(newSize, 1, 10, symmetric, unweighted, circular);
326+
const coords = makeXYCoords(newSize, min, max, circular);
326327
setData1(coords);
327328
setCoordsTxt(getCoordinateList(coords));
328329
setData2(edges);
@@ -349,7 +350,7 @@ function EuclideanMatrixParams({
349350
// to generate new random graphs of any size (thats still a bit
350351
// cumbersome with the current setup)
351352
// XXX maybe we should have a text box for the size instead of + and -
352-
const updateTableSize = (newSize) => {
353+
const updateTableSize = (newSize, circular=false) => {
353354
if (newSize < 1) return;
354355
if (newSize < startNode)
355356
setStartNode(newSize);
@@ -425,7 +426,6 @@ function EuclideanMatrixParams({
425426
// components/DataStructures/Graph/GraphRenderer/index.js) so
426427
// coordinates here can be updated
427428
const moveNode = (nodeID, x, y) => {
428-
console.log(['moveNode', nodeID, x, y]);
429429
const newData1 = data1.map((row, index) => {
430430
if (index === nodeID) {
431431
return {

src/algorithms/parameters/helpers/ParamHelper.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,18 @@ export const makeWeightsOld = (len, min, max, symmetric, unweighted) => {
245245
// for a large number - could make some adjustments. Could be nice for
246246
// this to have the X-Y coordinates passed in; currently they are done
247247
// 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) => {
249249
const rows = [];
250250

251251
// get pseudo-random len*len edge matrix
252252
// 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+
}
254260
for (let i = 0; i < len; i += 1) {
255261
let row = [];
256262
let edgeCount = 0;
@@ -269,20 +275,19 @@ export const makeWeights = (len, min, max, symmetric, unweighted) => {
269275
// code did...
270276
val = (symmetric? 0 : 1);
271277
// 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) {
275280
val = (unweighted? 1: getRandomInt(min, max));
276281
// else determine if we want an edge between i and j
277282
// - 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)) {
279284
val = (unweighted? 1: getRandomInt(min, max));
280285
}
281286
if (val > 0) edgeCount++;
282287
row.push(val);
283288
}
284289
// 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)
286291
// console.log('row' + i + ': ' + row);
287292
rows.push(row);
288293
}
@@ -300,7 +305,10 @@ export const makeWeights = (len, min, max, symmetric, unweighted) => {
300305

301306
// Create len random-ish XY coordinates in range min to max for
302307
// 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);
304312
const rows = [];
305313
let arr = [];
306314
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) => {
348356
return arr;
349357
}
350358

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+
351378
// Euclidean distance between two points (rounded up)
352379
// We round up so we can have the choice of admissible and inadmissible
353380
// heuristics in A* more easily (and avoid floating point)

src/components/DataStructures/Graph/GraphTracer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class GraphTracer extends Tracer {
115115
this.scaledCoords = coordinates;
116116
this.setNodeRadius(coordinates);
117117

118-
console.log(['coordinates', coordinates]);
119118
// Set layout to null if nodes are to be displayed by coordinates.
120119
if(coordinates.length > 0)
121120
{

0 commit comments

Comments
 (0)