Shortest Path Bar number problem-breadth First search

Source: Internet
Author: User
This summary is for the individual to prevent forgetting, do not reprint and commercial. Topics

Given the undirected connectivity diagram shown in the figure, assuming that all edges in the graph have weights of 1, it is clear that there are multiple shortest paths from source point A to end point T, and the number of different shortest paths is obtained.

PS: The above figure A is the No. 0 node, B is the 1th node, other similar. Analysis

The shortest path problem with the same weights, the single source point Dijkstra algorithm degenerated into BFS breadth-first search: breadth-first search, i.e.:

From a:

Take one step to get to B or E

Take two steps to reach C or F

Walk three steps to reach D or G or K

....

Like this, every step of the way, expanding backwards.

Therefore, the definition:

The starting point is 0 and the end point is n

Step[i]: Indicates the length of the shortest path to the node I, such as: step[4] = 1,step[6] = 3.

Pathnum[i]: The number of shortest paths to the number I node.

First, initialize step[i] and pathnum[i] to 0,i = 0, 1, ..., N-1], and make pathnum[0]= 0, you have 1 ways to yourself.

Then consider "How to find the I+1 value when you have already calculated the first I value of step and Pathnum":

if step[i] = 0, then I have not yet reached J, i.e.: The current is the first time to reach J, so:

Step[j] = step[i]+1,pathnum[j] = Pathnum[i]

if step[j] = step[i]+1, then you have reached J from I, and this is a solution to the shortest path, so:

Pathnum[j] = pathnum[i]

Finally, when extended to node n, the algorithm terminates.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.