Dependency Multi-Graph: topological sorting and evaluation of graphs with duplicate edges

I have a dependency graph in which one of the nodes requires two copies of the previous node. I want to use topological ordering to get the evaluation order, but the problem is that topological sorting ignores parallel / multiple edges and just treats it as one dependency. Am I modeling something wrong, or do I need a specific toposort that works with multigraphs?

+4
source share
1 answer

This can be done with a modification of the topological sorting algorithm.

(S), - node S, S , .

: node S, , node , S, , .

Wikipedia:

L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
    remove a node n from S
    add n to tail of L
    for each node m with an edge e from n to m do
        remove edge e from the graph
        if m has no other incoming edges then
            insert m into S

:

L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
    take a node n from S
    add n to tail of L
    for each neighbouring node m of n
        remove ONE edge (m,n) from the graph
        if m has no other incoming edges then
            insert m into S
    if n doesn't have outgoing edges
       remove n from S
+2

Source: https://habr.com/ru/post/1502707/


All Articles