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

Source: Internet
Author: User

Since the time complexity of this algorithm is O (V3), in most cases it is not as good as the Dijkstra algorithm, the Dijkstra algorithm is suitable for the node evacuation diagram.

The example diagram is as follows:


Step 1 creates a table of shortest path results for nodes and edges ( directly accessible to the relationship), numeric representation of distance, INF representation unreachable

 

1

2

3

4

1

0

8

INF

1

2

INF

0

1

INF

3

/ td>

4

INF

0

INF

4

INF

2

9

0

Step2 Find All paths that pass 1, update the shortest path between two points

After 1 of the path is the combination of all the in and out paths, the total 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, so to recursively update all the shortest path from 3 to 2 points , The 2 point is only 3,min (3->3) is 0, so it does not need to be updated.

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 the points from 3 to 4 of all the shortest path, 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 all the results after 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, min (1->3) is 9, because 3 has an update, it is necessary to recursively update all the shortest paths from 1 to 3 points, because 3 can be up to a point of 1, While min (1->1) does not need to be updated, it is 0. Now look at the second path:

Article II, 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 is up to 1, and min (4- >1) for Inf,min (4->2->3->1) is 7, so MIN (4->1) is 7. Since 1 has an update, continuation is recursive, 1 of the points are 4 and 2,min (4->4) remain 0; min (4->2) is 2, and min (4->2->3->1->2) =2+1+4+8=15 is greater than 2, so no update is required.

After finding all paths that pass 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, you need to update min (2->1) to 5. Since the update is 1, it is necessary to update all paths from 2 to 1 points, 1 of the points to 4 and 2,min (2->2) do not need to be updated; min (2->4) is INF, and Min (2->3->1->4) is 6, so min (2->4) is 6. Because the update is 4, it is necessary to recursively update the path 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, so there is no need to update.

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.

First, 1->4->2.

Min (1->2) is 8, and min (1->4->2) is 3, so min (1->2) is updated to 3, due to the update of 2, it is necessary to update all the shortest path from 1 to 2 points, and 2 to the point of 3,min (1-> 3) currently 9, and Min (1->4->2->3) is 4, so min (1->3) is updated to 4, due to the update of 3, so recursively update all the shortest path from 1 to 3 points, 3 of the point is 1, and min (1->1 ) does not require an update to remain at 0.

Article II, 1->4->3

Min (1->3) is 4, and Min (1->4->3) is 10, so no update is required. So the end 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.