Professional Documents
Culture Documents
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 1 / 34
Problem
Arrangement of Points
You are given a planar geometric structure comprising vertices, edges
connecting vertices, and faces bounded by edges. The expected operations
are:
Walk around the boundary of a given face in CCW order
Access a face from an adjacent one
Visit all the edges around a given vertex
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 2 / 34
Doubly
Connected
Edge
List
(DCEL)
• DCEL
is
one
of
the
most
commonly
used
representations
for
planar
subdivisions
such
as
Voronoi
diagrams.
• It
is
an
edge‐based
structure
which
links
together
the
three
sets
of
records:
– Vertex
– Edge
– Face
• It
facilitates
traversing
the
faces
of
planar
subdivision,
visiting
all
the
edges
around
a
given
vertex
Doubly
Connected
Edge
List
(DCEL)
f1
f2
f4
f3
f5
• Main
ideas:
– Edges
are
oriented
counterclockwise
inside
each
face
– Since
an
edge
borders
two
faces,
each
edge
is
replaced
by
two
half‐edges,
one
for
each
face
Doubly
Connected
Edge
List
(DCEL)
• The
vertex
record
of
a
vertex
v
stores
the
IncidentFace(e1)
coordinates
of
v.
It
also
stores
a
pointer
IncidentEdge(v)
to
an
arbitrary
half‐edge
next(e1)
e1
origin(e1)
that
has
v
as
its
origin
e2
e6
• The
half‐edge
record
of
a
half‐edge
e
stores
pointer
to:
• Origin
(e)
• Twin
of
e,
e.twin
or
twin(e)
• The
face
to
its
left
(
IncidentFace(e)
)
• Next(e)
:
next
half‐edge
on
the
boundary
of
IncidentFace(e)
• Previous(e)
:
previous
half‐edge
Doubly
Connected
Edge
List
(DCEL)
e7,2
v6
v3
e7,1
e1,2
f3
e5,2
e6,1
f5
e1,1
e5,1
e6,2
e
9,1
e9,2
f1
e3,1
e3,2
v1
f2
f
e2,1
e8,1
4
e4,1
v4
e2,2
e4,2
e8,2
v2 v5
v2 v5
Face
Edge
f1
e1,1
f2
e5,1
f3
e5,2
f4
e8,1
f5
e9,2
Doubly
Connected
Edge
List
(DCEL)
e7,2
v6
v3
e7,1
e1,2
f3
e5,2
e6,1
f5
e1,1
e5,1
e6,2
e
9,1
e9,2
f1
e3,1
e3,2
v1
f2
f
e2,1
e8,1
4
e4,1
v4
e2,2
e4,2
e8,2
v2 v5
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 9 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 10 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 10 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 10 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 11 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 11 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 11 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Trivial solution would be to sort L and take the k largest elements.
This is O(n log n). Can you improve the initial solution? What if k is
a constant?
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Trivial solution would be to sort L and take the k largest elements.
This is O(n log n). Can you improve the initial solution? What if k is
a constant?
Bubblesort?
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Trivial solution would be to sort L and take the k largest elements.
This is O(n log n). Can you improve the initial solution? What if k is
a constant?
Bubblesort?
Mergesort?
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Trivial solution would be to sort L and take the k largest elements.
This is O(n log n). Can you improve the initial solution? What if k is
a constant?
Bubblesort?
Mergesort?
Quicksort?
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partial Sorting
Given a set L of n distinct integers, we need to partially sort the n
numbers to find the top k largest elements in sequence.
Which is a better approach to use, Mergesort or Quicksort or
anything else?
Algorithm
Recurrence
Complexity
Trivial solution would be to sort L and take the k largest elements.
This is O(n log n). Can you improve the initial solution? What if k is
a constant?
Bubblesort?
Mergesort?
Quicksort?
Discuss with TAs for complete solution
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 12 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 13 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 13 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 13 / 34
Problem
Operations:
insert: List good, Array bad
delete: List good, Array bad
find: Array good, List bad
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 14 / 34
Search Data Structure
Operations:
insert: List good, Array bad
delete: List good, Array bad
find: Array good, List bad
Use BST – Needs balancing:
Guaranteed Bound: AVL, 2-3-4 Tree, Red-Black Tree, B+-Tree
Probabilistic Bound: Randomized BST, Skip List
Amortised Bound: Splay
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 14 / 34
Search Data Structure
Operations:
insert: List good, Array bad
delete: List good, Array bad
find: Array good, List bad
Use BST – Needs balancing:
Guaranteed Bound: AVL, 2-3-4 Tree, Red-Black Tree, B+-Tree
Probabilistic Bound: Randomized BST, Skip List
Amortised Bound: Splay
Applications:
Associative containers like set, map
Indexing
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 14 / 34
Skip List
A skip list for a set S of distinct (key, element) items is a series of lists S0 ,
S1 , · · · , Sh such that
Each list Si contains the special keys +∞ and −∞
List S0 contains the keys of S in non-decreasing order
Each list is a subsequence of the previous one:
S0 ⊇ S1 ⊇ · · · ⊇ Sh
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 15 / 34
Skip List
Searching in a Skip-List
√
2 linked list 2. n
√
3 linked list 3. 3 n
√
k linked list k. k n
√
log n linked list log n. log n n =
log n.n1/log n =
Θ(log n)
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 16 / 34
Problem
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 17 / 34
Introduction Database queries
Kd-trees 1D range trees
Databases
Database queries
G. Ometer
born: Aug 16, 1954
salary salary: $3,500
19,500,000 19,559,999
date of birth
Computational Geometry Lecture 7: Kd-trees and range trees
Introduction Database queries
Kd-trees 1D range trees
Database queries
Database queries
19,500,000 19,559,999
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
split node
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
Grey nodes: they occur on only two paths in the tree, and
since the tree is balanced, its depth is O(log n)
Result
Partha Pratim Das (IIT, Kharagpur) Sorting & Searching March 18, 2015 34 / 34