Reputation: 7477
I have a directed acyclic graph, an example of which may look like:
|
(1)
/ | \
/ | \
(3) (2) (4)
/ / | \ \
/ / | \ \
/ / | \ \
(6)(7) (5) (8)(9)
| | | | |
(10)(11) (12)(13)(14)
\ \ | / /
\ \ (15)_/ /
\ \ | /
\___(16)__/
|
|
So for example, at node (1)
there are three branches that must synchronise at node (16)
. In itself, this is relatively simple - mark node (16)
as the synch
for node (1)
and merge from node (1)
to (16)
down the branch being synched when execution hits (16)
.
However, node (2)
has two branches that also must synchronise at node (16)
(it also has two that must synch at node (15)
).
The problem is, in this example, when execution hits node (16)
it doesn't know how far back up the tree to start synchronising from (i.e. node (1)
or (2)
).
I need something like a graph colouring scheme whereby different paths of execution provide their own pointers to the node from which they originated, so when the path (11) -> (16)
is activated, the execution knows that the portion of the graph to be merged starts at node (2)
.
Is there some bit of theory or an algorithm that can help here? Or am I approaching the problem in the wrong way?
Upvotes: 3
Views: 3141
Reputation: 59705
Topological sorting is what you are looking for. You can use the algorithm to partition the nodes of the graph into three classes of nodes for every fixed node X - predecessor nodes of X, successor nodes of X, and nodes independent from X.
Note that your graph must be acyclic for the algorithm while you stated your graphs are cyclic (but I can see no cycle in your example).
ALGORITHM
DP
of node X
.Ni
in DP
find the set of predecessor nodes Pi
.CP
by intersecting all Pi
.CP
(if CP
is non-empty).EXAMPLE
Let's look at node 15. There arew two direct predecessors 12 and 13. Now find all predecessors of this both nodes - for 12 they are 5, 2, and 1. For 13 they are 8, 2, and 1. The intersection of this sets is 2 and 1 therefore this two nodes are the common predecessors and node 2 is the common predecessor without successor (while node 1 is a common predecessor but has node 2 as successor). Therefore at node 15 two branches originating from node 2 join.
Upvotes: 2