Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β·...

Preview:

Citation preview

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

ℬ 𝑣

(π‘žπ‘₯, π‘žπ‘¦) (π‘žπ‘₯, π‘žπ‘¦β€² )

Recommended