Languages and Finite Automata - PowerPoint by hQeoR4cc

VIEWS: 14 PAGES: 57

									                       Introduction to
     Theoretical Computer Science
                            COMP 335
                              Fall 2004
            Slides by Costas Busch, Rensselaer Polytechnic Institute,
             Modified by N. Shiri & G. Grahne, Concordia University

Fall 2004                            COMP 335                           1
            •This course: A study of
               abstract models of computers and computation.


            •Why theory, when computer field is so practical?


            •Theory provides concepts and principles, for both
            hardware and software that help us understand the
            general nature of the field.



Fall 2004                        COMP 335                        2
            Mathematical Preliminaries




Fall 2004              COMP 335          3
            Mathematical Preliminaries

• Sets
• Functions
• Relations
• Graphs
• Proof Techniques




Fall 2004              COMP 335          4
                       SETS
A set is a collection of elements
            A  {1, 2, 3}
            B  {train, bus, bicycle, airplane}

We write
            1 A
            ship  B
Fall 2004               COMP 335                  5
                Set Representations

       C = { a, b, c, d, e, f, g, h, i, j, k }

       C = { a, b, …, k }               finite set


       S = { 2, 4, 6, … }               infinite set

       S = { j : j > 0, and j = 2k for k>0 }

       S = { j : j is nonnegative and even }

Fall 2004                    COMP 335                  6
  A = { 1, 2, 3, 4, 5 }
                                                        U
                6                   A
                                    2       3       8
                            1
                  7             4       5
                                                9
                      10


Universal Set:         all possible elements

                             U = { 1 , … , 10 }
Fall 2004                    COMP 335                       7
                          Set Operations
                A = { 1, 2, 3 }           B = { 2, 3, 4, 5}
                                                 A           B
• Union
                                                         2   4
                                                 1
            A U B = { 1, 2, 3, 4, 5 }                    3       5


• Intersection
                U
            A       B = { 2, 3 }                         2
                                                         3
• Difference
            A-B={1}
                                                     1
            B - A = { 4, 5 }
                                                 Venn diagrams
Fall 2004                          COMP 335                          8
• Complement
            Universal set = {1, …, 7}
            A = { 1, 2, 3 }                  A = { 4, 5, 6, 7}


                 4
                         A
                                      A      3         6
                              1
                                      2
                     5                             7


                                  A=A
Fall 2004                         COMP 335                       9
            { even integers } = { odd integers }


                              Integers

                1       odd
                                   even
                                              6       5
                              2
                                      0
                                          4
                    3                             7


Fall 2004                         COMP 335                10
            DeMorgan’s Laws


             AUB=A                  B
                                U



             A       B=AUB
                 U




Fall 2004            COMP 335           11
                    Empty, Null Set:
            ={}

     SU           =S

     S          =
            U
                                       = Universal Set

     S-         =S

            -S=



Fall 2004                   COMP 335                     12
                             Subset
            A = { 1, 2, 3}                  B = { 1, 2, 3, 4, 5 }
                             A       B




                                 U
        Proper Subset: A              B

                                 U
                                     B
                                     A



Fall 2004                        COMP 335                           13
                   Disjoint Sets
            A = { 1, 2, 3 }              B = { 5, 6}


                      A       B=
                          U




                     A                    B




Fall 2004                     COMP 335                 14
                         Set Cardinality
    • For finite sets
       A = { 2, 5, 7 }


       |A| = 3


            (set size)




Fall 2004                    COMP 335      15
                      Powersets
     A powerset is a set of sets

    S = { a, b, c }


   Powerset of S = the set of all the subsets of S

    2S = {   , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }


    Observation: | 2S | = 2|S|           ( 8 = 23 )

Fall 2004                    COMP 335                           16
                    Cartesian Product
            A = { 2, 4 }               B = { 2, 3, 5 }


 A X B = { (2, 2), (2, 3), (2, 5), ( 4, 2), (4, 3), (4, 5) }


                     |A X B| = |A|*|B|


            Generalizes to more than two sets


                       AXBX…XZ
Fall 2004                   COMP 335                           17
                           FUNCTIONS
                      domain                range
                   4    A                       B
                                 f(1) = a       a
                       1
                       2                    b
                        3                       c
                  5
                               f : A -> B
        If A = domain
            then f is a total function
            otherwise f is a partial function
Fall 2004                      COMP 335             18
                             RELATIONS
            R = {(x1, y1), (x2, y2), (x3, y3), …}


            xi R yi


            e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1




Fall 2004                         COMP 335          19
                 Equivalence Relations
 • Reflexive:       xRx
 • Symmetric:       xRy               yRx
 • Transitive:      x R y and y R z         xRz


 Example: R = ‘=‘
 •x=x
 •x=y                y=x
 • x = y and y = z            x=z

Fall 2004                  COMP 335               20
                        Equivalence Classes
   For an equivalence relation R, we define
   equivalence class of x
              [x]R = {y : x R y}
   Example:
            R = { (1, 1), (2, 2), (1, 2), (2, 1),
                   (3, 3), (4, 4), (3, 4), (4, 3) }
   Equivalence class of [1]R = {1, 2}
   Equivalence class of [3]R = {3, 4}


Fall 2004                          COMP 335           21
                           GRAPHS
 A directed graph G=〈V, E〉
                                             e
                             b
              node
                a                                d

                                         c
• Nodes (Vertices)
                 V = { a, b, c, d, e }
• Edges
     E = { (a,b), (b,c), (b,e),(c,a), (c,e), (d,c), (e,b), (e,d) }
Fall 2004                     COMP 335                         22
                Labeled Graph


                                2
                                    6       e
                        b                       2
                1                       3
            a                               6       d
                    5
                                    c




Fall 2004                   COMP 335                    23
                                Walk

                                              e
                            b
                a                                 d

                                        c


            Walk is a sequence of adjacent edges
                     (e, d), (d, c), (c, a)


Fall 2004                    COMP 335                 24
                        Path

                                       e
                        b
             a                             d

                                   c


   A path is a walk where no edge is repeated
   A simple path is a path where no node is repeated

Fall 2004               COMP 335                       25
                               Cycle


                        base                  e
                               b
                    3
               a                   1              d
                          2
                                          c


      A cycle is a walk from a node (base) to itself
      A simple cycle: only the base node is repeated

Fall 2004                      COMP 335                26
                           Trees
                   root



                                     parent

            leaf

                                        child


 Trees have no cycles
 Ordered trees?

Fall 2004                 COMP 335              27
                   root
                           Level 0



                                Level 1
            leaf                               Height 3

                                     Level 2



                                     Level 3

Fall 2004                 COMP 335                        28
            PROOF TECHNIQUES



             • Proof by induction


             • Proof by contradiction




Fall 2004              COMP 335         29
                         Induction

     We have statements P1, P2, P3, …

    If we know
            • for some b that P1, P2, …, Pb are true
            • for any k >= b that
                  P1, P2, …, Pk imply Pk+1
    Then
            Every Pi is true, that is, ∀i P(i)

Fall 2004                    COMP 335                  30
                Proof by Contradiction

     We want to prove that a statement P is true


            • we assume that P is false
            • then we arrive at an incorrect conclusion
            • therefore, statement P must be true




Fall 2004                  COMP 335                       31
                            Example
Theorem:              2   is not rational


Proof:
            Assume by contradiction that it is rational

                  2   = n/m
                n and m have no common factors


            We will show that this is impossible

Fall 2004                     COMP 335                    32
            2   = n/m              2 m 2 = n2



                                                n is even
  Therefore,        n2   is even
                                                n=2k


                                                    m is even
   2 m2 = 4k2                m2 = 2k2
                                                    m=2p


        Thus, m and n have common factor 2

                 Contradiction!
Fall 2004                      COMP 335                         33
Pigeon Hole Principle:
If n+1 objects are put into n boxes, then at least
one box must contain 2 or more objects.
Ex: Can show if 5 points are placed inside a square whose
sides are 2 cm long  at least one pair of points are at a
distance ≤ 2 cm.
According to the PHP, if we divide the square into 4, at
least two of the points must be in one of these 4 squares.
But the length of the diagonals of these squares is 2.
 the two points cannot be further apart than 2 cm.




Fall 2004                   COMP 335                         34
            Languages




Fall 2004      COMP 335   35
A language is a set of strings


String: A sequence of letters/symbols

            Examples: “cat”, “dog”, “house”, …

            Defined over an alphabet:
                     a, b, c,, z

Fall 2004               COMP 335                 36
             Alphabets and Strings
We will use small alphabets:      a, b

Strings
            a
            ab                  u  ab
            abba                v  bbbaaa
            baba                w  abba
            aaabbbaabab
Fall 2004            COMP 335                37
            String Operations

w  a1a2 an                    abba
v  b1b2 bm                    bbbaaa


               Concatenation

wv  a1a2 anb1b2 bm          abbabbbaaa

Fall 2004           COMP 335                38
       w  a1a2 an               ababaaabbb


                  Reverse



      w  an  a2 a1
            R
                                  bbbaaababa

Fall 2004              COMP 335                39
                String Length
            w  a1a2 an
Length:     w n


Examples:      abba  4
               aa  2
               a 1
Fall 2004             COMP 335   40
            Length of Concatenation

                 uv  u  v

Example:     u  aab, u  3
             v  abaab, v  5


             uv  aababaab  8
             uv  u  v  3  5  8
Fall 2004            COMP 335         41
             The Empty String
A string with no letters:       


Observations:     0

                w  w  w

                abba  abba  abba
Fall 2004            COMP 335          42
                 Substring
Substring of string:
  a subsequence of consecutive characters

            String              Substring
            abbab                ab
            abbab                abba
            abbab                b
            abbab                bbab
Fall 2004            COMP 335               43
                    Prefix and Suffix
                abbab
      Prefixes          Suffixes
                        abbab              w  uv
            a            bbab
                                        prefix
            ab           bab
                                                 suffix
            abb          ab
            abba         b
            abbab        
Fall 2004                    COMP 335                44
              Another Operation
                 w  ww w
                    n
                      
                     
                                   n

Example:      abba   abbaabba
                        2



Definition:   w 
                0


              abba   
                        0

Fall 2004                   COMP 335   45
              The * Operation
 * : the set of all possible strings from
      alphabet 

  a, b
*   , a, b, aa, ab, ba, bb, aaa, aab,




Fall 2004            COMP 335                 46
               The + Operation
    : the set of all possible strings from
 
       alphabet  except 

  a, b
*   , a, b, aa, ab, ba, bb, aaa, aab,

   *  {}
      


    
  a, b, aa, ab, ba, bb, aaa, aab,
Fall 2004             COMP 335                47
                   Languages
A language is any subset of       *

Example:       a, b
             *   , a, b, aa, ab, ba, bb, aaa,


Languages:   
             a, aa, aab
             { , abba, baba, aa, ab, aaaaaa}
Fall 2004              COMP 335                 48
                       Note that:


            Sets         { }  {}

        Set size       {}    0

        Set size       {}  1
       String length    0
Fall 2004                COMP 335      49
               Another Example

An infinite language     L  {a b : n  0}
                                  n n



        
        ab
                       L         abb L
        aabb
        aaaaabbbbb
Fall 2004              COMP 335              50
                Operations on Languages
The usual set operations

 a, ab, aaaa  bb, ab  {a, ab, bb, aaaa}
 a, ab, aaaa  bb, ab  {ab}
 a, ab, aaaa  bb, ab  a, aaaa
Complement:             L   * L

            a, ba   , b, aa, ab, bb, aaa,
Fall 2004                   COMP 335               51
                      Reverse

Definition:    L  {w : w  L}
                  R         R


Examples:     ab, aab, baba  ba, baa, abab
                                   R


            L  {a b : n  0}
                      n n



            L  {b a : n  0}
              R        n n

Fall 2004               COMP 335            52
                 Concatenation

Definition:    L1L2  xy : x  L1, y  L2 



Example:    a, ab, bab, aa

             ab, aaa, abb, abaa, bab, baaa

Fall 2004              COMP 335                 53
                Another Operation
Definition:      L  
                  n
                     LL  L
                           n

a, b  a, ba, ba, b 
            3

aaa, aab, aba, abb, baa, bab, bba, bbb
                   0
Special case: L  


                  a , bba , aaa 0  
Fall 2004                COMP 335           54
                 More Examples


            L  {a b : n  0}
                  n n



            L  {a b a b : n, m  0}
             2    n n m m



                                   2
                 aabbaaabbb L

Fall 2004               COMP 335       55
            Star-Closure (Kleene *)

Definition:   L*  L  L  L 
                    0        1     2



Example:
                 ,                     
                a, bb,                  
                                        
     a, bb*                          
                 aa, abb, bba, bbbb,    
                aaa, aabb, abba, abbbb,
                                        
Fall 2004               COMP 335             56
              Positive Closure

Definition:   
              L  L  L 
                    1          2

                   L * 


              a, bb,                  
                                     
    a, bb  aa, abb, bba, bbbb,     
              aaa, aabb, abba, abbbb,
                                      
Fall 2004               COMP 335           57

								
To top