Floyd algorithm (iii) Java 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.

2nd Step : Vertex a (1th vertex) as a mediation point, if A[I][J] > A[i][0]+a[0][j], then set A[I][J]=A[I][0]+A[0][J].

With vertex a[1]6, after the previous operation, the a[1][6]=∞, and a as the mediation Point, (B,a) =12, (a,g) = 14, so the distance between B and G can be updated to 26.

In turn, the vertex b,c,d,e,f,g is used as a mediation point, and the size of the A[I][J is updated.

Code description of the Freud algorithm

The "adjacency matrix" as an example of the Freud algorithm, for the "adjacency table" to achieve the diagram in the following will give the corresponding source code.

1. Basic definition

public class MATRIXUDG {
    
    private int medgnum;        Number of sides
    private char[] Mvexs;       Vertex set
    private int[][] Mmatrix;    Adjacency matrix
    private static final int INF = Integer.max_value;   Maximum value
    
    ...
}

The MATRIXUDG is the corresponding structure of the adjacency matrix. Mvexs is used to save vertices, medgnum is used to save the number of edges, and Mmatrix is a two-dimensional array for storing matrix information. For example, mmatrix[i][j]=1, which means "vertex I (i.e. mvexs[i])" and "Vertex J (i.e. Mvexs[j])" are adjacency points, and mmatrix[i][j]=0 means that they are not adjacency points.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.