You are on page 1of 23

1

1
Algorithms and Data
Structures (ET3155)
Lecture 2
Functions, Sequence and Relations
(Chapter 3)
Said Hamdioui Carlo Galuzzi
Computer Engineering Lab
Delft University of Technology
2011-2012
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
2
Previous lecture
Met hods of proofs
Direct proof
Contradiction proof (Indirect proof)
Proof by contrapositive
Proof by cases
Proof by logical equivalence
Proof by induction
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
3
Goals
Funct ions
Explain t he concept of a funct ion and it s main charact erist ics
Prove if a funct ion has some propert ies ( e. g. , one- t o- one,
ont o, et c)
Sequences
Explain t he concept of a sequence and be able t o manipulat e
t hem
Relat ions
Explain t he concept of Relat ion and it s main propert ies
Prove if a relat ion has some propert ies
Prove t hat a relat ion is an equivalence relat ion
Use a mat rix t o describe a Relat ion and t o prove it s propert ies
2
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
4
Topics
Functions
Sequences and Strings
Relations
Matrices
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
5
Topics
Funct ions
Sequences and Strings
Relations
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
6
Functions (1)
A function f from X to Y (in symbols f : X Y) assigns to each
member of X exactly one member of Y; i.e., if two pairs (x,y)
and (x,y) e f, then y = y
Domain of f = X
Range of f ={y| y=f(x) for some xeX} [Range is a subset of Y]
To visualize a function:
Graph
Arrow diagram
Example 1:
f={(a,3),(b,3),(c,5),(d,1)} is a function
Dom(f) = X = {a, b, c, d},
Rng(f) = {1, 3, 5}
f(a) = f(b) = 3, f(c) = 5, f(d) = 1
Ar r ow di agr am
3
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
7
Functions (2)
Domain of f = X
Range of f =
{ y | y = f(x) for some x eX}
A function f : X Y assigns to each x in
Dom(f) = X a unique element y in Rng(f)
_ Y.
Therefore, no two pairs in f have the
same first coordinate.
Examples:
X={a,b,c,d}
f1={(a,3), (b,5), (c,1), (a,2),(d,3)}. Is f1 a function?
f2={(a,3),(b,2),(c,5)}. Is f2 a function?
difference wit h relat ions
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
8
Functions: Example
Let x be a integer and y a positive integer
r = x mod y is the remainder when x is divided by y
Examples:
1 = 13 mod 3
6 = 234 mod 19
4 = 2002 mod 111
mod is called the modulus operator
Modulus operator play an important role in math
and computer science
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
9
Hash functions
Hashing: a technique that allows fast searching
Element n is stored at position h(n) of a table
h is called the hashing function
Example:
Table with N=11 locations 0, 1, , 10
N has to be a prime to allow probing all cells
h(n) = n mod 11
Insert 15, 558, 32, 132, 102, 5
0 1 2 3 4 5 6 7 8 9 10
132 102 15 5 558 32
4
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
10
Collision Resolution
Suppose we want to insert 257
257 mod 11 = 4 but cell 4 is already occupied (with 15)
Collision has occurred
Need collision resolution policy
E.g.: find next highest unoccupied cell (0 succeeds 10)
(linear probing)
0 1 2 3 4 5 6 7 8 9 10
132 102 15 5 558 32 257
0 1 2 3 4 5 6 7 8 9 10
132 102 15 5 558 32
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
11
Collision Resolution
Suppose we want to insert 257
257 mod 11 = 4 but cell 4 is already occupied (with 15)
Need collision resolution policy
E.g.: use secondary hash function h
2
. Probe sequence:
( h
1
(n) + j h
2
(n) ) mod N where
j=1, 2, N-1, (N is a prime)
Common choice:
h
2
(n) = (m - n mod m) where m<N; m is a prime
Example: [m=5 <N=11]
h1(257)=4; h2(257)= (5- 257 mod 5) = 5-2=3
(h1(257)+h2(257))mod 11 =(4+3) mod 11= 7
0 1 2 3 4 5 6 7 8 9 10
132 102 15 5 558 32 257
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
12
Exercise
For the hash function h(x) = x mod 17 and a table
with cells indexed 0 to 16, show how the table
looks like if the following data is inserted in the
following order:
714, 631, 26, 373, 775, 906, 509, 2032, 42, 4, 136,
1028
Using linear probing:
i.e., find next highest resolution policy
Using double hashing
Use h
2
(n) = (11 - n mod 11) as secondary hash function
Note that m=11 is a prime
5
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
13
One-to-one functions
A function f : X Y is one-to-one (or injective) if for each
y e Y, there exists at most one x e X with f(x) = y.
Alt ernat ive definit ion: f : X Y is one- t o- one if for each
pair of dist inct element s x
1
, x
2
e X,
if f( x
1
) = f( x
2
) t hen x
1
= x
2
.
Examples:
The funct ion f( x) = 2x+ 1 from t he set of real numbers t o
it self is one- t o- one
The funct ion f : R R defined by f( x) = x
2
is not one- t o-
one, since for every real number x, f( x) = f( - x) .
f: N N defined by f( n) = 2
n
n
2
is not one- t o- one since
f( 2) = f( 4) = 0.
f= { ( 1, a) , ( 2, b) , ( 3, a) } is . . ?
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
14
Onto functions
A function f : X Y is onto Y (or surjective) if
for each y e Y there exists at least one x e X with
f(x) = y, i.e. Rng(f) = Y. (each y has an x)
Examples
1
2
3
4
a
b
c
d
X
Y
f
1
2
3
4
a
b
c
d
X
Y
f
Ont o ( and one- t o- one)
Not ont o
( and one- t o- one)
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
15
Bijective functions
A function f : XY is bijective
f is one-to-one and onto
Examples:
A linear function f(x) = ax + b is a bijective function from the set
of real numbers to itself
The function f(x) = x
3
is bijective from the set of real numbers to
itself.
The funct ion f : R R defined by f( x) = x
2
is not bijective
because it is not one- t o- one ( for every real number x,
f( x) = f( - x) ) .
6
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
16
Inverse function
Given a function y = f(x), the inverse f
-1
is the set
{(y, x) | y = f(x)}.
Example: f={(1,a),(2,b),(3,c)} f
-1
={(a,1),(b,2),(c,3)}
The inverse f
-1
of f is not necessarily a function.
Example: if f(x) = x
2
, then f
-1
(4) = \4 = 2, not a unique
value and therefore f
-1
is not a function.
However, if f is a bijective function, it can be
shown that f
-1
is a function.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
17
Exercises!!!
Exam Jan 08:
Given t he following funct ion: f( x) = 3
x
- 2
Define one- t o- one funct ion. Det ermine and proof
whet her t he funct ion f( x) is one- t o- one. The
domain is a set of real numbers
Define ont o funct ion. Det ermine if t he funct ion
f( x) is ont o funct ion.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
18
Composition of functions
Given two functions g : X Y and f : Y Z, the
composition f g is defined as follows:
(f g)(x) = f(g(x)) for every x e X.
Example: g(x) = x
2
-1, f(x) = 3x + 5. Then
(f g)(x) = f(g(x)) = f(x
2
-1) = 3(x
2
-1) +5 = 3x
2
+2
Composition of functions is associative:
f (g h) = (f g) h,
But, in general, it is not commutative:
f g = g f.
7
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
19
Binary operators*
A binary operator on a set X is a function f that
associates a single element of X to every pair of
elements in X.
In other words:
f : X x X X and
f(x
1
, x
2
) e X for every pair of elements x
1
, x
2
.
Examples:
X={1,2,3,}; f(x,y)= x+y for x,y e X => f a binary operator
Binary operators are addition, subtraction and
multiplication of real numbers, taking unions or
intersections of sets, concatenation of two strings over a
set X, etc.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
20
Unary operators*
A unary operator on a set X associates to each
single element of X one element of X.
In other words:
f : X X and
f(x) e X for every element x.
Example:
Let X = U be a universal set and P(U) the power set of U.
Define f : P(U) P(U) the function defined by f (A) = A
c
(i.e., the set complement of A in U) for every A _ U.
Then f defines a unary operator on P(U).
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
21
Summary (1)
Funct ions
Definit ion, present at ion ( arrow diagram, graph)
Hash funct ions
Collision resolut ion policies
One- t o- one funct ion
Ont o funct ion
Bij ect ive funct ion ( Bij ect ion)
I nverse of a funct ion
Composit ion of funct ions
Binary operat or versus unary operat or
8
22
Algorithms and Data
Structures (ET3155)
Lecture 3
Functions, Sequence and Relations
(Chapter 3)
Said Hamdioui
Computer Engineering Lab
Delft University of Technology
2010-2011
S.Hamdioiui, CE, EWI ,
TUDelft
ET3155 Algor it hms and Dat a St r uct ur es,
2011- 2012
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
23
Goals
Funct ions
Explain t he concept of a funct ion and it s main charact erist ics
Prove if a funct ion has some propert ies ( e. g. , one- t o- one,
ont o, et c)
Sequences
Explain t he concept of a sequence and be able t o manipulat e
t hem
Relat ions
Explain t he concept of Relat ion and it s main propert ies
Prove if a relat ion has some propert ies
Prove t hat a relat ion is an equivalence relat ion
Use a mat rix t o describe a Relat ion and t o prove it s propert ies
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
24
Topics
Functions
Sequences and Strings
Relations
9
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
25
Sequences and strings
A sequence is an ordered list of numbers, usually
defined according to a formula: s
n
= f(n) for n = 1, 2, 3,...
E.g., C
n
=1+0.5(n-1) for n=1,2,3,4
If s is a sequence {s
n
| n = 1, 2, 3,},
s
1
denotes the first element,
s
2
the second element,
s
n
the nth element
{n} is called the indexing set/ domain of the sequence.
Usually the indexing set is N (natural numbers) or an
infinite subset of N.
n is the index of the sequence
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
26
Examples of sequences
Let s = {s
n
} be the sequence defined by
s
n
= 1/n , for n = 1, 2, 3,
First few elements are: 1, , 1/3, , 1/5,1/6,
Let s = {s
n
} be the sequence defined by
s
n
= n
2
+ 1, for n = 1, 2, 3,
First few elements are: 2, 5, 10, 17, 26, 37, 50,
Fibonacci sequence:
f
1
= 1, f
2
= 1, f
n
= f
n-1
+ f
n-2
for n = 2, 3, 4,
First few elements: f
1
= f
2
=1, f
3
= 2, f
4
= 3, f
5
= 5, f
6
= 8
Recurrence relation, more later
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
27
Increasing and decreasing
A sequence s = {s
n
} is said to be
(strictly) increasing if s
n
< s
n+1
nondecreasing if s
n
< s
n+1
(strictly) decreasing if s
n
> s
n+1
,
nonincreasing if s
n
> s
n+1
,
for every n = 1, 2, 3,
Examples:
S
n
= 4 2n, n = 1, 2, 3, is decreasing: {2, 0, -2, -4, -6,}
S
n
= 2n -1, n = 1, 2, 3, is increasing: {1, 3, 5, 7, 9, }
S
n
= n/2, n = 1, 2, 3, is nondecreasing: {0, 1, 1, 2, 2, 3, }
10
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
28
Subsequences
A subsequence of a sequence s = {s
n
} is a
sequence t = {t
n
} that consists of certain
elements of s retained in the original order they
had in s
Example:
let s = {s
n
= n | n = 1, 2, 3,}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
Let t = {t
n
= 2n | n = 1, 2, 3,}
2, 4, 6, 8, 10, 12, 14, 16,
t is a subsequence of s
Let m={4,3,2,1}
m is not a subsequent of s (order not maintained)
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
29
Sum notation
If {a
n
} is a sequence, then the sum
n
E a
i
= a
m
+ a
m+1
+ + a
n
i = m
This is called the sum or sigma notation,
where the Greek letter E indicates a sum of
terms from the sequence
i is called the index
m is the lower limit
n is the upper limit
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
30
Product notation
If {a
n
} is a sequence, then the product
n
H a
i
= a
m
- a
m+1
a
n
i=m
This is called the product notation, where the
Greek letter H (pi) indicates a product of
terms of the sequence
11
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
31
Strings
Let X be a nonempty set. A string over X is a
finite sequence of elements from X.
Example: if X = {a, b, c}
Then o = bbaccc is a string over X
Notation: bbaccc = b
2
ac
3
The length of a string o is the number of elements of
o and is denoted by |o|. If o = b
2
ac
3
then |o| = 6.
The null string is the string with no elements and
is denoted by the Greek letter . || = 0.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
32
More on strings (1)
X* = {all strings over X including }
X
+
= X* - {}, the set of all non-null strings
E.g., X={a,b}
Some elements of X* are: , a, ab, abab, b
2
a
6
ba
Concatenation of two strings o and | is the
operation on strings consisting of writing o followed
by | to produce a new string o|
Example: o = bbaccc and | = caaba,
then o| = bbaccccaaba = b
2
ac
4
a
2
ba
|o|| = | o| + |||
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
33
More on strings (2)
Bit strings are strings over {0,1}
Substring of o: is obtained by selecting some of all
consecutive elements of o
Definition
A string | is a substring of the string o if there are strings o
and with o = o|
12
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
34
Exercises (Exam Jun 08)
Exam Jan 08
Given t he following sequence:
z
n
= ( 2+ n) . 3
n
; n > 0
Find a formula for z
n- 1
?
Find a formula for z
n- 2
?
Prove t hat { z
n
} sat isfies:
z
n
= 6 z
n- 1
9 z
n- 2
for n > 2

S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
35
Topics
Functions
Sequences and Strings
Relations
Equivalence relat ions
Mat rices and relat ions
Relat ional dat abase
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
36
Relations
Given two sets X and Y, the Cartesian product XxY
is the set of all ordered pairs (x,y) where xeX and
yeY
XxY = {(x, y) | xeX and yeY}
E.g., X={1,2}, Y={a,b}; XxY={(1,a), (1,b), (2,a), (2,b)}
A (binary) relation R from a set X to a set Y is a
subset of the Cartesian product XxY
If (x,y) e R, we write xRy (x is related to y)
Example: X = {1, 2, 3} and Y = {a, b}
R = {(1,a), (1,b), (2,b), (3,a)} is a relation from X to Y
13
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
37
Domain and range
Given a relation R from X to Y,
The domain of R is the set
Dom(R) = { xeX | (x, y) eR for some yeY}
The range of R is the set
Rng(R) = { yeY | (x, y) eR for some x eX}
Example:
if X = {1, 2, 3} and Y = {a, b, c}
R = {(1,a), (1,b), (2,b)}
Then: Dom(R)= {1, 2}, Rng(R) = {a, b}
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
38
Example of a relation
Let X = {1, 2, 3} and Y = {a, b, c, d}.
Define R = {(1,a), (1,d), (2,a), (2,b), (2,c)}
The relation can be pictured by a graph
What is the domain?
What is the range?
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
39
Properties of relations (1)
Let R be a relation on a set X (i.e. R is a subset of XxX)
R is reflexive if (x,x) eR for every xeX
Examples
- R={(a,a), (b,c), (c,b), (d,d)} on X={a,b,c,d} is not reflexive
- R on X={1,2,3,4} defined by (x,y) eR if x < y is reflexive
R is symmetric if for all x, y eX, if (x,y)eR then (y,x) eR
Examples
- R={(a,a), (b,c), (c,b), (d,d)} on X={a,b,c,d} is symmetric
- R on X={1,2,3,4} defined by (x,y) eR if x < y is not
symmetric
14
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
40
Properties of relations (2)
Let R be a relation on a set X (i.e. R is a subset of XxX)
R is antisymmetric
if for all x,yeX, if (x,y)eR and (y,x)eR, then x=y.
Examples
- R={(a,a), (b,c), (c,b), (d,d)} on X={a,b,c,d} is not antisymmetric
- R on X={1,2,3,4} defined by (x,y) eR if x < y is antisymmetric
R is transitive
if (x,y) eR and (y,z) eR imply (x,z)eR
Examples
- R={(a,a), (b,c), (c,b), (d,d)} is not transitive
- R on X={1,2,3,4} defined by (x,y) eR if x < y is transitive
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
41
Examples of relations
x divides y is a relation on lN
{(1,1),(1,2),(1,3),,
(2,2),(2,4),(2,6),,
(3,3),(3,6),(3,9),}
- Reflexive? Symmetric? Transitive?
x < y is a relation on lN
{(1,1),(1,2),(1,3),,
(2,2),(2,3),(2,4),,
(3,3),(3,4),(3,5),}
- Reflexive? Symmetric? Transitive?
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
42
Order relations
Let X be a set and R a relation on X
R is a partial order on X if R is reflexive,
antisymmetric and transitive.
Suppose R is partial order, let x,yeX
If (x,y) or (y,x) are in R, then x and y are comparable
If (x,y) eR and (y,x) eR then x and y are incomparable
If every pair of elements in X are comparable, then
R is a total order on X
x divides y
has some
incomp. el.
eit her
x< y or
y< x
are t rue
15
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
43
Partial Order
I s s on nat ural numbers a part ial order?
s is reflexive because n s n
s is t ransit ive because if l s n and n s m t hen lsm
s is ant isymmet r ic if m s n and n s m t hen n= m
is s a t ot al order?
yes, because for every n and m: n s m or m s n
I s < on nat ural numbers part ial order?
No because it is not reflexive : n< n is not t rue
Ex am pl e
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
44
Exercises
Is the relation R = {(x,y) 3 divides x-y} on lN
reflexive
symmetric
antisymmetric
transitive
a partial order?
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
45
Inverse of a relation
Given a relation R from X to Y, its inverse R
-1
is
the relation from Y to X defined by
R
-1
= { (y,x) | (x,y) e R }
Example: if R = {(1,a), (1,d), (2,a), (2,b), (2,c)}, then
R
-1
= {(a,1), (d,1), (a,2), (b,2), (c,2)}
16
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
46
Equivalence relations
Let X be a set and R a relation on X
R is an equivalence relation on X if R is
reflexive, symmetric and transitive.
Example:
R={(1,1),(1,3),(1,5),(2,2),(2,4),(3,1),(3,3),(3,5),
(4,2),(4,4),(5,1),(5,3),(5,5)} on {1,2,3,4,5}
R is reflexive because (1,1)(2,2) etc. are in R
R is symmetric because both (x,y) and (y,x) are in R
R is transitive because when (x,y) and (y,z) are in R
then (x,z) is also in R
R is an equivalence relation
Note: directed graphs can be used
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
47
Equivalence relation
Ex am pl es:
I s = on nat ural numbers an equivalence
relat ion ?
= is reflexive : n= n
= is symmet ric : if m= n t hen n= m
= is t ransit ive : if m= n and n= l t hen m= l
R is neit her reflexive nor t ransit ive
I s R= { ( a, a) , ( b, c) , ( c, b) , ( d, d) } on
X= { a, b, c, d} an equivalent relat ion?
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
48
Partitions
A partition S on a set X is a collection {A
1
, A
2
,, A
n
}
of subsets of X, such that
A
1
A
2
A
3
A
n
= X
A
j
A
k
= C for every j, k with j = k, 1 < j, k < n.
Example:
if X = {integers}, E = {even integers) and O = {odd integers}
then S = {E, O} is a partition of X.
I f X= { 1, 2, 3, 4, 5, 6, 7, 8} , t hen
S = { { 1, 4, 5} , { 2, 6} , { 3, 7} , { 8} } is a part it ion of X
S = { { 1, 2, 4, 5} , { 2, 3, 6} , { 7} , { 8} } is NOT a part it ion of X
17
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
49
Partitions and equivalence relations
Theorem: Let S be a partition on a set X.
Define a relation R on X by xRy to mean that for some subset
T e S both x, y belongs to T.
Then: R is an equivalence relation on X.
an equivalence relation on a set X corresponds to a
partition of X and conversely.
Proof:
By definition of partition, x belongs to T; thus xRx (reflexive)
Suppose xRy. Then both x and y belong to same subset T. So
do y and x, yRx (symmetric).
Suppose xRy and yRz. x and y belong to same subset A and
y and z belong to same subset T. Since y belongs to exactly
one member S, we must have T=A. Hence xRz (transitive).
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
50
Partitions and equivalence relations
Consider par t i t i on S= { { 1,3,5} , { 2, 6} , { 4} } of
X= { 1, 2, 3, 4, 5, 6}
Consider t he relat ion R on X defined by xRy if x, y
belongs to the same subset TeS (theorem of the previous
slide)
R = { ( 1, 1) ( 1, 3) ( 1, 5) ( 3, 1) ( 3, 3) ( 3, 5) ( 5, 1) ( 5, 3) ( 5, 5) }
is an equi v al ence r el at i on
because { 1,3,5} is in S.
The complet e relat ion is :
{ ( 1, 1) ( 1, 3) ( 1, 5) ( 3, 1) ( 3, 3) ( 3, 5) ( 5, 1) ( 5, 3) ( 5, 5)
( 2, 2) ( 2, 6) ( 6, 2) ( 6, 6)
( 4, 4) }
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
51
Equivalence classes
Let X be a set and let R be an equivalence relation on X.
Let a e X.
Define [a] ={ xeX | xRa }
(i.e., the set of all elements in X that are related to a)
Let S = { [a] | a e X }
( i.e., a collection of subset of [a])
Theorem: S is a partition on X. [proof: see the book]
The sets [a] are called equivalence classes of X given by
the relation R.
Given a, b e X, then [a] = [b] or [a][b] = C
18
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
52
Partitions and equivalence relations
Consider t he equi v al ence r el at i on R on
X= { 1, 2, 3, 4, 5, 6} defined as:
R= { ( 1, 1) ( 1, 3) ( 1, 5) ( 3, 1) ( 3, 3) ( 3, 5) ( 5, 1) ( 5, 3) ( 5, 5)
( 2, 2) ( 2, 6) ( 6, 2) ( 6, 6)
( 4, 4)
}
Equivalence class [ 1] consist s of all x such t hat ( x, 1) eR:
[1]={1,3,5}=[3]=[5]
The other equivalence classes are :
[2]=[6]={2,6}
[4]={4}
Hence S = { [ a] | aeX} [ Theorem previous slide]
= { { 1, 3, 5} , { 2, 6} , { 4} } is a par t i t i on
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
53
Partitions and equivalence relations
Example:
let X= { 1, 2, . . . , 10} . xRy means 3 divides x- y; R
is an equivalence relat ion
members of equivalence classes :
equivalence=has same remainder when divided by 3
[ 1] = { x eX| 3 divides x-1}= { xeX | xR1} {1,4,7,10}
[2]={2,5,8}
[3]={3,6,9}
[1]=[4]=[7]=[10]
[2]=[5]=[8]
[3]=[6]=[9]
Therefore S = { [ a] | aeX} = { { 1, 4, 7, 10} , { 8, 5, 2} { 3, 6, 9} }
is a part it ion
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
54
Exercises
Exercise 1:
How many equivalence relat ions are t here on t he set { 1, 2, 3} } ?
Exercise 2:
Let X= { 1, 2, , 10}
Define a relat ion R on XxX by ( a, b) R( c, d) if a+ d= b+ c
I s R an equivalence relat ion on XxX?
Exercise 3: Exam Jan 10
19
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
55
Matrices of Relations
Let X, Y be sets and R a relation from X to Y
Matrix of the relation R (say A = (a
ij
) ) is
defined as follows:
Rows of A = elements of X
Columns of A = elements of Y
Element a
i,j
= 0 if the element of X in row i and
the element of Y in column j are not related
Element a
i,j
= 1 if the element of X in row i and
the element of Y in column j are related
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
56
The matrix of a relation (1)
Example:
Let X = {1, 2, 3}, Y = {a, b, c, d}
Let R = {(1,a), (1,d), (2,a), (2,b), (2,c)}
The matrix A of the relation R is
Obviously the matrix depends on the ordering of X and Y
a b c d
1 1 0 0 1
2 1 1 1 0
3 0 0 0 0
A =
X
Y
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
57
The matrix of a relation (2)
If R is a relation from a set X to itself and A is the
matrix of R then A is a square matrix.
Example: Let X = {a, b, c, d} and R = {(a,a), (b,b),
(c,c), (d,d), b,c), (c,b)}. Then
|
|
|
|
|
.
|

\
|
=
1 0 0 0
0 1 1 0
0 1 1 0
0 0 0 1
d
c
b
a
A
20
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
58
The matrix of a relation on a set X
Let A be the square matrix of a relation R from X
to itself. Let A
2
= the matrix product AA.
R is reflexive All terms a
ii
in the main
diagonal of A are 1
R is symmetric a
ij
= a
ji
for all i and j,
i.e. R is a symmetric relation on X if A is a symmetric
matrix
R is transitive whenever c
ij
in C = A
2
is
nonzero then entry a
ij
in A is also nonzero.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
59
Example
R = {(a,a),(b,b},{b,c},(c,b),(c,c),(d,d)}
|
|
|
|
|
.
|

\
|
=
|
|
|
|
|
.
|

\
|
=
1 0 0 0
0 2 2 0
0 2 2 0
0 0 0 1
1 0 0 0
0 1 1 0
0 1 1 0
0 0 0 1
2
A
A
R is symmetric
R is reflexive
R is transitive
R is an equivalence relation
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
60
Relational databases
A binary relation R is a relation among two
sets X and Y, already defined as R _ X x Y.
An n-ary relation R is a relation among n sets
X
1
, X
2
,, X
n
, i.e. a subset of the Cartesian
product, R _ X
1
x X
2
xx X
n
.
Thus, R is a set of n-tuples (x
1
, x
2
,, x
n
) where x
k
e
X
k
, 1 < k < n.
21
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
61
Databases
A database is a collection of records that are
manipulated by a computer. They can be
considered as n sets X
1
through X
n
, each of which
contains a list of items with information.
Database management systems are programs
that help access and manipulate information
stored in databases.
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
62
Relational database model
Columns of an n-ary relation are called attributes
E.g., Attribute Age with a domain: all positive integers less
than 100
An attribute is a key if no two entries have the same
value
e.g. ID is a key but Age is NOT (ID is unique but age is not)
A query is a request for information from the database
E.g. find all persons with age 50
Employee
I D Name Age
22 Jan 22
71 Mark 50
101 Pet er 50
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
63
Operators
The selection operator chooses n-tuples from a relation by
giving conditions on the attributes
E.g., Employee [Age=50] will select (71,Mark,50) and (101,Peter,50)
The projection operator chooses two or more columns and
eliminates duplicates
E.g., Employee [ID, Name] will select (22,Jan), (71,Mark) and (101, Peter)
The join operator manipulates two relations
form the Cartesian product of two relations and select those tuples
satisfying a certain condition
E.g., merge each two rows of two
different tables when a certain
condition is satisfied
Employee
I D Name Age
22 Jan 22
71 Mark 50
101 Pet er 50
22
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
64
SQL - Structured Query Language*
SELECT
SELECT Name, Age
FROM Employee
WHERE Age> = 60
JOI N
SELECT employees. name
FROM employees
JOI N invoices
ON employees. I D= invoices. employeeI D
SQL t ut orial can be found at
ht t p: / / www. t izag. com/ sqlTut or ial
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
65
Summary (2)
Sequences and Strings
Definit ion of a sequence and a st ring
Subsequence versus subst ring
I ncreasing versus decreasing sequences
Nonincreasing versus nondecreasing sequences
Sum/ Sigma not at ion versus product not at ion
Null st ring
Let X be a nonempt y finit e set
X
*
: the set of all strings over X, including the null string
X
+
: the set of all nonnull strings over X
Concat enat ion of st rings
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
66
Summary (3)
Binary relat ions, domain, range
Relat ion propert ies
Reflexive, Symmet ric, Ant isymmet ric, Transit ive, Part ial order
I nverse relat ion
Equivalence relat ions and part it ions
Equivalence classes
Mat rix of a relat ion
Reflexive, symmet ric and t ransit ive
Binary relat ion versus n- ary relat ion
Relat ional dat abase ( at t ribut e, key, query)
Operat ors ( select ion, proj ect ion, j oin)
23
S.Hamdioiui, CE, EWI , TUDelft ET3155 Algor it hms and Dat a St r uct ur es, 2011- 2012
67
Suggested Exercises
From Chapt er 3 Self- t est
1, 4, 9, 14, 17

You might also like