简体   繁体   English

python算法:如何有效地找到两个整数集是否相交?

[英]python algorithm: how to efficiently find if two integer sets are intersected?

Given a set [2004, 2008], what is the fastest way to find if this set is intersected with other sets? 给定一组[2004,2008],找到这个集合与其他集合相交的最快方法是什么?

Actually I am dealing a problem with database, the table has 2 columns, one is the lower bound, the other one is the higher bound. 其实我在处理数据库的问题,表有2列,一列是下限,另一列是上限。 The task is to find all the intersected rows with the given 2 tuple(like [2004,2008]). 任务是找到具有给定2元组的所有相交行(如[2004,2008])。

I am using mongodb, is this intrinsically supported(I mean have keywords to do it). 我正在使用mongodb,这是内在支持的(我的意思是有关键字来做)。 I have large user base, so I want this task to be completed as fast as possible. 我有庞大的用户群,所以我希望尽快完成这项任务。

EDIT: To stat more clear, a database table contains following rows: 编辑:为了更清楚,数据库表包含以下行:

20 30
10 50
60 90
...

Given the input (25 40) range, I want to return the rows which represent a range, have intersection with the given range. 给定输入(25 40)范围,我想返回表示范围的行,与给定范围相交。

so return is: (20 30),(10 50) 所以回归是: (20 30),(10 50)

I don't know MongoDB at all, but you're basically looking for 我根本不知道MongoDB,但你基本上都在寻找

SELECT * from the_table where not (lower_bound > 2008 or upper_bound < 2004) . SELECT * from the_table where not (lower_bound > 2008 or upper_bound < 2004)

Try this, assuming low and high are your bound fields: 试试这个,假设你的绑定字段为lowhigh

db.ranges.find({'low': {'$lt': self.high}, 'high': {'$gt': self.low}})

Substitute $lte and $gte if you want your query to be inclusive rather than exclusive. 如果您希望查询具有包容性而非排他性,则替换$lte$gte

MongoDB does not support intersection. MongoDB不支持交集。 Perform intersection on the Python level using the intersection() API of sets. 使用集合的intersection()API在Python级别上执行交集。

Since you're dealing with lower bounds and upper bounds, you can just check bounds. 由于您正在处理下限和上限,因此您可以检查边界。

def intersects(this, others):
    upper, lower = this
    return [[u, l] for u, l in others 
            if (l < upper < u) or (l < lower < u)]

I don't know MongoDB but if you could implement that logic in the database, I can't help but think that it would be faster. 我不知道MongoDB,但如果你能在数据库中实现这个逻辑,我不禁会认为它会更快。

You could use a mongodb query with a Javascript expression (assuming lowerbounds and upperbounds are the limits of the set being intersected): 你可以使用MongoDB的查询与JavaScript表达式(假设lowerboundsupperbounds是被相交集的限制):

f = function() { return this.lower <= upperbounds && this.upper >= lowerbounds; }
db.ranges.find(f);

This should handle all cases including when [this.lower, this.upper] is a superset or proper subset of [lowerbounad, upperbounds]. 这应该处理所有情况,包括[this.lower,this.upper]是[lowerbounad,upperbounds]的超集或适当子集时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM