What algorithm can I use to find the shortest path between specified node types in a graph?

后端 未结 8 1832
自闭症患者
自闭症患者 2021-02-14 12:19

This is the problem:

I have n points (p1, p2, p3, .. pn), each of them can connect to any other with a determined cost x.

Each point belongs to one of a set of p

8条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 12:25

    As Jan mentioned, you just need a normal boring shortest path algorithm (like Dijkstra's or Floyd's algorithm); however, you need to transform your input graph so that the output path will respect your path constraint.

    Given a path constraint of: A - B - A

    Create a new graph G and insert all of the vertexes from A into G with new labels like a_01. Then insert all the vertexes from B into G and connect the A vertexes with the B vertexes (edges should be directed towards the newly inserted nodes) copying the costs from the original graph. You then repeat this step with A (and any other path components) connecting the newly inserted vertexes to those in B. Thus, you create a graph where only the paths that exist satisfy the path constraint. You can then use normal shortest path algorithms.

    The key insight is that when you revisit a class you are actually visiting a distinct set of nodes and that you only want edges that connect adjacent classes of nodes.

提交回复
热议问题