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.