确定图是否是凸的

时间:2015-04-29 01:09:00

标签: graph convex

我正在尝试确定连接多个点的线是否凸起。可以在x,y坐标上绘制点。这可以通过一种方式完成,除了基本上将每个点连接到每个其他点并查看所有这些线是否位于曲线上方?谢谢! 以下是示例点:

X           Y
1191.06     0.9655265 
1192.36     0.9644738 
1193.75     0.9633508 
1194.98     0.9623592 
1196.49     0.9611447 
1197.78     0.9601095
1199.02     0.9591166 
1200.29     0.9581017 
1201.56     0.9570891 
1202.77     0.9561263 
1204.01     0.9551415 
1205.26     0.9541510  

1 个答案:

答案 0 :(得分:0)

Convex function

  

当且仅当其导数在该区间上单调非递减时,一个变量的可微函数在一个区间上是凸的。如果函数是可微分和凸的,那么它也是连续可微的。对于从实数到实数(的一个子集)的可微函数的基本情况,“凸”相当于“以递增的速率增加”。

您可以迭代点并检查序列中每对连续点之间的斜率是否严格不减小。您不必将每个点连接到其他每个点。

伪代码:

boolean isConvex(float[] x, float[] y, int length)
{
    float previousSlope = 0;
    for(int i = 1; i < length; i++)
    {
        if(x[i] == x[i-1])
        {
            // handle infinite gradients to avoid div by zero
            // if you are not going to exclude this case from your data
        }
        float slope = (y[i] - y[i-1]) / (x[i] - x[i-1]);
        // only compare the slope after the first iteration:
        if((i > 1) && (slope < previousSlope))
            return false;
        previousSlope = slope;
    }
    return true;
}

这假设y是x的函数,并且数组中的值基于x按升序排序,x是单调递增的。