Note algorithm

(Python) algorithm:

1. The introduction of the concept:

如果a+b+c=1000,且a^2+b^2=c^2(a,b,c为自然数),如何求出a,b,c的组合?

Single 举法:

The workload is too large, a waste of time

for a in range(0,1001):
    for b in range(0,1001):
        c = 1000-a-b
        if a**2+b**2=c**2:
            print('a,b,c:%d,%d,%d'%(a,b,c)
#T(n)=n*n*(1+max(1,0))=n^2*2=O(n^2)
#或者:
for a in range(0,1001):
     for b in range(0,1001):
           for c in range (0,1001):
                if a+b+c=1000 and a**2+b**2=c**2:
                    print('a,b,c:%d,%d,%d'%(a,b,c)
#时间复杂度:T=1000*1000*1000*2
#T(n)=n**3*2
#T(n)=k* (n**a)函数的走势决定力量是n**a,而k是决定函数的陡峭,决定不了函数的走向
#渐进函数:T(n)=g(n)=n**a,g(n)大O表示法,忽略系数     

algorithm:

Problem-solving ideas or methods, the nature of computer processing of information, because the computer is essentially an algorithm exact steps Levin high-speed computer to perform a specific task, in general, when the computer processing of information, will be from the input device or data storing the address data is read, the result written to the output device, or a memory address for later recall.

One algorithm is an independent solution to the problem and ideas

Implementation language is not important, important is the idea

Five features algorithm:

  • Input : Algorithm particularly zero or more output
  • Output : output at least one algorithm or a plurality of output
  • Finite nature : algorithm will automatically end after a finite number of steps without infinite loop, and each step can be completed within the time reception
  • Uncertainty : Algorithms Each step has a definite meaning, does not appear ambiguous
  • Feasibility : Each step of the algorithm is executable, which means that every step enough for a limited number of executions completed

Efficiency of the algorithm to measure:

Reaction time algorithm execution efficiency is not absolutely credible.

time complexity:

Time complexity is the same problem can be solved different algorithms, and a quality of the merits of the algorithm will affect the efficiency of the algorithm to the program. (Number perform basic arithmetic, the execution frequency of each line of code)

In computer science, leave complexity of the algorithm is a function of his qualitative descriptions of the running time of the algorithm, which is a function of the length of the input string representing the value of the algorithm. Commonly used for large time complexity O symbolic representation, does not include low-order term coefficient and the first function. when used in this manner, the time complexity can be gradual, if he considers the case when the input value of the size approaches infinity.

Complexity of the algorithm:

  • Time complexity: refers to computational effort (basic) required for execution of the algorithm
  • Space complexity: refers to the implementation of this algorithm memory space needed.

Big O notation:

Used to describe the asymptotic behavior of the function of mathematical symbols. Rather, it is the other (typically simpler) function to describe a function of magnitude bound on the asymptotic. In mathematics, it is generally used to characterize the truncated infinite series , especially the remaining term of the asymptotic series; in computer science, it is very useful in the analysis of algorithm complexity.

The worst time complexity:

How many basic operation algorithm requires a minimum amount of work completed, that is the optimal time complexity.

How many basic arithmetic operations take up work completed, that is the worst time complexity.

How many basic arithmetic operations to complete work on average, that the average time complexity.

  • For optimal time complexity, not its value, does not provide any useful information, the ideal state reaction, no reference value.
  • For the worst case time complexity, to provide a guarantee, show that the algorithm in the basic operation on a certain extent this can be done.
  • For the average time complexity, is a comprehensive evaluation algorithm, complete comprehensive response to the nature of the algorithm, on the other hand, this trade-off is no guarantee that not every computer can be completed within this basic operation, for the average case computing, also because the application instance may not be uniformly distributed algorithm is difficult to calculate.
  • Therefore, we only focus on the worst-case algorithm, that is the worst time complexity.

Time complexity of computing a few basic rules:

  1. Basic operation: that is, only the constant term, that the time complexity of O (1)
  2. Sequence Structure: time complexity calculated by adding
  3. Loop structure: the time complexity is calculated by multiplying
  4. Condition structure: take the maximum time complexity
  5. When determining the efficiency of an algorithm, often only need to focus on operating the number of items the highest level, and secondly to term and the constant term can be ignored.
  6. In the absence of special instructions, we analyzed the algorithm time complexity is the worst time complexity.

Performance analysis of Python built-in types:

timeitModules:

timeitModule can be used to test the short pythoncode execution speed.

  • class timeit.Timer(stmt = 'pass',setup='pass',timer=<timer function>)
  • TimerSmall pieces of code execution speed is a measure of the class
  • stmtParameter code to test the statement, noted that the type is a string, the function name () placed directly passed in quotes
  • setupParameters are settings required to run code
  • setup parameter set is required when running the code
  • timerParameter is a timer function, regardless of the platform.
  • timeit Timer.timeit(number = 1000000)

The method of execution speed of the object in the test class statement Timer parameter is the number of tests .number test code, default 1,000,000. The method returns the average time executing code, a type of a float seconds.

The list of operational test:

import timeit 
#生成列表的四种方式:
#列表的加法生成新的列表
li1 = [1,2]
li2 = [3,4]
li = li2+li1


li = [i for i in range(10000)]#列表生成器


li = list(range(10000))#把可迭代对象直接生成列表

#空列表追加形式生成列表
li = []
for i in range(10000):
    li.append(i)
from timeit import Timer 

def test1():
    li = []
    for i in range(10000):
        li.append(i)

def test2():
    li = []
    for i in range(10000):
        li +=[i]
        
def test3():
    li = [i for i in range(10000)]
    
def test4():
    li = list(range(10000))
    
def test5():
    li = []
    for i in range(10000):
        li.extend([i]) #列表或可迭代对象
        
 def test6():
    li = []
    for i in range(10000):
        li.insert(0,i)
        
        
#注意不能直接把函数名直接传进来,是一个字符串的形式
t1  = Timer('test1()','from __main__ import test1')
print('append',t1.timeit(number = 1000),'seconds')
t2 = Timer('test2()','from __main__ import test2')
print('+',t2.timeit(number = 1000),'seconds')  
t3 = Timer('test3()','from __main__ import test3')
print('列表生成器',t3.timeit(number = 1000),'seconds') 
t4 = Timer('test4()','from __main__ import test4')
print('可迭代对象直接生成列表',t4.timeit(number = 1000),'seconds') 
t5 = Timer('test5()', 'from __main__ import test5')
print('列表extend:', t5.timeit(number=1000), 'seconds')
t6 = Timer('test6()', 'from __main__ import test6')
print('列表insert:', t6.timeit(number=1000), 'seconds')


#由结果可以看出:可迭代对象生成新列表时间最短,其次是列表生成器的时间,再次就是加法生成新列表的时间,最后是追加形式的时间最长,insert要比append慢的太多,extend是对列表或者可迭代对象的插入

1565249384561

from timeit import Timer
x = list(range(2000000))

pop_zero = Timer('x.pop(0)','from __main__ import x')
print('pop_zero耗时:',pop_zero.timeit(number=1000),'seconds')


y = list(range(2000000))

pop_end = Timer('y.pop()','from __main__ import y')
print('pop_end耗时:',pop_end.timeit(number=1000),'seconds')

1565249877790

data structure:

  • DSA =Data Structure + Algorithm
  • 度量:To measure is to know,if you can not measure it,you can not improve it

Turing machine:

  • tape: uniformly dividing cells sequentially, each marked with a character, the default is '#'
  • alphabet: the type of character
  • head: always align a cell, wherein the characters can be read and rewritten, after a beat, turning left or right neighbor cell
  • state: TM always be in one of the finite states, each through a beat, steerable another state.

Transition Function:(q,c;d,L/R,p)

  • If the current state is p and the current character is C, then the current character into d, turn left or right side of the grid o, p-state transfer. Upon entering a particular state of the 'h', then it will stop.

Turing Machine Examples:

  • (<, 1,0, L, <) ---- left, 1-> 0
  • (<, 0,1, R,>) --- turn, 0-> 1
  • (<,#,1,R,>)---
  • (>,0,0,R,>)
  • (>,#,#,L,h)

Models RAM: Random Access Machine

A random access machine model is an important model for serial computation algorithm complexity in the theoretical analysis and calculation, simply referred to as RAM.

一个RAM有:
    k个变址器/1,/2,/3,....,/k.
    无穷多个普通寄存器R0,R1,R2,...,
    和一个有穷长的程序组成.
#变址器也是寄存器,每个寄存器中可以存放一个自然数,但只有变址器的内容可以作为间接地址.
RAM的程序使用两种形式地址:
    1.直接地址:形式/j(j=1,2,...,k)或Ri(i=0,1,2...)
    2.间接地址:/j(j=1,2,3,...,k)
        3.如果/j中的存自然数为i,则/j代表地址Ri.
RAM的指令形式解读:
    1.A<-a,表示将地址A的内容改为自然数a.
    2.A<-B,表示将地址A的内容改为地址B的内容.
    3.A<-B*C,表示把地址B中的内容和地址C的内容作运算*之后,送入地址A中.
    4.A<-F(B<C),此时F是一个可以用多带图灵机器在多项式空间和对数多项式的巡回中实现的变换,A,B,C可以是直接地址也可以是间接地址,A为写入地址,B,C是读出地址.
    RAM除了可以用以上的指令编程序外,还可以判断某个寄存器或变址器的内容是否为0,实现条件转移.
    IF R[i] = 0 GOTO 1
    IF R[i] > 0 GOTO 1
    GOTO 1   (绝对转向)
    STOP     (终止语句)

Analysis of Algorithms:

Its correctness and complexity

Complexity: unnecessary real basic instruction RAM algorithm to do the statistics accumulated number of executions.

The complexity of the method:
  • Iteration: Summation
  • Recursion: recursive recursive scheme tracking +
  • Guess + authentication
series:

Arithmetic progression: the square of the same order with the last item
$$
T (n-) =. 3. 1 + 2 + + \ + n-cdots = \ {n-FRAC (n-+. 1)} = {2} O (^ n-2)
$$

Series Power Sum:
$$
\ sum_ K = {0}} ^ {n-D} = {K ^ \ the int_ {0}} ^ {X ^ n-DDX = \ {FRAC. 1. 1} + {D} ^ {X }. 1 + D | _} {0} = ^ {n-\ FRAC. 1 {{}} n-D +. 1. 1 + ^} = {D (n-D ^ {+}. 1) O
$$

$$
T (n-). 1 ^ = ^ 2 + 2 + 2 + ... + 2. 3 ^ 2 = n-n-^ (n-+. 1) (2N +. 1) / = O. 6 (n-^. 3)
$$
convergence series, harmonic series, logarithmic progression

Example:

Take non-extreme elements

问题:给定整数子集S,|S|=n>=3,找出元素a 属于 S,a!=max(s)且a!=min(s)
算法:从s中任取三个元素{x,y,z}
//若s以数组形式给出,不妨取前三个
//由于S是集合,这三个元素必互异
确定排除最大最小者
//不妨设 x =max{x,y,z},y=min{x,y,z}
输出剩下的元素

#起泡排序
#问题:算法必然结束?至多迭代多少次
不变性:经过k轮扫描交换后,最大的元素必将就位
单调性:经过k轮交换后,问题的规模缩减至n-k.
正确性:经过至多n次扫描,算法必然会终止,且能给出正确答案
#封底估计:测量地球周长



1565344727653

Guess you like

Origin www.cnblogs.com/Zhao159461/p/11329092.html