Study Floyd and Dijkstra

Floyd:

Problem: Floyd algorithm for solving the shortest distance at each vertex map

 

 

Resolution:

No more than 2 possible from any arbitrary node i to node j is the shortest path, directly from the 1 i to j, 2 i from the nodes via a plurality of j to k. Therefore, the algorithm assumes Dis (i, j) node u to the shortest path node v distances, for each node k, the algorithm checks Dis (i, k) + Dis (k, j) <Dis (i, j) is satisfied, if established, proven short then from i to j path than k i to j path directly, provided they Dis (i, j) = Dis (i, k) + Dis (k, j), so that a to, when finished traversing all nodes k, Dis (i, j) is recorded in the shortest path from i to j,

 

Core code:

for(u = 0; u < G.vexnum; ++ u)
    for(v = 0; v < G.vexnum; ++ v)
        for(w = 0; w < G.vexnum; ++ w)
            if(D[v][u] + D[u][w] < D[v][w])// 从v经u到w的一条路径更短
                D[v][w] = D[v][u] + D[u][w];
View Code

 

 

Dijkstra:

Problem: Find the Dijkstra's algorithm using a shortest path from the vertex of the vertex b

 

Resolution:

① constantly running breadth-first algorithm to find a visible point, calculate visible point to source point distance length

② From currently known shortest path to its apex S is added as a vertex to find the shortest path is determined.

 

a

b

c

d

e

f

g

h

A

0

1

2

B

0

2

C

2

0

D

1

0

8

E

2

0

2

F

2

0

G

3

0

3

h

2

0

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhhhb/p/12403899.html