简体   繁体   中英

Proving a Turing Machine counts in O(n)?

So for the past few days I've been designing a Turing Machine and found out that with my implementation my counting in binary runs at about 4n, where n is the number I count up to. So O(4n) -> O(n). I am not very good at proving complexity, but from what I understand from research is that, if you have a Turing Machine, M, for each n in {0,1}*, t_{M}(n) will be how long it will take to count to n, correct? Then if it doesn't halt, then it's highest bound is infinity. Is there a way to make a bridge between these two to conclude that it is indeed worst case to have n steps? I am never sure where to start proofs, any ideas?

Update:

Below is my Turing Machine representation for counting in binary:

import java.util.Scanner;
public class TuringMachine {

/**
 * Divide a number n by 2 shifting right (shift-right-logical) 
 * to figure out the minimum number of bits needed to represent
 * the number in binary.
 */
private static int getNumBits(int n) {
    int c = 0;
    while (n > 0) {
        c++;
        n = n >> 1;
    }
    return c;
}

private static void computeBinaryValues(int n) {
    System.out.println();

    int i, c, state;
    char symbol;
    String t;

    System.out.println("Computed binary values for:"); 

    // Compute values of n = 1 to n  
    for (int j = 1; j <= n; j++) {
        t = ""; // temp string
        state = 0; // current state
        symbol = ' '; // current symbol being read
        c = getNumBits(j) + 1; // minimum number of bits needed + 1 for a buffer
        i = c - 1; // indexing starting from end of the string 

        // initialize temp string to contain all ' ' characters
        for (int k = 0; k < c; k++) {
            t += " ";
        }

        // String builder off "empty" t string 
        StringBuilder s = new StringBuilder(t);
        // The actual binary representation of n + end space buffer
        String a = Integer.toBinaryString(j) + " ";

        // Turing Cycle  
        while (!(s.toString()).equals(a)) { // if the binary build is successful, these match.
            if (state == 0) {
                if (symbol == ' ') {
                    state = 1;
                    i--; // left
                } else { // symbols 0 and 1 rewrite themselves && move right 1
                    i++; // right
                } 
            } else if (state == 1) {
                if (symbol == ' ') {
                    s.setCharAt(i, '1');
                    state = 0;
                    i++; // right
                } else if (symbol == '0') {
                    s.setCharAt(i, '1');
                    state = 0;
                    i++; // right
                } else {
                    s.setCharAt(i, '0');
                    i--; // left
                }
            }
            symbol = s.charAt(i); // get symbol to read from
        }
        System.out.println(j + " -> " + s); // print binary string created from machine
    }
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter value for n >= 1: ");;
    computeBinaryValues(in.nextInt());
}
}

So, using Ishtar's suggestion for induction:

C(0) = a C(1) = b C(2) = c

Let k be a constant - assumed 4 from my experiments with my code.

c = k + b b = k + a

And he says we have to prove c(i+1) = c(i) + k then I gave my interpretation of what it meant? (I do not understand his case for induction)

If i'm understanding your question correctly you are trying to figure if your turing machine halts or not and if it doesn't than the worst time complexity is infinite right? If that's your question than no you can't draw a bridge between the two , why ? because the halting problem( the problem of figuring out if a program halts or not ) is undecidable over turing machines ,meaning that an algorithm for deciding wheather your TM halts or not doesn't exist (and will never exist).

One way of proving that your machine runs in O(n) is a proof by induction. (I can't tell you if this is the right approach for your machine, as I don't know what your machine looks like.) Define c(n) as the number of steps your machine needs to count a input tape of length n . (Or what are you counting?) Now try to prove:

  1. c(0) = k
  2. c(1) = l
  3. c(2) = m

If it indeed runs in 4n steps, then m = 4 + l and l = 4 + k . Now the hardest part, prove

  1. c(i+1) = c(i) + 4

Then we may conclude that c(n) = 4*n + k for all n>=0 , from the proofs c(0) = k and c(i+1) = c(i) + 4 . Because c(n) = O(4n) = O(n) the machine runs in O(n).

(You should also prove that the machine always terminates and gives the correct result, but this may be out of scope.)


Update:

So, your machine isn't actually counting anything. It runs forever, writing all binary numbers. The Java program stops the machine if it wrote the input number, the machine itself would go on forever, right?

Let's define the point at which the machine wrote a number successfully to be: state = 0 and prior to reading input = 'blank'. Agreed?

Define c(n) as the number of steps, the machine needs to write n , (so that state = 0, input = 'blank' and the binary representation of n is written on the tape). Now, try to prove c(0) = k , c(1) = l and c(2) = m . The actual values of k, l and m! For example c(0) = 2 , c(1) = 8 ?? (I didn't try these values.) Just follow the machine step by step and count.

What you really need to prove is c(i+1) = c(i) + something . Then you can solve c(i) to a closed form. Think of it like this, if 111 has been written on the tape (and state = 0, next input = 'blank'), how many steps will the machine go through to have 1000 written (and state = 0, next input = 'blank'): 4, 5, 6, 7, 8 or...?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM