Talking about the algorithm - a third of the text thoroughly publicize algorithm

This article originating in individual public number: TechFlow

Previous article which we elaborated dichotomy, especially in border issues discussed our time writing code. Portal:

Talking about the algorithm - everyone knows very many people do not write dichotomy

Today this article, we continue in terms of algorithms, we do not speak the dichotomy. Laijiangjiang dichotomy advanced version - the rule of thirds .

Yes, you read that right, this is not my wayward from the name, but there is a real algorithm. But if you go to a search engine in the search, the search will be a high probability of photography thirds, one-third search algorithm is difficult to find.

The main reason is its dichotomy and compared to a much smaller minority, almost all algorithms books which have not introduced the rule of thirds. It is more present in the ACM-ICPC competition among these algorithms, but a small minority does not matter, as long as helpful enough. The principle of the rule of thirds is also very simple, and almost exactly the same dichotomy, but we separated the interval when the interval is not divided into two but split into three. After that, we also want to find where the value is determined by reducing the interval method.

See here, I believe you should be able to understand the principles of arithmetic, but there will certainly be a question to ask: since split into two will solve the problem, why do we have to be divided into three?

Before answering this question, let's look at another issue. Mathematically, a dichotomy really solve the problem?

Remember the premise dichotomy use it? Array must be ordered, so the dichotomy in fact solve the problem is solved monotonic function. As long as the array are ordered according to the definition of the function it can be seen as a function mapped to an array subscript value of the array. Obviously, this is a monotonic function. We find an element v dichotomy by which, in fact, the essence is to find f v (x) =
solution to this function.

Therefore, the use of dichotomy scene is a monotonic function, which is a linear function. What if I want to search the minimum value of a quadratic function with a dichotomy feasible?

Obviously not feasible, because after we get finished mid, do not know the answer about what may occur in the interval.
This time we need the rule of thirds played.

三分法会将区间分成三份,这个我们都已经知道了。分成三份,自然需要两个端点。这两个端点各有一个值,我们分别叫做m1和m2。我们要求的是函数的最小值,所以我们要想极值逼近。

但是我们有两个中间点,该怎么逼近呢?

我们直接根据函数图像来分析,根据上图我们可以看出来,m1和m2的函数值和它们距离极值点的远近是有关系的。离极值点越近,函数值越小(也有可能越大,视函数而定)。在上图当中,

,所以m2离极值点更近。我们要缩小区间范围,逼近极值点,所以我们应该让l=m1

这里有一点小问题,我们怎么确定极值点在m2和m1中间呢?万一在m2的右侧该怎么办呢?

我们画出图像来看,这种情况其实并没有区别,我们只会抛弃区间[l, m1]
,并不会影响极值点。

会不会极值点在m1左侧呢?这是不可能的,因为如果极值点在m1左侧,那么m2距离极值点一定比m1远,这种情况下m2处的函数值是不可能小于m1的。

也就是说,三分法的精髓在于,每次通过比较两个值的大小,缩小三分之一的区间。直到最后区间的范围小于我们设定的阈值为止。算法并不难理解,但是当我们真正碰到二次函数的极值问题的时候,如果没有事先接触过三分法,很难一下想到算法。

三分法本身并不难,我们理解了算法之后,写出伪代码来就很容易了:

def trichotomy():
    l, r = start, end
    epsilon = 1e-6
    # 我们自定义的终止条件是区间长度小于1d-6
    while l + epsilon < r:
        margin = (r - l) / 3.0
        m1 = l + margin
        m2 = m1 + margin
        if f(m1) <= f(m2):
            r = m2
        else:
            l = m1
    return l

最后, 我们看一道三分法相关的算法题,来实际演练一下三分法吧。

这是一道POJ3737,链接如下:http://poj.org/problem?id=3737

我来简单介绍一下题意。题意很简单,当下我们拥有一块布,我们知道布的大小,要将布做成一个圆锥形。要使得圆锥形的体积最大,假设布在加工的过程当中没有损耗,请问这个圆锥的体积、底面半径和高分别是多少?

我这里先不放答案,大家不妨自己想一下,实在做不出来,在公众号内回复三分法答案,获取题解。

如果觉得文章有所帮助,请扫码给个关注吧:

Guess you like

Origin www.cnblogs.com/techflow/p/12131376.html