[7] algorithm path: algorithm path
Left axis, center axis, and right axis
Question:
Quick sort is currently one of the most recognized fastest sorting methods (depending on the problem-solving objects). Although quick sort can reach O (n2) in the worst case ), however, in most cases, the efficiency of quick sorting is quite good.
Quick sorting-Algorithm
1. The basic spirit of quick sorting is to find the appropriate axis in the series, and then split the series into two.
2. Sort the columns on the left and right respectively.
Left axis calculation:
// Quick sorting-Left axis calculation // 1. with two pointers left/right, and set the number of leftmost to the initial pivot vertex // 2. search from the right to left, find the first number smaller than the limit, and cache it to the pivot Position // 3. search from left to right, find the first number greater than the number of shards, and cache it into a location smaller than the number of shards. 2. loop // 4. the valid null position obtained after the last exchange is a [I], and then the value of I is assigned to a [I]. At this time, the number on the left of a [I] is smaller than that on a [I, the number on the right is greater than a [I] // sort int QuickSort2 (int a [], int left, int right) on the left and left respectively) {if (left <right) {int I = left, j = right, fig = a [left]; while (I <j) {while (I <j & a [j]> = javastloc) j --; if (I <j) a [I ++] = a [j]; while (I <j & a [I] <pivotloc) I ++; if (I <j) a [j --] = a [I];} a [I] = repeated tloc; QuickSort2 (a, left, I-1); QuickSort2 (a, I + 1, right);} return 0 ;}
int QuickSort5(int a[],int left,int right){if(left<right){int i = left,j=right,pivotloc=a[left];while(i < j){while(i<j && a[i]<=pivotloc) i++;while(i<j && a[j]>pivotloc) j--;if(i<j){SWAPER(a[i],a[j]);i++;j--;}}if(a[j]<a[left]) {SWAPER(a[left],a[j]);}QuickSort5(a,left,j-1);QuickSort5(a,j,right);}return 0;}
Central Axis Calculation
// Quick sorting int QuickSort3 (int a [], int left, int right) {int I, j, distinct; if (left <right) {cursor = a [(left + right)/2]; I = left-1; j = right + 1; while (I <j) {while (a [++ I] <signature); while (a [-- j]> signature); if (I <j) SWAPER (a [I], a [j]);} QuickSort3 (a, left, I-1); QuickSort3 (a, j + 1, right);} return 0 ;}
Right axis Calculation
// Quick sorting calculation // 1. select the rightmost element as the pivot character // 2. search from left to right, find the number smaller than limit, and switch it with the first unexchanged element on the left. // 3. when the rightmost result is found, the number of exchanges on the left side is smaller than that on the left side, and the number of unexchanged values is larger than that on the left side. // 4. swap the first unexchanged element on the left with the unexchanged element on the rightmost, and return the index of the unexchanged element. // at this time, the element on the left of I is smaller than that on the I, larger than I on the right. // enter the program. the loop recursively performs (fast sorting) on 0 → I-1 and the rightmost I + 1 → right respectively. Eventually, the sequence will become an ordered int Partition2 (int a [], int left, int right) {int I = left, j; int right = a [right]; for (j = left; j <right; j ++) {if (a [j] <strong) {if (I! = J) SWAPER (a [I], a [j]); I ++ ;}} if (I! = Right) SWAPER (a [I], a [right]); return I;} int QuickSort4 (int a [], int left, int right) {int comment; if (left <right) {partition = Partition2 (a, left, right); QuickSort4 (a, left, semi-1); QuickSort4 (a, semi + 1, right);} return 0 ;}
The shortest path from the seven starting points to the 15 ending points of the dijela Algorithm in MATLAB
Do you have knowledge about graph theory ~ W is the association matrix, and s and t are the sequence numbers of the start and end nodes respectively. The returned d is the shortest weighted path length, and p is the sequence number vector of the Optimal Path node. Note that the vertex weight of W matrix 0 is automatically set to infinity. For more information, see MATLAB for advanced mathematical problems. I will assign it to you.
You can use the M function.
Function [d, path] = dijkstra (W, s, t)
[N, m] = size (W); ix = (W = 0); W (ix) = inf;
If n ~ = M, error ('square W required'); end
Visited (1: n) = 0; dist (1: n) = inf; parent (1: n) = 0; dist (s) = 0; d = inf;
For I = 1 :( N-1), % to find the relationship between each node and the Starting Point
Ix = (visited = 0); vec (1: n) = inf; vec (ix) = dist (ix );
[A, u] = min (vec); visited (u) = 1;
For v = 1: n, if (W (u, v) + dist (u) <dist (v )),
Dist (v) = dist (u) + W (u, v); parent (v) = u;
End; end
If parent (t )~ = 0, path = t; d = dist (t); % trace the Shortest path
While t ~ = S, p = parent (t); path = [p path]; t = p; end;
End;
Hope to be useful to you
Pathfinding Algorithm Implementation
Using Dijkstra algorithm, I will give you a reference. The following points have a distance (moving force), and BIG indicates that it cannot pass
# Define BIG 32767 // infinity
Int pre [6] = {0}; // This is the key! Pre array is used to record the forward trend of each vertex. After the shortest path is calculated, the vertex passing through the path can be recursively obtained.
Int Distance [6]; // used to record the shortest path length from the start point to each vertex
Int Cost [6] [6] = {0, 1450, 1650, BIG}, // returns the ing Network's Adjacent matrix. Here we use six vertices as an example.
???????? {1450,0, BIG, 1350,2350, BIG },
???????? {1650, BIG, 0, BIG, 2550, BIG },
???????? {BIG, 1350, BIG, 0, BIG, 1200 },
???????? {BIG, 2350,2550, BIG, 0,850 },
???????? {BIG, 1200,850, 0 },
????????};
// Dijkstra algorithm function to find the shortest path from the given point to other points
// Parameter: adjacent matrix, number of vertices, subscript of the starting point, result array, and frontend of each vertex
Void Dijkstra (int Cost [] [6], int n, int v0, int Distance [], int pre [])
{
?? Int s [6];
?? Int mindis, dis;
?? Int I, j, u;
?? // Initialization
?? For (I = 0; I <n; I ++ ){
???? Distance [I] = Cost [v0] [I];
???? S [I] = 0;
??}
?? S [v0] = 1; // mark v0
?? // Find the vertex with the shortest distance from the vertex that has not yet found the Shortest Path
?? For (I = 0; I <n; I ++ ){
???? If (Distance [I] <BIG) pre [I] = v0;
??}
?? For (I = 1; I <n; I ++) {// obtain a shortest path for each loop
???? Mindis = BIG;
???? For (j = 0; j <n; j ++) // find the vertex closest to the starting point.
?????? If (s [j] = 0 & Distance [j] <mindis ){
???????? Mindis = Distance [j];
???????? U = j;
??????}
???? For (j = 0; j <n; j ++) // modify the incremental path sequence (SET)
?????? If (s [j] = 0) {// vertex of the unobtained Shortest Path
???????? // Obtain the distance from the nearest vertex to each vertex
???????? Dis = Distance [u] + Cost [u] [j];
???????? // If the new path is shorter, replace the original path.
???????? If (Distance [j]> dis ){
?????????? Distance [j] = dis;
?????????? Pre [j] = u;
????????}
??????}
???? S [u] = 1; // the shortest path has been obtained.
??}
}
After the Dijkstra function is used to calculate the shortest path, we can recursively export each vertex from the given vertex to the shortest path of each vertex. The function is as follows (... the remaining full text>