9-10-11turing

Upload: sammar-abbas

Post on 04-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 9-10-11Turing

    1/115

    1

    Turing Machines

  • 8/13/2019 9-10-11Turing

    2/115

    2

    The Language Hierarchy

    *aRegular Languages

    Context-Free Languagesnnba Rww

    nnn cba ww?

    ** ba

    ?

  • 8/13/2019 9-10-11Turing

    3/115

  • 8/13/2019 9-10-11Turing

    4/115

    4

    A Turing Machine

    ............

    Tape

    Read-Write headControl Unit

  • 8/13/2019 9-10-11Turing

    5/115

    5

    The Tape

    ............

    Read-Write head

    No boundaries -- infinite length

    The head moves Left or Right

  • 8/13/2019 9-10-11Turing

    6/115

    6

    ............

    Read-Write head

    The head at each transition (time step):

    1. Reads a symbol2. Writes a symbol3. Moves Left or Right

  • 8/13/2019 9-10-11Turing

    7/1157

    ............

    Example:Time 0

    ............Time 1

    1. Reads2. Writes

    a a cb

    a b k c

    ak

    3. Moves Left

  • 8/13/2019 9-10-11Turing

    8/1158

    ............Time 1

    a b k c

    ............ Time 2a k c f

    1. Reads2. Writes

    b f

    3. Moves Right

  • 8/13/2019 9-10-11Turing

    9/115

  • 8/13/2019 9-10-11Turing

    10/11510

    States & Transitions

    1q 2q Lba ,

    Read Write Move Left

    1q 2q Rba ,

    Move Right

  • 8/13/2019 9-10-11Turing

    11/11511

    Example:

    1q 2q Rba ,

    ............ a b ca

    Time 1

    1qcurrent state

  • 8/13/2019 9-10-11Turing

    12/11512

    ............ a b caTime 1

    1q 2q Rba ,

    ............ a b cbTime 2

    1q

    2q

  • 8/13/2019 9-10-11Turing

    13/11513

    ............ a b caTime 1

    1q 2q Lba ,

    ............ a b cbTime 2

    1q

    2q

    Example:

  • 8/13/2019 9-10-11Turing

    14/11514

    ............ a b caTime 1

    1q 2q R g ,

    ............ g a b cbTime 2

    1q

    2q

    Example:

  • 8/13/2019 9-10-11Turing

    15/11515

    Determinism

    1q

    2q Rba ,

    Allowed Not Allowed

    3q Ld b ,

    1q

    2q Rba ,

    3q Ld a ,

    No lambda transitions allowed

    Turing Machines are deterministic

  • 8/13/2019 9-10-11Turing

    16/11516

    Partial Transition Function

    1q

    2q Rba ,

    3q Ld b ,

    ............ a b ca

    1q

    Example:

    No transitionfor input symbol c

    Allowed:

  • 8/13/2019 9-10-11Turing

    17/11517

    Halting

    The machine halts in a state if there isno transition to follow

  • 8/13/2019 9-10-11Turing

    18/11518

    Halting Example 1:

    ............ a b ca

    1q

    1q No transition fromHALT!!!

    1q

  • 8/13/2019 9-10-11Turing

    19/115

    19

    Halting Example 2:

    ............ a b ca

    1q

    1q

    2q Rba ,

    3q Ld b ,

    No possible transitionfrom and symbol

    HALT!!!

    1q c

  • 8/13/2019 9-10-11Turing

    20/115

    20

    Accepting States

    1q 2q Allowed

    1q 2q Not Allowed

    Accepting states have no outgoing transitionsThe machine halts and accepts

  • 8/13/2019 9-10-11Turing

    21/115

    21

    Acceptance

    Accept Input If machine haltsin an accept state

    Reject Input

    If machine haltsin a non-accept state

    orIf machine entersan infinite loop

    string

    string

  • 8/13/2019 9-10-11Turing

    22/115

    22

    Observation:

    In order to accept an input string,it is not necessary to scan all thesymbols in the string

  • 8/13/2019 9-10-11Turing

    23/115

    23

    Turing Machine Example

    Accepts the language: *a

    0q

    Raa ,

    L,1q

    Input alphabet },{ b a

  • 8/13/2019 9-10-11Turing

    24/115

    24

    aaTime 0

    0q

    a

    0q

    Raa ,

    L,1q

  • 8/13/2019 9-10-11Turing

    25/115

    25

    aaTime 1

    0q

    a

    0q

    Raa ,

    L,1q

  • 8/13/2019 9-10-11Turing

    26/115

    26

    aaTime 2

    0q

    a

    0q

    Raa ,

    L,1q

  • 8/13/2019 9-10-11Turing

    27/115

    27

    aaTime 3

    0q

    a

    0q

    Raa ,

    L,1q

  • 8/13/2019 9-10-11Turing

    28/115

    28

    aaTime 4

    1q

    a

    0q

    Raa ,

    L,1q

    Halt & Accept

  • 8/13/2019 9-10-11Turing

    29/115

    29

    Rejection Example

    0q

    Raa ,

    L,1q

    baTime 0

    0q

    a

  • 8/13/2019 9-10-11Turing

    30/115

    30

    0q

    Raa ,

    L,1q

    baTime 1

    0q

    a

    No possible TransitionHalt & Reject

    A i l hi f l

  • 8/13/2019 9-10-11Turing

    31/115

    31

    Accepts the language: *a

    0q

    but for input alphabet }{a A simpler machine for same language

  • 8/13/2019 9-10-11Turing

    32/115

    32

    aaTime 0

    0q

    a

    0q

    Halt & Accept

    Not necessary to scan input

  • 8/13/2019 9-10-11Turing

    33/115

    33

    Infinite Loop Example

    0q

    Raa ,

    L,1q

    Lbb ,

    A Turing machinefor language *)(* baba

  • 8/13/2019 9-10-11Turing

    34/115

    34

    baTime 0

    0q

    a

    0q

    Raa ,

    L,1q

    Lbb ,

  • 8/13/2019 9-10-11Turing

    35/115

    35

    baTime 1

    0q

    a

    0q

    Raa ,

    L,1q

    Lbb ,

  • 8/13/2019 9-10-11Turing

    36/115

    36

    baTime 2

    0q

    a

    0q

    Raa ,

    L,1q

    Lbb ,

  • 8/13/2019 9-10-11Turing

    37/115

    37

    baTime 2

    0q

    a

    baTime 3

    0q

    a

    baTime 4

    0q

    a

    baTime 5

    0q

    a

    I n f i n

    i t e l o

    o p

  • 8/13/2019 9-10-11Turing

    38/115

    38

    Because of the infinite loop:

    The accepting state cannot be reached

    The machine never halts

    The input string is rejected

    h h l

  • 8/13/2019 9-10-11Turing

    39/115

    39

    Another Turing Machine Example

    Turing machine for the language }{ nnba

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    1n

  • 8/13/2019 9-10-11Turing

    40/115

    40

    Match as with bs: Repeat:

    replace leftmost a with xfind leftmost b and replace it with yUntil there are no more as or bs

    If there is a remaining a or b reject

    Basic Idea:

  • 8/13/2019 9-10-11Turing

    41/115

    41

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    ba

    0q

    a bTime 0

  • 8/13/2019 9-10-11Turing

    42/115

    42

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    b x

    1q

    a b Time 1

  • 8/13/2019 9-10-11Turing

    43/115

    43

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    b x

    1q

    a b Time 2

  • 8/13/2019 9-10-11Turing

    44/115

    44

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    y x

    2q

    a b Time 3

  • 8/13/2019 9-10-11Turing

    45/115

    45

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    y x

    2q

    a b Time 4

  • 8/13/2019 9-10-11Turing

    46/115

    46

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    y x

    0q

    a b Time 5

  • 8/13/2019 9-10-11Turing

    47/115

    47

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    y x

    1q

    x b Time 6

  • 8/13/2019 9-10-11Turing

    48/115

    48

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    R x x ,

    R y y ,

    R yy , 4q

    L,

    y x

    1q

    x b Time 7

  • 8/13/2019 9-10-11Turing

    49/115

    49

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x x y

    2q

    Time 8

  • 8/13/2019 9-10-11Turing

    50/115

    50

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x x y

    2q

    Time 9

  • 8/13/2019 9-10-11Turing

    51/115

    51

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x

    0q

    x y Time 10

  • 8/13/2019 9-10-11Turing

    52/115

    52

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x

    3q

    x y Time 11

  • 8/13/2019 9-10-11Turing

    53/115

    53

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x

    3q

    x y Time 12

  • 8/13/2019 9-10-11Turing

    54/115

    54

    0q 1q 2q3q R xa ,

    Raa , R y y ,

    L yb ,

    Laa , L y y ,

    Rxx ,

    R y y ,

    R yy , 4q

    L,

    y x

    4q

    x y

    Halt & Accept

    Time 13

    Another Turing Machine Example

  • 8/13/2019 9-10-11Turing

    55/115

    55

    Another Turing Machine Example

    Turing machine for the languageA = {02n n >= O}

    Another Turing Machine Example

  • 8/13/2019 9-10-11Turing

    56/115

    56

    Another Turing Machine Example

    Turing machine for the languageA = {02n n >= O}

  • 8/13/2019 9-10-11Turing

    57/115

    57

    modify themachine for the language }{ nnba

    And constructa machine for the language }{ nnn cba

    Home Work:

  • 8/13/2019 9-10-11Turing

    58/115

    58

    Formal Definitionsfor

    Turing Machines

  • 8/13/2019 9-10-11Turing

    59/115

    59

    Transition Function

    1q 2q Rba ,

    ),,(),( 21 Rbqaq

  • 8/13/2019 9-10-11Turing

    60/115

    60

    1q 2q Ld c ,

    ),,(),( 21 Ld qcq

    Transition Function

    Turing Machine:

  • 8/13/2019 9-10-11Turing

    61/115

    61

    g

    ),,,,,,( 0 F qQ

    States

    Inputalphabet

    Tapealphabet

    TransitionfunctionInitial

    stateblank

    Acceptstates

    Configuration

  • 8/13/2019 9-10-11Turing

    62/115

    62

    Configuration

    ba

    1q

    a

    Instantaneous description:

    c

    baqca 1

    Time 4 Time 5

  • 8/13/2019 9-10-11Turing

    63/115

    63

    y x

    2q

    a b y x

    0q

    a b

    A Move: aybq x xaybq 02

    (yields in one mode)

    Time 4 Time 5

  • 8/13/2019 9-10-11Turing

    64/115

    64

    y x

    2q

    a b y x

    0q

    a b

    bq xxy ybq xxaybq x xaybq 1102

    y x

    1q

    x b

    Time 6 y x

    1q

    x b

    Time 7

    A computation

  • 8/13/2019 9-10-11Turing

    65/115

    65

    bq xxy ybq xxaybq x xaybq 1102

    bq xxy xaybq 12Equivalent notation:

  • 8/13/2019 9-10-11Turing

    66/115

    66

    Initial configuration: wq0

    ba

    0q

    a b

    w

    Input string

    The Accepted Language

  • 8/13/2019 9-10-11Turing

    67/115

    67

    The Accepted Language

    For any Turing Machine

    }:{)( 210 xq xwqw M L f

    Initial state Accept state

    If l i dL

  • 8/13/2019 9-10-11Turing

    68/115

    68

    If a language is acceptedby a Turing machinethen we say that is:

    Turing AcceptableRecursively Enumerable

    M L

    L Turing Recognizable

    Other names used:

  • 8/13/2019 9-10-11Turing

    69/115

    69

    Computing Functionswith

    Turing Machines

    )(

  • 8/13/2019 9-10-11Turing

    70/115

    70

    A function )(w f

    Domain: Result Region:

    has:

    D

    Dw

    S

    S w f )()(w f

    A function may have many parameters:

  • 8/13/2019 9-10-11Turing

    71/115

    71

    A function may have many parameters:

    y x y x f ),(

    Example: Addition function

    Integer Domain

  • 8/13/2019 9-10-11Turing

    72/115

    72

    Integer Domain

    Unary:

    Binary:

    Decimal:

    11111

    101

    5

    We prefer unary representation:

    easier to manipulate with Turing machines

    Definition:

  • 8/13/2019 9-10-11Turing

    73/115

    73

    A function is computable if

    there is a Turing Machine such that:

    f

    Initial configuration Final configuration

    Dw Domain

    0q

    w

    f q

    )(w f

    accept stateinitial state

    For all

    n other words:

  • 8/13/2019 9-10-11Turing

    74/115

    74

    )(0

    w f qwq f

    InitialConfiguration

    FinalConfiguration

    A function is computable if

    there is a Turing Machine such that:

    f

    Dw DomainFor all

    Example

  • 8/13/2019 9-10-11Turing

    75/115

    75

    Example

    The function y x y x f ),( is computable

    Turing Machine:

    Input string: y x0 unary

    Output string: 0 xy unary

    y x, are integers

    x y

  • 8/13/2019 9-10-11Turing

    76/115

    76

    0

    0q

    1 1 1 1

    y

    1 Start

    initial state

    The 0 is the delimiter thatseparates the two numbers

    x y

  • 8/13/2019 9-10-11Turing

    77/115

    77

    0

    0q

    1 1 1 11

    0

    f q

    1 1

    y x

    11

    Start

    Finish

    final state

    initial state

  • 8/13/2019 9-10-11Turing

    78/115

    78

    0

    f q

    1 1

    y x

    11Finish

    final state

    The 0 here helps when we use

    the result for other operations

  • 8/13/2019 9-10-11Turing

    79/115

    79

    Construct the TM for Unary Addition

    Turing machine for function y x y x f ),(

  • 8/13/2019 9-10-11Turing

    80/115

    80

    0q

    g

    1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    Execution Example: Time 0

  • 8/13/2019 9-10-11Turing

    81/115

    81

    p

    11 x

    11 y 0

    0q

    1 1 1 1

    Time 0 x y

    Final Result

    0

    4q

    1 1 1 1

    y x

    (=2)(=2)

  • 8/13/2019 9-10-11Turing

    82/115

    82

    0

    0q

    1 1Time 0

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    1 1

  • 8/13/2019 9-10-11Turing

    83/115

    83

    0q

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    01 11 1Time 1

  • 8/13/2019 9-10-11Turing

    84/115

    84

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    0

    0q

    1 1 1 1Time 2

  • 8/13/2019 9-10-11Turing

    85/115

    85

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    1q

    1 11 11Time 3

  • 8/13/2019 9-10-11Turing

    86/115

    86

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    1q

    1 1 1 11Time 4

  • 8/13/2019 9-10-11Turing

    87/115

    87

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    1q

    1 11 11Time 5

  • 8/13/2019 9-10-11Turing

    88/115

    88

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    2q

    1 1 1 11Time 6

  • 8/13/2019 9-10-11Turing

    89/115

    89

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    3q

    1 11 01Time 7

  • 8/13/2019 9-10-11Turing

    90/115

    90

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    3q

    1 1 1 01Time 8

  • 8/13/2019 9-10-11Turing

    91/115

    91

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    3q

    1 11 01Time 9

  • 8/13/2019 9-10-11Turing

    92/115

    92

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    3q

    1 1 1 01Time 10

  • 8/13/2019 9-10-11Turing

    93/115

    93

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    3q

    1 11 01Time 11

  • 8/13/2019 9-10-11Turing

    94/115

    94

    0q 1q 2q

    3q L, L,01

    L,11

    R,

    R,10

    R,11

    4q

    R,11

    4q

    1 1 1 01

    HALT & accept

    Time 12

    Another Example

  • 8/13/2019 9-10-11Turing

    95/115

    95

    The function x x f 2)( is computable

    Turing Machine:

    Input string: x unary

    Output string: xx unary

    x is integer

    x

  • 8/13/2019 9-10-11Turing

    96/115

    96

    0q

    1

    1 1

    1

    fq

    1 1

    x2

    11

    Start

    Finish

    accept state

    initial state

    Turing Machine Pseudocode for x x f 2)(

  • 8/13/2019 9-10-11Turing

    97/115

    97

    Replace every 1 with $ Repeat:

    Find rightmost $ , replace it with 1

    Go to right end, insert 1

    Until no more $ remain

    Turing Machine for xxf 2)(

  • 8/13/2019 9-10-11Turing

    98/115

    98

    0q 1q 2q

    3q

    R,1$

    L,1

    L,

    R$,1 L,11 R,11

    R,

    Turing Machine for x x f 2)(

    ExampleStart Finish

  • 8/13/2019 9-10-11Turing

    99/115

    99

    0q 1q 2q

    3q

    R,1$

    L,1

    L,

    R$,1 L,11 R,11

    R,

    0q

    1 1

    3q

    1 11 1

    Finish

  • 8/13/2019 9-10-11Turing

    100/115

    100

    Combining Turing Machines

  • 8/13/2019 9-10-11Turing

    101/115

    101

    Block Diagram

    TuringMachineinput output

    Example: y x yx if

  • 8/13/2019 9-10-11Turing

    102/115

    102

    ),( y x f 0

    y y x

    y x if

    Comparator

    Adder

    Eraser

    y x,

    y x,

    y x

    y x

    y x

    0

  • 8/13/2019 9-10-11Turing

    103/115

    103

    A Universal Turing Machine

    A limitation of Turing Machines:

  • 8/13/2019 9-10-11Turing

    104/115

    104

    Turing Machines are hardwired

    they executeonly one program

    A limitation of Turing Machines:

    Real Computers are re-programmable

    Solution: Universal Turing Machine

  • 8/13/2019 9-10-11Turing

    105/115

    105

    Reprogrammable machine

    Simulates any other Turing Machine

    Attributes:

    Universal Turing Machine

  • 8/13/2019 9-10-11Turing

    106/115

    106

    simulates any Turing Machine

    Input of Universal Turing Machine:

    Description of transitions of

    Input string of

    Three tapes Tape 1

  • 8/13/2019 9-10-11Turing

    107/115

    107

    Universal

    TuringMachine

    Description of

    Tape Contents of

    State of

    p

    Tape 2

    Tape 3

    Tape 1

  • 8/13/2019 9-10-11Turing

    108/115

    108

    We describe Turing machineas a string of symbols:

    We encode as a string of symbols

    Description of

    Alphabet Encoding

  • 8/13/2019 9-10-11Turing

    109/115

    109

    Alphabet Encoding

    Symbols: a b c d

    Encoding:1

    11 111 1111

    State Encoding

  • 8/13/2019 9-10-11Turing

    110/115

    110

    States: 1q 2q 3q 4q

    Encoding: 1 11 111 111

    Head Move Encoding

    Move:

    Encoding:

    L R

    1 11

    T iti E di g

  • 8/13/2019 9-10-11Turing

    111/115

    111

    Transition Encoding

    Transition: ),,(),( 21 Lbqaq

    Encoding: 10110110101

    separator

    T ring Machine Encoding

  • 8/13/2019 9-10-11Turing

    112/115

    112

    Turing Machine Encoding

    Transitions:),,(),( 21 Lbqaq

    Encoding:10110110101

    ),,(),( 32 Rcqbq

    110111011110101100

    separator

    Tape 1 contents of Universal Turing Machine:

  • 8/13/2019 9-10-11Turing

    113/115

    113

    binary encodingof the simulated machine

    1100011101111010100110110110101

    Tape 1

    A Turing Machine is describedf

  • 8/13/2019 9-10-11Turing

    114/115

    114

    with a binary string of 0s and 1s

    The set of Turing machinesforms a language:

    each string of this language isthe binary encoding of a Turing Machine

    Therefore:

    Language of Turing Machines

  • 8/13/2019 9-10-11Turing

    115/115

    g g g

    L = { 010100101,

    00100100101111,

    111010011110010101,

    }

    (Turing Machine 1)

    (Turing Machine 2)