【问题标题】:Running time of finding nearest neighbour using d3 voronoi使用 d3 voronoi 查找最近邻居的运行时间
【发布时间】:2019-01-06 16:34:44
【问题描述】:

d3.voronoi.find方法的运行时间复杂度是多少?

这里,https://visionscarto.net/the-state-of-d3-voronoi,它的原始速度是 O(sqrt(n)),但是有什么证据呢?

另外,有没有什么方法可以在 O(logn) 时间内使用计算的 voronoi 图找到最近的邻居?

【问题讨论】:

    标签: algorithm d3.js nearest-neighbor voronoi


    【解决方案1】:

    正如你所看到的,following codefind 函数,它正在讨论它:

    find: function(x, y, radius) {
        var that = this, i0, i1 = that._found || 0, n = that.cells.length, cell;
    
        // Use the previously-found cell, or start with an arbitrary one.
        while (!(cell = that.cells[i1])) if (++i1 >= n) return null;
        var dx = x - cell.site[0], dy = y - cell.site[1], d2 = dx * dx + dy * dy;
    
        // Traverse the half-edges to find a closer cell, if any.
        do {
          cell = that.cells[i0 = i1], i1 = null;
          cell.halfedges.forEach(function(e) {
            var edge = that.edges[e], v = edge.left;
            if ((v === cell.site || !v) && !(v = edge.right)) return;
            var vx = x - v[0], vy = y - v[1], v2 = vx * vx + vy * vy;
            if (v2 < d2) d2 = v2, i1 = v.index;
          });
        } while (i1 !== null);
    
        that._found = i0;
    
        return radius == null || d2 <= radius * radius ? cell.site : null;
    }
    

    根据指定的半径搜索数据点。无论如何,如您所见,它会遍历所有半边(因此,在最坏的情况下,它可能是 O(n))。

    无论如何,通过良好的数据结构,可以在 log(n) 中找到最近的邻居。你可以看this answer了解更多。

    【讨论】:

    • 那么,这个方法中使用的这个算法的时间复杂度可以说什么呢?
    • @ninjawarrior 在最坏的情况下可能是 O(n)
    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多