# Data structure and algorithm: algorithm analysis

Source: Internet
Author: User
Tags python list

Catalogue

`Asymptotic analysis of common functions in experimental research`

I. Experimental studies (Experimental studies)

1. Run-time measurement

• Clock time Time.time ()
• CPU Time Time.clock ()
• Base Time Timeit.timeit ()

When executing the algorithm, we can study the run time by changing the size of the input size and the time it takes to record.

Use the time function in Python to record the runtime of the algorithm:

`From time import timestart_time=time () run Algorithmend_time=time () Elapsed=end_time-start_time #运行时间`

Because many processes use the computer's CPU, the run time will also depend on other processes running on the computer when testing. So a more fair

The metric for calculating CPU cycles is to use the Time.clock () function. But even with this method, the same algorithm is executed on the same computer,

The test results may also vary, depending on the computer's system, so a better approach in Python is to use the Timeit module.

2. Challenges/difficulties in experimental research

• The running time of the two algorithms in the experiment is difficult to compare directly unless the experiment is performed in the same hardware and software environment.
• Only limited input size can be achieved in the experiment, so some important inputs may be omitted.
• An algorithm must be fully executed in order to study its experimental run time (the most serious disadvantage of the experimental study)

3. Beyond experimental Analysis

To analyze the run time of an algorithm without performing an experiment, we direct a high-level description of an algorithm (which can be an actual code fragment or a

Language-independent pseudo-code (PSEUDO-CODE)) for analysis. We define a series of basic operations:

assigning an identifier to an object
determining the object associated with an identifier
Performing an arithmetic operation (for example, adding, numbers)
Comparing numbers
accessing a single element of a Python list by index
calling a function (excluding operations executed within the function)
returning from a function

Instead of determining the exact time of each basic operation, we simply record the number of executions per basic operation and use T to measure the algorithm's execution time.

4. Worst case analysis and best case analysis

Worst case analysis is much easier than average analysis, it only needs to identify the worst case input, which is easier for us.

Two. Common functions

1.The Constant function-constant function

F (N) =c

2.The logarithm function-logarithmic function

F (n) =logbn, n>1

Typically, the cardinality is ignored: logn=log2n

Nature: For any real a>0,b>1,c>0,d>1,

3.The Linear function-linear function

F (N) =n

4.The N-log-n Function

F (N) =nlogn

F (N) =n2

6.The Cubic function and other polynomials-cubic functions and other polynomial

Cubic function: F (n) =n3

Polynomial:

7.The exponential function-exponential function

F (N) =bn

Three. Asymptotic analysis

1. Asymptotic notation

01--Big O Mark

definition --o (g (n)) ={f (n): There are normal numbers C and n0, so that for all n≥n0, there is 0≤f (n) ≤c*g (n)}

Understanding --o (g (n)) is a set of functions, and F (n) is a function that satisfies a condition.

The large O notation gives an upper bound of a function within a constant factor.

02--Large Omega Mark

definition --ω (g (n)) ={f (n): There are normal numbers C and n0, so that for all n≥n0, there is 0≤c*g (n) ≤f (n)}

understand --here, the big Omega notation gives a lower bound to a function.

03--large θ Mark

definition --θ (g (n)) ={f (n): There are normal numbers c1,c2 and n0, so that for all n≥n0, there is 0≤c1*g (n) ≤f (n) ≤c2*g (n)}

Understanding -for any function f (n), if there is a normal number c1,c2, when n is sufficiently large, f (n) can be sandwiched between C1*g (n) and C2*g (n)

, the legend of three symbols:

2. Example Analysis

01--analysis of the maximum-finding algorithm

Find the maximum value code as follows

`def find_max (data):    biggest = data for    val in data:        if val > biggest:            biggest = val     `

Analysis -what we are analyzing here is the number of times the biggest is assigned:

In the worst case, if data is ordered in ascending order, biggest needs to be assigned n-1 times, and the assignment statement runs at O (n)

But if the data is arranged randomly. In the loop, biggest is only assigned when Val>biggest. If the sequence is in random order,

When looping to the J element, the first J element is the maximum probability of 1/j in the first J element, so the number of times the biggest is assigned is =1+1/2+1/3...+1/n.

That

According to the following theorem, it can be learned that biggest assignment statement run time is O (logn)

02--three-way Set disjointness

There are 3 sequence a,b,c, assuming that there are no duplicate elements in each sequence, but the same elements are in the three sequence.

The DISJOINT1 function is used to determine whether the three sequence contains an element x in both A and B and C.

`def disjoint1 (a,b,c): For    A in a: for        B in B: For C in            C:                if a==b==c:                    return False    return True`

Analysis -it is easy to see from the code that----worst case, the algorithm runs at O (N3)

Enhanced version: Disjoint2 function as follows

`def disjoint2 (a,b,c): For    A in a: for        B in B:            if a==b: for                C in C:                    if a==c:                        return False    retur N True`

Analysis -in the worst case, the algorithm runs at O (N2). In the worst case, each element in a is the same as in B,

The loop in C executes n times, so the loop run time in A and C is O (n) and the cycle time in B is O (N2). So the worst

case, the algorithm has an O (N2) run time.

03--element Uniqueness

The problem with the previous example is that each element in the collection is a unique issue.

The first workaround is to use a nested loop with an outer index of J and an inner index of k,k to iterate through the array starting with j+1. The code is as follows:

`def unique1 (s): for    J in Range (Len (s)): for        K in range (J+1,len (s)):            if s[j]==s[k]:                return False    Return True`

Analysis -When the outer loop is executed for the first time, the inner loop executes n-1 times, and the second time the outer loop is executed, the inner loop executes n-2 times,

so worst case, inner loop Total Execution (n-1) + (n-2) +...+2+1=n (n-1)/2, run time O (n2)

The second solution is to sort the sequence before comparing it with the following code:

`def unique2 (s):    temp=sorted (s) for    J in range (1,len (s)):        if S[J-1]==S[J]:            return False    return True`

Analysis -the sequence is ordered using the Python built-in function sorted () in the code, and the sorting algorithm ensures that the worst run time is O (Nlogn).

When the sort is complete, the loop sequence runs at O (n). So the worst run time for this algorithm is O (NLOGN)

Data structures and algorithmin python are very thin.

The initial algorithm analysis is read, progress so slow, only vacation to gather

NOTE: Reprint please indicate the source

Data structure and algorithm: algorithm analysis

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.