【问题标题】:Determining if graph is convex确定图是否凸
【发布时间】:2015-04-29 01:08:59
【问题描述】:

我试图确定连接多个点的线是否是凸的。这些点可以绘制在 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  

【问题讨论】:

    标签: graph convex


    【解决方案1】:

    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是单调递增的。

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 1970-01-01
      • 2013-06-14
      • 2021-09-22
      • 2010-10-19
      • 1970-01-01
      • 2012-02-02
      • 2010-10-03
      相关资源
      最近更新 更多