【问题标题】:How to use KDTree to make top-k query and range query on arbitrary dimensions如何使用KDTree对任意维度进行top-k查询和范围查询
【发布时间】:2011-03-16 16:37:53
【问题描述】:

我用KD-tree(libkdtree++)存储了一个多维数据集,这里的要求是这个数据集可以支持不同维度的top-k/range查询。例如,KDTree 树:查找具有最高 Point[1](y 轴)值的前 100 个点。

从 libkdtree++ 的实现来看,类似的是“find_within_range”函数,但它是根据“曼哈顿距离”计算的,这里等于 max(x_dist, max(y_dist, z_dist))。如何只在一维上使用范围查询?

【问题讨论】:

    标签: c++ performance data-structures kdtree


    【解决方案1】:

    查看代码,您似乎无法以直接的方式做到这一点,这太荒谬了。如果我是你,我会很想破解图书馆或编写我自己的 kd-tree。我会在他们的邮件列表中询问以确保,但看起来您可能必须执行以下操作:

    kdtreetype::_Region_ r(point_with_min_y);
    r.set_low_bound(min_x, 0);
    r.set_high_bound(max_x, 0);
    r.set_low_bound(min_z, 2);
    r.set_high_bound(max_z, 2);
    r.set_high_bound((min_y + max_y) / 2, 1);
    
    double search_min = min_y, search_max = max_y;
    
    // binary search to get 100 points
    int c;
    while (c = tree.count_within_range(r) != 100) {
        if (c > 100) search_max = (search_min + search_max) / 2;
        else         search_min = (search_min + search_max) / 2;
        r.set_high_bound((search_min + search_max) / 2);
    }
    
    tree.visit_within_range(r, process_min_y_point);
    

    这是对 Y 的非常低效的二分搜索,其中 count(y

    【讨论】:

    • 谢谢。 Region 类可以解决我的问题! top-k 查询不需要那么准确,这意味着当 tree.count_within_range(r)
    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多