discrete structure for computer science

download discrete structure for computer science

of 47

Transcript of discrete structure for computer science

  • 8/13/2019 discrete structure for computer science

    1/47

    What is a set?A Set is any well-definedcollection of objects called the elements or members

    of the set.

    Well-definedmeans that it is possible to describe if a given object belongs to

    the collection or not.

    Describing a Set

    Way one: List the elements of the set between braces (f ini te elements)

    e.g. the set of all positive integers that are less than 4 : {1, 2, 3}

    Way two: Specify a property that the elements of the set have in common

    e.g. R={x | x is a real number }

    1

    1.1. Sets and Subset

    Property of the elements

  • 8/13/2019 discrete structure for computer science

    2/47

    The order of the Set

    {1, 2, 3}={1, 3, 2}={2, 3, 1}={2, 1,3 }={3, 1, 2}={3, 2, 1}

    Repeated elements can be ignored{1, 2, 3, 1} = {1, 2, 3}

    Several commonly used sets

    2

    1.1. Sets and Subset

  • 8/13/2019 discrete structure for computer science

    3/47

    The relationships between Element & Set

    Usually, we use uppercase letters such as A, B and C to denote

    sets, and lowercase letters such as a, b, c, x, y and z to denote the

    elements of sets

    Binary cases: for a given element x and set A1: x belongs to A denoted by x A

    2: x does not belong to A denoted by x A

    Fuzzy Sets

    The collections of richpeople, young girls, so on and so forthNote: The words rich, young, beautiful , cool, hot, fat, thin etc. are

    fuzzy (not well def ined).

    3

    1.1. Sets and Subset

  • 8/13/2019 discrete structure for computer science

    4/47

    SubsetIf every element of A is also an element of B, namely, if whether x A then x

    B, we say that A is a subsetof B, denoted by A B . Otherwise, .

    Venn diagrams

    A B

    4

    1.1. Sets and Subset

    B A BA A B

  • 8/13/2019 discrete structure for computer science

    5/47

    5

    1.1. Sets and Subset

    A B

    A=B: A B & B A A

    U

    An universal set (U) is a set containing all objects for

    which the discussion is meaningful.

  • 8/13/2019 discrete structure for computer science

    6/47

  • 8/13/2019 discrete structure for computer science

    7/47

    The cardinality of a finite setA set A is called finite if it has ndistinctelements, where n N.

    In this case, n is the cardinality of A and is denoted by |A|.

    e.g. A={1,2,3,1} |A| = 3

    B={a, b, c, d, e, a}, |B| = 5

    |A| < |B|

    A set that is not finite is called infinite, for instances, N, Z, Q, R

    as mentioned in Example 3.

    7

    1.1. Sets and Subset

  • 8/13/2019 discrete structure for computer science

    8/47

    Power set of a set A

    If A is a set, then the set of all subsets of A is called the power

    set of A and is denoted by P(A).

    e.g. A={1,2,3}

    Then P(A) consists of the following subsets of A: {}, {1}, {2},{3}, {1,2}, {1,3}, {2,3}, and {1,2,3}

    |P(A)| = 2^n, why? Assuming n = |A| N

    1.1. Sets and Subset

    8

  • 8/13/2019 discrete structure for computer science

    9/47

    UnionIf A and B are sets, we define their union as the set consisting of

    all elements that belong to A or B and denote it by A U B.

    A U B ={ x | x A or x B}

    1.2. Operations on Sets

    9

    A

    U

    B

  • 8/13/2019 discrete structure for computer science

    10/47

    Intersection

    If A and B are sets, we define their intersection as the set

    consisting of all elements that belong to both A and B and

    denoted it by A B.

    A B ={ x | x A and x B}

    1.2. Operations on Sets

    10

    U

    A B

  • 8/13/2019 discrete structure for computer science

    11/47

    Complement of B with respect to AIf A and B are two sets, we define the complement of B with

    respect to A as the set of all elements that belong to A but not to

    B, and we denote it by A - B

    A - B ={ x | x A and x B}

    1.2. Operations on Sets

    11

    U

    AA B

  • 8/13/2019 discrete structure for computer science

    12/47

    ComplementIf U is a universal set containing A, then U-A is called the

    complement of A and is denoted by

    = {x | x A}

    1.2. Operations on Sets

    12

    U

    A

  • 8/13/2019 discrete structure for computer science

    13/47

    Symmetric differenceIf A and B are two sets, we define their symmetric difference as

    the set of all elements that belong to A or to B, but not to both A

    and B, and we denote it by A B

    A B = {x | (x A and x B) or (x B and x A) }

    1.2. Operations on Sets

    13

    U

    A B

  • 8/13/2019 discrete structure for computer science

    14/47

    Commutative PropertiesA U B = B U A ; A B = B A

    Associative Properties

    A U (B U C) = ( A U B ) U C

    A (B C) = ( A B ) C

    Distribution Properties

    A (B U C) = ( A B ) U ( A C )

    A U (B C) = ( A U B ) ( A U C )

    14

    1.2. Operations on Sets

  • 8/13/2019 discrete structure for computer science

    15/47

    Idempotent Properties

    A U A =A ; A A = A

    Properties of the complement

    De Morgans Law

    15

    1.2. Operations on Sets

  • 8/13/2019 discrete structure for computer science

    16/47

    Properties of a Universal setA U U = U

    A U = A

    Properties of the empty set

    A U = A

    A =

    16

    1.2. Operations on Sets

  • 8/13/2019 discrete structure for computer science

    17/47

    How to proof above properties?e.g. Proof:

    Proof: suppose x , then we have x AB, so

    x or ,x which means that x . Thus,

    Conversely, suppose x then we have x A or x B ,

    so x A B, which means that x .Thus

    Therefore,

    A common style of proof for statements about sets is to choose an

    element in one of the sets and see what we know about it.17

    1.2. Operations on Sets

  • 8/13/2019 discrete structure for computer science

    18/47

    Addition PrincipleTheorem 2: If A and B are finite sets, then

    |A U B| = |A| + |B| - |A B |

    18

    1.2. Operations on Sets

    U

    A B

    A B

  • 8/13/2019 discrete structure for computer science

    19/47

    Example 9A computer company wants to hire 25 programmers to handle

    systems programming jobs and 40 programmers for applications

    programming. Of those hired, 10 will be expected to perform

    jobs of both types. How many programmers must be hired? (at

    least? )Solution:

    A: the set of system programmers hired

    B: the set of applications programmers hired, then

    |A| = 25, |B| = 40, |A B| =10

    |A U B| = |A| + |B| - |A B |

    = 25 + 40 -10 =55

    1.2. Operations on Sets

    19

  • 8/13/2019 discrete structure for computer science

    20/47

    Generalized case for three setsTheorem 3: Let A, B and C be finite sets. Then

    |A U B U C| = |A| + |B| + |C| - |AB| - |BC|-|AC| + |ABC|

    20

    1.2. Operations on Sets

    A B

    C

    AB

    BCAC

    ABC

  • 8/13/2019 discrete structure for computer science

    21/47

    Sequences

    a list of objects arranged in a definite order

    finite or infinite, explicit or recursive

    some elements may be repeated in a sequence

    set corresponding to a sequenceset of all distinctelements in the sequence;

    A set is called countable if it is the set corresponding

    to some sequence All finite sets are countable Not all infinite sets are countable (e.g. the set of all real

    numbers)

    AU, characteristic function fAof A is defined foreach xU,

    fA (x)=1 if xA; fA(x)=0 if xA

    fAB= fAfB fAB= fA+fB-fAfB

    fAB= fA+fB-2fAfB

  • 8/13/2019 discrete structure for computer science

    22/47

    While sets have no order and no repeated elements,sequenceshave order and can contain repeats at differingpositions in the order.

    The set {5,2,5} = {5,2} = {2,5} The sequence (5,2,5) (5,2) (2,5)

    Actually, there is a notion of a multisetor bagthat wesometimes use. It has no order, but repeated elements areallowed. Since position is irrelevant, we just record eachunique elements with a count.

    We can talk about the k-th elementof a sequence, but not ofa set or multiset.

    Finite sequences are often called tuples. Those of length kare k-tuples. A 2-tuple is also called apair.

    Computer representation of Sets and Subsets

  • 8/13/2019 discrete structure for computer science

    23/47

    Computer representation of Sets and Subsets

    Let U={a,b,e,g,h,r,s,w}. The array of length 8 shown below represents U.

    1, if x= a,b,e,g,h,r,s,w

    fU(x)=

    0 otherwise

    If S={a,e,r,w} then

    1, if x= a,e,r,w

    fS(x)=0 if x= b,g,h,s

    The array representation of subset S is

    1 0 1 0 0 1 0 1

  • 8/13/2019 discrete structure for computer science

    24/47

    Strings and Regular Expressions

    alphabet A: a set of symbols string: a finitesequence of symbols in A

    A*consists of all finitestrings from A

    empty string : contains no symbols

    catenation of w1and w2is w1 w2

    a regular expressionover A is a stringconstructed from the elements of A and thesymbols (, ), , *, , according to the followingdefinition: (a recursive definition)

    RE1. The symbol is a regular expression.

    RE2. IfxA, the symbolxis a regular expression. RE1 and RE2 provide initial regular expressions

  • 8/13/2019 discrete structure for computer science

    25/47

    Strings and Regular Expressions (cont)

    RE3. If and are regular expressions, then the

    expression is regular.RE4. If and are regular expressions, then the

    expression () is regular.

    RE5.If is a regular expression, then the expression

    ()*is regular. each regular expression corresponds to a regular

    subsetsof A*, or regular setif no reference to A isneeded

    the concept of regular expressions is important forthe study of the syntax of programming languages

    To compute the regular set corresponding to aregular expression, use the following correspondencerules:

  • 8/13/2019 discrete structure for computer science

    26/47

    Example

  • 8/13/2019 discrete structure for computer science

    27/47

    Example

    Ex 1. Let A=(a,b,c,..,z}, the usual English alphabet. Then A* consists of

    ordinary words, such as ape,queen and so on as well as yxaloble,esy etc.All

    finite sequences from are A*, whether they have meaning or not.

    Ex 2. If w1=s1s2snand w2=t1t2tnare the elements of A* for some set A wedefine the catenation of w1and w2as the sequence s1s2snt1t2t2. The

    catenation of w1and w2is written as w1.w2or w1w2and its another element

    of A*.

    Ex 3. If A={Ram,swims,Sam, walks, sleep,slowly} then A* contain real

    sentences like Ram walks slowly as well as nonsense sentences likeswim sleep walks

    Ex 4. Let A={0,1} 0*(0V1)* is regular expression.

    By RE2, 0 and 1 are regular expression. Thus (0V1) us a regular expression

    by RE4 and so 0* and (0V1)* are regular by RE5 and finally we see that

    0*(0V1)* is regular by RE3.

  • 8/13/2019 discrete structure for computer science

    28/47

    Associated with each regular expression over A, there is a corresponding

    subset of A*. Such sets are called regular subsets of A* or just regular sets.

    Ex 1. Let A={a,b,c}. Then the regular expression a* corresponds to the set of all

    finite sequences of as, such as aaa,aaaaa and so on. The regular

    expression a(bVc) corresponds to the set {ab,ac} A* The regularexpression ab(bc)*corresponds to the set of all strings that begin with ab and

    then repeat the symbol bc n times where n>=0. This set includes ab,abbc,

    abbcbc and so on.

    Ex 2. If A={0,1} the regular set of corresponding to regular expression

    00*(0V1)*1 corresponds to the set of all sequences of 0s and 1s that beginwith at least one 0 and end with at least one 1.

    M t i

  • 8/13/2019 discrete structure for computer science

    29/47

    Matrix

    An array of numbers arranged in mhorizontal rowsand nvertical columns:

    A = a11 a12 a13 . a1na21 a22 a23 a2n

    am1 am2 am3 amn

    The ith row of A is [ai1, ai2, ai3, ain]; 1 im

    Thejth column of A is a1ja2j ; 1 jna3jamj

  • 8/13/2019 discrete structure for computer science

    30/47

    We say that A is a matrix mx n. If m= n, then A

    is a square matrix of order n, and a11, a22, a33,

    ..annform the main diagonal of A.

    aijwhich is in the ith row andjth column, is said

    to be the i ,jth element of Aor the (i , j) entry

    of A, often written as A = [aij].

  • 8/13/2019 discrete structure for computer science

    31/47

    Ex 2:

    A = 8 0 0 0

    0 3 0 0

    0 0 7 00 0 0 1

    A square matrix A = [aij], for which every entryoff the main diagonal is zero, that is aij= 0 for i

    j, is called a diagonal matrix.

  • 8/13/2019 discrete structure for computer science

    32/47

    Two mx nmatrices A and B, A = [aij] and B =

    [bij], are said to be equalif aij= bijfor 1 im,

    1 jn; that is, if corresponding elements are

    the same.

    Ex 3:

    A = a 5 3 B = 1 5 x

    2 7 -1 y 7 -1

    3 b 0 3 4 0

    So, if A = B, then a = 1, x = 3, y = 2, b = 4.

    Matrix summation

  • 8/13/2019 discrete structure for computer science

    33/47

    Matrix summation

    If A = [aij] and B = [bij] are mx nmatrices, then

    the sum of A and B is matrix C = [cij], defined by

    cij= a

    ij+ b

    ij; 1 i m, 1 j n.

    C is obtained by adding the corresponding

    elements of A and B.

  • 8/13/2019 discrete structure for computer science

    34/47

    A = 1 5 3 B = 2 0 3

    2 7 -1 6 1 3

    3 4 0 -3 1 9

    C = 3 5 6

    8 8 4

    0 5 9

    The sum of the matrices A and B is defined onlywhenA and B have the same number of rows and the samenumber of columns (same dimension).

  • 8/13/2019 discrete structure for computer science

    35/47

    A matrix in when all of its entries are zero is

    called zero matrix, denoted by 0.

    Theorems involved in summation :

    A + B = B + A.

    (A + B) + C = A + (B + C).A + 0 = 0 + A = A.

  • 8/13/2019 discrete structure for computer science

    36/47

    Matrices Product

    If A = [aij] is an mxpmatrix and B = [bij] is apx

    nmatrix, then the product of A and B, denotedAB, will produce the mx nmatrix C = [cij],

    defined by

    cij= ai1b1j+ ai2b2j+ + aipbpj;1 in, 1 j m

    That is, elements ai1, ai2, .. aipfrom ith row of A

    and elements b1j, b2j, .. bpjfromjth column of B,

    are multiplied for each corresponding entries

    and add all the products.

  • 8/13/2019 discrete structure for computer science

    37/47

    Ex 5:

    A = 2 3 -4 B = 3 1

    1 2 3 -2 2

    5 -3

    2 x 3 3 x 2

    AB = 2(3) + 3(-2) + -4(5) 2(1) + 3(2) + -4(-3)

    1(3) + 2(-2) + 3(5) 1(1) + 2(2) + 3(-3)

  • 8/13/2019 discrete structure for computer science

    38/47

    = 6620 2 + 6 + 12

    34 + 15 1 + 49

    = -20 20

    14 -42 x 2

  • 8/13/2019 discrete structure for computer science

    39/47

    If A is an mxp matrix and B is apx nmatrix, in

    which AB will produce mx n, BA might be

    produce or not depends on:

    nm, then BA cannot be produced.

    n= m,pm@ n, then we can get BA but the

    size will be different from AB. n = m=p, A B, then we can get BA, the size

    of BA and AB is the same, but AB BA.

    n= m=p, A = B, then we can get BA, the sizeof BA and AB is the same, and AB = BA.

    Identity matrix

  • 8/13/2019 discrete structure for computer science

    40/47

    Identity matrix

    Let say A is a diagonal matrix nx n. If all entries on its

    diagonal are 1, it is called identity matrix, ordered n,

    written as I. Ex 7:

    1 0 1 0 0 1 0 0 0

    0 1 0 1 0 0 1 0 0

    0 0 1 0 0 1 0

    0 0 0 1

    Theorems involved are: A(BC) = (AB)C.

    A(B + C) = AB + AC.

    (A + B)C = AC + BC.

    IA = AI = A.

    Transposition Matrix

  • 8/13/2019 discrete structure for computer science

    41/47

    Transposition Matrix If A = [aij] is an mx nmatrix, then A

    T= [aij]Tis a nx

    mmatrix, where

    aijT= aji; 1 i m, 1 jn

    It is called transposition matrixfor A.

    Ex 8:

    A = 2 -3 5 AT= 2 6

    6 1 3 -3 1

    5 3

    Theorems involved are: (AT)T= A

    (A + B)T= AT+ BT

    (AB)T= BTAT

  • 8/13/2019 discrete structure for computer science

    42/47

    Matrix A = [aij] is said to be symmetricif AT= A, that

    is aij= aji,

    A is said to be symmetric if all entries aresymmetrical to its main diagonal.

    Ex 9:

    A = 1 2 -3 B = 1 2 -3

    2 4 5 2 4 0

    -3 5 6 3 2 1

    Symmetric Not Symmetric, why?

    Boolean Matrix and Its Operations

  • 8/13/2019 discrete structure for computer science

    43/47

    Boolean Matrix and Its Operations

    Boolean matrixis an mx nmatrix where all of its entriesare either 1 or 0 only.

    There are three operations on Boolean:

    a)Join by

    Given A = [aij] and B = [bij] are Boolean matrices with thesame dimension,jo in byA and B, written as A B, will

    produce a matrix C = [cij], wherecij= 1 if aij= 1 OR bij= 1

    0 if aij= 0 AND bij= 0

    b)MeetMeetfor A and B, both with the same dimension, writtenas A B, will produce matrix D = [dij] where

    dij= 1 if aij= 1 AND bij= 1

    0 if aij= 0 OR bij= 0

  • 8/13/2019 discrete structure for computer science

    44/47

    Ex 10: A =1 0 1 B = 1 1 0

    0 1 1 0 0 11 1 0 0 1 0

    0 1 0 1 1 0

    A B= 1 1 1 A B= 1 0 00 1 1 0 0 1

    1 1 0 0 1 0

    1 1 0 0 1 0

  • 8/13/2019 discrete structure for computer science

    45/47

    c) Boolean product

    If A = [aij] is an mxpBoolean matrix, and B = [bij] is apx nBoolean matrix, we can get a Boolean productfor A and B written as A B, producing C, where:

    cij= 1 if aik= 1 AND bkj= 1; 1 kp.

    0 other than that

    It is using the same way as normal matrix product.

    +

    +

    E 11

  • 8/13/2019 discrete structure for computer science

    46/47

    Ex 11:

    A = 1 0 0 0 B = 1 1 0

    0 1 1 0 0 1 0

    1 0 1 1 1 1 03 x 4 0 0 1

    4 x 3

    A B = 1 + 0 + 0 + 0 1 + 0 + 0 + 0 0 + 0 + 0 + 0

    0 + 0 + 1 + 0 0 + 1 + 1 + 0 0 + 0 + 0 + 01 + 0 + 1 + 0 1 + 0 + 1 + 0 0 + 0 + 0 + 1

    A B = 1 1 0

    1 1 01 1 1

    3 x 3

    +

  • 8/13/2019 discrete structure for computer science

    47/47

    Exercise 3: A = 1 0 0 0 B = 0 1 0 0 C = 0 0 1 0

    0 1 1 0 0 0 1 1 1 0 0 00 0 0 1 0 1 0 1 1 1 0 01 1 0 0 0 0 1 0 1 1 1 0

    Find:a) A Bb) A Bc) A Bd) A Ce) A Cf) A Cg) B Ch) B Ci) B C

    +

    +