"Algorithm refinement C language description"

Source: Internet
Author: User

The general method of algorithm design

Stochastic method relies on the statistical characteristics of random numbers. An example of applying a random method is a quick sort.

Divide and conquer the law

Dynamic planning

Greedy method

Approximate method --Traveling quotient problem

Pointer

In the C language, a void pointer is typically declared to represent a generic pointer.

Type conversion is important for generic pointers because the generic pointer can correctly fetch the value only if it tells the generic pointer what type to access the address. This is because the generic pointer does not tell the compiler what type of data it points to, so the compiler does not know how many bytes are to be accessed. And I don't know how to parse the bytes.

A function pointer is a pointer to an executable code snippet or an information block that invokes an executable code snippet.

For example, in the following section of code, match is declared as a function pointer that accepts arguments of two void pointer types and returns an integral type.

    1. int(*match)(void*key1,void*key2);

Suppose there is a match_int function whose two void pointer parameters point to an integral type and return 1. You can assign values like this:

    1. match = match_int;

Call:

    1. retval = match(&x,&y);
Recursive

When a recursive call is the last executed statement in the body of the function and its return value is not part of the expression, the recursive call is the tail recursion.

When the compiler detects that a function call is tail-recursive, it overwrites the current active record instead of creating a new one in the stack.

Example: a function implementation of factorial calculation in the form of tail recursion

  1. //facttail.c
  2. #include"facttail.h"
  3. int facttail(int n,int a)
  4. {
  5. if(n <0)
  6. return0;
  7. elseif(0== n)
  8. return1;
  9. elseif(1== n)
  10. return a;
  11. else
  12. return facttail( n -1, n * a);
  13. }

Calculate n!:

    1. facttail(n,1);
Hash table

Example: a hash function for working with strings

  1. //hashpjw.c
  2. #include"hashpjw.h"
  3. //hashpjw
  4. unsignedint hashpjw(constvoid*key)
  5. {
  6. constchar*ptr;
  7. unsignedint val;
  8. //hash the key by performing a number of bit operations on it
  9. val =0;
  10. ptr = key;
  11. while(*ptr !=‘\0‘)
  12. {
  13. unsignedint tmp;
  14. val =(val <<4)+(*ptr);
  15. if( tmp =(val &0xf0000000))
  16. {
  17. val = val ^(tmp >>24);
  18. val = val ^ tmp;
  19. }
  20. ptr++;
  21. }
  22. //In practice, replace PRIME_TBLSIZ with the actual table size.
  23. return val % PRIME_TBLSIZ;
  24. }
The balance of the tree

the balance of a tree is the process of ensuring that the height of the tree is as short as possible for a given number of nodes. This means that the node must be guaranteed to be in the next layer before the node is added. Formally, the tree is balanced if all the leaf nodes of the tree are satisfied on the same level, or if all the leaf nodes are on the last two levels, and the second-lowest level is full.

If all the leaf nodes in the last layer of a balanced tree are on the left-most position, the tree is said to be left-balanced .

An expression tree


Expression tree for expression (((74-10)/32x (23+17))

The prefix, infix, and suffix expressions are obtained by using the first order, the middle order, and the sequential traversal of the tree, and the operands and related operators are enclosed in parentheses.

Prefix-expression

(X (/(-74 10) 32) (+ 23 17)) = 80

Infix expression

(((74-10)/32x (23+17)) = 80

Suffix expression (inverse Polish expression)

((32/-) (17+) x) = 80

Use abstract stack machine to process suffix expressions (inverse Polish expressions)

Binary search Tree

To further understand the importance of maintaining the balance of a binary search tree, consider a situation where a binary search tree becomes increasingly unbalanced. If so, the complexity of finding a node becomes O (n), which is equivalent to retrieving all the nodes from beginning to end.

NP Complete problem

There is no known algorithm for solving polynomial time, but there is no proof that this polynomial does not exist, and such problems are called NP-complete problems .

LRU page substitution algorithm

The Second chance substitution method is a way to implement the LRU page permutation algorithm.

It works by maintaining a circular linked list of pages that currently exist in physical memory. To simplify the description, assume that each element in the list stores only one page number and one reference value, and that the reference value is either 1 or 0. All of the page initial reference values are set to 0. Each time the system accesses a page (for example, a process starts reading or writing a page), the page's reference value is set to 1.

When a page frame is needed, the operating system uses the circular linked list and reference values it maintains to determine which pages should release their page frames. To determine this, start traversing the list until you find an element with a reference value of 0. When traversing each page, the operating system resets the page's reference value back from 1 to 0. Once it encounters an element with a reference value of 0, it finds a page that has not been accessed by the system since the last time the list was traversed, so this is the least recently used page.

Collection Overrides merge sort

Merging sort requires additional storage space to run, which is also a drawback. However, the merge sort is very valuable for massive data processing because it separates the datasets as expected. This allows us to split the datasets into more manageable data, then use a merge sort to process the data and then merge the data continuously, without having to store all the data at once.

Polynomial interpolation

A method of finding the approximate value of a function. Where the function value is known only on a few points. The basis of this algorithm is to establish an interpolation polynomial pn (z) with a progression of less than or equal to N, where n+1 is the number of known function values.

Least squares

Given the function y (x) =b1x+b0, the estimator method used to determine the B1 and B0, so that Y (x) is the best fit line for the set of n points (x0,y0),..., (xn-1,yn-1). The best fit line uses least squares estimation to minimize each point (xi,yi), i=0,..., the sum of squares of the vertical distance between the n-1 and the corresponding point along Y (x) (Xi,y (xi)).

Equation solving

Using Newton Iterative Method

LZ77




Related data structures (one implementation)

12-bit lets the sliding window hold 4KB, in a 32-byte buffer, the duplicated bytes will not exceed 32 bytes, that is, the length is 5 bits, the next byte is saved in the "unmatched symbols" field.

The first bit, with 1 and a%, represents the difference in data representation when there are duplicates and no duplicates.

Travel business Issues

In a picture, each vertex is accessed once and finally the starting vertex is returned, and this access trajectory is called the Hamiltonian ring .

To solve the problem of traveling quotient, we need to use Figure g= (V,E) as the model to find the shortest Hamiltonian circle in the graph.

One way to approximate the travel quotient route is to use the nearest neighbor method . It is calculated as follows, which begins with a route that contains only the starting point, and the vertex is blacked out. The other vertices are white, and when the other vertices join the route, the corresponding vertices are blackened. Next, for each vertex that is not in the alignment, the weight is calculated for the edge between the vertex u and V that the route was last added to. Recall that the weight of the side between U and V in the travel quotient problem is the distance between U and v. This distance can be calculated using the coordinates of each vertex.



The approximate solution obtained

The actual optimization effect is:

Geometry algorithm to determine whether segments intersect convex hull

From for notes (Wiz)

"Algorithm refinement C language description"

Contact Us

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.