CSC 378: Data Structures and Algorithm Analysis

BINARY SPACE PARTITIONS



A 'scene' is a set of line segments in the plane. They could represent building walls. Each segment has a height :
 

Problem
How to display the walls from a viewpoint?

Solution I
Draw the walls in arbitrary order.

But - hidden walls might be drawn after visible walls, and hence drawn over them.

Solution II
Draw the walls in order of decreasing distance.

Idea
Partition the plane in two!

1. First draw things on the side of the partition not containing the viewpoint ( v ).
2. Then draw things on the side containing the viewpoint.

Step 1 draws distant walls first.
Step 2 requires a recursive subdivision of the side containing the viewpoint.
Idea
Extend the walls themselves into partitioning lines!

Algorithm
Make a cut, then recursively partition the two sides.
Where a partition line intersects a segment, split the segment and put the pieces on either side.



Binary Space Partition
There's a tree structure here!
A node represents a partition line.
Left and right subtrees are the partitions on either side.

Definition

A partition has a positive side (which does not include the origin) and a negative side (which does include it).

Let's put the  side in a partition node's left subtree, and the  side in the right.

From before -
And to display our walls -
Note that each node stores the wall that defines that node's partition line, and also that 'v' denotes the viewpoint.

Draw(v,root)
if v is on the  side of partition(root)
Draw(v,right(root))
Draw wall(root)
Draw(v,left(root))
Else
Draw(v,left(root))
Draw wall(root)
Draw(v,right(root))

Insert the walls into the tree one-by-one -
Insert(w,root)
if w is on the  side of root partition
Insert(w,left(root))
else if w is on the  side
Insert(w,right(root))
else if w is cut by root partition
split w into w+ and w-
Insert(w-,left(root))
Insert(w+,right(root))
Example



In order to insert D as shown -


- we take the following steps:
Insert( D, A )
 bsP_1.gif split D into D+ and D-
Insert( D-, left( A ) )
Insert( D+, right( A ) )

Insert( D-, C )
 bsP_2.gif becomes right child of C

Insert( D+, B )
 bsP_3.gif Split D+ into D+- and D+ +
Insert( D+-, left( B ) )
Insert( D+ +, right( B ) )- these become L and R children

Result :
Leaves, if present, represent regions of the plane, as indicated in the following diagram :
Analysis of Draw( )
Draw( ) visits every node once.

T( Draw ) bsP_4.gif O( # nodes in the BSP )
- where # nodes in BSP = n + # cuts
- where n is the original number of edges (i.e. walls).

But # of cuts bsP_5.gif , since each edge might cut O( n ) other edges, like so:

Theorem
If edges are inserted in random order, E( # nodes ) bsP_6.gif !

Conclusion
It is better to read the edges, randomize their order, then insert them.

Theorem
Let bsP_7.gif
be a random permutation of bsP_8.gif

Insert segments of bsP_9.gif
in order bsP_10.gif

The expected BSP size is in O( n log n ).

Proof

size = n + E( # of cuts )

When does an edge u cut an edge v?
 bsP_11.gif u must already be in the tree when v is inserted
and bsP_12.gif

Suppose we have :

Then u cuts v if u was inserted before bsP_13.gifbsP_14.gifbsP_15.gif , and v
- otherwise, line( u ) stops at the already-inserted edge.

i.e. In the subsequence of bsP_16.gif containing u, v, bsP_17.gif . . . bsP_18.gif , u must be first.

Let index( u, v ) = # of other edges that intersect line( u ) between u and v (in the illustration ablove, there are three).

Then P( u cuts v ) = P( u is first in the subsequence containing bsP_19.gif bsP_20.gif . . . bsP_21.gif u v )
bsP_22.gif

Note that 'index' means -

Expected Size
bsP_23.gif

 bsP_24.gif
 bsP_25.gif

 bsP_26.gif

bsP_27.gif
 bsP_28.gif

 bsP_29.gif

drawing takes expected O( n log n ) time!




Back to the lecture index

Copyright 2001     James Stewart