Floyd Algorithm (a) C language detailed

Source: Internet
Author: User

Introduction to Freud's algorithm

Like the Dijkstra algorithm, the Freud (Floyd) algorithm is an algorithm for finding the shortest path between vertices in a given weighted graph. The algorithm is named after one of the founders, the 1978 Turing Prize winner, and Professor Robert Floyd of the Stanford University computer Science department.

Basic ideas

by Floyd calculating the shortest path of each vertex in g= (v,e), we need to introduce a matrix s, the element a[i][j in matrix S] to represent the distance between vertex I (vertex) and Vertex J (J Vertex).

Assuming that the number of vertices in graph G is n, you need to update the Matrix S n times. At the beginning, the distance of the vertex a[i][j] in the matrix S is the weight of vertex i to vertex j, and if I and J are not adjacent, then a[i][j]=∞. The next step is to update the Matrix S n times. On the 1th update, if the distance of "a[i][j" > "A[i][0]+a[0][j]" (A[i][0]+a[0][j] means "distance from the 1th vertex between I and J"), update a[i][j] to "a[i][0]+a[0][j". Similarly, when the K update, if the "a[i][j] distance" > "a[i][k]+a[k][j]", then update a[i][j] to "a[i][k]+a[k][j." After the update n times, the operation is complete!

Simply looking at the above theory may be more difficult to understand, the following examples to illustrate the algorithm.

Diagram of Freud's algorithm

The above figure G4 is an example to illustrate the algorithm of Freud.

Initial state : S is the matrix that records the shortest path between each vertex.
1th Step : Initialize S.

The distance of the vertex a[i][j] in the matrix S is the weighted value of vertex i to vertex j; If I and J are not adjacent, then a[i][j]=∞. In fact, the original matrix of the graph is copied into S.

Note: A[i][j] represents the distance between vertex i (vertex i) and Vertex J (j Vertex) in matrix S.

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.