The shortest path is generally a connected graph with a valid value based on the network graph. If the value is not set to 1, the shortest path can be calculated based on the weighted value.
The shortest path refers to the path with the least sum of edge weights between two vertices. The first Vertex on the path is the Source Vertex, And the last vertex is the destination vertex.
Two Algorithms for solving the shortest path. The dijela algorithm and the Freud algorithm.
1. dijela Dijkstra) Algorithm
Used to find the shortest path from a vertex to all other vertices.
Algorithm Introduction
Calculate the shortest path from V0 to other vertices.
1) initialization, P = {V0}, distance from D0V0 to V0) = 0; Di indicates the distance from V0 to Vi vertex, Di = di0, I is not equal to 0, indicates that if the Vi vertex is not directly connected to V0, Di = infinity.
2) Find the next target vertex, that is, in Di (I does not belong to P), select a vertex j, which is the smallest of Dj, then the selected vertex Vj is placed in P, the path of Vj is the shortest path.
3) Update Di (I does not belong to P), Di = min [old Di, Before update), Dj + dji], update all I (not belongs to P ), return to step 2. Store all vertices directly to P.
# Define MAXVEX 9 # define INFINITY 65535 typedef int Pathmatirx [MAXVEX]; typedef int character pathtable [MAXVEX]; void shortestpaht_character stramgraph G, int V0, Pathmatirx * P, #pathtable * D) {int v, w, k, min; int final [MAXVEX]; /* final [w] = 1 indicates obtaining the shortest path from vertex V0 to Vw */for (v = 0; v <G. numVertexes; v ++) {final [v] = 0; (* D) [v] = G. matirx [V0] [v]; (* P) [v] = 0 ;}( * D) [V0] = 0; /* The v0 to v0 path is 0 */final [V0] = 1;/* the path from v0 to v0 does not need to be required * // * Start the main loop, and each time the V0 is obtained to a certain top The shortest path of point V */for (v = 1; v <G. numVertexes; v ++) {min = INFINITY;/* initialize the shortest path */for (w = 0; w <G. numVertexes; w ++)/* Find the vertex closest to V0 */{if (! Final [w] & (* D) [w] <min) {k = w; min = (* D) [w]; /* w vertices are closer to v0 vertices */} final [k] = 1;/* set the closest vertex found to 1 */for (w = 0; w <G. numVertexes; w ++)/* modify the current Shortest Path and distance */{/* if the path passing through the V vertex is shorter than the current path */if (! Final [w] & (min + G. matirx [k] [w] <(* D) [w]) {/* indicates that a shorter path is found, modify D [w] and P [w] */(* D) [w] = min + G. matirx [k] [w];/* modify the current path length */(* P) [w] = k ;}}}}
2. Freudian Floyd) Algorithm
Used to solve the shortest path from any vertex to any vertex.
Is an algorithm used to find the shortest path between vertices in a given weighted graph.
Core Ideas:
The shortest path between each two points is obtained through the weight matrix of a graph.
1) starting from any unilateral path, the distance between all two points is the edge right. If the two points are not connected, the right is infinite.
2) for each point U and V, check whether a vertex W exists so that the path from U to W and then to V is shorter than the known path, if it is updated.
The graph is represented by the adjacent matrix G. If there is a path reachable from Vi to Vj, G [I, j] = d, d represents the length of the path; otherwise, G [I, j] = infinity.
Define a matrix D to record the information of the inserted vertex. D [I, j] indicates the vertex that needs to pass through from Vi to Vj, and initialize D [I, j] = j.
Insert each vertex into the graph and compare the distance between the inserted vertex and the original distance G [I, j] = min (G [I, j], G [I, k] + G [k, j]);
If the value of G [I, j] decreases, D [I, j] = k.
G contains the length of the shortest path between two points, and D contains the specific path of the shortest path.
# Include <stdio. h> # define MAXVEX 100 typedef int Pathmatirx [MAXVEX] [MAXVEX]; typedef int ShorPathTable [MAXVEX] [MAXVEX];/* Floyd algorithm, find the Shortest Path P [v] [w] And D [v] [w] */void ShortestPath_Floyd (MGraph G, pathmatirx * P, ShortPathTable * D) {int v, w, k; for (v = 0; v <G. numVertexes; ++ v)/* initialize D and P */{for (w = 0; w <G. numVertexes; ++ w)/* D [v] [w] values are the weights between corresponding points */{(* D) [v] [w] = G. matirx [v] [w]; (* P) [v] [w] = w;/* initialize P */} for (k = 0; k <G. numVertexes; ++ k) {for (v = 0; v <G. numVertexes; ++ v) {for (w = 0; w <G. numVertexes; ++ w)/* if the path passing through the subscript k vertex is shorter than the path between the original two points */{if (* D) [v] [w]> (* D) [v] [k] + (D) [k] [w]) {(* D) [v] [w] = (* D) [v] [k] + (* D) [k] [w]; (* P) [v] [w] = (* P) [v] [k] ;}}}
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1293459