2

I'm trying to optimize my maze generation algorithm. At the moment i have a list of sets of nodes and a list of nodes themselves. Nodes are stored as (x,y) tuples. At the beginning each set contains only one node. I pick a random border between two nodes and check if they are in the same set. Here's the problem - i have to iterate through the list of sets and look at every single item until i find the set which contains given node/nodes. I want to be able to access sets as a property of Node class, but also i want my sets to contain objects of "Node" class and i run into this:

class Node:

   def __init__(self, xy:tuple, group:set):
       self.xy = xy
       self.group = group

node = Node((10, 10),{Node(10, 10),{Node(10, 10),{... and so on }}})

How do i create such a relation so that i can access sets as node.group and at the same time group property would point to the needed set with other Node objects without recursion?

1 Answer 1

1

Is this what you wanted ?

class Node:

    def __init__(self, xy:tuple):
        self.xy = xy
        self.group = None

    def set_group(self, group:set):
        if  self.group is not None:
            self.group.remove(self)
        group.add(self)
        self.group = group

node1 = Node((1,1))
node2 = Node((2,2))

group1 = set()
group2 = set()

node1.set_group(group1)
node2.set_group(group2)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thank you, it did help indeed! I got to put a little bit extra stuff in it but it's working just fine. I'm kinda disappointed i didn't figure it out myself :( Thank you A LOT again
Just to put it into perspective of how much you helped me - the time increased from 45 sec for 1500x1500 jpg image to 15 sec for the same image. God bless you :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.