So I'm going to solve this by enumerate and check, which is an example of what's called a brute-force algorithm.
你懂的,所以我得靠列举和检验来解决这个问题,这就是我们说的穷举算法。
Cut the problem in half. Cut the problem in half again. And that's a typical characterization of a log algorithm.
是每次除以特定的量,将问题减一半,再减一半,如此,这就是对数算法的典型特性。
Now, as obvious a statement as that is in this algorithm, it turns out that is the key to this whole problem being solved correctly without my algorithm looping infinitely.
这是此算法中很明显的一句话,也是正确解决,整个问题的关键,否则此算法就会陷入死循环。
And then the things I have to ask is how much of a division do I want to do?
如果可以这么做,那么分治算法是个好的选择,接下的问题就是我需要把问题分成多少份?
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?
我开始这个话题的第一个问题是,假设我问你,由这个算法实现的程序大概,要运行多久才能给出答案?
It's a problem, as you'll see, designed to give you some practice at dealing with some of the, dare I say, more theoretical concepts we've covered in class.
这种问题会让你在处理我们,课堂上并没有讲到的更多,理论概念这方面上有更多实践,这种问题就像算法复杂度一样。
One of the things we want to help you do is identify the different classes of algorithms, what their costs are, and how you can map problems into the most efficient class to do the computation.
我们想要教会你们的事情之一就是,学会辨别不同种类的算法,它们的代价是怎么样的,以及怎么样把问题映射到,最有效的算法种类中去做计算。
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?
这就说明此算法会一次又一次地调用自己,每次我要排序的问题规模大小,会除以多少呢?
If I'm running a quadratic algorithm, it'll take one millisecond to complete.
算法会在1毫秒内完成,如果问题的复。
So things that are good candidates for divide And conquer are problems where it's easy to figure out how to divide down, and the combination is of little complexity.
因为适合用分治算法解决的问题,最好是能够简单的将问题进行分解,并且合并的过程不是非常的复杂,只要比线性方案要小。
One of the things you should begin to do, is to recognize what identifies those classes of algorithms, so you can map your problems into those ranges.
你们现在开始要做的事情之一,就是要学会辨别这些算法,这样你就能为你的问题,选择对应种类的算法了。
Alright, I have the problem so clearly this algorithm whatever it's gonna be is gonna be at least twice as fast because I'm doing half as much work.
现在,这个问题已变得清晰,不管怎么样,排序算法的效率,至少是之前的两倍,因为我只需要做一半的工作。
Linear algorithms tend to be things where, at one pass-through, you reduce the problem by a constant amount by one. If you reduce it by two, 1 it's going to be the same thing.
有问题么?,线性复杂度的算法,当进行了一个,常量级步数的操作的时候,将问题的规模缩小了一个。
The algorithm doesn't know that it's going to take this long to compute, it's just busy crunching away, trying to see if it can make it happen.
这很可能会发生,但是你知道的,算法本身并不知道,计算这个问题需要多长的时间,它就一直忙碌的算啊算。
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?
以及哪种算法-,可能可以应用到这类问题上,问题本身又能提取出什么东西来,以及可能解决问题的一系列方法的简介?
OK. At this point, if we stop, you'll think all algorithms are linear. This is really boring.
可能我们就认为所有的解决这个问题的,算法都是线性增长的了,真很没意思,但是他们真不是对不对?
But let's look for a slight variant of it, where greedy is not so good. And that's what's called the zero-one knapsack problem.
但是让我们找一找它的一些变种,在这些变种中贪婪算法用处不大,这些问题也就是0/1背包问题。
And we want to help you learn how to map a problem into a class of algorithms of some efficiency That's our goal.
学会如何将一个问题,映射到一类,提升效率的算法中去,这就是我们的目标。
People use bin packing algorithms to figure out, for example, how to load up container ships.
人么通常用装箱算法来计算这个问题,比如说怎样装载货船。
with the continuous knapsack problem as we've formulated it, greedy is good.
因为正如我们已经归越过的,对于一般连续性背包问题贪婪算法很实用。
How does this thing grow as I make the problem size big?
也就是说当问题规模变大的时候,算法计算的时间会怎样增长?
And part of it is we want you to learn how to have a catalog, if you like, of different classes of algorithms, so that when you get a problem, you try and map it into an appropriate class, and use the leverage, if you like, of that class of algorithms.
另外我希望你们能学会,如何制作一个关于,算法不同课程的目录,这样当你碰到问题的时候,你可以去找到合适的课程,然后在那门课程中,寻找解决方法。
The algorithm on mine professed to be implementing now is this thing.
这个算法现在要,解决的就是这个问题。
In Week 2 we'll look at things like cryptography and functions and the tools that by which you begin to assemble solutions to problems; in Week 3 we'll introduce what are generally ; known as algorithms and data structures; mechanisms by which you can solve problems more efficiently, effectively.
第2周,我们学习加密技术及其功能,还会学到一些操作工具,通过这些工具的使用来收集解决问题的办法,第3周,我们将介绍;,算法和数据结构,以及可以更有效地解决问题的机制。
So as you can see, as we escalate the problem difficulty, the difference between the good method and the not quite as good method gets bigger and bigger and bigger.
如你所见,当我们处理更复杂的,问题的时候,优良的算法,和并不那么好的算法之间的,差异会变的越来越大,这一点很重要。
So in fact, over the next thirty or forty minutes we're going to show you a set of examples of sort of canonical algorithms, and the different classes of complexity.
在接下来的三四十分钟里面,我们将要讲一系列的,权威算法,以及不同种类的复杂度问题。
Usually what I want to talk about is what's the smallest size class in which this function grows? With all of that, what that says b is that this we would write is order b.
通常我们想要的是,方法增长时最贴切的描述,也就是说,在这个问题上,算法关于。
And then we're going to turn to the question of efficiency. How do we measure efficiency of algorithms? Which is going to be a really important thing that we want to deal with, and we'll start it today, it's undoubtedly going to take us a couple more lectures to finish it off.
然后我们会回头继续讨论效率问题,我们如何计算算法的效率?,我们处理这些问题的时候,最重要的事情是什么?,我们今天要去讨论这个问题,毫无疑问我们要用,挺多节课来学习这个。
What you're much better at doing is saying how do I take the problem I've got and map it into a class of algorithms about which I know and use the efficiencies of those to try and figure out how to make it work.
当你碰到问题的时候,更好的方法是把遇到的问题映射,到已经设计好的算法中去,并用这些算法来提高效率,来保证问题得到解决。
PROFESSOR: Great question. So the question is, how do you choose an algorithm, why would I choose to use a pseudo-polynomial algorithm when I don't know how big the solution is likely to be, I think that's one way to think about it.
教授:问得好,所以问题是,你怎样选择算法,为什么当我,不知道解决方案会有多大的时候,我要选伪多项式算法呢,我想这是一种思考问题的方式。
应用推荐