<s>
In	O
computational	O
geometry	O
,	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
is	O
a	O
sweep	B-Algorithm
line	I-Algorithm
algorithm	I-Algorithm
for	O
listing	O
all	O
crossings	O
in	O
a	O
set	O
of	O
line	O
segments	O
,	O
i.e.	O
</s>
<s>
For	O
an	O
input	O
consisting	O
of	O
line	O
segments	O
with	O
crossings	O
(	O
or	O
intersections	O
)	O
,	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
takes	O
time	O
.	O
</s>
<s>
Although	O
asymptotically	O
faster	O
algorithms	O
are	O
now	O
known	O
by	O
and	O
,	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
remains	O
a	O
practical	O
choice	O
due	O
to	O
its	O
simplicity	O
and	O
low	O
memory	O
requirements	O
.	O
</s>
<s>
The	O
main	O
idea	O
of	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
is	O
to	O
use	O
a	O
sweep	B-Algorithm
line	I-Algorithm
approach	O
,	O
in	O
which	O
a	O
vertical	O
line	O
L	O
moves	O
from	O
left	O
to	O
right	O
(	O
or	O
,	O
e.g.	O
,	O
from	O
top	O
to	O
bottom	O
)	O
across	O
the	O
plane	O
,	O
intersecting	O
the	O
input	O
line	O
segments	O
in	O
sequence	O
as	O
it	O
moves	O
.	O
</s>
<s>
The	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
itself	O
maintains	O
data	B-General_Concept
structures	I-General_Concept
representing	O
the	O
current	O
vertical	O
ordering	O
of	O
the	O
intersection	O
points	O
of	O
the	O
sweep	B-Algorithm
line	I-Algorithm
with	O
the	O
input	O
line	O
segments	O
,	O
and	O
a	O
collection	O
of	O
potential	O
future	O
events	O
formed	O
by	O
adjacent	O
pairs	O
of	O
intersection	O
points	O
.	O
</s>
<s>
It	O
processes	O
each	O
event	O
in	O
turn	O
,	O
updating	O
its	O
data	B-General_Concept
structures	I-General_Concept
to	O
represent	O
the	O
new	O
set	O
of	O
intersection	O
points	O
.	O
</s>
<s>
In	O
order	O
to	O
efficiently	O
maintain	O
the	O
intersection	O
points	O
of	O
the	O
sweep	B-Algorithm
line	I-Algorithm
L	O
with	O
the	O
input	O
line	O
segments	O
and	O
the	O
sequence	O
of	O
future	O
events	O
,	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
maintains	O
two	O
data	B-General_Concept
structures	I-General_Concept
:	O
</s>
<s>
A	O
binary	B-Language
search	I-Language
tree	I-Language
(	O
the	O
"	O
sweep	B-Algorithm
line	I-Algorithm
status	O
tree	O
"	O
)	O
,	O
containing	O
the	O
set	O
of	O
input	O
line	O
segments	O
that	O
cross	O
L	O
,	O
ordered	O
by	O
the	O
y-coordinates	O
of	O
the	O
points	O
where	O
these	O
segments	O
cross	O
L	O
.	O
The	O
crossing	O
points	O
themselves	O
are	O
not	O
represented	O
explicitly	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
The	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
will	O
insert	O
a	O
new	O
segment	O
s	O
into	O
this	O
data	B-General_Concept
structure	I-General_Concept
when	O
the	O
sweep	B-Algorithm
line	I-Algorithm
L	O
crosses	O
the	O
left	O
endpoint	O
p	O
of	O
this	O
segment	O
(	O
i.e.	O
</s>
<s>
the	O
endpoint	O
of	O
the	O
segment	O
with	O
the	O
smallest	O
x-coordinate	O
,	O
provided	O
the	O
sweep	B-Algorithm
line	I-Algorithm
L	O
starts	O
from	O
the	O
left	O
,	O
as	O
explained	O
above	O
in	O
this	O
article	O
)	O
.	O
</s>
<s>
The	O
correct	O
position	O
of	O
segment	O
s	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
may	O
be	O
determined	O
by	O
a	O
binary	O
search	O
,	O
each	O
step	O
of	O
which	O
tests	O
whether	O
p	O
is	O
above	O
or	O
below	O
some	O
other	O
segment	O
that	O
is	O
crossed	O
by	O
L	O
.	O
Thus	O
,	O
an	O
insertion	O
may	O
be	O
performed	O
in	O
logarithmic	O
time	O
.	O
</s>
<s>
The	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
will	O
also	O
delete	O
segments	O
from	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
and	O
use	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
to	O
determine	O
the	O
segments	O
that	O
are	O
immediately	O
above	O
or	O
below	O
other	O
segments	O
;	O
these	O
operations	O
may	O
be	O
performed	O
using	O
only	O
the	O
tree	O
structure	O
itself	O
without	O
reference	O
to	O
the	O
underlying	O
geometry	O
of	O
the	O
segments	O
.	O
</s>
<s>
A	O
priority	B-Application
queue	I-Application
(	O
the	O
"	O
event	O
queue	O
"	O
)	O
,	O
used	O
to	O
maintain	O
a	O
sequence	O
of	O
potential	O
future	O
events	O
in	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
In	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
,	O
the	O
potential	O
future	O
events	O
consist	O
of	O
line	O
segment	O
endpoints	O
that	O
have	O
not	O
yet	O
been	O
swept	O
over	O
,	O
and	O
the	O
points	O
of	O
intersection	O
of	O
pairs	O
of	O
lines	O
containing	O
pairs	O
of	O
segments	O
that	O
are	O
immediately	O
above	O
or	O
below	O
each	O
other	O
.	O
</s>
<s>
The	O
algorithm	O
does	O
not	O
need	O
to	O
maintain	O
explicitly	O
a	O
representation	O
of	O
the	O
sweep	B-Algorithm
line	I-Algorithm
L	O
or	O
its	O
position	O
in	O
the	O
plane	O
.	O
</s>
<s>
The	O
binary	B-Language
search	I-Language
tree	I-Language
may	O
be	O
any	O
balanced	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
data	B-General_Concept
structure	I-General_Concept
,	O
such	O
as	O
a	O
red	O
–	O
black	O
tree	O
;	O
all	O
that	O
is	O
required	O
is	O
that	O
insertions	O
,	O
deletions	O
,	O
and	O
searches	O
take	O
logarithmic	O
time	O
.	O
</s>
<s>
Similarly	O
,	O
the	O
priority	B-Application
queue	I-Application
may	O
be	O
a	O
binary	B-Application
heap	I-Application
or	O
any	O
other	O
logarithmic-time	O
priority	B-Application
queue	I-Application
;	O
more	O
sophisticated	O
priority	B-Application
queues	I-Application
such	O
as	O
a	O
Fibonacci	B-Application
heap	I-Application
are	O
not	O
necessary	O
.	O
</s>
<s>
Note	O
that	O
the	O
space	O
complexity	O
of	O
the	O
priority	B-Application
queue	I-Application
depends	O
on	O
the	O
data	B-General_Concept
structure	I-General_Concept
used	O
to	O
implement	O
it	O
.	O
</s>
<s>
The	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
performs	O
the	O
following	O
steps	O
.	O
</s>
<s>
Initialize	O
a	O
priority	B-Application
queue	I-Application
Q	O
of	O
potential	O
future	O
events	O
,	O
each	O
associated	O
with	O
a	O
point	O
in	O
the	O
plane	O
and	O
prioritized	O
by	O
the	O
x-coordinate	O
of	O
the	O
point	O
.	O
</s>
<s>
Initialize	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
T	O
of	O
the	O
line	O
segments	O
that	O
cross	O
the	O
sweep	B-Algorithm
line	I-Algorithm
L	O
,	O
ordered	O
by	O
the	O
y-coordinates	O
of	O
the	O
crossing	O
points	O
.	O
</s>
<s>
If	O
p	O
is	O
the	O
left	O
endpoint	O
of	O
a	O
line	O
segment	O
s	O
,	O
insert	O
s	O
into	O
T	O
.	O
Find	O
the	O
line-segments	O
r	O
and	O
t	O
that	O
are	O
respectively	O
immediately	O
above	O
and	O
below	O
s	O
in	O
T	O
(	O
if	O
they	O
exist	O
)	O
;	O
if	O
the	O
crossing	O
of	O
r	O
and	O
t	O
(	O
the	O
neighbours	O
of	O
s	O
in	O
the	O
status	O
data	B-General_Concept
structure	I-General_Concept
)	O
forms	O
a	O
potential	O
future	O
event	O
in	O
the	O
event	O
queue	O
,	O
remove	O
this	O
possible	O
future	O
event	O
from	O
the	O
event	O
queue	O
.	O
</s>
<s>
The	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
processes	O
a	O
sequence	O
of	O
events	O
,	O
where	O
denotes	O
the	O
number	O
of	O
input	O
line	O
segments	O
and	O
denotes	O
the	O
number	O
of	O
crossings	O
.	O
</s>
<s>
Each	O
event	O
is	O
processed	O
by	O
a	O
constant	O
number	O
of	O
operations	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
and	O
the	O
event	O
queue	O
,	O
and	O
(	O
because	O
it	O
contains	O
only	O
segment	O
endpoints	O
and	O
crossings	O
between	O
adjacent	O
segments	O
)	O
the	O
event	O
queue	O
never	O
contains	O
more	O
than	O
events	O
.	O
</s>
<s>
If	O
the	O
crossings	O
found	O
by	O
the	O
algorithm	O
do	O
not	O
need	O
to	O
be	O
stored	O
once	O
they	O
have	O
been	O
found	O
,	O
the	O
space	O
used	O
by	O
the	O
algorithm	O
at	O
any	O
point	O
in	O
time	O
is	O
:	O
each	O
of	O
the	O
input	O
line	O
segments	O
corresponds	O
to	O
at	O
most	O
one	O
node	O
of	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
T	O
,	O
and	O
as	O
stated	O
above	O
the	O
event	O
queue	O
contains	O
at	O
most	O
elements	O
.	O
</s>
<s>
described	O
a	O
highly	O
space-efficient	O
version	O
of	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
that	O
encodes	O
most	O
of	O
its	O
information	O
in	O
the	O
ordering	O
of	O
the	O
segments	O
in	O
an	O
array	O
representing	O
the	O
input	O
,	O
requiring	O
only	O
additional	O
memory	O
cells	O
.	O
</s>
<s>
The	O
updates	O
to	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
caused	O
by	O
this	O
event	O
may	O
involve	O
removing	O
any	O
line	O
segments	O
for	O
which	O
this	O
is	O
the	O
right	O
endpoint	O
,	O
inserting	O
new	O
line	O
segments	O
for	O
which	O
this	O
is	O
the	O
left	O
endpoint	O
,	O
and	O
reversing	O
the	O
order	O
of	O
the	O
remaining	O
line	O
segments	O
containing	O
this	O
event	O
point	O
.	O
</s>
<s>
A	O
similar	O
approach	O
to	O
degeneracies	O
was	O
used	O
in	O
the	O
LEDA	B-Library
implementation	O
of	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
For	O
this	O
reason	O
it	O
is	O
standard	O
to	O
use	O
integer	O
coordinates	O
for	O
the	O
endpoints	O
of	O
the	O
input	O
line	O
segments	O
,	O
and	O
to	O
represent	O
the	O
rational	O
number	O
coordinates	O
of	O
the	O
intersection	O
points	O
of	O
two	O
segments	O
exactly	O
,	O
using	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
.	O
</s>
<s>
However	O
,	O
it	O
may	O
be	O
possible	O
to	O
speed	O
up	O
the	O
calculations	O
and	O
comparisons	O
of	O
these	O
coordinates	O
by	O
using	O
floating	B-Algorithm
point	I-Algorithm
calculations	O
and	O
testing	O
whether	O
the	O
values	O
calculated	O
in	O
this	O
way	O
are	O
sufficiently	O
far	O
from	O
zero	O
that	O
they	O
may	O
be	O
used	O
without	O
any	O
possibility	O
of	O
error	O
.	O
</s>
<s>
The	O
exact	O
arithmetic	O
calculations	O
required	O
by	O
a	O
naïve	O
implementation	O
of	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
may	O
require	O
five	O
times	O
as	O
many	O
bits	O
of	O
precision	O
as	O
the	O
input	O
coordinates	O
,	O
but	O
describe	O
modifications	O
to	O
the	O
algorithm	O
that	O
reduce	O
the	O
needed	O
amount	O
of	O
precision	O
to	O
twice	O
the	O
number	O
of	O
bits	O
as	O
the	O
input	O
coordinates	O
.	O
</s>
<s>
The	O
O(n log n )	O
part	O
of	O
the	O
time	O
bound	O
for	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
is	O
necessary	O
,	O
as	O
there	O
are	O
matching	O
lower	O
bounds	O
for	O
the	O
problem	O
of	O
detecting	O
intersecting	O
line	O
segments	O
in	O
algebraic	B-Application
decision	I-Application
tree	I-Application
models	O
of	O
computation	O
.	O
</s>
<s>
and	O
both	O
provided	O
randomized	O
algorithms	O
for	O
constructing	O
the	O
planar	O
graph	O
whose	O
vertices	O
are	O
endpoints	O
and	O
crossings	O
of	O
line	O
segments	O
,	O
and	O
whose	O
edges	O
are	O
the	O
portions	O
of	O
the	O
segments	O
connecting	O
these	O
vertices	O
,	O
in	O
expected	O
time	O
O( n	O
log	O
n	O
+	O
k	O
)	O
,	O
and	O
this	O
problem	O
of	O
arrangement	O
construction	O
was	O
solved	O
deterministically	B-General_Concept
in	O
the	O
same	O
O( n	O
log	O
n	O
+	O
k	O
)	O
time	O
bound	O
by	O
.	O
</s>
<s>
However	O
,	O
constructing	O
this	O
arrangement	O
as	O
a	O
whole	O
requires	O
space	O
O( n	O
+	O
k	O
)	O
,	O
greater	O
than	O
the	O
O(n )	O
space	O
bound	O
of	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
;	O
described	O
a	O
different	O
algorithm	O
that	O
lists	O
all	O
intersections	O
in	O
time	O
O( n	O
log	O
n	O
+	O
k	O
)	O
and	O
space	O
O(n )	O
.	O
</s>
<s>
If	O
the	O
input	O
line	O
segments	O
and	O
their	O
endpoints	O
form	O
the	O
edges	O
and	O
vertices	O
of	O
a	O
connected	O
graph	O
(	O
possibly	O
with	O
crossings	O
)	O
,	O
the	O
O(n log n )	O
part	O
of	O
the	O
time	O
bound	O
for	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
may	O
also	O
be	O
reduced	O
.	O
</s>
<s>
Both	O
of	O
these	O
algorithms	O
involve	O
applying	O
the	O
Bentley	B-Algorithm
–	I-Algorithm
Ottmann	I-Algorithm
algorithm	I-Algorithm
to	O
small	O
random	O
samples	O
of	O
the	O
input	O
.	O
</s>
