I'm struggling with a problem in which I have to find a way to store all the possible paths a program might follow.
Take this image as an example.
In that image each number represents a complex process that might call other processes to execute itself, describing all the paths you can see in the image.
All the continuous lines represent paths that the process necessarily has to follow, while the dashed lines represent paths that are optional.
Knowing the execution starts from left to right and from top to bottom, one must always keep in mind that if a branch has already been built it must be reused and never rebuilt again.
In this another image for example the yellow line represents all the paths that were follow during the execution of process number 37.
In it you can see that the path starting with the process 18 (18->17->16) was previously built and so when process 19 is reached it shouldn't be rebuilt as all these processes take quite some time and it'd be a waste of time to try to build them again already knowing the results they produced. Instead, if a certain number in found to have been built previously (e.g process 18) it should be copy/attached to the process that called it (process 19 in the image). All this is for a log in which I have to store all the paths complete, that's why I mention the part of copying/reusing branches as I'll later have to query that log to display all these paths.
To execute all these process currently a recursive process is used, but since it doesn't consider that it can reuse paths that were previously build, the whole thing takes ages.
Do you know of any data structure that might help me optimize this process so that if a process was already executed it is only reused. As for the log, as I mentioned above I need to store the complete paths.
Any ideas or resources would be highly appreciated.
Thanks
Edit 1
----------------------------
One thing that perhaps I didn't make very clear, is that the data struture I need to create has two purposes:
- To keep track of all the paths that the main process (37 in the example) followed during its execution,giving me the chance to tell at any time if a certain path was already followed, being able to then copy that path to the node that was supposed to called it (in the example, copying the whole branch: 18->17->16 to process 19.
- By, giving me the opportunity to tell whether a path is already in this data structure or not, I can avoid executing subprocesses that were already executed and which results are known and with that, optimizing the whole proccess of execution.
Edit 2
----------------------------
Regarding the question about why I didn't consider using a Dictionary
, well I had that idea at first, but then I couldn't find a way that I dictionary could tell me ,for example, that the path starting with 18 (18->17->16
) descends from both process 37 and 19. You see, a node can have one or more parents. How could I express that with a Dictionary?