Floyd-warshall algorithm-Shortest path (suitable for node-dense graphs)

Source: Internet
Author: User

? Because this algorithm has a time complexity of O (V3). In most cases it is not as good as the Dijkstra algorithm. The Dijkstra algorithm is suitable for node evacuation diagrams.

The demo sample diagram is as follows:

?

?


Step 1 Creates a table of the shortest path results for nodes and edges ( directly up to the relationship ). The value represents the distance. INF indicates unreachable

?

1

2

3

4

1

0

8

/ td>

INF

1

2

INF

0

1

INF

3

4

INF

0

INF

4

inf

/td>

2

9

0

Step2 find all paths that go through 1. Update the shortest path between two points

After 1 of the path is the combination of the full and the out path, the total number is in the X-out degree:

?

?

After 1 paths are:

First, 3-1-2.

At present min (3->2) is the INF, and Min (3->1->2) =4+8=12 so min (3->2) isA, because 2 is updated, it is necessary to recursively update all the shortest path from 3 to 2 points . The 2 point is only 3,min (3->3) is 0, so there is no need to update.

Article II, 3-1-4

At present min (3->4) is the INF, and Min (3->1->4) =4+1=5, so min (3->4) is 5, because 4 is updated, it is necessary to recursively update all from 3 to 4 of all the shortest path of the point, and 4 of the points are 3 and 2:

MIN (3->3) =0,min (3->2) =min (3->1->2) =12? > MIN (3->1->4->2) = 7. So MIN (3->2) =7

Find out the results of all 1 paths:

?

?

?

?

1

2

3

4

1

0

8

Inf

1

2

Inf

0

1

Inf

3

4

7

0

5

4

Inf

2

9

0

Step3 Find all paths that go through 2.

?

1th article, 1->2->3

Because min (1->3) is INF, and Min (1->2->3) is 9. so min (1->3) is 9, because 3 is updated, so it is necessary to recursively update all the shortest path from 1 to 3 points, because 3 of the point is 1, and min (1->1) does not need to be updated, 0. Now look at the second path:

The second article. 4->2->3

So min (4->3) is 9, while min (4->2->3) is 3. So min (4->3) =3, because 3 has an update, need to recursively update the shortest path from 4 to 3 points, because 3 of the point is 1, and MIN (4->1) is Inf,min (4->2->3->1) 7. So MIN (4->1) is 7. Since 1 has an update, the continuation is recursive, 1 of the points are 4 and 2,min (4->4) remain 0, the min (4->2) is 2, and min (4->2->3->1->2) =2+1+4+8=15 is greater than 2. So there is no need to update.

After you find all the paths that go through 2, the result is:

?

1

2

3

4

1

0

8

9

1

2

Inf

0

1

Inf

3

4

7

0

5

4

7

2

3

0

Step4 Find all paths that go through 3.


1th article. 4->3->1

MIN (4->1) is 7. and Min (4->3->1) is 13. So there is no need to update

Article II, 2->3->1

Because min (2->1) is INF, and Min (2->3->1) is 5. Therefore , the need to update MIN (2->1) is 5.

Due to the update of 1, it is necessary to update all the paths from 2 to 1 points, 1 points to 4 and 2,min (2->2) do not need to be updated; now MIN (2->4) is INF. and Min (2->3->1->4) is 6. So MIN (2->4) is 6. Due to the update of 4, it is necessary to recursively update the paths from 2 to 4 points, 4 to 2 and 3,min (2->2) to 0;min (2->3) to 1 less than MIN (2->3->1->4->3) =1+4+1+9=15. It does not need to be updated.

So after this step, the result table is:

?

?

?

?

1

2

3

4

1

0

8

9

1

2

5

0

1

6

3

4

7

0

5

4

7

2

3

0

Finally, find the path that passes through 4.

?

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

First article. 1->4->2

Min (1->2) is 8, while min (1->4->2) is 3, so min (1->2) is updated to 3, because 2 is updated. It is necessary to update the shortest path from 1 to 2 points, and 2 to 3. MIN (1->3) is now 9. Min (1->4->2->3) is 4, so min (1->3) is updated to 4. Due to the update of 3. Therefore recursive update all from 1 to 3 the shortest path of the point, 3 of the point is 1, and min (1->1) does not need to update to remain 0.

Article II, 1->4->3

Min (1->3) is 4, while min (1->4->3) is 10. So there is no need to update.

So finally the result is:

?

1

2

3

4

1

0

3

4

1

2

5

0

1

6

3

4

7

0

5

4

7

2

3

0

?

Floyd-warshall algorithm-Shortest path (suitable for node-dense graphs)

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.