The shortest path algorithm Dijkstra algorithm Floyd algorithm brief _dijkstra

Source: Internet
Author: User
Dijkstra algorithm

Also known as Dijkstra algorithm, is a classical shortest path algorithm, the main feature is to expand from the starting point to the outer layer, until the extension to the end point, the use of breadth-first search to solve the problem of the single source shortest path of the weighted direction graph, the algorithm finally get a shortest path tree. Time Complexity of O (n^2)
To perform an animation:

Instance:



Abstract steps:

1. Place the start a into the set, the weight of point A is 0, because a->a=0.

2. The weight of all points connected to start a is set to the distance of the a-> point, and the set of the connection is infinite. and find the smallest weight of B into the set (at this time a->b must be the minimum distance).

3. The weights of all points connected with point B are set to the distance of the b-> point, and the C-point where the minimum weights are found is put into the set (at which point the weight of C must be the minimum distance).

4. Repeat step 3 until all points are added to the collection. We can get the shortest distance between all points and point A.


Floyd algorithm

Full name Floyd-warshall algorithm, also known as Freud algorithm, is to solve any two of the shortest path between an algorithm, but the time complexity is higher than Dijkstra, time complexity of O (n^3), the space complexity of O (n^2). Simple case:

Steps:

1. Convert the graph into a matrix:

2. Select the V0 point as the first intermediate point:

3. Whether the middle point of the V0 can shorten the distance between the other two points: in short, it is not diagonal (red arrow) to the addition of the Judge,< is replaced.

4. Proceed to step 2-3 for subsequent points.


Example:

The INF represents an infinite size.



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.