Dijkstra algorithm Analysis

Source: Internet
Author: User

I. Theoretical basis

The Dijkstra algorithm (hereinafter referred to as the DJ algorithm) is the theoretical basis for a simple theorem:

The next shortest path is either an arc (V0, Vx), or some vertex in the middle through s, and then the path to Vx. (S is a collection of vertices known as the shortest path)

(For an accurate description of the theorem, here is a direct excerpt.) As far as it is right, well, it is said that the proof of the law can be proved) it is obvious that this is a recursive method: repeatedly seeking the next one, until there is no next one.

Two. Problem analysis

The existing weighted graph is as follows:

The shortest path from V0 to other vertices is obtained.

First, in order to convert it into a mathematical form for descriptive purposes, the corresponding adjacency matrix (x means unreachable) must be written:

0 50 10 X 45 X
X 0 15 X 10 X
20 X 0 15 X X
X 20 X 0 35 X
X X X 30 0 X
X X X 3 X 0

The analysis theorem can be drawn: At present there are only 1 vertices in the S set: V0 (V0 to itself is definitely the shortest path), in addition to the S collection of this auxiliary space, we also need:

    • V-Set: a collection of vertices that are unknown to the shortest path, that is, the complement set of S collection
    • Path[]: Record shortest path for displaying results
    • dist[]: Record path length, function as above
    • Vx: Record the next vertex

The preparation is over and you can begin to solve the

Three. Solution Process

0. Initial state

Dist[] Initializes the direct distance from the V0 to each vertex (x means non-direct), path[] is initialized to the corresponding path, non-direct null is selected, the vertex V2 with the smallest dist value in the V set is chosen as the next vertex (VX in the theorem)

1.1th Step

Add V2 to the S collection, we have a transit point V2, Next update dist[], see with V2 relay words is not shorter, according to the results of the calculation dist[] and path[], the shorter path length and corresponding path, the dist value of the V3 in the V set as the next vertex

2.2–5 Step

Continue to select the next vertex until there are no optional vertices in the V collection

Four. Summary

The DJ algorithm process is very simple:

    1. Determine the first point in S (that is, the source point V0);
    2. Recursive according to theorem (always find the shortest, and try to use the shortest transit)

The idea of the algorithm itself is not difficult, initially can not understand is because the specific pseudo-code implementation around, so the learning algorithm should pay attention to the idea rather than the implementation, especially the pseudo-code algorithm, usually arbitrarily declare some strange data structure, but do not explain why need these auxiliary space, such as this example, s set and V set only need to have one, path[] and dist[] can also be used to clear the structure of the expression, but the pseudo-code fragmented all used, too much auxiliary space to hinder the understanding of the algorithm itself.

Dijkstra algorithm Analysis

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.