Dijkstra algorithm---slack through edges

Source: Internet
Author: User

Picture material for reference Ah Halle's Blog

The algorithm is used to solve the shortest path of one point to the remaining vertices

Let's start with a diagram and ask for the shortest path from 1 to 6.

It reminds me of the almost-hung branch of operations research.

Use a two-dimensional array first

There is also a one-dimensional array that stores the distance from 1 points to each point

The value of this one-dimensional array is called an estimate. Select a nearest point, point 2, at a point that can be reached directly at 1 points. Then the value of point 2 becomes the determined value.

Why Because the path is positive, it is not possible to shorten the distance from point 1 to 2 by other transit points.

After a bit of 2, look at what points 2 can reach. Point 3 and point 4. Let's discuss whether the transit point of point 2 can shorten the distance from point 1 to 3. The answer is yes. 1+9<12. Modifies the value of a one-dimensional array.

This is called relaxation .

In the same way compare points 1 to 4 distance and distance through point 2 relay.

The rest of the 3,4,5,6 points to try it out for themselves. Otherwise, operational research is easy to hang.

The final answer is

Sticking out the core algorithm

  for (i=1;i<=n-1;i++)    {        //finds the vertex closest to number 1th vertex        min=inf;//initial min is positive infinity for        (j=1;j<=n;j++)        {            if ( Book[j]==0 && dis[j]<min)            {                min=dis[j];//gets the distance from the nearest point directly connected to the source point                u=j;            }        }        book[u]=1;//the point into the set of points that have been determined to go for        (v=1;v<=n;v++)//This is like traversing the brother to point 2 distance        {            if (E[u][v]<inf)            {                if (Dis[v]>dis[u]+e[u][v])                     dis[v]=dis[u]+e[u][v];}}        }

  

Dijkstra algorithm---slack through edges

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.