--Floyd graph theory algorithms move to expand its regulatory nature

A, Floyd algorithm nature

  First, Floyd algorithm:

  Floyd-Warshall algorithm having a positive or negative edge weight (but not a negative period) of the shortest path algorithm to find weighting FIG. Single execution of the algorithm to find the shortest path length between all pairs of vertices (weighted).

  Popular point, the Floyd algorithm that may be used for multi-source shortest path sinks, i.e. find the shortest path in FIG communication between any two points, of course, if none, it returns it is infinite (initialized to infinity). Floyd can handle negative weights, but can not handle negative right ring of FIG.

  Next into the topic:

  As we all know, in fact, it is the essence of dynamic programming algorithm Floyd. It actually comes from the DP to optimize three-dimensional array.

  We use an array dis [i, j, k] indicates the point i to point j, k points previously as a transit point in the shortest path length.

  In order to achieve the state transition, we divide the set current dis [i, j, k] for all states is two, one is through the k-th point, one is not through k-th point. For the former, clearly dis [i, j, k] = dis [i, j, k-1]; for the latter, we can obtain a dis [i, j, k] = dis [i, k, k-1] + dis [k, j, k-1], that is, the shortest path length k to i plus k j is the length of the shortest path. So we will be able to get the state transition equation:

  December [i, j, k] = min (DIS [i, j, k-1], disincentives [i, j, k-1] + December [k, j, k-1]

  Boundary conditions: dis [i, j, 0] = w [i, j], i.e., weight of the side directly between i and j, if there was a positive infinity; and dis [i, i, 0] = 0.

  code show as below:

void floyd_original() {
    for(int k=1;k<=n;k++) {
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                dis[i][j][k]=min(dis[i][j][k-1],dis[i][k][k-1]+dis[k][j][k-1]);
            }
        }
    }
}

  Analogy to optimize the way in front of knapsack problem, we found that for each layer k, its state is calculated only with the state of the first k-1 layer about, then we can omit this dimension. After omitted because, when calculating the k-th layer dis [i, j], we need to dis [i, k] and dis [k, j] or the upper hierarchy.

  This can be proved by a formula:

  December [i, j, k]  = min (, disincentives [i, j, k-1] December [i, j, k-1] + December [k, k, k-1])  = mean of the (dis [i , k, k-1], d [i, j, k-1] + 0)  = d [i, j, k-1]

  dis [k, j] Similarly to proved.

  So we can get the most common two-dimensional array of state transition equation:

  December [i, j] = min (DIS [i, j], disincentives [i, k] + December [k, j])

  From the three-dimensional space becomes a two-dimensional does reduce overhead, but we can also find the time complexity is unchanged, is still O (n³) .

 

Two, Floyd deformation algorithm to solve the shortest path problem has limited the number of sides

  We d [i, j, e] represented by three-dimensional array point i to point j, e shortest path length through the edges.

  We assume that after the penultimate point is k, then we can easily get the state transition equation:

  d [i, j, e] = min {d [i, k, e-1] + x [k, j]} k∈ [1, n]

  code show as below:

 

for(int e=1;e<=n;e++)
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                d[i][j][e]=min(d[i][j][e],d[i,k,e-1]+w[k][j]);

 

  But the time complexity of this process up to O (the n- 4 ) , so we naturally think of to do some optimization.

  We have to achieve significant optimization results exponentially, we have chosen the binary optimization.

  It is assumed to limit the number of edges when s, we have a third dimension expressed as e 2 e edges. So we only need to 2:00 pretreatment E <e k all eligible enumeration can be completed. Then it can be expressed by s integer power of 2 and a number.

  We then array f [i, j, t] to indicate the status where t t represents a number of sides for the first integer and a power of 2, then we can get the state transition equation:

  f [i, j, t] = min {f [i, k, t-1] + d [k, j, t]}

  Where k i is a point j of the shortest path to the intermediate passes, all of the edges in the path divided into a front 2 0 +2 . 1 + 2 + ... T. 1- strip and after 2 T bars.

  Then we simply enumerate the outermost layer of k can be. The time complexity of this algorithm can be reduced to O (n- . 3 logN) .

   We are interested in this question , then you can take a look this question: POJ 3613

Guess you like

Origin www.cnblogs.com/ninedream/p/11203285.html