1、1 The Role of Algorithms in Computing,1 what are algorithms? 2 why is the study of algorithms worthwhile? 3 what is the role of algorithms?,An algorithm is said to be correct if, for every input instance, it halts with the correct output,The problem of sorting,Input: sequence of numbers. Output: per
2、mutation such that a 1 a 2 a n . Example: 解释什么是实例? Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9,Time Is Important,“Time is important for everybody, especially for the computer scientist who studies algorithms.”J. S. Yu,What kinds of problems are solved by algorithms?Levels of Hardness1. There is no method
3、 to solve the problem in finitely many steps. 2. Theoretically, there is an algorithm, but the running time increases too much with the size of input, even to the universe age(NPC problems). 3. Among the “ good“ algorithms, there is still a hierarchy of running time.,多项式算法:把渐近复杂性与规模N的幂同阶的这类算法称为多项式算法
4、。 指数型算法:把渐近复杂性与规模N的指数同阶的这类算法称为指数型算法。这两种算法在效率上有质的区别的内在原因是算法渐近复杂性的阶的区别。可见,算法的渐近复杂性的阶对于算法的效率有着决定性的意义。多项式算法是有效的算法。很多问题都有多项式算法。但也有一些问题还未找到多项式算法,只找到指数型算法。,Homework,1.1-3, 1.1-4,1.2 Algorithms as a technology,Suppose computers were infinitely fast and computer memory was free. Would you have any reason to
5、study algorithms? Compare the running time of insert sort and merge sort.,Why study algorithms and performance?, Algorithms help us to understand scalability. Performance often draws the line between what is feasible and what is impossible. Algorithmic mathematics provides a language for talking abo
6、ut program behavior., Performance is the currency of computing. The lessons of program performance generalize to other computing resources. Speed is fun!,2 Getting Started,Analysis of Algorithms Insertion Sort Merge Sort,The problem of sorting,Input: sequence of numbers. Output: permutation such tha
7、t a 1 a 2 a n . Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and
8、 (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,30,10,40,20,1,2,3,4,i = j = key = Aj = Aj+1 = ,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,30,10,40,20,1,2,3,4,i = 2 j = 1 key = 10 Aj = 30 Aj+1 = 10,An Example: I
9、nsertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2 j = 1 key = 10 Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = A
10、j j = j - 1 Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2 j = 1 key = 10 Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2 j = 0 key = 10 Aj = Aj+1 = 30,An Example: Insertion
11、Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,30,30,40,20,1,2,3,4,i = 2 j = 0 key = 10 Aj = Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1
12、Aj+1 = key ,10,30,40,20,1,2,3,4,i = 2 j = 0 key = 10 Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3 j = 0 key = 10 Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionS
13、ort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3 j = 0 key = 40 Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,
14、30,40,20,1,2,3,4,i = 3 j = 0 key = 40 Aj = Aj+1 = 10,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3 j = 2 key = 40 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) fo
15、r i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 3 j = 2 key = 40 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,
16、1,2,3,4,i = 3 j = 2 key = 40 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4 j = 2 key = 40 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i =
17、2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4 j = 2 key = 20 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,
18、4,i = 4 j = 2 key = 20 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4 j = 3 key = 20 Aj = 40 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n
19、 key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,20,1,2,3,4,i = 4 j = 3 key = 20 Aj = 40 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,40,1,2,3,4,i =
20、4 j = 3 key = 20 Aj = 40 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4 j = 3 key = 20 Aj = 40 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key =
21、 Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4 j = 3 key = 20 Aj = 40 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4 j =
22、2 key = 20 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,40,40,1,2,3,4,i = 4 j = 2 key = 20 Aj = 30 Aj+1 = 40,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j
23、= i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4 j = 2 key = 20 Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4 j = 2 key
24、= 20 Aj = 30 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4 j = 1 key = 20 Aj = 10 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i -
25、1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,30,30,40,1,2,3,4,i = 4 j = 1 key = 20 Aj = 10 Aj+1 = 30,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,20,30,40,1,2,3,4,i = 4 j = 1 key = 20 A
26、j = 10 Aj+1 = 20,An Example: Insertion Sort,InsertionSort(A, n) for i = 2 to n key = Ai j = i - 1; while (j 0) and (Aj key) Aj+1 = Aj j = j - 1 Aj+1 = key ,10,20,30,40,1,2,3,4,i = 4 j = 1 key = 20 Aj = 10 Aj+1 = 20,Done!,Animating Insertion Sort,Try it out with random, ascending, and descending inpu
27、ts Homework:2.1-1, 2.1-2, 2.1-3,2.2 Analyzing algorithm, The running time depends on the input: an already sorted sequence is easier to sort. Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones. Generally, we seek upper bounds on the runnin
28、g time, because everybody likes a guarantee.,时间复杂性和空间复杂性,P25-26详细讲解,Kinds of analyses,Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T(n) = expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of i
29、nputs.,P26,Best-case: (bogus) Cheat with a slow algorithm that works fast on some input.,Machine-independent time,What is insertion sorts worst-case time? It depends on the speed of our computer: relative speed (on the same machine), absolute speed (on different machines). BIG IDEA: Ignore machine-d
30、ependent constants. Look at growth of T(n) as n .,Insertion sort analysis,Worst case: Input reverse sorted.Average case: All permutations equally likely.,Quadratic function,Engineering: Drop low-order terms; ignore leading constants. Example: 3n3 + 90n2 5n + 6046 = (n3),Order of growth: 渐进记号作业: 2.2-
31、1 2.2-3,Is insertion sort a fast sorting algorithm? Moderately so, for small n. Not at all, for large n.,2.3 Designing algorithms,合并排序,算法mergeSort的递归过程可以消去。,Merge sort,Merging two sorted arrays,Analyzing merge sort,P51,Divide-and-conquer approach,Recurrence for merge sort,(1) if n = 1 T(n) =2T(n/2)
32、+ (n) if n 1.,Recursion tree,Solve T(n) = 2T(n/2) + cn, where c 0 is constant.,Solve T(n) = 2T(n/2) + cn, where c 0 is constant.T(n),P35 举例说明,let us pit a faster computer (computer A) running insertion sort against a slower computer (computer B) running merge sort.,Efficiency,Practical Complexity,Practical Complexity,Practical Complexity,Practical Complexity,Practical Complexity,Homework,2.3-1 2.3-5,