Reading notes-Introduction to Algorithms (Preface + Part I)

Source: Internet
Author: User

What is the basis? is to learn from our university's discrete mathematics, algorithms and data structures, operating systems, computer architecture, compiling principles and other courses. To the computer system, CPU itself, operating system kernel, system platform, object-oriented programming, the performance of the program to have a deep grasp. To write good code is also a solid foundation, if the data structure and algorithm is not good, how to optimize the performance of the program, how to choose the appropriate data structure from the class library. If you do not know the operating system, how to understand the principles of these development tools, they are based on the operating system. Do not understand the Assembly, the principle of compiling, how to know how long the program to run the amount of memory, you can not compile efficient code. The object-oriented, software engineering, design patterns of these basic learning to see these can be status quo. If their foundation is not solid, like walking in the foggy, can only see the front, can not see farther away. These fresh technologies mask many of the underlying principles,To really learn the technology or go down to the cloud, solid basic knowledge to learn, with these foundations, to master those new technologies is very easy。 Programming is like building, the foundation does not play well sooner or later one day will collapse, and the higher the cover, the greater the loss. These underlying knowledge and textbooks are neither useless nor profound to learn, but the foundation that we must master.   The first part of the basic knowledge   The 1th Chapter the function of the algorithm in the computation   1.1 AlgorithmThe so-called algorithm is a well-defined calculation process, which takes a value or a set of values as input, and produces a value or a set of values as output. Data StructureThis book contains several data structures. A data structure is a way to store and organize your information so that it can be accessed and modified. No data structure can be used for all purposes and purposes, so it is important to understand the strengths and limitations of several data structures. NP Complete ProblemAlthough no one can find an effective solution to the NP-complete problem so far, nobody can prove that the effective solution to the NP-complete problem does not exist. It is valuable to have an understanding of NP-complete problems, because some NP-complete problems sometimes have to be used in practical applications. For example, if you are asked to find an effective algorithm for a NP-complete problem, you are likely to spend a lot of time exploring it, and the result is futile. If you can prove that the problem is NP-complete, you can spend your time designing an effective algorithm that can give better, but not necessarily the best, possible results. Having solid algorithmic knowledge and technical fundamentals is an important feature of distinguishing between truly skilled programmers and novices. With modern computing, there is no need to know a lot about algorithmic aspects, or to accomplish some tasks. However, with a good algorithm base and background, you can do much more. 2nd. Getting Started with algorithms   2.1 Insert SortAn effective algorithm for ordering a small number of elements: working mechanism with many people playing cards, the practice of sorting hands is similar: when the card starts, the left hand is empty, face down on the table. Then one card is picked from the table and inserted into the right position of the left hand. At any time, the cards in the left hand are orderly. Sort in situ (sorted in place) the numbers are rearranged in the array.    Insertion-sortfor (j = 1; j< a.length; j + +) {var key = A[j];    Insert A[j] into the sorted sequence a[1:j-1] var i = j-1;        while (i > 0 and A[i] > key) {a[i+1] = A[i];    I--; } a[i+1] = key;}   2.2 Algorithm AnalysisInsert Sort-O (n^2) 2.3 Algorithm Design 2.3.1 Division and Treatment MethodThere are many algorithms that are structurally recursive: in order to solve a given problem, the algorithm can recursively call itself to solve the related sub-problems one or more times. These algorithms usually adopt the divide-and-conquer strategy: Divide the original problem into a sub-problem of n smaller size and similar structure to the original problem; solve these sub-problems recursively, then merge the results, then get the solution of the original problem. The divide-and-conquer model has three steps on each level of recursion:
    1. Decomposition (Divide): Decomposition of the original problem into a series of sub-problems;
    2. Solution (Conquer): Solve each sub-problem recursively. If the sub-problem is small enough, it is solved directly;
    3. Merge (Combine): Merges the results of a sub-problem into the solution of the original problem.
The merge sort algorithm completely follows the above pattern and operates intuitively:
    1. Decomposition: The n elements are divided into sub-sequences containing N/2 elements;
    2. Solution: The two sub-sequences are sorted recursively by the combined sorting method;
    3. Merge: Merges two sorted sub-sequences to get sorted results.
MERGE (a,p,q,r) {    var n1 = q-p+1;    var n2 = r-q;    var l[1:n1+1];    var r[1:n 2+1];    for (var i = 1; i < N1, i++)         L[i] = a[p+i-1];    for (Var j = 1; J < N2; J + +)         R[j] = a[q+j]    i = 1; j = 1;    for (var k = p; k < r; k + +)     {        if (L[i] <= r[j]) &nbs p;       {            A[k] = l[i];    & nbsp;       i = i+1;        }        else& nbsp;       {            A[k] = r[j];            j = j+1;        }    }} run Time is O (n) & nbsp; You can use the merge process as a subroutine in the merge sort: Merge-sort (A, p, r) {    if (P < r)     {        var q = (p+r)/2;        Merge-sort (A, p, q); &nbs P       merge-sort (A, q+1, R);        MERGE (A, p, Q, R);    }}& nbsp; run time is O (NLOGN)-reason-recursive tree    Although the worst-case run time for the merge sort is O (NLGN), the worst-case runtime for the insertion sort is O (n^2), but the constant factor in the insertion sort makes it run faster when n is smaller. Therefore, in the merge sort algorithm, when the sub-problem is enough, the insertion sort is more appropriate.   Study questions: 2-3 the correctness of Horner's rules? 2-4 in reverse order?   the 3rd chapter The growth of functionsO Mark:The 4th chapter recursive typeWhen an algorithm contains recursive calls to itself, its run time can usually be represented by recursion (recurrence). This chapter describes three ways to solve recursion:
    1. Substitution (Substitution method)
      1. First guess there's a certain realm
      2. Then the mathematical induction is used to prove the correctness of the speculation.
    2. Recursive tree approach (Recursion-tree method)
      1. To convert a recursive to a tree-shaped structure
      2. The nodes in the tree represent the price paid at different levels of recursion.
      3. Finally, we use the technique of the alignment gauge to solve the recursive
    3. Master Method: T (n) = at (n/b) + f (n)
      1. Where a>= 1, b >= 1, f (n) is the given function
4.1 substitution MethodIt takes two steps to solve the recursion using the substitution method: 1) Guess the form of the solution 2) make a good guess by using the mathematical induction method to find the constants that make the solution truly effective-unfortunately, there is no universal way to guess the correct solution of recursion. This kind of speculation requires experience, sometimes even creativity. Fortunately, there are some heuristics that can help make good guesses. (e.g. recursive tree) 4.2 Recursive Tree methodAlthough the substitution method provides a simple proof method for the correctness of the recursive solution, it is difficult to get a good guess at that time. Like we analyzed merge sort recursion in 2.3.2 section, drawing a recursive tree is a direct way to get good guesses. In a recursive tree, each node represents the cost of a recursive function calling a sub-problem in the collection. We add the cost of each layer in the tree to a set of cost per layer, and then add the cost of each layer to the total cost of recursion at all levels. Recursive tree method is especially useful when the running time of the split algorithm is expressed by recursion. Recursive trees are best used to generate good guesses, which are then validated by substitution methods. But when using recursive trees to produce good guesses, a small amount of "bad" (sloppiness) is usually tolerated, as the guesswork is later proven. 4.3 Main methodThe main method relies on the following theorems: Set A>=1 and B>1 as constants, set F (n) as a function, T (n) by recursive t (n) = at (n/b) + f (n) 5th Chapter Probabilistic Analysis and stochastic algorithms (Probabilistic analyses & randomized algorithm)Review the basic knowledge of probability theory: Appendix C count and Probability review elementary combinatorics and probability theory: C1 count (permutation combination) C2 (probability axiom and probability distribution) C3 (random variable, expected value and variance) C4 (Bernoulli experiment, geometric distribution and two-item distribution) C5 (two-item distribution and its tail characteristics) 5.1 Employment issues   Question: Hire a new Office Assistant decision: At any time, find the best person to hold the job. After interviewing each candidate, if the candidate is more qualified than the current Office Assistant, you will quit your current Office Assistant and hire the new candidate. Algorithm: Hire-assistant (n) {    var best = 0;    for (int i = 0; i < n.length-1; i++)     {         interview (N[i]);        if (N[i] better than N[best])    & nbsp;    {            best = i;        & nbsp   hire[i];        }    } In the employment problem, you can assume that the candidate appears in a random order. But in many cases, we know very little about the input distribution:  in the employment problem, it seems that the candidate appears in a random order, but we don't know if it's correct. Therefore, in order to design a random algorithm of employment problems, it is necessary to have more control over the order of interview candidates. So change the model a little bit: assuming the hiring agent has n candidates, and give us a list of candidates in advance, we randomly choose one of them to interview each day. Although we do not know anything about the candidate, we have made a significant change: We control the candidate's arrival process and strengthen the random order.   More generally, if an algorithm's behavior is determined not only by the input, but also by the number generated by the random number generator, the algorithm is called Random.  5.1.3:unbiasedrandom, Biasedrandomwhile (true) {    var x = biasedrandom;    var y = biasedrandom;    if (x! = y)     {        return x;    }}  5.2 Indicator Random variables1-> If an event occurs 0-> if the event does not occur use indicator random variables to analyze the employment problem lemma: Assuming that the candidate appears in a random order, the algorithm hire-assistant the total cost of employment is O (CLN (n)) 5.3 Stochastic algorithm  In the previous section, the understanding of how the distribution of inputs can help analyze the average behavior of an algorithm is explained. However, many times we are unable to get information about the input distribution, and it is not possible to have an average distribution.   In these cases, consider using a random algorithm: for problems such as employment, it is often useful to assume that all permutations of input are likely to be. In such a problem, we are not assuming a distribution of the input, but rather a distribution. In particular, before the algorithm is run, we randomly arrange the candidates to strengthen the feature that all permutations are possible.   for employment issues, the only place in the code that needs to be changed is the random arrangement of the candidate sequence: randomly permute the list of candidates  with this simple change, we have built a random algorithm, Its performance is consistent with the assumption that candidates appear in random order.   Random Array Many random algorithms make input randomization by arranging the given input array. (Are there other ways to use random??) Here we discuss two methods of randomization:  1. Assigns a random priority to each element of an array, and then sorts the elements in array A by Priority  permute-by-sorting (A) {    var n = a.length;    for (int i = 0; i < n-1; i++)     {        P[i] = Random (1,n^ 3);    }    Sort (a), using P as sort keys    return A; The third row takes a random number between 1 and n^3, so that all the priorities in P are as unique as possible. The time-consuming steps in this process are the sort in line fourth, and in the eighth chapter you will see that if you use a comparison-based sort, the sort will cost O (NLGN). This nether can be achieved because we already know that the time cost of the merge sort is O (NLGN)   In this way we get a sort, but it is still to be further proved that this process can produce uniform random permutations = = number 1 to n each permutation is likely to be generated.   One might have the idea of proving that an arrangement is uniformly random, as long as it proves that for each element the probability of its being in each position is 1/n sufficient. (Practice 5.3.4 proves that this weak condition actuallyis inadequate)  2. A better way to generate random permutations is to arrange the given sequence in place. The program is completed in O (n) time, and at the time of iteration, element A[i] is randomly selected from element A[i] to a[n].  randomize-in-place (A) {    var n = a.length;    for (int i = 0; i < n-1; i++)     {&NB sp;       Swap (A[i], A[random (i,n)]);    }    return A;   Stochastic algorithms are often the simplest and most effective way to solve problems.   5.4 Probability analysis and further use of indicator random variablesThis section contains more advanced content, with 4 examples to further explain probability analysis:
    1. Determine the probability of a two person having the same birthday in a room with a K-man
    2. Put the ball into the box at random
    3. A continuous positive condition in a coin toss
    4. Make a decision before interviewing all candidates (a variant of the employment problem)
5.4.1 Birthday paradox (to do) (birthday attack)-Cryptography  Randomly selected k individuals in n individuals, when K is how big can guarantee that two people in K person's birthday is the same? You may say that the answer is 366, because 365 days a year, according to the Pigeon Cage principle (pigeon hole principle), if there are 366 people, then two of them will be on the same day birthday but in fact, using a statistical method to consider the birthday problem, we will be surprised to find that The required number of K is far less than 366. As long as the k=70, randomly selected 70 people, two of them have the same birthday is 99.9%, the likelihood is very big! When k=100, less than 366 of one-third, this probability can reach 99.99997%, while in k=200, the probability is 100%! Ah! What a! Surprised! The good or bad of a hash function is whether it can make the output value as scattered as possible, reduce the output value of the collision (collision), so that two things can be avoided the same output. In terms of security, hash function must be complex enough to allow people to guess what kind of algorithm this hash function uses with a few simple input and output results. Said so much, in fact, the birthday attack is to write a good hash function the biggest stumbling block. If you think of each input value of the hash function as one of the n individuals, and then the output value to think of everyone's birthday, then the birthday problem tells us that only a small number of input values, there will be a large probability of at least two output value is identical, also violates the hash One of the conditions of function??? (Note that this is not a given output value and the input value to find another can get the same output value of the number of times required, but at random to try a few input values, can get the same output value in the total number of times) a bit further, you can put the hash function is considered as a mapping: the input value is the definition field and the output is the range. Note that hash function is not necessarily one by one mapped because the size of the domain and range of the definition is not necessarily the same. In most cases, for higher efficiency and operability, the value of the hash function will be much smaller than the domain defined. In such a range of ranges, collisions are inevitable. Can imagine the Easter eggs into the basket, all kinds of eggs can only be put into 5 numbered different baskets, there must be more than one egg in the same basket. Here the eggs are defined fields, the basket is the range. A birthday attack can work because the range is too small, like our birthday problem, with a range of only 365, so as long as 70 people will be able to find two people of the same birthday.   5.4.2 Ball and BoxThrow the same ball into the B box at random, each time the ball is independent. The pitching process is a set of Bernoulli tests, each with a probability of 1/b, where success refers to the ball falling into the specified box. This model is particularly useful for analyzing hashing techniques. 5.4.3 SequenceImagine you throw a uniform coin n times, how long do you expect to see the longest sequence of successive positives? The answer is O (LG (n))

Reading notes-Introduction to Algorithms (Preface + Part I)

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.