问题
I would like to know if there is an algorithm for finding the shortest sequence of nodes though a graph from its a head node to the tail node. The graph branches out from the head node and is arbitrarily complex and converges at the tail node. All connections between nodes are unweighted.
I'm considering tackling this problem taking exploratory steps from the head and tail nodes and until the nodes from either end of the graph touch etc, but I'd like to know if a "better wheel" exists before I (re)invent one.
回答1:
Use breadth first search, which runs in O(E+V). It's the fastest you'll get on an unweighted graph.
回答2:
This is one of the beautiful "standard" problems of computer science. Given your description of the graph, you should first look at Dijkstra's algorithm
回答3:
BFS is the best for these types of problems, even if you want to find out single node shortest path you have traverse whole graph for finding if there any other possible path other than the already obtained shortest path.
You can also draw a BFS Tree which will tell you exactly the shortest route between source and any(also single) node.
来源:https://stackoverflow.com/questions/4528962/shortest-sequence-of-nodes-though-an-unweighted-graph