Created
August 17, 2023 08:56
-
-
Save siliconjungle/0f3a89dff8f35375bfe442572936be2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hasCircularReference = (graph, startUID) => { | |
const visited = new Set() | |
const stack = [startUID] | |
while (stack.length > 0) { | |
let currentUID = stack.pop() | |
if (visited.has(currentUID)) { | |
return true | |
} | |
visited.add(currentUID) | |
const node = graph[currentUID] | |
if (node && node.children) { | |
for (let childUID of Object.values(node.children)) { | |
stack.push(childUID) | |
} | |
} | |
} | |
return false | |
} | |
const graph = { | |
'james:0': { | |
children: { | |
'123abc': 'james:1', | |
}, | |
}, | |
'james:1': { | |
children: { | |
'position': 'james:2', | |
'size': 'james:5', | |
}, | |
}, | |
'james:2': { | |
children: { | |
'x': 'james:3', | |
'y': 'james:4', | |
}, | |
}, | |
'james:3': { | |
children: {}, | |
}, | |
'james:4': { | |
children: {}, | |
}, | |
'james:5': { | |
children: { | |
'width': 'james:0', | |
'height': 'james:7', | |
}, | |
}, | |
'james:6': { | |
children: {}, | |
}, | |
'james:7': { | |
children: {}, | |
}, | |
} | |
console.log(hasCircularReference(graph, 'james:0')) | |
console.log(hasCircularReference(graph, 'james:1')) | |
console.log(hasCircularReference(graph, 'james:2')) | |
console.log(hasCircularReference(graph, 'james:3')) | |
console.log(hasCircularReference(graph, 'james:4')) | |
console.log(hasCircularReference(graph, 'james:5')) | |
console.log(hasCircularReference(graph, 'james:6')) | |
console.log(hasCircularReference(graph, 'james:7')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment