Algorithm design and Analysis Course review notes (1)

Source: Internet
Author: User
Tags greatest common divisor

First, the computational model

1.1 Definition:

We are thinking and dealing with algorithms that are machine-independent and language-neutral. All algorithms run on an "abstract machine", which is the computational model. 

1.2 Kinds

Turing is the most famous computational model, and this lesson uses a simpler and more appropriate RAM calculation model.

1.3 RAM (Random Access machine) model

The basic composition of the RAM model is as follows:

The RAM calculation model has the following features:

    • A simple operation takes one step: key-value comparison, plus-minus, memory-access
    • No action can be decomposed: loops, subroutines
    • Memory: Access is a simple operation, unlimited memory
Second, the algorithm design

2.1 Algorithmic Problem Specification

The algorithm problem is strictly defined as the "Protocol" form of the exact qualification input \ Output:

    • Input: Explicitly defines all legitimate inputs accepted by the algorithm
    • Output: clearly defines what the corresponding output value should be for each valid input value

Example 1:

Euclid algorithm, the algorithm of the Euclidean method, calculates the greatest common divisor of M and N

Input: Non-negative integer m,n

Output: gcd (m,n)

intEuclid (intMintN) {if(M <=N) swap (m,n);  while(m%n! =0) {n= m%N; M=N; }    returnN;}//This is a test caseintMain () {cout<< Euclid ( the, A) <<Endl; System ("Pause"); return 0;}
View Code

Example 2:

Sequential search, searching for a specific number in an array

Input: keyword K, array E[1...N]

Output: If k is in E, returns the position of K in E, or 1 if not

intSequentialsearch (vector<int> E,intk) { for(inti =0; I < e.size (); ++i)if(k = =E[i])returni; return-1;}//This is a test caseintmain () {vector<int> e = {1,2,3,4,5,6}; cout<< Sequentialsearch (E,8) <<Endl; System ("Pause"); return 0;}
View Code

2.2 Proof of the correctness of the algorithm: Mathematical inductive method

Proof of correctness of the Euclid algorithm:

When n=0, for any m, there is Euclid (m,0) =0

Assuming that when N<=N is established, consider the situation of n=n+1:

first Euclid (M, n+ 1) = Euclid (n+1, M mod (n+ 1)) , and M mod (n+1) <=n, based on assumptions c9>Euclid (n+1, M mod (n+ 1)) always comes up with the right answer, that is, the n=n+1 proof.

Three, algorithm analysis

Performance indicators for the 3.1 algorithm

    • Complexity of Time
    • Complexity of space

In RAM, the time complexity is measured by the number of simple operations performed in RAM, and the spatial complexity is measured using the number of registers in RAM. Thus, the performance analysis of the algorithm becomes a counting problem. Because RAM is abstract, our metrics are machine-independent and language-independent.

3.2 Worst case complexity

Worst-case complexity means the worst input for the complexity

Example:

In the sequential search, the number of k to be searched in the position of the array e, the more the number of times required to search, when k in the last of E, at this time the worst case.

3.3 Average degree of complexity

The worst-case time complexity alone does not adequately represent the performance of the algorithm. The complexity of the average case can be used.

    • Clear the distribution of all the input of the algorithm
    • Calculate expectations

Example:

For the problem of sequential search, the given array is e[], the length is n, the search target is k, assuming all the input conditions such as the occurrence of probabilities. The average probability of success should be as follows:

Algorithm design and Analysis Course review notes (1)

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.