You must implement the graph as a binary decision chart data structure .
In short, the idea is that a graph can be represented as a binary function using the characteristic function of a graph.
There are several ways to encode a graph as a binary function using a characteristic function. There is a way to do this in the article and video that I posted at the end of my post.
BDD encodes binary functions in a compact way with fast operations. This is probably the most powerful data structure in the universe.
The idea of โโBDD is almost the same as in the tree , but on each node we do not send the next input function, but instead each node has an attribute X , which represents the index of the variable and, if the function F (.. X = true ..) is true , continue on the upper branch of the node and reach the leaf true , if F (.. X = true ..) is true, continue on the lower branch down to the leaf node, representing true. This is called Shannon extension of the Boolean function (using the same extension formula also allows you to calculate the hardware design of the Boolean function using multiplexers).
In general, for every possible combination of input values โโX_i for which the function is true, we have a unique branch that goes from the root node to the true leaf, branching in each node depending on the input variable Xi (we branch out to a low high direction depending from the value of true or false Xi). The same diagram can be used to save several functions (each node is a separate function).
There are 2 optimizations for converting from a binary decision tree to a binary decision diagram, which makes it compact. The idea of โโoptimization is identical to optimization from the finite state machine minimization algorithm. As in the case of automata, the minimum BDD is unique for a function (therefore, to see if 2 arbitrary functions coincide, it is enough to convert them to BDD and see if the node representing one function coincides with the root node for another function (complexity O (1 ) (constant time) to compare 2 pointer values)).
One optimization says: if a node has all edges in the same physical nodes as another node, we merge both nodes into one (this can be done during creation, while maintaining a hash table of all created nodes).
Another optimization says that if the lower edge and the upper edge of the node for the variable X are in the same physical node of the variable Y, the node X disappears because the function has the same value for F (... X = true ...) = F (... X = false ...).
There are thousands of articles about BDD and its derivatives (changing the interpretation of scheduling at each node, we get, for example, ZDD, for a compact representation of disordered sets). A typical article on the topic " What graphics can be effectively presented by BDD?". C. Dong P. Molitor.
Once you understand the basics of BDD, if you have the opportunity for a longer presentation, this video is excellent and it summarizes how to encode graphics as BDD.
BDD is what professional software does today when you need to manage millions of nodes.