2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

Embed Size (px)

Citation preview

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    1/23

    1

    1

    Algorithms and DataStructures (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 Algorithms and Data Structures, 2011-20122

    Previous lecture

    Methods 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 Algorithms and Data Structures, 2011-20123

    Goals

    Functions Explain the concept of a function and its main characteristics

    Prove if a function has some properties (e.g., one-to-one,

    onto, etc)

    Sequences Explain the concept of a sequence and be able to manipulate

    them

    Relations Explain the concept of Relation and its main properties

    Prove if a relation has some properties

    Prove that a relation is an equivalence relation

    Use a matrix to describe a Relation and to prove its properties

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    2/23

    2

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20124

    Topics

    Functions

    Sequences and Strings

    Relations

    Matrices

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20125

    Topics

    Functions

    Sequences and Strings

    Relations

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20126

    Functions (1)

    A functionf from X to Y (in symbols f : X Y) assigns to eachmember of X exactly one memberof Y; i.e., if two pairs (x,y)and (x,y) f, then y = y

    Domainof f = X

    Rangeof f ={y| y=f(x) for some xX} [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

    Arrow diagram

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    3/23

    3

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20127

    Functions (2) Domainof f = X

    Rangeof f =

    { y | y = f(x) for some x X}

    A function f : X Y assigns to each x inDom(f) = X a unique element y in Rng(f) Y.

    Therefore, no two pairs in f have thesame 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 with relations

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20128

    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 mathand computer science

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-20129

    Hash functions

    Hashing: a technique that allows fast searching

    Element n is stored at position h(n) of a table

    his 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

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    4/23

    4

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201210

    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 32257

    0 1 2 3 4 5 6 7 8 9 10

    132 102 15 5 558 32

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201211

    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 h2. Probe sequence:

    ( h1(n) + j h2(n) ) mod N where

    j=1, 2, N-1, (N is a prime)

    Common choice:

    h2(n) = (m - n mod m) where m

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    5/23

    5

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201213

    One-to-one functions

    A function f : X Y is one-to-one (orinjective) if for eachy Y, there exists at most one x X with f(x) = y.

    Alternative definition: f : X Y is one-to-one if for eachpair of distinct elements x1, x2 X,if f(x1) = f(x2) then x1 = x2.

    Examples: The function f(x) = 2x+1 from the set of real numbers to

    itself is one-to-one

    The function f : R Rdefined by f(x) = x2 is not one-to-one, since for every real number x, f(x) = f(-x).

    f: N N defined by f(n) = 2n n2 is not one-to-one sincef(2) = f(4) = 0.

    f={(1,a),(2,b),(3,a)} is ..?

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201214

    Onto functions

    A function f : X Y is ontoY (orsurjective) iffor each y Y there exists at least one x X withf(x) = y, i.e. Rng(f) = Y. (each y has an x)

    Examples

    1

    234

    a

    bcd

    X Yf

    1

    234

    a

    bcd

    X Yf

    Onto (and one-to-one) Not onto(and one-to-one)

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201215

    Bijective functions

    A function f : X Y is bijective f is one-to-one andonto

    Examples:

    A linear function f(x) = ax + b is a bijective function from the set

    of real numbers to itself

    The function f(x) = x3 is bijective from the set of real numbers toitself.

    The function f : R Rdefined by f(x) = x2 is not bijectivebecause it is not one-to-one (for every real number x,f(x) = f(-x)).

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    6/23

    6

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201216

    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) = x2, then f-1 (4) = 4 = 2, not a uniquevalue and therefore f-1 is not a function.

    However, if f is a bijective function, it can beshown that f-1 is a function.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201217

    Exercises!!!

    Exam Jan 08:

    Given the following function: f(x)=3x -2

    Define one-to-one function. Determine and proofwhether the function f(x) is one-to-one. Thedomain is a set of real numbers

    Define onto function. Determine if the functionf(x) is onto function.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201218

    Composition of functions

    Given two functions g : X Y and f : Y Z, thecomposition f g is defined as follows:

    (f g)(x) = f(g(x)) for every x X.

    Example: g(x) = x2 -1, f(x) = 3x + 5. Then

    (f g)(x) = f(g(x)) = f(x2 -1) = 3(x2 -1) +5 = 3x2 +2

    Composition of functions is associative:

    f (g h) = (f g) h,

    But, in general, it is not commutative:

    f g g f.

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    7/23

    7

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201219

    Binary operators* A binary operatoron 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 andf(x1, x2) X for every pair of elements x1, x2.

    Examples:

    X={1,2,3,}; f(x,y)= x+y for x,y X => f a binary operator

    Binary operators are addition, subtraction andmultiplication of real numbers, taking unions orintersections of sets, concatenation of two strings over aset X, etc.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201220

    Unary operators* A unary operatoron a set X associates to each

    single element of X one element of X.

    In other words:

    f : X X andf(x) 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) = Ac(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 Algorithms and Data Structures, 2011-201221

    Summary (1)

    Functions

    Definition, presentation (arrow diagram, graph)

    Hash functions Collision resolution policies

    One-to-one function

    Onto function

    Bijective function (Bijection)

    Inverse of a function

    Composition of functions

    Binary operator versus unary operator

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    8/23

    8

    22

    Algorithms and DataStructures (ET3155)

    Lecture 3

    Functions, Sequence and Relations(Chapter 3)

    Said Hamdioui

    Computer Engineering Lab

    Delft University of Technology

    2010-2011S.Hamdioiui, CE, EWI,TUDelft

    ET3155 Algorithms and Data Structures,2011-2012

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201223

    Goals Functions

    Explain the concept of a function and its main characteristics

    Prove if a function has some properties (e.g., one-to-one,onto, etc)

    Sequences Explain the concept of a sequence and be able to manipulate

    them

    Relations Explain the concept of Relation and its main properties

    Prove if a relation has some properties

    Prove that a relation is an equivalence relation

    Use a matrix to describe a Relation and to prove its properties

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201224

    Topics

    Functions

    Sequences and Strings

    Relations

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    9/23

    9

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201225

    Sequences and strings

    A sequenceis an ordered list of numbers, usually

    defined according to a formula: sn = f(n) for n = 1, 2, 3,...

    E.g., Cn=1+0.5(n-1) for n=1,2,3,4

    If s is a sequence {sn| n = 1, 2, 3,},

    s1 denotes the first element,

    s2 the second element,

    sn the nth element

    {n} is called the indexing set/ domainof the sequence.

    Usually the indexing set is N (natural numbers) or an

    infinite subset of N.

    nis the index of the sequence

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201226

    Examples of sequences

    Let s = {sn} be the sequence defined by sn = 1/n , for n = 1, 2, 3, First few elements are: 1, , 1/3, , 1/5,1/6,

    Let s = {sn} be the sequence defined by sn = n

    2 + 1, for n = 1, 2, 3, First few elements are: 2, 5, 10, 17, 26, 37, 50,

    Fibonacci sequence: f1 = 1, f2 = 1, fn = fn-1 + fn-2 for n = 2, 3, 4,

    First few elements: f1 = f2 =1, f3 = 2, f4 = 3, f5 = 5, f6 = 8

    Recurrence relation, more later

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201227

    Increasing and decreasing

    A sequence s = {sn} is said to be

    (strictly) increasingif sn < sn+1

    nondecreasingif sn < sn+1

    (strictly) decreasingif sn > sn+1,

    nonincreasingif sn > sn+1,

    for every n = 1, 2, 3,

    Examples: Sn = 4 2n, n = 1, 2, 3, is decreasing: {2, 0, -2, -4, -6,}

    Sn = 2n -1, n = 1, 2, 3, is increasing: {1, 3, 5, 7, 9, }

    Sn = n/2, n = 1, 2, 3, is nondecreasing: {0, 1, 1, 2, 2, 3, }

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    10/23

    10

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201228

    Subsequences

    A subsequenceof a sequence s = {sn} is asequence t = {tn} that consists of certain

    elements ofsretained in the original ordertheyhad in s

    Example:

    let s = {sn = n | n = 1, 2, 3,}

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

    Let t = {tn = 2n | n = 1, 2, 3,}

    2, 4, 6, 8, 10, 12, 14, 16,

    t is a subsequence ofs

    Let m={4,3,2,1}

    m is not a subsequent of s (order not maintained)

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201229

    Sum notation If {an} is a sequence, then the sum

    n

    ai = am + am+1 + + ani = m

    This is called the sum orsigma notation,

    where the Greek letter indicates a sum ofterms from the sequence

    iis called the index

    mis the lower limit

    nis the upper limit

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201230

    Product notation

    If {an} is a sequence, then the product

    n

    ai = am am+1ani=m

    This is called the product notation, where the

    Greek letter (pi) indicates a product ofterms of the sequence

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    11/23

    11

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201231

    Strings

    Let X be a nonempty set. A string over Xis afinite sequence of elements from X.

    Example: if X = {a, b, c}

    Then = bbaccc is a string over X

    Notation: bbaccc = b2ac3

    The length of a string is the number of elements of and is denoted by ||. If = b2ac3 then || = 6.

    The null stringis the string with no elements andis denoted by the Greek letter. || = 0.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201232

    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, b2a6ba

    Concatenationof two strings and is theoperation on strings consisting of writing followed

    by to produce a new string Example: = bbaccc and = caaba,

    then = bbaccccaaba = b2ac4a2ba

    || = | | + ||

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201233

    More on strings (2)

    Bit strings are strings over {0,1}

    Substring of: is obtained by selecting some of allconsecutive elements of

    Definition

    A string is a substring of the string if there are strings and with =

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    12/23

    12

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201234

    Exercises (Exam Jun 08)

    Exam Jan 08

    Given the following sequence:

    zn=(2+n).3n; n 0

    Find a formula for zn-1 ?

    Find a formula for zn-2 ?

    Prove that {zn} satisfies:

    zn = 6 zn-1 9 zn-2 for n 2

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201235

    Topics

    Functions

    Sequences and Strings

    Relations

    Equivalence relations

    Matrices and relations

    Relational database

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201236

    Relations

    Given two sets X and Y, the Cartesian productXxYis the set of all ordered pairs (x,y) where xX andyY XxY = {(x, y) | xX and yY}

    E.g., X={1,2}, Y={a,b}; XxY={(1,a), (1,b), (2,a), (2,b)}

    A (binary) relationRfrom a set X to a set Y is asubset of the Cartesian product XxY

    If (x,y) 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

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    13/23

    13

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201237

    Domain and range

    Given a relation Rfrom X to Y,

    The domainofRis the set Dom(R) = { xX | (x, y) Rforsome yY}

    The rangeofRis the set Rng(R) = { yY | (x, y) Rforsome x X}

    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 Algorithms and Data Structures, 2011-201238

    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 Algorithms and Data Structures, 2011-201239

    Properties of relations (1)

    Let Rbe a relation on a set X (i.e. Ris a subset of XxX)

    Ris reflexiveif (x,x) Rfor every xX 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) R if x < y is reflexive

    Ris symmetricif for all x, y X, if (x,y)Rthen (y,x) R 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) R if x < y is notsymmetric

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    14/23

    14

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201240

    Properties of relations (2)Let Rbe a relation on a set X (i.e. Ris a subset of XxX)

    Ris antisymmetric

    if for all x,yX, if (x,y)R and (y,x)R, 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) R if x < y is antisymmetric

    Ris transitive

    if (x,y) Rand (y,z) Rimply (x,z)R Examples

    - R={(a,a), (b,c), (c,b), (d,d)} is not transitive

    - R on X={1,2,3,4} defined by (x,y) R if x < y is transitive

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201241

    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 Algorithms and Data Structures, 2011-201242

    Order relations

    Let X be a set and Ra relation on X

    Ris a partial orderon X if R is reflexive,antisymmetric and transitive.

    Suppose R is partial order, let x,yX If (x,y) or (y,x) are in R, then x and y are comparable

    If (x,y) Rand (y,x) Rthen x and y are incomparable

    Ifevery pair of elements in X are comparable, thenRis a total orderon X

    x divides yhas someincomp. el.

    eitherx

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    15/23

    15

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201243

    Partial Order

    Is on natural numbers a partial order?

    is reflexive because n n

    is transitive because if l n and n m then lm

    is antisymmetric if m n and n m then n=m

    is a total order?

    yes, because for every n and m: n m or m n

    Is < on natural numbers partial order?

    No because it is not reflexive : n

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    16/23

    16

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201246

    Equivalence relationsLet X be a set and Ra relation on X

    Ris an

    equivalence relationon X if

    Ris

    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 Rthen (x,z) is also in R

    R is an equivalence relation

    Note: directed graphs can be used

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201247

    Equivalence relation

    Examples:

    Is = on natural numbers an equivalencerelation ?

    = is reflexive : n=n = is symmetric : if m=n then n=m = is transitive : if m=n and n=l then m=l

    R is neither reflexive nor transitive

    Is R={(a,a), (b,c), (c,b), (d,d)} onX={a,b,c,d} an equivalent relation?

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201248

    Partitions

    A partitionSon a set X is a collection {A1, A2,, An}of subsets of X, such that

    A1A2A3An = X

    Aj Ak = 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.

    If X={1,2,3,4,5,6,7,8}, thenS = {{1,4,5}, {2,6}, {3,7}, {8}} is a partition of X

    S = {{1,2,4,5}, {2, 3, 6}, {7}, {8}} is NOT a partition of X

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    17/23

    17

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201249

    Partitions and equivalence relations

    Theorem: Let Sbe a partition on a set X.

    Define a relation Ron X by xRy to mean that for some subset

    T Sboth x, y belongs to T.Then: Ris an equivalence relationon X. an equivalence relation on a set X corresponds to apartition 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. Sodo y and x, yRx (symmetric).

    Suppose xRy and yRz. x and y belong to same subset A andy and z belong to same subset T. Since y belongs to exactlyone memberS, we must have T=A. Hence xRz (transitive).

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201250

    Partitions and equivalence relations

    Consider partition S={ {1,3,5} ,{2,6},{4}} ofX={1,2,3,4,5,6}

    Consider the relation R on X defined by xRy if x, ybelongs to the same subset TS (theorem of the previousslide)

    R = {(1,1)(1,3)(1,5)(3,1)(3,3)(3,5)(5,1)(5,3)(5,5)}

    is an equivalence relation

    because {1,3,5} is in S.

    The complete relation 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 Algorithms and Data Structures, 2011-201251

    Equivalence classes

    Let X be a set and let Rbe an equivalence relation on X.Let a X.

    Define [a] ={ xX | xRa }(i.e., the set of all elements in X that are related to a)

    Let S = { [a] | a 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 classesof X given bythe relation R.

    Given a, b X, then [a] = [b] or [a][b] =

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    18/23

    18

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201252

    Partitions and equivalence relations Consider the equivalence relation 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] consists of all x such that (x,1)R: [1]={1,3,5}=[3]=[5]

    The other equivalence classes are : [2]=[6]={2,6}

    [4]={4}

    Hence S = { [a] | aX} [Theorem previous slide]={{1,3,5},{2,6},{4}} is a partition

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201253

    Partitions and equivalence relations Example:

    let X={1,2,...,10}. xRy means 3 divides x-y; Ris an equivalence relation

    members of equivalence classes :

    equivalence=has same remainder when divided by 3

    [1]={x X| 3 divides x-1}={xX | 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] | aX}= {{1,4,7,10},{8,5,2}{3,6,9}}is a partition

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201254

    Exercises

    Exercise 1: How many equivalence relations are there on the set {1,2,3}}?

    Exercise 2:

    Let X={1, 2, , 10}

    Define a relation R on XxX by (a,b)R(c,d) if a+d=b+c

    Is R an equivalence relation on XxX?

    Exercise 3: Exam Jan 10

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    19/23

    19

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201255

    Matrices of Relations

    Let X, Y be sets and Ra relation from X to Y

    Matrix of the relation R (say A = (aij) ) isdefined as follows:

    Rows of A = elements of X

    Columns of A = elements of Y

    Element ai,j = 0 if the element of X in row i and

    the element of Y in column j are not related

    Element ai,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 Algorithms and Data Structures, 2011-201256

    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 Ris

    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 Algorithms and Data Structures, 2011-201257

    The matrix of a relation (2)

    IfRis a relation from a set X to itself and A is thematrix ofRthen 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

    1000

    0110

    0110

    0001

    d

    c

    b

    a

    A

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    20/23

    20

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201258

    The matrix of a relation on a set X

    Let A be the square matrix of a relation R from Xto itself. Let A2 = the matrix product AA.

    Ris reflexive All terms aii in the maindiagonal of A are 1

    Ris symmetric aij = aji for all i and j, i.e. R is a symmetric relation on X if A is a symmetric

    matrix

    Ris transitive whenevercij in C = A2 isnonzero then entry aij in A is also nonzero.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201259

    Example

    R = {(a,a),(b,b},{b,c},(c,b),(c,c),(d,d)}

    1000

    0220

    0220

    0001

    1000

    0110

    0110

    0001

    2A

    A

    R is symmetric

    R is reflexive

    R is transitive

    R is an equivalence relation

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201260

    Relational databases

    A binaryrelation Ris a relation among twosets X and Y, already defined as R X x Y.

    An n-aryrelation Ris a relation among n setsX1, X2,, Xn, i.e. a subset of the Cartesian

    product, R X1 x X2 xx Xn. Thus, Ris a set of n-tuples (x1, x2,, xn) where xk

    Xk, 1 < k < n.

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    21/23

    21

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201261

    Databases

    A databaseis a collection of records that are

    manipulated by a computer. They can beconsidered as nsets X1 through Xn, each of whichcontains a list of items with information.

    Database management systemsare programsthat help access and manipulate information

    stored in databases.

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201262

    Relational database model Columns of an n-ary relation are called attributes

    E.g., Attribute Age with a domain: all positive integers lessthan 100

    An attribute is a keyifno two entries have the samevalue e.g. ID is a key but Age is NOT (ID is unique but age is not)

    A queryis a request for information from the database E.g. find all persons with age 50

    EmployeeID Name Age

    22 Jan 22

    71 Mark 50

    101 Peter 50

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201263

    Operators

    The selection operatorchooses n-tuples from a relation bygiving conditions on the attributes

    E.g., Employee [Age=50] will select (71,Mark,50) and (101,Peter,50)

    The projection operatorchooses two or more columns and

    eliminates duplicates

    E.g., Employee [ID, Name] will select (22,Jan), (71,Mark) and (101, Peter)

    Thejoin operatormanipulates two relations

    form the Cartesian product of two relations and select those tuplessatisfying a certain condition

    E.g., merge each two rows of two

    different tables when a certain

    condition is satisfied

    Employee

    ID Name Age

    22 Jan 22

    71 Mark 50

    101 Peter 50

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    22/23

    22

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201264

    SQL - Structured Query Language*

    SELECT

    SELECT Name, AgeFROM Employee

    WHERE Age>=60

    JOINSELECT employees.name

    FROM employees

    JOIN invoices

    ON employees.ID=invoices.employeeID

    SQL tutorial can be found at

    http://www.tizag.com/sqlTutorial

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201265

    Summary (2)

    Sequences and Strings

    Definition of a sequence and a string

    Subsequence versus substring

    Increasing versus decreasing sequences

    Nonincreasing versus nondecreasing sequences

    Sum/Sigma notation versus product notation

    Null string

    Let X be a nonempty finite set X*: the set of all strings over X, including the null string

    X+: the set of all nonnull strings over X

    Concatenation of strings

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201266

    Summary (3)

    Binary relations, domain, range

    Relation properties Reflexive, Symmetric, Antisymmetric, Transitive, Partial order

    Inverse relation

    Equivalence relations and partitions

    Equivalence classes

    Matrix of a relation Reflexive, symmetric and transitive

    Binary relation versus n-ary relation

    Relational database (attribute, key, query)

    Operators (selection, projection, join)

  • 8/3/2019 2011-2012 Lecture 2.2 3.1 Functions Sequences Relations CH3

    23/23

    S.Hamdioiui, CE, EWI, TUDelft ET3155 Algorithms and Data Structures, 2011-201267

    Suggested Exercises

    From Chapter 3 Self-test 1, 4, 9, 14, 17