JSFiddle - React, Tailwind, and code Playground
by Konstantin Cryman
JavaScript
const TEST_FOR_FIND_CIRCLES = {
a: [ 'b', 'c' ],
b: [ 'a', 'd', 'e' ],
c: [ 'a', 'd', 'i' ],
d: [ 'b', 'e', 'c', 'i', 'h' ],
e: [ 'b', 'd', 'f', 'g', 'l' ],
f: [ 'e', 'g', 'l' ],
g: [ 'e', 'f', 'k', 'j', 'l' ],
h: [ 'd', 'i', 'j' ],
i: [ 'c', 'd', 'h' ],
j: [ 'h', 'g', 'k' ],
k: [ 'g', 'j' ],
l: [ 'g', 'f', 'e' ],
};
const isCircled = ( blockID, totalCollisionMap ) => {
const currentPointName = blockID;
const currentPointConnections = totalCollisionMap[ currentPointName ];
const indexedPoints = {};
const recursiveCicleTest = ( nextPointID, currentPath, searchedPointID, c_length ) => {
const nextPointConnections = totalCollisionMap[ nextPointID ];
if( nextPointConnections.indexOf( searchedPointID ) !== -1 && c_length > 1 ) {
indexedPoints[ nextPointID ] = true;
currentPath.push( {
circle: true,
lastPoint: nextPointID
} );
} else if( nextPointID in indexedPoints ) {
currentPath.push( {
circle: false
} );
} else {
const results = [];
for( const nextID of nextPointConnections ) {
results.push( recursiveCicleTest( nextID, currentPath, searchedPointID, c_length + 1 ) ) ;
}
indexedPoints[ nextPointID ] = true;
currentPath.push( results );
}
return currentPath;
};
const testedWays = [];
try {
for( const nextForTestPointID of currentPointConnections ) {
const tested = recursiveCicleTest( nextForTestPointID, [], currentPointName, 0 );
testedWays.push( tested );
}
} catch( e ){
console.log( {
indexedPoints
} );
}
return testedWays;
};
const findCircles = function( connectionsList ){
const unsedBlocks = {};
const circledBlocks = [];
for( const nextBlockName in connectionsList...