Finding overlaps between two lists of axis-aligned rectangles
up vote
6
down vote
favorite
I am trying to find an efficient solution for finding overlapping of n rectangles where rectangles are stored in two separate lists. We are looking for all rectangles in listA
that overlap with rectangles in listB
(and vice versa). Comparing one element from the first list to second list could take immensely large amount of time. I am looking for an efficient solution.
I have two lists of axis-aligned rectangles:
rect = Rectangle(10, 112, 56, 15)
rect2 = Rectangle(0, 0, 1, 15)
rect3 = Rectangle (10, 12, 56, 15)
listA = [rect, rect2]
listB= [rect3]
which is created from the class:
import numpy as np
import itertools as it
class Rectangle(object):
def __init__(self, left, right, bottom, top):
self. left = left
self.bottom = bottom
self.right = right
self.top = top
def overlap(r1, r2):
hoverlaps = True
voverlaps = True
if r1.left > r2.right or r1.right < r2.left:
hoverlaps = False
if r1.top < r2.bottom or r1.bottom > r2.top:
voverlaps = False
return hoverlaps and voverlaps
I need to compare rectangle in listA
to listB
the code goes like this which is highly inefficient - comparing one by one
for a in it.combinations(listB) :
for b in it.combinations(listA):
if a.overlap(b):
Any better efficient method to deal with the problem?
python algorithm computational-geometry
|
show 3 more comments
up vote
6
down vote
favorite
I am trying to find an efficient solution for finding overlapping of n rectangles where rectangles are stored in two separate lists. We are looking for all rectangles in listA
that overlap with rectangles in listB
(and vice versa). Comparing one element from the first list to second list could take immensely large amount of time. I am looking for an efficient solution.
I have two lists of axis-aligned rectangles:
rect = Rectangle(10, 112, 56, 15)
rect2 = Rectangle(0, 0, 1, 15)
rect3 = Rectangle (10, 12, 56, 15)
listA = [rect, rect2]
listB= [rect3]
which is created from the class:
import numpy as np
import itertools as it
class Rectangle(object):
def __init__(self, left, right, bottom, top):
self. left = left
self.bottom = bottom
self.right = right
self.top = top
def overlap(r1, r2):
hoverlaps = True
voverlaps = True
if r1.left > r2.right or r1.right < r2.left:
hoverlaps = False
if r1.top < r2.bottom or r1.bottom > r2.top:
voverlaps = False
return hoverlaps and voverlaps
I need to compare rectangle in listA
to listB
the code goes like this which is highly inefficient - comparing one by one
for a in it.combinations(listB) :
for b in it.combinations(listA):
if a.overlap(b):
Any better efficient method to deal with the problem?
python algorithm computational-geometry
1
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangleslistA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?
– Justin
Nov 16 '16 at 8:51
for a,b in it.combinations(listA,2):
: I see only one list used here, what islistB
good for? Also can you provide a bit more context around thisfor
loop (what do you do when they overlap, how do you initialize stuff if any)?
– Mathias Ettinger
Nov 16 '16 at 9:16
1
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining aRectangle
:radious
, in particular.)
– greybeard
Nov 16 '16 at 10:45
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47
|
show 3 more comments
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I am trying to find an efficient solution for finding overlapping of n rectangles where rectangles are stored in two separate lists. We are looking for all rectangles in listA
that overlap with rectangles in listB
(and vice versa). Comparing one element from the first list to second list could take immensely large amount of time. I am looking for an efficient solution.
I have two lists of axis-aligned rectangles:
rect = Rectangle(10, 112, 56, 15)
rect2 = Rectangle(0, 0, 1, 15)
rect3 = Rectangle (10, 12, 56, 15)
listA = [rect, rect2]
listB= [rect3]
which is created from the class:
import numpy as np
import itertools as it
class Rectangle(object):
def __init__(self, left, right, bottom, top):
self. left = left
self.bottom = bottom
self.right = right
self.top = top
def overlap(r1, r2):
hoverlaps = True
voverlaps = True
if r1.left > r2.right or r1.right < r2.left:
hoverlaps = False
if r1.top < r2.bottom or r1.bottom > r2.top:
voverlaps = False
return hoverlaps and voverlaps
I need to compare rectangle in listA
to listB
the code goes like this which is highly inefficient - comparing one by one
for a in it.combinations(listB) :
for b in it.combinations(listA):
if a.overlap(b):
Any better efficient method to deal with the problem?
python algorithm computational-geometry
I am trying to find an efficient solution for finding overlapping of n rectangles where rectangles are stored in two separate lists. We are looking for all rectangles in listA
that overlap with rectangles in listB
(and vice versa). Comparing one element from the first list to second list could take immensely large amount of time. I am looking for an efficient solution.
I have two lists of axis-aligned rectangles:
rect = Rectangle(10, 112, 56, 15)
rect2 = Rectangle(0, 0, 1, 15)
rect3 = Rectangle (10, 12, 56, 15)
listA = [rect, rect2]
listB= [rect3]
which is created from the class:
import numpy as np
import itertools as it
class Rectangle(object):
def __init__(self, left, right, bottom, top):
self. left = left
self.bottom = bottom
self.right = right
self.top = top
def overlap(r1, r2):
hoverlaps = True
voverlaps = True
if r1.left > r2.right or r1.right < r2.left:
hoverlaps = False
if r1.top < r2.bottom or r1.bottom > r2.top:
voverlaps = False
return hoverlaps and voverlaps
I need to compare rectangle in listA
to listB
the code goes like this which is highly inefficient - comparing one by one
for a in it.combinations(listB) :
for b in it.combinations(listA):
if a.overlap(b):
Any better efficient method to deal with the problem?
python algorithm computational-geometry
python algorithm computational-geometry
edited Nov 20 '16 at 18:31
Andrew Svetlov
15816
15816
asked Nov 16 '16 at 8:39
karu
338
338
1
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangleslistA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?
– Justin
Nov 16 '16 at 8:51
for a,b in it.combinations(listA,2):
: I see only one list used here, what islistB
good for? Also can you provide a bit more context around thisfor
loop (what do you do when they overlap, how do you initialize stuff if any)?
– Mathias Ettinger
Nov 16 '16 at 9:16
1
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining aRectangle
:radious
, in particular.)
– greybeard
Nov 16 '16 at 10:45
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47
|
show 3 more comments
1
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangleslistA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?
– Justin
Nov 16 '16 at 8:51
for a,b in it.combinations(listA,2):
: I see only one list used here, what islistB
good for? Also can you provide a bit more context around thisfor
loop (what do you do when they overlap, how do you initialize stuff if any)?
– Mathias Ettinger
Nov 16 '16 at 9:16
1
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining aRectangle
:radious
, in particular.)
– greybeard
Nov 16 '16 at 10:45
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47
1
1
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangles
listA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?– Justin
Nov 16 '16 at 8:51
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangles
listA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?– Justin
Nov 16 '16 at 8:51
for a,b in it.combinations(listA,2):
: I see only one list used here, what is listB
good for? Also can you provide a bit more context around this for
loop (what do you do when they overlap, how do you initialize stuff if any)?– Mathias Ettinger
Nov 16 '16 at 9:16
for a,b in it.combinations(listA,2):
: I see only one list used here, what is listB
good for? Also can you provide a bit more context around this for
loop (what do you do when they overlap, how do you initialize stuff if any)?– Mathias Ettinger
Nov 16 '16 at 9:16
1
1
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining a
Rectangle
: radious
, in particular.)– greybeard
Nov 16 '16 at 10:45
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining a
Rectangle
: radious
, in particular.)– greybeard
Nov 16 '16 at 10:45
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Dec. 10, 2018
The simple answer is to use a 2-d segment tree.
Given that we have low value for dimension (i.e. two), an appropriate approach would be to use a 2-d segment tree. We use what is known as stabbing query or intersection query. Store all rectangles from list one in such a tree. Query using rectangles from list two. Time to construct and query are $O(n_1 cdot log^2(n_1))$ and $O(log^2(n_1) cdot k)$, respectively, s.t. $k$ is number of matches to report. If we use fractional cascading and interval tree, times become $O(n_1 cdot log(n_1))$ and $O(log(n_1) cdot k)$, respectively. Assuming these better times, time for all queries is $O(textrm{max}(n_2, k_textrm{overall}) cdot log(n_1))$. Overall time becomes $O((textrm{max}(n_2, k_textrm{overall}) + n_1) cdot log(n_1))$. Given that $k_textrm{overall}$ is in $O(n_1 cdot n_2)$, time could be loosely $O((n_1 cdot n_2) cdot log(n_1))$.
If rectangles are not axis-aligned, one might wish to have bounding boxes and then use a tree. In principle we then could use a segment tree again, but a natural alternative (though we usually do not have good guaranteed theoretical running time for it unless we use "look-ahead") could be an R-tree.
A 2-d R-tree implementation is here:
https://github.com/bzliu94/algorithms/blob/master/r-tree/rtree.py
The important method is doOverlapQuery().
(It's a little disorganized and kludgey.)
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Dec. 10, 2018
The simple answer is to use a 2-d segment tree.
Given that we have low value for dimension (i.e. two), an appropriate approach would be to use a 2-d segment tree. We use what is known as stabbing query or intersection query. Store all rectangles from list one in such a tree. Query using rectangles from list two. Time to construct and query are $O(n_1 cdot log^2(n_1))$ and $O(log^2(n_1) cdot k)$, respectively, s.t. $k$ is number of matches to report. If we use fractional cascading and interval tree, times become $O(n_1 cdot log(n_1))$ and $O(log(n_1) cdot k)$, respectively. Assuming these better times, time for all queries is $O(textrm{max}(n_2, k_textrm{overall}) cdot log(n_1))$. Overall time becomes $O((textrm{max}(n_2, k_textrm{overall}) + n_1) cdot log(n_1))$. Given that $k_textrm{overall}$ is in $O(n_1 cdot n_2)$, time could be loosely $O((n_1 cdot n_2) cdot log(n_1))$.
If rectangles are not axis-aligned, one might wish to have bounding boxes and then use a tree. In principle we then could use a segment tree again, but a natural alternative (though we usually do not have good guaranteed theoretical running time for it unless we use "look-ahead") could be an R-tree.
A 2-d R-tree implementation is here:
https://github.com/bzliu94/algorithms/blob/master/r-tree/rtree.py
The important method is doOverlapQuery().
(It's a little disorganized and kludgey.)
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
add a comment |
up vote
4
down vote
accepted
Dec. 10, 2018
The simple answer is to use a 2-d segment tree.
Given that we have low value for dimension (i.e. two), an appropriate approach would be to use a 2-d segment tree. We use what is known as stabbing query or intersection query. Store all rectangles from list one in such a tree. Query using rectangles from list two. Time to construct and query are $O(n_1 cdot log^2(n_1))$ and $O(log^2(n_1) cdot k)$, respectively, s.t. $k$ is number of matches to report. If we use fractional cascading and interval tree, times become $O(n_1 cdot log(n_1))$ and $O(log(n_1) cdot k)$, respectively. Assuming these better times, time for all queries is $O(textrm{max}(n_2, k_textrm{overall}) cdot log(n_1))$. Overall time becomes $O((textrm{max}(n_2, k_textrm{overall}) + n_1) cdot log(n_1))$. Given that $k_textrm{overall}$ is in $O(n_1 cdot n_2)$, time could be loosely $O((n_1 cdot n_2) cdot log(n_1))$.
If rectangles are not axis-aligned, one might wish to have bounding boxes and then use a tree. In principle we then could use a segment tree again, but a natural alternative (though we usually do not have good guaranteed theoretical running time for it unless we use "look-ahead") could be an R-tree.
A 2-d R-tree implementation is here:
https://github.com/bzliu94/algorithms/blob/master/r-tree/rtree.py
The important method is doOverlapQuery().
(It's a little disorganized and kludgey.)
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Dec. 10, 2018
The simple answer is to use a 2-d segment tree.
Given that we have low value for dimension (i.e. two), an appropriate approach would be to use a 2-d segment tree. We use what is known as stabbing query or intersection query. Store all rectangles from list one in such a tree. Query using rectangles from list two. Time to construct and query are $O(n_1 cdot log^2(n_1))$ and $O(log^2(n_1) cdot k)$, respectively, s.t. $k$ is number of matches to report. If we use fractional cascading and interval tree, times become $O(n_1 cdot log(n_1))$ and $O(log(n_1) cdot k)$, respectively. Assuming these better times, time for all queries is $O(textrm{max}(n_2, k_textrm{overall}) cdot log(n_1))$. Overall time becomes $O((textrm{max}(n_2, k_textrm{overall}) + n_1) cdot log(n_1))$. Given that $k_textrm{overall}$ is in $O(n_1 cdot n_2)$, time could be loosely $O((n_1 cdot n_2) cdot log(n_1))$.
If rectangles are not axis-aligned, one might wish to have bounding boxes and then use a tree. In principle we then could use a segment tree again, but a natural alternative (though we usually do not have good guaranteed theoretical running time for it unless we use "look-ahead") could be an R-tree.
A 2-d R-tree implementation is here:
https://github.com/bzliu94/algorithms/blob/master/r-tree/rtree.py
The important method is doOverlapQuery().
(It's a little disorganized and kludgey.)
Dec. 10, 2018
The simple answer is to use a 2-d segment tree.
Given that we have low value for dimension (i.e. two), an appropriate approach would be to use a 2-d segment tree. We use what is known as stabbing query or intersection query. Store all rectangles from list one in such a tree. Query using rectangles from list two. Time to construct and query are $O(n_1 cdot log^2(n_1))$ and $O(log^2(n_1) cdot k)$, respectively, s.t. $k$ is number of matches to report. If we use fractional cascading and interval tree, times become $O(n_1 cdot log(n_1))$ and $O(log(n_1) cdot k)$, respectively. Assuming these better times, time for all queries is $O(textrm{max}(n_2, k_textrm{overall}) cdot log(n_1))$. Overall time becomes $O((textrm{max}(n_2, k_textrm{overall}) + n_1) cdot log(n_1))$. Given that $k_textrm{overall}$ is in $O(n_1 cdot n_2)$, time could be loosely $O((n_1 cdot n_2) cdot log(n_1))$.
If rectangles are not axis-aligned, one might wish to have bounding boxes and then use a tree. In principle we then could use a segment tree again, but a natural alternative (though we usually do not have good guaranteed theoretical running time for it unless we use "look-ahead") could be an R-tree.
A 2-d R-tree implementation is here:
https://github.com/bzliu94/algorithms/blob/master/r-tree/rtree.py
The important method is doOverlapQuery().
(It's a little disorganized and kludgey.)
edited 17 hours ago
answered Nov 16 '16 at 23:00
bzliu94
21213
21213
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
add a comment |
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
(This answer was given when it wasn't clear that the rectangles were aligned.)
– greybeard
Nov 18 '16 at 11:22
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
@bziliu If you could provide the implement the code, I would accept the answer
– karu
Nov 19 '16 at 22:26
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
(Just noticed your answer wasn't eligible to be awarded (half of) the bounty automatically. 100 of karu's reputation points gone - nowhere.)
– greybeard
Nov 26 '16 at 12:01
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f147177%2ffinding-overlaps-between-two-lists-of-axis-aligned-rectangles%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"overlapping of n rectangles where rectangles are stored in two separate lists." What does this mean? I don't understand what you are saying. Are you saying that we have 2 lists of rectangles
listA = [...]; listB = [...];
, and we are looking for all rectangles in listA that overlap with rectangles in listB (and vice versa)? Or that we are looking for the region that is the overlap of all rectangles? Or something else?– Justin
Nov 16 '16 at 8:51
for a,b in it.combinations(listA,2):
: I see only one list used here, what islistB
good for? Also can you provide a bit more context around thisfor
loop (what do you do when they overlap, how do you initialize stuff if any)?– Mathias Ettinger
Nov 16 '16 at 9:16
1
I understand the requirements you have, it's just that the code does not match, and I am trying to understand why.
– Mathias Ettinger
Nov 16 '16 at 9:32
Have you been introduced to line sweep / sweep line algorithms? (Please describe the meaning of the parameters defining a
Rectangle
:radious
, in particular.)– greybeard
Nov 16 '16 at 10:45
@greybeard No. What is it ? Where I can find the implementation to understand it?
– karu
Nov 16 '16 at 10:47