interval tree + priority search treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β·...
TRANSCRIPT
Interval Tree + Priority Search Trees
π£
πΌππππ‘ πΌπππβπ‘
πΌπππ πΌπππ
PST: min heap over
the π₯-coordinate
(of left endpoint)
while a BST over
the π¦-coordinate
PST: max heap over
the π₯-coordinate
(of right endpoint)
while a BST over
the π¦-coordinate
π₯πππ
Query object: vertical segment ππ₯ Γ [ππ¦ , ππ¦β² ]
rootSet: π horizontal line segments
Applicable to axis-parallel segments only.
IT
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
General Windowing Queries
Line segments can have arbitrary orientations.
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
β’ Replace each segment with its bounding box.
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
β’ Replace each segment with its bounding box.
β’ Find all bounding boxes intersected the query.
β’ Check the segments defining these boxes.
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
β’ Replace each segment with its bounding box.
β’ Find all bounding boxes intersected the query.
β’ Check the segments defining these boxes.
Works well generally in practice.
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
β’ Replace each segment with its bounding box.
β’ Find all bounding boxes intersected the query.
β’ Check the segments defining these boxes.
Works well generally in practice.
Worst case can be very bad.
No segment intersects
π even though all the
bounding boxes do!
π€
π€
General Windowing Queries
Line segments can have arbitrary orientations.
A bounding box approach?
β’ Replace each segment with its bounding box.
β’ Find all bounding boxes intersected the query.
β’ Check the segments defining these boxes.
Works well generally in practice.
Worst case can be very bad.
No segment intersects
π even though all the
bounding boxes do!
π€
π€
No guarantee on a fast query time.
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Segments intersecting the window boundary.
One intersection query with each of the 4 boundary edges.
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Segments intersecting the window boundary.
One intersection query with each of the 4 boundary edges.
Focus on a vertical boundary edge.
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Segments intersecting the window boundary.
One intersection query with each of the 4 boundary edges.
Focus on a vertical boundary edge.
Query problem
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Segments intersecting the window boundary.
One intersection query with each of the 4 boundary edges.
Focus on a vertical boundary edge.
Query problem
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Thinking Top-Down
Segments with β₯ 1 endpoints in π.
Range trees.
Segments intersecting the window boundary.
One intersection query with each of the 4 boundary edges.
Focus on a vertical boundary edge.
Query problem
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set: π = π 1, π 2, β¦ , π π of π segments
β’ arbitrarily oriented
β’ non-intersecting in the interior
β’ possibly sharing endpoints
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
π₯ πππ π£ > π π₯π₯ π₯ π₯ πππ π πππ π£ > ππ₯
Checking if left endpoint β (ββ, ππ₯] Γ [ππ¦, ππ¦β² ]
(for a horizontal segment).
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
π₯ πππ π£ > π π₯π₯ π₯ π₯ πππ π πππ π£ > ππ₯
Checking if left endpoint β (ββ, ππ₯] Γ [ππ¦, ππ¦β² ]
(for a horizontal segment).
No such reduction to range
query when the segment is
arbitrarily oriented
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
π₯ πππ π£ > π π₯π₯ π₯ π₯ πππ π πππ π£ > ππ₯
Checking if left endpoint β (ββ, ππ₯] Γ [ππ¦, ππ¦β² ]
(for a horizontal segment).
No such reduction to range
query when the segment is
arbitrarily oriented
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
π₯ πππ π£ > π π₯π₯ π₯ π₯ πππ π πππ π£ > ππ₯
Checking if left endpoint β (ββ, ππ₯] Γ [ππ¦, ππ¦β² ]
(for a horizontal segment).
No such reduction to range
query when the segment is
arbitrarily oriented
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
π
Segment π 1 β πΌπππ(π£) intersects πbut its left endpoint β (ββ, ππ₯] Γ [ππ¦ , ππ¦
β² ].
π 2
π 1
Inapplicability of an Interval Tree
An interval tree is not very helpful.
Search with ππ₯ πΌ πππ π£ dat node π£at node π£
π₯ πππ π£ > π π₯π₯ π₯ π₯ πππ π πππ π£ > ππ₯
Checking if left endpoint β (ββ, ππ₯] Γ [ππ¦, ππ¦β² ]
(for a horizontal segment).
No such reduction to range
query when the segment is
arbitrarily oriented
ββ,ππ₯ Γ [ππ¦, ππ¦β² ]
π₯πππ
π 3
Segment π 3 β πΌπππ(π£) and
its left endpoint β (ββ, ππ₯] Γ [ππ¦ , ππ¦β² ].
But it has no intersection with π.
π
Segment π 1 β πΌπππ(π£) intersects πbut its left endpoint β (ββ, ππ₯] Γ [ππ¦ , ππ¦
β² ].
Locus Approach
Window π: ππ₯, ππ₯β² Γ [ππ¦ , ππ¦
β² ] defined by four parameters.
Idea: Partition the parameter space into regions such that queries in
the region have the same answer.
Locus Approach
Window π: ππ₯, ππ₯β² Γ [ππ¦ , ππ¦
β² ] defined by four parameters.
Idea: Partition the parameter space into regions such that queries in
the region have the same answer.
Input: interval set: πΌ = { π₯1, π₯1β² , π₯2, π₯2
β² , β¦ , π₯π, π₯πβ² }
1D Query
point ππ₯
Output: all intervals containing ππ₯
Locus Approach
Window π: ππ₯, ππ₯β² Γ [ππ¦ , ππ¦
β² ] defined by four parameters.
Idea: Partition the parameter space into regions such that queries in
the region have the same answer.
Input: interval set: πΌ = { π₯1, π₯1β² , π₯2, π₯2
β² , β¦ , π₯π, π₯πβ² }
1D Query
point ππ₯
Output: all intervals containing ππ₯
π1, π2, β¦ , ππ: distinct interval endpoints in the increasing order.
ππβ1π1 π2 ππ
Locus Approach
Window π: ππ₯, ππ₯β² Γ [ππ¦ , ππ¦
β² ] defined by four parameters.
Idea: Partition the parameter space into regions such that queries in
the region have the same answer.
Input: interval set: πΌ = { π₯1, π₯1β² , π₯2, π₯2
β² , β¦ , π₯π, π₯πβ² }
1D Query
point ππ₯
Output: all intervals containing ππ₯
π1, π2, β¦ , ππ: distinct interval endpoints in the increasing order.
ππβ1π1 π2 ππ
The parameter space ββ,β is partitioned into elementary intervals.
ββ, π1 , π1, π1 , π1, π2 , π2, π2 ,β¦ , ππβ1, ππ , ππ, ππ , ππ, β
Locus Approach
Window π: ππ₯, ππ₯β² Γ [ππ¦ , ππ¦
β² ] defined by four parameters.
Idea: Partition the parameter space into regions such that queries in
the region have the same answer.
Input: interval set: πΌ = { π₯1, π₯1β² , π₯2, π₯2
β² , β¦ , π₯π, π₯πβ² }
1D Query
point ππ₯
Output: all intervals containing ππ₯
π1, π2, β¦ , ππ: distinct interval endpoints in the increasing order.
ππβ1π1 π2 ππ
The parameter space ββ,β is partitioned into elementary intervals.
ββ, π1 , π1, π1 , π1, π2 , π2, π2 ,β¦ , ππβ1, ππ , ππ, ππ , ππ, β
Every open interval has two consecutive endpoints.
Every closed interval consists of a single endpoint.
Open intervals alternate with closed intervals.
Using a Binary Search Tree?
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Using a Binary Search Tree?
Store at the leaf π all the intervals
in πΌ that contain Int π .
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Using a Binary Search Tree?
Store at the leaf π all the intervals
in πΌ that contain Int π .
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Query time
π(log π + π)
Using a Binary Search Tree?
Store at the leaf π all the intervals
in πΌ that contain Int π .
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Query time
π(log π + π)
BST search #reported
intervals
Using a Binary Search Tree?
Store at the leaf π all the intervals
in πΌ that contain Int π .
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Query time
π(log π + π)
BST search #reported
intervals
High storage if the intervals overlap
a lot.
Using a Binary Search Tree?
Store at the leaf π all the intervals
in πΌ that contain Int π .
π
ββ, π1 π1, π1 β¦ ππ , ππ+1 β¦ ππ, ππ ππ, β
Int π : elementary interval
corresponding to π.
π : a leaf
Query time
π(log π + π)
BST search #reported
intervals
High storage if the intervals overlap
a lot.
π π2 possible!
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Observation A search path ends at
π1, π2, π3 , π4 if and only if
it passes through π£.
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Why not store π only two times
at π£ and π5?
Observation A search path ends at
π1, π2, π3 , π4 if and only if
it passes through π£.
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Why not store π only two times
at π£ and π5?
Observation A search path ends at
π1, π2, π3 , π4 if and only if
it passes through π£.
Idea: Store a segment π at the fewest nodes whose corresponding intervals
form a partitioning of π .
Store an Interval As High as Possible
π2π1 π3 π4 π5
π£
π
Interval π is stored five times at
π1, π2, π3 , π4, π5.
Why not store π only two times
at π£ and π5?
Observation A search path ends at
π1, π2, π3 , π4 if and only if
it passes through π£.
Idea: Store a segment π at the fewest nodes whose corresponding intervals
form a partitioning of π .
These nodes must be as high as possible in the tree.
Segment Tree
(ββ,β)
(ββ, π4]
[π1, π1] (π2, π3)(ββ, π1) [π2, π2] [π3, π3] [π4, π4] [π5, π5] [π6, π6] [π7, π7](π3, π4) (π4, π5) (π5, π6) (π7, β)(π6, π7)(π1, π2)
(ββ, π1] (π1, π2] (π2, π3] (π3, π4] (π4, π5] (π5, π6] (π6, π7]
(ββ, π2] (π2, π4] (π4, π6] (π6, β)
(π4, β)
π1 π2 π7π6π5π4π3π 1 π 3
π 2 π 4
π 5
Segment Tree
(ββ,β)
(ββ, π4]
[π1, π1] (π2, π3)(ββ, π1) [π2, π2] [π3, π3] [π4, π4] [π5, π5] [π6, π6] [π7, π7](π3, π4) (π4, π5) (π5, π6) (π7, β)(π6, π7)(π1, π2)
(ββ, π1] (π1, π2] (π2, π3] (π3, π4] (π4, π5] (π5, π6] (π6, π7]
(ββ, π2] (π2, π4] (π4, π6] (π6, β)
(π4, β)
π1 π2 π7π6π5π4π3π 1 π 3
π 2 π 4
π 5
{π 1} {π 1}{π 3} {π 3, π 4}
{π 2, π 5} {π 5}
Tree Structure
Leaves β elementary intervals (left-to-right)
Tree Structure
Leaves β elementary intervals (left-to-right)
π£
Tree Structure
Leaves β elementary intervals (left-to-right)
Internal node π£ β union Int(π£) of elementary intervals at the leaves in the
subtree π―(π£) rooted at π£.
π£
Tree Structure
Leaves β elementary intervals (left-to-right)
Internal node π£ β union Int(π£) of elementary intervals at the leaves in the
subtree π―(π£) rooted at π£.
π£
At π£ stores Int(π£) and the canonical subset (as a linked list) defined below:
Tree Structure
Leaves β elementary intervals (left-to-right)
Internal node π£ β union Int(π£) of elementary intervals at the leaves in the
subtree π―(π£) rooted at π£.
π£
At π£ stores Int(π£) and the canonical subset (as a linked list) defined below:
πΆ π£ = π₯, π₯β² β πΌ Int π£ β [π₯, π₯β²]and Int(parent π£) β [π₯, π₯β²]}
Tree Structure
Leaves β elementary intervals (left-to-right)
Internal node π£ β union Int(π£) of elementary intervals at the leaves in the
subtree π―(π£) rooted at π£.
π£
At π£ stores Int(π£) and the canonical subset (as a linked list) defined below:
πΆ π£ = π₯, π₯β² β πΌ Int π£ β [π₯, π₯β²]and Int(parent π£) β [π₯, π₯β²]}
Store intervals at nodes
as high as possible.
A Closer Look at Storage
πΆ π£ = π₯, π₯β² β πΌ Int π£ β [π₯, π₯β²] and Int(parent(π£)) β [π₯, π₯β²]}
π’
π£
Int π’ β [π₯, π₯β²]
Int π£ β [π₯, π₯β²]πΆ(π£):(π₯, π₯β²)
Linked list
Number of Leaves
π = πΌ : #segments
ππ , ππ+1 πβ 1
[ππ , ππ] π
(ββ, π1) 1
(ππ, β) 1
π β€ 2π: #distinct endpoints
Number of Leaves
β’ 2π + 1 β€ 4π + 1 leaves
π = πΌ : #segments
ππ , ππ+1 πβ 1
[ππ , ππ] π
(ββ, π1) 1
(ππ, β) 1
π β€ 2π: #distinct endpoints
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
π£1 π£2 π£3
parent(π£2)
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)Suppose its sibling is π£1 without loss of generality.
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)Suppose its sibling is π£1 without loss of generality.
Int π£1 β [π₯, π₯β²]
Int π£2 β [π₯, π₯β²]
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)Suppose its sibling is π£1 without loss of generality.
Int π£1 β [π₯, π₯β²]
Int π£2 β [π₯, π₯β²]
Int(parent(π£2)) = Int(π£1) βͺ Int(π£2) β [π₯, π₯β²]
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)Suppose its sibling is π£1 without loss of generality.
Int π£1 β [π₯, π₯β²]
Int π£2 β [π₯, π₯β²]
Int(parent(π£2)) = Int(π£1) βͺ Int(π£2) β [π₯, π₯β²]
[π₯, π₯β²] should be stored at parent(π£2) or above instead of at π£2. Contradiction.
Storage of an Interval
Claim Each interval [π₯, π₯β²] β πΌ is stored at β€ 2 nodes at any depth.
Proof Suppose the interval is stored at the nodes π£1, π£2, β¦ , π£π, π β₯ 3, at
the same depth in the left to right order.
Then π£2 must be a sibling of either π£1 or π£3.
π£1 π£2 π£3
parent(π£2)Suppose its sibling is π£1 without loss of generality.
Int π£1 β [π₯, π₯β²]
Int π£2 β [π₯, π₯β²]
Int(parent(π£2)) = Int(π£1) βͺ Int(π£2) β [π₯, π₯β²]
[π₯, π₯β²] should be stored at parent(π£2) or above instead of at π£2. Contradiction.
Total Storage
Following the claim, any interval is stored at most twice at any
given length.
Total Storage
Following the claim, any interval is stored at most twice at any
given length.
The required storage at each depth is π(π).
Total Storage
Following the claim, any interval is stored at most twice at any
given length.
The required storage at each depth is π(π).
Maximum tree depth (i.e., height) is π(log π).
Total Storage
Following the claim, any interval is stored at most twice at any
given length.
The required storage at each depth is π(π).
Maximum tree depth (i.e., height) is π(log π).
π(π log π)
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯QuerySegmentTree(root, ππ₯)
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯QuerySegmentTree(root, ππ₯)
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯
Output in order:
π 2, π 5, π 1
QuerySegmentTree(root, ππ₯)
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯
Output in order:
π 2, π 5, π 1
Query time:
π(log π + π)
QuerySegmentTree(root, ππ₯)
root
Query Algorithm
QuerySegmentTree(π£, ππ₯)
1. report all the intervals in πΆ(π£) // canonical subset2. if π£ is not a leaf3. then if ππ₯ β Int(ππ(π£))4. then QuerySegmentTree(ππ π£ , ππ₯)5. else QuerySegmentTree(ππ π£ , ππ₯)
ππ₯
Example: ππ₯
Output in order:
π 2, π 5, π 1
Query time:
π(log π + π)
#reported intervals
QuerySegmentTree(root, ππ₯)
root
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
π(π log π)
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
Construct a balanced BST in a bottom-up way.
π(π log π)
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
Construct a balanced BST in a bottom-up way.
Determine interval πΌ(π£) for each node π£.
π(π log π)
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
Construct a balanced BST in a bottom-up way.
Determine interval πΌ(π£) for each node π£.
π(π log π)
π(π) for all the nodes together
Segment Tree Construction
Sort endpoints from πΌ to yield elementary intervals.
Construct a balanced BST in a bottom-up way.
Determine interval πΌ(π£) for each node π£.
Compute canonical subsets by inserting original intervals [π₯, π₯β²]from πΌ one by one.
π(π log π)
π(π) for all the nodes together
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
β’ At each visited node π£, either [π₯, π₯β²] is stored or Int(π£) contains an endpoint
of [π₯, π₯β²].
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
β’ At each visited node π£, either [π₯, π₯β²] is stored or Int(π£) contains an endpoint
of [π₯, π₯β²].
An interval is stored β€ 2 times at each level.
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
β’ At each visited node π£, either [π₯, π₯β²] is stored or Int(π£) contains an endpoint
of [π₯, π₯β²].
An interval is stored β€ 2 times at each level.
At each depth, there exist
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
β’ At each visited node π£, either [π₯, π₯β²] is stored or Int(π£) contains an endpoint
of [π₯, π₯β²].
An interval is stored β€ 2 times at each level.
At each depth, there exist
β€ 1 node π’ such that π₯ β Int(π’)
Segment Tree Insertion
InsertSegmentTree(π£, [π₯, π₯β²])
1. if Int(π£)β [π₯, π₯β²] // Int(parent(π£))β [π₯, π₯β²] holds2. then store [π₯, π₯β²] at π£3. else if Int(ππ π£ )β© [π₯, π₯β²] β β 4. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
5. if Int(ππ π£ )β© [π₯, π₯β²] β β 6. then InsertSegmentTree(ππ π£ , [π₯, π₯β²])
β’ At each visited node π£, either [π₯, π₯β²] is stored or Int(π£) contains an endpoint
of [π₯, π₯β²].
An interval is stored β€ 2 times at each level.
At each depth, there exist
β€ 1 node π’ such that π₯ β Int(π’)
β€ 1 node π’β² such that π₯β² β Int(π’β²)
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
β€ 4 nodes visited per level.
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
β€ 4 nodes visited per level.
π log π time to insert an interval.me to insert an interval.
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
β€ 4 nodes visited per level.
π log π time to insert an interval.me to insert an interval.
π π log π time for segment tree construction.
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
β€ 4 nodes visited per level.
π log π time to insert an interval.me to insert an interval.
π π log π time for segment tree construction.
Compared with an interval tree, a segment tree has
the same query time π(log π + π)
a larger storage π(π log π) than π π ,
Construction Time
β’ β€ 2 storage actions + β€ 2 containments
β€ 4 nodes visited per level.
π log π time to insert an interval.me to insert an interval.
π π log π time for segment tree construction.
Compared with an interval tree, a segment tree has
the same query time π(log π + π)
a larger storage π(π log π) than π π ,
but it allows to answer more complicated queries.
Back to Windowing
Query segment: π = ππ₯ Γ [ππ¦, ππ¦β² ]
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
π(π£2) π(π£3)
π(π£1)
π 1
π 3
π 2
π 4
π 5
π 6
π 7
π£1
π£2 π£3
πΆ π£1 = {π 3}
πΆ π£3 = {π 4, π 6}πΆ π£2 = {π 1, π 2}
Over π arbitrarily oriented segments
Back to Windowing
Query segment: π = ππ₯ Γ [ππ¦, ππ¦β² ]
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
Construct a segment tree π―.
π(π£2) π(π£3)
π(π£1)
π 1
π 3
π 2
π 4
π 5
π 6
π 7
π£1
π£2 π£3
πΆ π£1 = {π 3}
πΆ π£3 = {π 4, π 6}πΆ π£2 = {π 1, π 2}
Over π arbitrarily oriented segments
Back to Windowing
Query segment: π = ππ₯ Γ [ππ¦, ππ¦β² ]
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
Construct a segment tree π―.
On the π₯-intervals of the segments in π.
Canonical subset πΆ(π£) at a vertex π£ store segments rather than their π₯-intervals.
π(π£2) π(π£3)
π(π£1)
π 1
π 3
π 2
π 4
π 5
π 6
π 7
π£1
π£2 π£3
πΆ π£1 = {π 3}
πΆ π£3 = {π 4, π 6}πΆ π£2 = {π 1, π 2}
Over π arbitrarily oriented segments
Back to Windowing
Query segment: π = ππ₯ Γ [ππ¦, ππ¦β² ]
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
Construct a segment tree π―.
Segment π β πΆ(π£) if it crosses the slab π π£ : Int(π£) Γ (ββ,β)but does not cross its parentβs slab.
On the π₯-intervals of the segments in π.
Canonical subset πΆ(π£) at a vertex π£ store segments rather than their π₯-intervals.
π(π£2) π(π£3)
π(π£1)
π 1
π 3
π 2
π 4
π 5
π 6
π 7
π£1
π£2 π£3
πΆ π£1 = {π 3}
πΆ π£3 = {π 4, π 6}πΆ π£2 = {π 1, π 2}
Over π arbitrarily oriented segments
Back to Windowing
Query segment: π = ππ₯ Γ [ππ¦, ππ¦β² ]
π
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
Construct a segment tree π―.
Segment π β πΆ(π£) if it crosses the slab π π£ : Int(π£) Γ (ββ,β)but does not cross its parentβs slab.
On the π₯-intervals of the segments in π.
Canonical subset πΆ(π£) at a vertex π£ store segments rather than their π₯-intervals.
π(π£2) π(π£3)
π(π£1)
π 1
π 3
π 2
π 4
π 5
π 6
π 7
π£1
π£2 π£3
πΆ π£1 = {π 3}
πΆ π£3 = {π 4, π 6}πΆ π£2 = {π 1, π 2}
Over π arbitrarily oriented segments
Segments on the Search Path
β’ Search with ππ₯ in π―. (ββ,β)
Segment
tree
Segments on the Search Path
β’ Search with ππ₯ in π―. (ββ,β)
π£
Segment
tree
Segments on the Search Path
β’ Search with ππ₯ in π―. (ββ,β)
π£
Segment
treeβ’ π(logπ) canonical sets together include
all the segments intersected by the
vertical line π₯ = ππ₯.
Segments on the Search Path
β’ Search with ππ₯ in π―. (ββ,β)
π£
Segment
tree
πΆ(π£)
β’ π£ is a node on the search path.
β’ π(logπ) canonical sets together include
all the segments intersected by the
vertical line π₯ = ππ₯.
Segments on the Search Path
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
π
π
β’ Search with ππ₯ in π―. (ββ,β)
π£
Segment
tree
πΆ(π£)
β’ Segment π β πΆ(π£) is intersected by π iff
β’ π£ is a node on the search path.
β’ π(logπ) canonical sets together include
all the segments intersected by the
vertical line π₯ = ππ₯.
Segments on the Search Path
(ππ₯, ππ¦)
(ππ₯, ππ¦β² )
π
π
β’ Search with ππ₯ in π―. (ββ,β)
π£
Segment
tree
πΆ(π£)
β’ Segment π β πΆ(π£) is intersected by π iff
β’ π£ is a node on the search path.
(ππ₯, ππ¦) below π and (ππ₯, ππ¦β² ) above π
β’ π(logπ) canonical sets together include
all the segments intersected by the
vertical line π₯ = ππ₯.
Storage of a Canonical Set
β’ Segments in πΆ(π£) do not intersect each other.(ββ,β)
π£πΆ(π£)
β’ Each segment is over an π₯-interval containing
Int π£ .
Int(π£)
π 1
π 2
π 3
π 4
π 5
Slab
Storage of a Canonical Set
β’ Segments in πΆ(π£) do not intersect each other.(ββ,β)
π£πΆ(π£)
β’ These segments span the slab Int π£ Γ [ββ,β].
β’ Each segment is over an π₯-interval containing
Int π£ .
Int(π£)
π 1
π 2
π 3
π 4
π 5
β’ They do not intersect each other in the interior.Slab
Storage of a Canonical Set
β’ Segments in πΆ(π£) do not intersect each other.(ββ,β)
π£πΆ(π£)
β’ These segments span the slab Int π£ Γ [ββ,β].
β’ Each segment is over an π₯-interval containing
Int π£ .
Int(π£)
π 1
π 2
π 3
π 4
π 5
β’ They do not intersect each other in the interior.
β’ Store the segments in a balanced BST β¬ π£ in
the vertical order.
Slab
Storage of a Canonical Set
β’ Segments in πΆ(π£) do not intersect each other.(ββ,β)
π£πΆ(π£)
β’ These segments span the slab Int π£ Γ [ββ,β].
β’ Each segment is over an π₯-interval containing
Int π£ .
Int(π£)
π 1
π 2
π 3
π 4
π 5
β’ They do not intersect each other in the interior.
β’ Store the segments in a balanced BST β¬ π£ in
the vertical order.
π 3
π 5 π 2
π 1π 4
Slab
associate structure β¬ π£
Storage of a Canonical Set
β’ Segments in πΆ(π£) do not intersect each other.(ββ,β)
π£πΆ(π£)
β’ These segments span the slab Int π£ Γ [ββ,β].
β’ Each segment is over an π₯-interval containing
Int π£ .
Int(π£)
π 1
π 2
π 3
π 4
π 5
β’ They do not intersect each other in the interior.
β’ Store the segments in a balanced BST β¬ π£ in
the vertical order.
π 3
π 5 π 2
π 1π 4
π(log π + ππ£) query time
within β¬ π£
Slab
#intersected
segments in πΆ(π£)
associate structure β¬ π£
Segment Tree Storage Summary
The set π of segments is stored in a segment tree π― based on
their π₯-intervals.
The canonical subset πΆ(π£) of every internal node π£ in π― is stored in
a BST β¬(π£) based on
the vertical order within the slab Int π£ Γ [ββ,β].
Total storage: π(π log π)
Construction time: π(π log π)
Query Algorithm
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
(ββ,β)
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
(ββ,β)
π£πΆ(π£)
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β¬ π£
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β¬ π£
(ππ₯, ππ¦)
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β¦
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β¦
segments satisfying the query
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β’ π(log π) nodes on the search path, each requiring
such a search.
β’ The search takes π(log π + ππ£) time.
β¦
segments satisfying the query
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )
Query Algorithm
Search with ππ₯ in a segment tree π―.
Query object: a vertical line segment π: ππ₯ Γ [ππ¦, ππ¦β² ]
Set π = π 1, π 2, β¦ , π π of arbitrarily oriented segments
At every node π£ on the search path, search with the two endpoints
of π to report segments in πΆ(π£) intersected by π.
(ββ,β)
π£πΆ(π£)
β’ π(log π) nodes on the search path, each requiring
such a search.
β’ The search takes π(log π + ππ£) time.
Query time: π(log2π + π)β¦
segments satisfying the query
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )