I ask you for the running time of this algorithm and you give me the running time in terms of the running time, right.
我需要得到此算法的时间复杂度,那就明确地给出其,运行时间。
and then they kind of match it up with some algorithm.
然后他们用某算法将信息匹配一下,
So I'm going to solve this by enumerate and check, which is an example of what's called a brute-force algorithm.
你懂的,所以我得靠列举和检验来解决这个问题,这就是我们说的穷举算法。
And so you care-- you care about ultimately how much time is my algorithm gonna take to perform on that worst case running time.
因此你最终所关心的是,算法在最坏情况下需要多长的,运行时间。
We started off talking about binary search, and I suggested that this was a log algorithm which it is, which is really kind of nice.
我告诉了你们这是一个对,数级的算法,这是很棒的,我们来一起看看这个算法到底做了什么。
And the obvious question I could start with is, and suppose I ask you, how long does the algorithm implemented by this program take to run?
我开始这个话题的第一个问题是,假设我问你,由这个算法实现的程序大概,要运行多久才能给出答案?
And that's because that is a version of a divide and conquer algorithm.
这个想法非常重要,因为这就是分而治之算法的一个版本。
So this is a nice little search-- sorry, a nice little sort algorithm . And in fact, it's relying on something that we're going to come back to, called the loop invariant.
恩,这是一个很棒的小搜索,抱歉,和很棒的小排序算法,事实上,它依赖于一些我们要回顾的东西,被称作循环不变量。
I want you to recognize classes of algorithms and match what you see in the performance of the algorithm to the complexity of that algorithm. All right?
让我再试一次这么说,我期望大家能通过,算法的复杂度,来看到算法的性能?
Notice here that it's different than the binary search case. We're certainly dividing down, but the combination now actually takes some work. I'll have to actually figure out how to put them back together. And that's a general thing you want to keep in mind when you're thinking about designing a divide and conquer kind of algorithm.
一个分治的例子,注这里,与二分查找所不同的地方,我们肯定是分解了,但是合并的过程还是需要一些工作量的,我会详细说明怎样把它们合并在一起的,当你在考虑设计一个分治算法时,这是你要必须记住的最基本的东西。
So I propose this as a new algorithm for sorting N elements and being 8 in this case or really a thousand in the case of the phonebook, or anything of larger size.
所以我提出一种新的算法,来解决N个元素的排序问题,在这个问题中N是8,在电话簿的问题中N是一千,或者是大规模的任何问题。
Where those pieces, I would do the same thing with, I would divide them up into smaller chunks, and sort those. Is that going to give me a more efficient algorithm?
合并起来,而那些小列表,我又会把他们拆成更小的列表,再排序,这会给我,一个更高效的算法么?
What is the class of algorithm that's- that probably applies to this, and how do I pull something out of that, if you like, a briefcase of possible algorithms to solve?
以及哪种算法-,可能可以应用到这类问题上,问题本身又能提取出什么东西来,以及可能解决问题的一系列方法的简介?
And you really, wherever possible, want to avoid that exponential algorithm, because that's really deadly. Yes.
比你的电脑的性能增长的快多了,并且大家无论何时,都要避免去用指数级的函数。
And we can just extrapolate in a straight line We before saw some examples where I had an algorithm to generate points, and we fit a curve to it, used the curve to predict future points and discovered it was nowhere close.
我们可以干脆用一条直线来描述它,我们之前看到在一些例子中,我用一个算法去生成一些点,然后用一条曲线对它进行拟合,然后用这条曲线来预测未来的点,最后却发现结果完全不对。
If I'm running at nanosecond speed, 1000 n, the size of the problem, whatever it is, is 1000, and I've got a log algorithm, it takes 10 nanoseconds to complete.
如果这个问题的规模,也就是n,是,如果这个问题是对数级的,这将会占据10纳秒的时间,你一眨眼的时间。
And in this case, we go from 8 to 4 to 2 to 1 three times and then on each iteration of this algorithm, each pass across the board I'm touching N numbers, so that means I'm doing N things, log N times.
在这个例子中,我们从8得到4,到2,再到1,是3次,在这个算法的每次迭代中,每一趟我都会操作N个数,也就是所我每次要做N步操作,一共要做,log,N,次。
And so the fact that in this whole slide here, this algorithm for sorting, I'm using the verb sort.
在这个排序算法中,我用到了一个动词排序。
Cut the problem in half. Cut the problem in half again. And that's a typical characterization of a log algorithm.
是每次除以特定的量,将问题减一半,再减一半,如此,这就是对数算法的典型特性。
It implies that this algorithm is calling itself again and again, and again, and on each time the size of the problem I'm trying to sort is being divided by what?
这就说明此算法会一次又一次地调用自己,每次我要排序的问题规模大小,会除以多少呢?
and we like log algorithms, because they're really fast. A typical characteristic of a log algorithm is a pro-- or sorry, an algorithm where it reduces the size of the problem by a constant factor.
并且我们也很喜欢对数算法,因为它很快,对数算法的典型特性是高速,哦,抱歉,是他能以常数因子的速度,降低问题的大小,很明显。
And so one of the things we'll look at today is how can we leverage an algorithm, how can we implement an algorithm that at least at first glance the second time we've now seen it feels so obviously better.
因此,今天我们要学习的一点就是,我们怎样才能影响一个算法,怎样实现一个算法,使你在第二次看它时会觉得,至少比你第一次看到它时更好。
And probably the obvious thing you'll think about is, we'll come up with a clever algorithm. And I want to disabuse you of that notion.
可能你想到的最有效的方法,就是设计一个,聪明点的算法了,这正是我想你们注意到的。
A really nice algorithm. As I said, an example of divide and conquer.
真的很好的一个算法,就如我说的。
And finally, we saw an example of an exponential algorithm, those Towers of Hanoi.
最后,我们来看一个,指数算法的例子,汉诺依塔的例子。
N But it's definitely not one and in fact it wasn't N in the case of Selection Sort because remember the algorithm we implemented on stage last week had me going back and forth across the stage selecting on iteration, the smallest person I can find, the smallest number and then putting them into place.
但在选择排序中,肯定不会是1,也不是,注意,上周我们在这儿,实现的算法中,反复地,迭代进行选择,选出最小的数,然后将其放在合适的位置。
And if I'm running an exponential algorithm, any guesses?
杂度是指数级的呢?,有人猜猜么?
So he's packing and unpacking, packing and unpacking, trying all possible combinations of objects that will obey the constraint. And then choosing the winner. Well, this is like an algorithm we've seen before. It's not greedy.
因此它不断装包和清包,尝试了所有满足约束条件的物品组合,最后选择最优者,这很像我们以前看过的一个算法,这不是贪婪算法。
I think one of the classic ones for strings is called the Rabin-Karp algorithm. And it's simply the same idea that you have a mapping from your import into a set of integers.
我认为字符串哈希,最经典的是Rabin-Karp算法,它只是简单的相同概念,你有一个映射关系将你的输入映射成一组数字。
应用推荐