Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSPs & Relational DBs
Problem Solving with Constraints
CSCE421/821, Fall 2012
www.cse.unl.edu/~choueiry/F12-421-821/
All questions Piazza
Berthe Y. Choueiry (Shu-we-ri)
Avery Hall, Room 360
Tel: +1(402)472-5444
Foundations of Constraint Processing
CSPs and Relational DBs
1
Background
Strong historical & conceptual connections exist between:
• Constraint Databases & Constraint Logic Programming
• Query processing in Relational DBs & Solving CSPs
Indeed:
• Constraint databases (deductive BD, Datalog) and
constraint logic programming (CLP) share the
representation language (restricted forms of FOL)
• Relational databases and Constraint Satisfaction share
computation mechanisms
Foundations of Constraint Processing
CSPs and Relational DBs
2
Relations
Binary relation: Given two sets Da and Db, a set of any 2-tuples
< x, y> with Da and y Db defines a relation
Rab
Ra,b = {(x, y)} Da x Db
Function: (special binary relation)
For any element x in Da there is at most one tuple < x, ? > Rab
Da is called the domain of the the function
Db is called the co-domain of the function
k-ary relation: Given k sets D1, D2, …, Dk, any set of k-tuples
< x1, x2, ..., xk > with x1 D1, x2 D2, …, xk Dk defines a
k-ary relation: R1, 2, ..., k = {(x1, x2, ..., xk)} D1 x D2 x … x Dk
Foundations of Constraint Processing
CSPs and Relational DBs
3
Representation of relations
Binary arrays:
2-dim binary array (i.e., bit matrix):
CVk ,Vl RVk ,Vl {( x, y )} DVk DVl
1 0 1
1
0
1
1
more generally, k-dimensional binary arrays
Tables:
C2
A
2
C1
B
1
V
1
D
V
1
1
4
1
1
6
1
2
3
1
2
1
3
2
4
1
1
2
2
6
2
2
3
2
5
4
6
1
2
6
6
C3
E
1
F
2
G
1
V
1
2
3
1
2
1
2
2
2
1
2
6
3
2
2
3
2
4
4
3
2
1
2
6
5
4
6
1
3
3
6
3
2
1
6
6
6
Foundations of Constraint Processing
CSPs and Relational DBs
4
Comparison of terminology
DB terminology
CSP terminology
Table, relation
Constraint
Relation arity
Constraint arity
Attribute
CSP variable
Value of an attribute
Value of a variable
Domain of an attribute
Domain of a variable
Tuple in a table
Tuple in a constraint
Tuple allowed by an constraint
Tuple consistent with a constraint
Constraint relation (in
constraint databases)
Constraint of linear (in)equality
Foundations of Constraint Processing
CSPs and Relational DBs
5
Relational Algebra: operations on relations
Database:
• Intersection
• Union
• Difference
• Selection
• Projection
• Join (Cartesian product), etc.
CSP:
• The above and composition (= combination of join and
projection)
Foundations of Constraint Processing
CSPs and Relational DBs
6
Operators in Relational Algebra
• Selection, projection
– unary operators, defined on one relation
• Intersection, union, difference
– binary operators
– relations must have same scope
• Join
– binary operator
– relations have different scopes
Foundations of Constraint Processing
CSPs and Relational DBs
7
Intersection
• Input: two relations of the same scope
• Output: a new more restrictive relation with the same scope,
made of tuples that are in all the input relations (simultaneously)
• Bit-matrix operation: logical AND
x - y > 10
x + y > 10
X1
a
b
c
c
R
X2
b
b
b
b
X3
c
c
c
s
• R R’ ? Okay
R’’
R'
X1
X2
X3
X1
X2
X4
b
c
c
b
b
n
c
c
n
a
b
b
a
c
c
1
2
3
R R'’ ?
Not defined
Foundations of Constraint Processing
CSPs and Relational DBs
8
Union
• Input: two relations of the same scope
• Output: a new less restrictive relation with the same scope made
of tuples that are in any of the input relations
• Bit-matrix operation: logical OR
X1
a
b
c
c
X2
b
b
b
b
R’’
R'
R
X3
c
c
c
s
• R R'? Okay
X1
X2
X3
X1
X2
X4
b
c
c
b
b
n
c
c
n
a
b
b
a
c
c
1
2
3
R R''? Not defined
Foundations of Constraint Processing
CSPs and Relational DBs
9
Difference
• Input: two relations R and R' of the same scope
• Output: a new more restrictive relation than R made of tuples that
are in R but not in R'
• Bit-matrix operation: Boolean difference
R
R'
X1
a
b
c
c
X2
b
b
b
b
X3
c
c
c
s
R - R'? Okay
R’’
X1
X2
X3
X1
X2
X4
b
c
c
b
b
n
c
c
n
a
b
b
a
c
c
1
2
3
R - R''? Not defined
Foundations of Constraint Processing
CSPs and Relational DBs
10
Selection
• Input: A relation R and some test/predicate on attributes of R
• Output: A relation R', same scope as R but containing only a
subset of the tuples in R (those that satisfy the predicate)
• Relation operation: row selection
R
X1
a
b
c
c
X2
b
b
b
b
X3
c
c
c
s
Select such that x1> x2, x1> x2(R)?
Foundations of Constraint Processing
CSPs and Relational DBs
11
Projection
• Input: A relation R and a subset s of the scope (attributes)
• Output: A relation R' of scope s with the tuples rewritten such that
positions not in s are removed
• Relation operation: column elimination
R
X1
a
b
c
c
X2
b
b
b
b
X3
c
c
c
s
Project R on x1, x2, x1, x2(R)?
Foundations of Constraint Processing
CSPs and Relational DBs
12
Join (natural join)
Input: Two relations R and R'
Output: A relation R'', whose scope is union of scopes of R and R'
and tuples satisfy both R and R'.
R and R' have no attribute common: Cartesian product
R and R' have an attribute in common, compute all possibilities
Operation: Compute all solutions to a CSP.
R
R"
X1
a
b
c
c
Join
R
X2
b
b
b
b
and R'',
X3
c
c
c
s
R
X1
X2
X4
a
b
c
b
c
b
1
2
3
R''?
Foundations of Constraint Processing
CSPs and Relational DBs
13
Composition of relations
Montanari'74
Input: two binary relations Rab and Rbc with 1 variable in common.
Output: a new induced relation Rac (to be combined by intersection to
a pre-existing relation between them, if any).
Bit-matrix operation: matrix multiplication
Rac Rab Rbc
Rac,rs tDb1 ( Rab,rt Rbc,ts )
Note: - generalization as constraint synthesis [Freuder, 1978]
- Direct (explicit) vs. induced (implicit) relations
1 0
1 0 1
, Rbc 1 1 , Rac ?
Rab
1 0 0
0 1
Foundations of Constraint Processing
CSPs and Relational DBs
14
Questions
1. Given
•
•
two variables V1 and V2 and
two constraints C1 and C2 between them
How do the two expressions C1 C2 and C1 C2 relate?
2. Given
•
•
three variables V1, V2, V3 and
the binary constraints CV1, V2 and CV2, V3
write the induced CV1, V3, in relational algebra
3. Given
•
•
three variables V1, V2, V3 and
the binary constraints CV1, V2, CV1, V3, and CV2, V3,
write the new induced C’V1, V3 in relational algebra
Foundations of Constraint Processing
CSPs and Relational DBs
15
Comparison of Terminology
Databases
CSPs
(Natural, inner) join
Synthesized constraint
Left/right outer join
Synthesized constraint
including (some) inconsistent
tuples
Projection of a join
Induced constraint
(Composition of two
constraints)
Computing r1
r2
…
ri
Finding all solutions to the
conjunction of the constraints rk
i
k 1
r
k
Foundations of Constraint Processing
CSPs and Relational DBs
16
DB versus CSP
CSPs:
• Relations
DB:
• Relations
– Many, many relations
– Mostly low-arity relations (binary)
– Typically, much looser than in DB
– Few relations in a query
– Usually, high arity relations
– Usually, selective relations
•
•
•
•
Domains
– Large domains, many, many tuples
– Mostly finite (CDB introduce
continuous domains, restricted to
linear constraints)
•
Seeking (almost) all tuples
Cost model: I/O disk access,
memory size
•
Domains
– Small-size domains
– Finite (frequent), but also continuous
(with arbitrary relations).
Seeking (in general) one solution
Cost model: computational cost
Foundations of Constraint Processing
CSPs and Relational DBs
17