# [Algorithm review 4] computing complexity and algorithm analysis-algorithm analysis

Source: Internet
Author: User

1. Generate functions and Recursion

Recursive relationship example

[Example 1] Hanoi: This is a famous issue in composite mathematics. N disks are mounted from the bottom to the column according to their radius, as shown in. Only one disk can be moved to Column B or column C at a time, and the tray cannot be placed above a small disk. If you want to move n disks on column A to column C, design a method and estimate the number of disks to be moved. Currently, only three columns A, B, and C are available.

|
|

|
|

A
B c

Step 1 move the N-1 in a to B (with C)

Step 2 move the oldest of A to C

Step 3: Move B to C (using)

Algorithm complexity:

H (n) indicates the number of transfers required for N plates.

H (n) = 2 H (n-1) + 1

H (1) = 1

Recursive Algorithm:

`# Include <iostream> using namespace STD; int num = 0; void move (int n, char X, char y) {num ++; cout <"move" <n <"from" <x <"to" <Y <Endl;} void hannoi (int n, char A, char B, char c) // move n plates from A to C Using B {If (n = 1) Move (1, a, c ); else {hannoi (n-1, A, C, B); // move n-1 plates from A to B using C move (n, a, c); hannoi (n-1, B, a, c); // move n-1 plates from B to C using a} int main () {cout <"The following is a three-layer tower solution: "<Endl; hannoi (7, 'A', 'B', 'C'); cout <" output is complete! Required Times: "<num <" times "<Endl; return 0 ;}`

[Example 2] troubleshooting. N ordered elements should have a different arrangement. If one arrangement makes all elements not in the original position, this arrangement is called the wrong arrangement; some are called the arrangement.

Take the, and numbers as examples to analyze the structure and find out the regularity.

1) The error line of 1 2 is unique, that is, 2 1.

2) The error lines of 1 2 3 are 3 1 2 2 3 1. The two can be regarded as 1
2 wrong rows, 3 and 1, respectively.

That is
2 1 32 and 3 transposition 3 1 2

2 1 3 1 and 3 2 3 1

3) Errors of 1, 2, 3, and 4

4 3 2 1 1 2 3 3 1 2,

3 4 1 2 3 4 2 2 4 1 3,

2 1 4 3 3 1 4 2 2 3 4 1.

The first column is the interchange position between 4 and 1, 2, and 3, respectively, and the other two elements are incorrectly arranged.

The second column is 4, which is obtained by interchange with each number of 3, 1, 2 (an error row of 123 ).

Analysis:

Set n numbers to 1, 2 ,..., N the number of the error row is DN, any one of them I, the number I and one of the other n-1 number interchange, the number of other N-2 error row, n-1) dn-2 error lines. Another part of the number of n-1 other than the number of I is in the wrong row, and then I swap with each of the numbers (n-1) Dn-1 an error row.

DN = (n-1) (Dn-1 + Dn-2) d1 = 0 D2 = 1

DN-nDn-1 = (-1) 'n'

2. Structure of Recursive Algorithms

Recursive structure definition

When designing a recursive algorithm, we usually write the recursive definition of the problem first. The recursive definition consists of the basic items and the inductive items.

• The basic item, that is, recursive exit. It describes the termination status of one or more recursive processes. The termination State refers to the state that directly solves the problem without the need to continue recursion.

• Inductive items, also known as recursive processes. It describes how to change from the current status to the terminated status.

`# Include <iostream> using namespace STD; int multiplication (int n) {If (n = 1) return 1; return N * multiplication (n-1);} int main () {cout <"10 factorial:" <multiplication (10) <Endl; return 0 ;}`

• Recursive insertion Sorting Algorithm

`# Include <iostream> using namespace STD; void in_rec (int A [], int N) // recursive insertion Sorting Algorithm {int A, K; If (n <1) return; in_rec (A, n-1); // induction item. Otherwise, sort the first n-1 elements. A = A [n]; // insert nth element into the proper position K = n-1; while (k> = 0) & (A [k]> )) {A [k + 1] = A [k]; k = k-1;} A [k + 1] = A;} int main () {int A [5] = {3, 2, 1, 5}; in_rec (A, 5); For (INT I = 0; I <5; ++ I) cout <A [I] <"; cout <Endl; return 0 ;}`

3. combined algorithm analysis

1) Full Sorting Algorithm

• Model correspondence

• Ordinal Method

• Lexicographic Method
• Intermediary Number Method

• Location change method

Example: 1, 2, 3, 4, 6, 7, 8, 9 in the Lexicographic Order.

Analysis: A string can be viewed as a string in a full arrangement. Strings can have prefixes and suffixes. The next one is that there is no other between this one and the next one. This requires that this one and the next one have the same prefix as long as possible, that is, the change is limited to the suffix as short as possible.

For example, 839647521 is a 1--9 arrangement. The first part of the sorting from 1 to 9 is 123456789, And the last part is 987654321. If the scanning from right to left is increased, it will reach 987654321, and there will be no next one. Otherwise, find the position where the first drop occurs.

Answer: 1. The longest descending sequence of the search end.
2. Remember that the number on the left side of the sequence is.
3. Search for the first number greater than a from right to left in the sequence as B.
4. Exchange A and B to reverse the new sequence after B is converted into the original sequence.

(Output in full order) algorithm:

`#include <stdio.h> int n = 0; void swap(int *a, int *b) { int m; m = *a; *a = *b; *b = m; } void perm(int list[], int k, int m) { int i; if(k > m) { for(i = 0; i <= m; i++) printf("%d ", list[i]); printf("\n"); n++; } else { for(i = k; i <= m; i++) { swap(&list[k], &list[i]); perm(list, k + 1, m); swap(&list[k], &list[i]); } } } int main() { int list[] = {1, 2, 3, 4, 5}; perm(list, 0, 4); printf("total:%d\n", n); return 0; } `

(Output) algorithm:

`# Include <iostream> using namespace STD; void swap (int * X, int * Y) {int temp; temp = * X; * x = * Y; * Y = temp;} void reverse (int * First, int * Last) {-- last; For (; first <last; first ++, last --) Swap (first, last);} bool nextpermutation (int * First, int * Last) {If (first = last) return false; // null if (first + 1 = last) return false; // only one element int * I = last; -- I; // The last element for (;) {int * II = I; // The last element -- I; // The first element if (* I <* II ){ // Before <and then adjacent int * j = last; while (! (* I <* -- J); // find the first swap (I, j) greater than I from the back and forth; // switch * I, * j reverse (II, last); // reverse arrangement [II, last) return true;} if (I = first) {// The previous element has pointed to the first element, reverse the entire interval. No reverse (first, last); Return false ;}}// output result 8 3 9 6 5 1 2 4 7 int main () {int d [] = {8, 3, 9, 6, 4, 7, 5, 2, 1}; If (nextpermutation (D, D + 9) {for (INT I = 0; I <9; I ++) cout <D [I] <""; cout <Endl;} return 0 ;}`

2) number of intermediaries

In the full arrangement of [1, N], nn-1...
321 is the last arrangement, and its intermediary number is (n-1, N-2,..., 3, 2, 1 ). And its serial number is
1*1! + 2*2! + ...... (N-1 )!

Calculates the serial number of a given arrangement.

The numbers of 8 3 9 6 4 7 5 2 1 are the numbers listed above. Will be prior to this arrangementBy prefix.

8 !, 7 !,..., 1! The coefficient above is extracted and put together to get 7 2 6 4 2 2 1.

7 2 6 4 2 3 2 1 is calculated and arranged 8 3
The intermediate link of the serial number of 9 6, 4, 7, 5, 2, 1 is called the number of intermediary.

※This is a useful concept.

[Example 12] An algorithm arranged by the number of intermediaries, for example, 7
2 6 4 2 3 2 1 (8 numbers) are estimated to be in full order: 8 3 9 6 4 7 5 2 1

Method 1: Derivation

P1 P2 P3 P4 P5 P6 P7 P8 P9

8 2 1

8 3 2 1

8 3 4 2 1

8 3 9 4 2 1

...... ...... ......

For more details, see blog
Http://blog.csdn.net/tianshuai11/article/details/7520370

Related Keywords:

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.