Introduction to computer science and programming (8) Complexity of Algorithms

Source: Internet
Author: User
1. Complexity Calculation Method Based on problem Scale

When considering time efficiency, there are two problems: Input scale and steps.

The input size is affected by many factors: the parameter size, parameter type (the access green of arrays and tuples is different), and the time for different operation steps (addition, subtraction, and judgment) is not the same, to facilitate computing, we need to establish the following assumptions:

Assume that the time for obtaining any variable from the computer is the same

Assuming that the basic operation time is constant

The following situations can be considered:

Best case: What input will minimize the program running time?

Worst case: What input will keep the program running for the longest time?

Average

If we consider the average condition, we need to imagine the distribution of input scale, which greatly increases the difficulty of calculation. Therefore, we usually consider the worst case, because the worst case can let us know the maximum running time and avoid unexpected occurrence.

2. instance analysis example 1: Calculate the power B of A. Method 1: conventional method
# Method 1: Calculate the power of B def exp1 (A, B): ANS = 1 # A while (B> 0) of a using the conventional method ): # 3B ans * = a B-= 1 return ans # once

Procedure: 1 + 3B + 1 = 2 + 3B

B = 300, number of executions: 902;

B = 3000, number of executions: 90002;

B = 30000, number of executions, 9000002;

It can be found that with the growth of B, the role of 2 and 3 is getting smaller and smaller, because B alone can reflect the upper limit of function growth. Therefore, we introduced the concept of Big O:

Big O: upper limit to growth of function as the input gets large.

Large O: the upper limit of the corresponding solution when the problem grows.

Therefore, the time complexity of method 1 is O (B ).

Method 2: Recursive Method
# Method 2: recursive method to obtain the power of a B def exp2 (A, B): If B = 1: # One return a else: return a * exp2 (A, B-1) #2 times, one multiplication, one Subtraction

Assume that the time is T (B). The following derivation can be performed:

T (B) = 3 + T (b-1)

= 3 + 3 + T (b-2)

=...

= 3 K + T (B-K)

Where, B-k = 1, substituted, because T (1) = 2, so, you can get

T (B) = 3b-1

Therefore, the time complexity of method 2 is still O (B)

Method 3: Judge Based on the parity of B

A ** B:

B is an even number: (A * A) ** (B/2). The scale is halved.

B is an odd number: A * (a ** (b-1 ))

 

#8.3 calculate the square root def exp3 (a, B) by classifier: If B = 1: return a if B % 2 = 0: Return exp3 (A * a, B/2) else: return a * exp3 (A, B-1)

 

The time complexity can be calculated simply:

B is an even number: T (B) = 5 + T (B/2)

B is odd: T (B) = 5 + T (b-1) = 10 + T (B-1/2)

That is to say, after two rounds, the scale is halved, so the time complexity is O (logb)

Example 2: the time complexity is O (n² ).

This example is very simple.

#8.4def g(n): x=0   for i in range(n):      for j in range(m):x += 1 return x
Example 3: hanlotta Problems

This article mainly introduces how to consider the hamrota issue from the perspective of recursion.

Solution:

The names of the three disks are fromstack, sparestack, and tostack)

If the number is 1, directly move from fromstack to tostack

If the number is N:

  1. Move n-1 disks to sparestack
  2. Move the nth disc to tostack
  3. Then, move n-1 disks to tostack.

Code:

#8.4 solve the hanlotta problem def towers (size, fromstack, tostack, sparestack) using recursive Methods: If size = 1: Print ('move disk from ', fromstack, 'to', tostack) else: towers (size-1, fromstack, sparestack, tostack) towers (1, fromstack, tostack, sparestack) towers (size-1, sparestack, tostack, fromstack)

The time complexity of this algorithm is O (n²)

 

 

Related Article

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.