小节摘要
-
时间复杂性
-
最坏情况复杂性
-
算法范例
-
理解算法的复杂性
Section Summary
-
Time Complexity
-
Worst-Case Complexity
-
Algorithmic Paradigms
-
Understanding the Complexity of Algorithms
算法的复杂性
-
给定一个算法,这个算法在给定特定大小的输入时解决问题的效率如何? 要回答这个问题,我们会问:
-
此算法用于解决问题的时间是多少?
-
此算法用于解决问题的计算机内存量是多少?
-
The Complexity of Algorithms
-
Given an algorithm, how efficient is this algorithm for solving a problem given input of a particular size? To answer this question, we ask:
-
How much time does this algorithm use to solve a problem?
-
How much computer memory does this algorithm use to solve a problem?
-
算法的复杂性
-
当我们分析算法用于解决特定大小输入的问题的时间时,我们正在研究算法的时间复杂度。
-
当我们分析计算机内存时,算法用来解决给定特定大小的输入问题,我们正在研究算法的空间复杂度。
-
在本课程中,我们关注时间复杂性。 在以后的课程中研究算法的空间复杂性。
-
我们将根据算法使用的操作数来衡量时间复杂度,我们将使用big-O和big-Theta表示法来估计时间复杂度。
-
我们可以使用此分析来查看使用此算法解决特定大小输入问题是否切实可行。 我们还可以比较不同算法的效率来解决同样的问题。
-
我们忽略了实现细节(包括使用的数据结构以及硬件和软件平台),因为考虑它们非常复杂。
The Complexity of Algorithms
-
When we analyze the time the algorithm uses to solve the problem given input of a particular size, we are studying the time complexity of the algorithm.
-
When we analyze the computer memory the algorithm uses to solve the problem given input of a particular size, we are studying the space complexity of the algorithm.
-
In this course, we focus on time complexity. The space complexity of algorithms is studied in later courses.
-
We will measure time complexity in terms of the number of operations an algorithm uses and we will use big-O and big-Theta notation to estimate the time complexity.
-
We can use this analysis to see whether it is practical to use this algorithm to solve problems with input of a particular size. We can also compare the efficiency of different algorithms for solving the same problem.
-
We ignore implementation details (including the data structures used and both the hardware and software platforms) because it is extremely complicated to consider them.
时间复杂性
-
为了分析算法的时间复杂度,我们确定操作的数量,例如比较和算术运算(加法,乘法等)。 我们可以使用执行基本操作所需的时间来估计计算机实际用于解决问题的时间。
-
我们忽略了一些细微的细节,例如算法的“管家”方面。
-
我们将关注算法的最坏情况时间复杂度。 这提供了算法用于解决特定大小的输入问题的操作数量的上限。
-
确定算法的平均案例时间复杂度通常要困难得多。 这是算法用于解决特定大小的所有输入上的问题的平均操作数。
Time Complexity
-
To analyze the time complexity of algorithms, we determine the number of operations, such as comparisons and arithmetic operations (addition, multiplication, etc.). We can estimate the time a computer may actually use to solve a problem using the amount of time required to do basic operations.
-
We ignore minor details, such as the “house keeping” aspects of the algorithm.
-
We will focus on the worst-case time complexity of an algorithm. This provides an upper bound on the number of operations an algorithm uses to solve a problem with input of a particular size.
-
It is usually much more difficult to determine the average case time complexity of an algorithm. This is the average number of operations an algorithm uses to solve a problem over all inputs of a particular size.
算法的复杂性分析
示例:描述用于在有限序列中查找最大元素的算法的时间复杂度。
过程max(a1,a2,....,an:整数)max:= a1
对于i:= 2到n
ifmax <ai thenmax:= ai
return max {max是最大的元素}
解决方案:计算比较次数。
-
最大<ai比较为n - 1次。
-
每次i递增时,都会进行测试以查看i是否为n。
-
最后一个比较确定i> n。
-
恰好2(n - 1)+ 1 = 2n - 1进行比较。
因此,算法的时间复杂度是Θ(n)。
Complexity Analysis of Algorithms
Example: Describe the time complexity of the algorithm for finding the maximum element in a finite sequence.
procedure max(a1, a2, ...., an: integers) max := a1
for i := 2 to n
ifmax<ai thenmax:=ai
return max{max is the largest element}
Solution: Count the number of comparisons.
-
The max < ai comparison is made n − 1 times.
-
Each time i is incremented, a test is made to see if i ≤ n.
-
One last comparison determines that i > n.
-
Exactly 2(n − 1) + 1 = 2n − 1 comparisons are made.
Hence, the time complexity of the algorithm is Θ(n).
线性搜索的最坏情况复杂性
示例:确定线性搜索算法的时间复杂度。
程序 线性搜索(x:整数,a1,a2,a3,……,an:不同的整数)
i:= 1
while(i≤n且x≠ai)
i:= i + 1
如果 i≤n那么位置:=i
否则位置:= 0
返回位置
{位置是等于x的术语的下标,如果未找到x则为0}
Worst-Case Complexity of Linear Search
Example: Determine the time complexity of the linear search algorithm.
procedure linear search(x:integer, a1,a2,a3,……,an:distinct integer)
i := 1
while (i ≤ n and x ≠ ai)
i := i + 1
if i ≤ n then location := I
else location := 0
return location
{location is the subscript of the term that equals x, or is 0 if x is not found}
线性搜索的最坏情况复杂性
解决方案:计算比较次数。
-
在每个步骤进行两次比较; i≤n且x≠ai。
-
要结束循环,需要进行一次比较i≤n。
-
循环之后,再进行一次i≤n比较。
如果x = ai,则使用2i + 1比较。 如果x不在列表中,则进行2n + 1次比较,然后使用另外的比较来退出循环。 因此,在最坏的情况下,进行2n + 2次比较。 因此,复杂度是Θ(n)。
Worst-Case Complexity of Linear Search
Solution: Count the number of comparisons.
-
At each step two comparisons are made; i ≤ n and x ≠ ai .
-
To end the loop, one comparison i ≤ n is made.
-
After the loop, one more i ≤ n comparison is made.
If x = ai , 2i + 1 comparisons are used. If x is not on the list, 2n + 1 comparisons are made and then an additional comparison is used to exit the loop. So, in the worst case 2n + 2 comparisons are made. Hence, the complexity is Θ(n).
二进制搜索的最坏情况复杂性
示例:根据使用的比较次数描述二进制搜索的时间复杂度。
过程二进制搜索(x:整数,a1,a2,...,an:增加整数)
i:= 1 {i是区间的左端点} j:= n {j是区间的右端点}
而我<j
m:=⌊(i + j)/2⌋
ifx> am theni:= m + 1
否则j:= m
如果x = ai则位置:= i其他位置:= 0
返回位置{location是术语ai的下标i等于x,如果未找到x则为0}
Worst-Case Complexity of Binary Search
Example: Describe the time complexity of binary search in terms of the number of comparisons used.
procedure binary search(x: integer, a1,a2,..., an: increasing integers)
i := 1 {i is the left endpoint of interval} j := n {j is right endpoint of interval}
while i < j
m := ⌊(i + j)/2⌋
ifx>am theni:=m+1
else j := m
if x = ai then location := i else location := 0
return location{location is the subscript i of the term ai equal to x, or 0 if x is not found}
二进制搜索的最坏情况复杂性
解决方案:假设(为简单起见)n = 2k个元素。 请注意,k = log n。
•每个阶段进行两次比较; 我<j,而x> am。
•在第一次迭代时,列表的大小为2k,在第一次迭代后,它的大小为2k-1。 然后是2k-2,依此类推,直到列表的大小为21 = 2。
•在最后一步,比较告诉我们列表的大小是20 = 1,并且元素与单个剩余元素进行比较。
•因此,atmost2k + 2 = 2logn + 2比较。
•因此,时间复杂度为Θ(log n),优于线性搜索。
Worst-Case Complexity of Binary Search
Solution: Assume (for simplicity) n = 2k elements. Note that k = log n.
• Two comparisons are made at each stage; i < j, and x > am .
• At the first iteration the size of the list is 2k and after the first iteration it is 2k-1. Then 2k-2 and so on until the size of the list is21 =2.
• At the last step, a comparison tells us that the size of the list is the size is 20 = 1 and the element is compared with the single remaining element.
• Hence,atmost2k+2=2logn+2comparisonsaremade.
• Therefore, the time complexity is Θ (log n), better than linear search.
冒泡分类的最坏情况复杂性
示例:根据比较次数,冒泡排序的最坏情况复杂度是多少?
程序bubblesort(a1,...,an:实数n≥2),i:= 1到n-1
对于j:= 1到n - i
如果aj> aj + 1则交换aj和aj + 1
{a1,...,现在正在递增}
解决方案:通过列表进行一系列n-1次传递。 在每次通过n - i时进行比较。
气泡排序的最坏情况复杂度是Θ(n2)。
Worst-Case Complexity of Bubble Sort
Example: What is the worst-case complexity of bubble sort in terms of the number of comparisons made?
procedure bubblesort(a1,...,an: real numbers with n ≥ 2) for i := 1 to n− 1
for j := 1 to n − i
if aj >aj+1 then interchange aj and aj+1
{a1,..., an is now in increasing order}
Solution: A sequence of n−1 passes is made through the list. On each pass n − i comparisons are made.
The worst-case complexity of bubble sort is Θ(n2) since .
插入排序的最坏情况复杂性
示例:根据所进行的比较次数,插入排序的最坏情况复杂度是多少?
解决方案:比较总数为:
因此,复杂度是Θ(n2)。
过程插入排序(a1,...,an:n≥2的实数)
对于j:= 2到n i:= 1
而aj> ai i:= i + 1
m:= aj
fork:= 0toj -i-1
aj-k:= aj-k-1 ai:= m
Worst-Case Complexity of Insertion Sort
Example: What is the worst-case complexity of insertion sort in terms of the number of comparisons made?
Solution: The total number of comparisons are:
Therefore the complexity is Θ(n2).
procedure insertion sort(a1,...,an: real numbers with n ≥ 2)
for j := 2 to n i := 1
while aj > ai i := i + 1
m := aj
fork:=0toj −i−1
aj-k := aj-k-1 ai := m
矩阵乘法算法
-
矩阵乘法的定义可以表示为算法; C = AB其中,Cisanm nmatrix是m k矩阵A和k n矩阵B的乘积。
-
该算法根据其定义执行矩阵乘法。
i:= 1到m的过程矩阵乘法(A,B:矩阵)
对于j:= 1到n cij:= 0
对于q:= 1到k
cij:= cij + aiq bqj
返回C {C = [cij]是A和B的乘积}
Matrix Multiplication Algorithm
-
The definition for matrix multiplication can be expressed asanalgorithm;C =AB whereCisanm nmatrixthatis the product of the m k matrix A and the k n matrix B.
-
This algorithm carries out matrix multiplication based on its definition.
procedure matrix multiplication(A,B: matrices) for i := 1 to m
for j := 1 to n cij := 0
for q := 1 to k
cij := cij + aiq bqj
return C{C = [cij] is the product of A and B}
示例:矩阵乘法算法使用多少个整数和整数乘法来乘以两个n n矩阵。
解决方案:产品中有n2个条目。 找到每个条目需要n次乘法和n - 1次加法。因此,使用n3次乘法和n2(n-1)次加法。
因此,矩阵乘法的复杂度为O(n3)。
Example: How many additions of integers and multiplications of integers are used by the matrix multiplication algorithm to multiply two n n matrices.
Solution: There are n2 entries in the product. Finding each entry requires n multiplications and n − 1 additions.Hence,n3 multiplicationsandn2(n−1) additions are used.
Hence, the complexity of matrix multiplication is O(n3).
布尔乘积算法
-
零一矩阵的布尔乘积的定义也可以转换为算法。
过程布尔乘积(A,B:零一矩阵),i:= 1到m
对于j:= 1到n cij:= 0
对于q:= 1到k
cij:=cij∨(aiq∧bqj)
return C {C = [cij]是A和B的布尔乘积}
Boolean Product Algorithm
-
The definition of Boolean product of zero-one matrices can also be converted to an algorithm.
procedure Boolean product(A,B: zero-one matrices) for i := 1 to m
for j := 1 to n cij := 0
for q := 1 to k
cij := cij ∨ (aiq ∧ bqj)
return C{C = [cij] is the Boolean product of A and B}
布尔乘积算法的复杂性
示例:使用多少位操作来查找A⊙B,其中A和B是n n个零一个矩阵?
解答:A⊙B中有n2个条目。总共有n个O和n个AND用于查找每个条目。 因此,每个条目需要2n位操作。 总共使用了2n3个操作。
因此复杂度为O(n3)
Complexity of Boolean Product Algorithm
Example: How many bit operations are used to find A ⊙ B, where A and B are n n zero-one matrices?
Solution: There are n2 entries in the A ⊙ B. A total of n Ors and n ANDs are used to find each entry. Hence, each entry takes 2n bit operations. A total of 2n3 operations are used.
Therefore the complexity is O(n3)
算法范例
-
算法范例是一种基于特定概念的通用方法,用于构建算法以解决各种问题。
-
第3.1节介绍了贪婪算法。
-
我们将在本节讨论蛮力算法。
-
我们将看到分而治之的算法(第8章),动态编程(第8章),回溯(第11章)和概率算法(第7章)。 您可以在以后的课程中看到许多其他范例。
Algorithmic Paradigms
-
An algorithmic paradigm is a general approach based on a particular concept for constructing algorithms to solve a variety of problems.
-
Greedy algorithms were introduced in Section 3.1.
-
We discuss brute-force algorithms in this section.
-
We will see divide-and-conquer algorithms (Chapter 8), dynamic programming (Chapter 8), backtracking (Chapter 11), and probabilistic algorithms (Chapter 7). There are many other paradigms that you may see in later courses.
暴力算法
-
以最直接的方式解决蛮力算法,而不利用任何可以提高算法效率的想法。
-
我们之前看到的强力算法是顺序搜索,冒泡排序和插入排序。
Brute-Force Algorithms
-
A brute-force algorithm is solved in the most straightforward manner, without taking advantage of any ideas that can make the algorithm more efficient.
-
Brute-force algorithms we have previously seen are sequential search, bubble sort, and insertion sort.
用蛮力计算最接近的点数
示例:构造一个强力算法,用于在平面中的一组n个点中查找最接近的点对,并提供算术运算数的最坏情况估计。
解答:回想一下(xi,yi)和(xj,yj)之间的距离是。 蛮力算法简单地计算所有点对之间的距离,并选择具有最小距离的对。
注意:不需要计算平方根,因为当距离最小时,两点之间距离的平方最小。
Computing the Closest Pair of Points by Brute-Force
Example: Construct a brute-force algorithm for finding the closest pair of points in a set of n points in the plane and provide a worst-case estimate of the number of arithmetic operations.
Solution: Recall that the distance between (xi,yi) and (xj, yj) is . A brute-force algorithm simply computes the distance between all pairs of points and picks the pair with the smallest distance.
Note: There is no need to compute the square root, since the square of the distance between two points is smallest when the distance is smallest.
用蛮力计算最接近的点数
-
在一组n个点中找到最近对的算法。
过程最接近的对((x1,y1),(x2,y2),...,(xn,yn):xi,yi实数)min =∞
对于i:= 1到n表示j:= 1到i
if(xj - xi)2 +(yj - yi)2 <min then min:=(xj - xi)2 +(yj - yi)2
最近的一对:=(xi,yi),(xj,yj)返回最近的一对
-
算法循环通过n(n -1)/ 2对点,计算值(xj - xi)2 +(yj - yi)2并将其与最小值进行比较等。因此,算法使用Θ(n2 )算术和比较运算。
Computing the Closest Pair of Points by Brute-Force
-
Algorithm for finding the closest pair in a set of n points.
procedure closest pair((x1, y1), (x2, y2), ... ,(xn, yn): xi, yi real numbers) min= ∞
for i := 1 to n for j := 1 to i
if (xj − xi)2 + (yj − yi)2 < min then min := (xj − xi)2 + (yj − yi)2
closest pair := (xi, yi), (xj, yj) return closest pair
-
The algorithm loops through n(n −1)/2 pairs of points, computes the value (xj − xi)2 + (yj − yi)2 and compares it with the minimum, etc. So, the algorithm uses Θ(n2) arithmetic and comparison operations.
理解算法的复杂性(Understanding the Complexity of Algorithms)
超过10100年的时间用*表示。
Times of more than 10100 years are indicated with an *.