Is this a decent approach for linking a list of nodes?

Algorithms

I'm trying to link a random list of nodes such that each node may have 4 directions (up/down/left/right) that point to another node. I was thinking of just starting with the first node and randomly selecting how many of the 4 directions I want and linking those nodes up. After that I go to those nodes and call that function until I'm out of nodes. Issue I see is how do I know when to populate other directions so I'm not just favoring a single path from initial setup? I do want them to distribute somewhat evenly (or at least randomly)

Another issue is that even though I'd know that every node on the list is linked I'd also like to link any of the nearby nodes that can be linked with each other. Like if a node has a node above and below it but there also exists a node on its right than they link to each other. I was thinking of trying to check out the node below see if a link exists to node to the right and than above and if that node exists than link them, if not than try the node above+right+below and if that path exists than link them. I feel like there would be a lot of exceptions where this might not work or probably a much faster way than checking each node's missing links?

Posted Jul 23, 2021, 1:59 AM UTC

11
Blazerboy65•

It sounds like what you really want is a grid. It's there any reason you're using an arbitrary graph structure instead of a 2d array?

1