简体   繁体   English

证明图灵机计入O(n)?

[英]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. 因此,在过去的几天里,我一直在设计Turing Machine,发现在实现过程中,我对二进制文件的计数大约为4n,其中n是我要计数的数字。 So O(4n) -> O(n). 所以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? 我不太擅长证明复杂性,但是从研究中我了解到,如果您有图灵机M,则{0,1} *中的每n个,t_ {M}(n)将持续多长时间需要数到n,对吗? 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? 有没有办法在这两者之间架起桥梁,得出结论,有n个台阶确实是最坏的情况? 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: 因此,使用Ishtar的建议进行归纳:

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

Let k be a constant - assumed 4 from my experiments with my code. 令k为常数-根据我的代码实验得出的假设为4。

c = k + b b = k + a 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? 他说我们必须证明c(i+1) = c(i) + k然后我才解释了这是什么意思? (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). 因为停止问题(确定程序是否停止的问题)在机器上无法确定,这意味着不存在用于确定是否停止TM的算法(并且永远不会存在)。

One way of proving that your machine runs in O(n) is a proof by induction. 证明您的机器以O(n)运行的一种方法是归纳证明。 (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 . (由于我不知道您的机器是什么样,所以我不能告诉您这是否是您的机器的正确方法。)将c(n)定义为机器计算长度的输入磁带所需的步数n (Or what are you counting?) Now try to prove: (或者您在数什么?)现在尝试证明:

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

If it indeed runs in 4n steps, then m = 4 + l and l = 4 + k . 如果确实以4n步长运行,则m = 4 + ll = 4 + k Now the hardest part, prove 现在最难的部分证明

  1. c(i+1) = c(i) + 4 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 . 然后我们可以根据证明c(0) = kc(i+1) = c(i) + 4得出结论,对于所有n>=0c(n) = 4*n + k Because c(n) = O(4n) = O(n) the machine runs in O(n). 因为c(n) = O(4n) = O(n)所以机器以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? 如果Java程序输入了输入数字,它将停止机器,机器本身将永远运行,对吗?

Let's define the point at which the machine wrote a number successfully to be: state = 0 and prior to reading input = 'blank'. 让我们将机器成功写入数字的点定义为:state = 0且在读取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). c(n)定义为步数,机器需要写n ,(因此状态= 0,输入='空白',并且n的二进制表示形式写在磁带上)。 Now, try to prove c(0) = k , c(1) = l and c(2) = m . 现在,尝试证明c(0) = kc(1) = lc(2) = m The actual values of k, l and m! k,l和m的实际值! For example c(0) = 2 , c(1) = 8 ?? 例如c(0) = 2c(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 . 真正需要证明的是c(i+1) = c(i) + something Then you can solve c(i) to a closed form. 然后,您可以将c(i)求解为封闭形式。 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...? 可以这样想,如果在磁带上写了111 (状态= 0,下一个输入='空白'),则机器要经过多少步才能写入1000 (状态= 0,下一个输入='空白”):4、5、6、7、8或...?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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