Question

I am working on a graph problem which is special in the sense that the nodes are computed only when required. This is because the graph is so huge that the entire graph won't fit in memory.

So, in my code I only have a node class and a computeChildren() method which gets called only when needed. I also have certain global parameters which are to be computed when the initNode is initialized. I am trying to figure out the best way to store these parameters. I thought about updating the parameters in the init method of the class but this causes the same computations to be repeated whenever a new node is computed.

I am using python3, could someone please suggest the right way to do this ...

UPD: The values used to initialize the init node decide the graph structure, this information is same for all nodes.

Some details regarding my coding background : I have mostly coded in C with some limited expertise in C++.

Was it helpful?

Solution

If it's global state, don't store it in the node class. Store it in the object which represents the whole graph, and reference the single instance of that object in your node objects.

OTHER TIPS

The suggestion of adding a class representing the whole graph makes sense. Instances of this class can take care of computing the parameters when and only when they should be computed. It also avoids the need for global state, which is a problem by itself.

class Graph:
    def __init__(self):
        self.first_node = Node()
        self.parameters = compute_parameters()
Licensed under: CC-BY-SA with attribution
scroll top