Question

Let's assume I've got a graph of a signal flow based "programm" (e.g. something similar to Simulink). I.e. I've got a directed graph with a few starting nodes and a few end nodes as well as lots of nodes inbetween (and hopefully no circular relation)

Is there a good and/or well known algorithm (perhaps even available as a Python library) that will walk that graph and give me the computation order?

Example (direction not showed, assume the obvious):

In1     In2
   \       \
    [-]     [*]-- Out1
   /   \   /
In3     [+]------ Out2
       /
    In4 

This should result in the instructions / order:

1. tmp1 := In1 - In3
2. Out2 := tmp1 + In4
3. Out1 := In2 * Out2

Thanks!

Was it helpful?

Solution

Are you looking for some kind of variation on a Topological Sort?

Since you know the starting nodes, you can start the algorithm from them. When you meet an "operation" node, you create the computation with the nodes leading you to it. Naturally, the graph should be consistent in some ways specific to your problem.

To implement this in Python, what you need is a good graph library (for example NetworkX). Topological sort is very simple to implement, and many graph libraries already have it as an implemented algorithm. For example, in the case of NetworkX - networkx.algorithms.dag.topological_sort. However, as mentioned above it's probably not exactly a topological sort, but a variation thereof.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top