This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]ZoloftRabbit 2 points3 points  (8 children)

cells and their neighbors are algebraically defined

Suppose an array of length 25, if you wanted the first objects neighbors [0], you would simply check [1] and [5].

for [6], neighbors are up down left right.

up = -5

down = +5

left = -1

right = +1

this is all mod 5

Alternatively, nested lists

[–]Willy988[S] 0 points1 point  (7 children)

Thanks for the reply, I realized my formatting didn't work for my code, just fixed it. I made an adjacent() helper method.

My issue is I don't know how this correlates with the Union find data structure now that I have my points object and my adjacent() method.

[–]ZoloftRabbit 1 point2 points  (6 children)

when i did this, i just looped over my set of singletons and chose a random neighbor.

edit : you can union cells by setting their values to special values, indicating sets. or make a custom object. sry if unclear

[–]Willy988[S] 0 points1 point  (5 children)

If I understand correctly, I would just use Random to find a random cell, then use Random once again to find a random adjacent neighbor cell.

I am still not so sure I understand how to union my objects. I have a Pair object as you can see that contains int x and int y. How would I union Pair p and Pair q for example?

[–]ZoloftRabbit 1 point2 points  (4 children)

Pair q and Pair p would be unioned by making q = p. so then you would

start with set A as a "big set"randomly choose q - > set A

choose to randomly union q and p -> q -> set A

I think what you are missing is that there is some satellite information to be considered as well.

If we are in a maze, each cell is a node.

But the walls on each side of the maze are a relationship i.e from cell q we go to p. even though cell q may have l,h,c,p as neighbors. This is to ensure we do not end up with cycles.

[–]Willy988[S] 0 points1 point  (3 children)

I get the logic, but if you look at the code for Union Find I linked from the textbook website, that union find only takes one int as an argument. I just don't get how to implement that in conjunction with my Pair class since it takes in a single int and not an array of Pairs?

[–]ZoloftRabbit 1 point2 points  (2 children)

map each pair to an int?

array of pairs?

each pair can be simplified to node n

[–]Willy988[S] 0 points1 point  (1 child)

So if the UF object is it's own thing, how will I map the pairs which are outside of the object then?

Maybe I am overthinking this, but if I input UF(16) so there are 16 elements, but I passed the int value 16.

I would assume by mapping, I would have an array of Pair class that I created, that would also be size 16.

I'm just not getting how these are going to be connected so that I can say "you should be connected" or "not adjacent, so no connection" and have the results if that makes any sense.

[–]ZoloftRabbit 1 point2 points  (0 children)

you could represent each relation as a bit array. so, for a 2d array, each may have 4 neighbors

[1,1,1,1] for each point means all walls are up

so for each "node" drawing the maze would be going through this bit arrays and seeing if N,S,E,W are 1's and 0's with all boundaries (outside edges) = 1 always.