Eliminating extraneous edges in directed acyclic graph while trying to find longest paths

蓝咒 提交于 2019-12-05 15:55:23
Keldon Alleyne

Furthermore, notice how 0 always occurs between B and 3, so I would like to eliminate the pair B3 (again, B should always go through 0 before it jumps to 3, since all sets have the positions of these three letters as: B < 0 < 3).

Hmm, okay so if n0 < n1 < n2 holds on all sets then remove all (n0, n2) pairs? This can be achieved with this (in pseudoPython):

for(edge in node):
    if(len(LongestPath(node, edge.Node)) > 1):
        RemovePair(node, edge.Node)

Easy is easy. If the graph isn't too large, it's probably also efficient enough.

  • For each node (start with nodes without incoming edges), follow all paths, marking distances, mark all direct children with 1 and put them in a queue. While the queue is not empty, pull a node n out, let d be its distance form the start. Look at all its direct children, if any is marked with 1, remove the edge from start to that, put all children of n into the queue marked with distance d+1. Pull next node from queue.

What JSPerfUnkn0wn said, only with a bit more detail.

Since the graph is acyclic, a possible solution is applying your favorite shortest-path algorithm (Bellman-frod, Floyd-Warshal, etc) but with the comparison condition flipped (so that longer paths win over shorter paths).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!