How do I find the shortest path that covers all nodes in a directed cyclic graph?

后端 未结 4 2107
一整个雨季
一整个雨季 2020-12-18 05:05

I need an example of the shortest path of a directed cyclic graph from one node (it should reach to all nodes of the graph from a node that will be the input).

Pleas

4条回答
  •  借酒劲吻你
    2020-12-18 05:59

    For non weighted graph, BFS will do the job. Since there is potential cycle in your graph, you need to keep track of visited node (you sort of need to do this for BFS anyway).

    For weighted graph, bellman–Ford algorithm can be used. It is also able to detect cycles.

提交回复
热议问题