简体   繁体   English

递归算法的最坏情况下运行时间复杂度的计算

[英]Calculating worst-case run time complexity of recursive algorithm

In clasas i have started learning how to calculate the run time complexity functions of various algorithms and am finding it difficult. 在clasas中,我开始学习如何计算各种算法的运行时复杂度函数,并且发现它很困难。 I am trying to calculate the worst case run time complexity of my recursive algorithm below. 我试图在下面计算我的递归算法的最坏情况运行时复杂度。

At the moment i am choosing my fundamental operation to be a comparison between the index of two chars, which occurs within an if-statement. 目前,我选择的基本操作是将两个字符的索引进行比较,这是在if语句中进行的。 However this if-statement is nested, and i am not sure how this affects t(n) within a recursive algorithm. 但是,此if语句是嵌套的,并且我不确定这如何影响递归算法中的t(n)。

Would i be correct in thinking that the worst case run time complexity would be t(n) = N(N-1) = N^2 -1 or just O(n)=N^2? 我认为最坏情况下的运行时间复杂度是t(n) = N(N-1) = N^2 -1 or just O(n)=N^2? I got this logic from thinking that in a worst-case scenario each n chars would be checked in the outer if-statement, which would mean n-1 chars would be compared in the inner if statement. 我认为在最坏的情况下,将在外部if语句中检查每个n个字符,这意味着在内部if语句中将对n-1个字符进行比较,从而得出了这种逻辑。

public class StringShuffleTest {

    public static boolean isOrderedShuffle(String a, String b, String c){

        //variables for the size of Strings a, b and c.
        int n = a.length();
        int m = b.length();
        int len = c.length();     

        //if the length of c is not the length of a + b, return false.
        if (len != (n + m)){
            return false;
        }

        //if String c contains String b as a substring, then remove String b from c and make m = 0.
        //This statement avoids errors when dealing with Strings with very similar characters.
        if (c.contains(b)){
            c = c.replace(b, "");
            m = 0;
        }

        //if the length of a or b is 0, and c equals a or b, return true, otherwise,
        //return false.
        if (n == 0 || m == 0){
            if (c.equals(a) || c.equals(b)){
                return true;
            }
            else
                return false;
        }

        //if String a has length 1, remove a from String c and make String a empty.
        if (n == 1){
                c = c.substring(0, c.indexOf(a.charAt(0))) + c.substring(c.indexOf(a.charAt(0)) +1);
                a = "";
                return isOrderedShuffle(a, b, c);

            }

        //An ordered shuffle of two given strings, a and b, is a string that can be formed by interspersing
        //the characters of a and b in a way that maintains the left-to-right order of the characters from each
        //string.

        //Recursive algorithm to determine if String c is an ordered shuffle of a and b.
        else
        if (c.indexOf(a.charAt(0)) >= 0){

            int indexOfFirsta = c.indexOf(a.charAt(0));
            int indexOfSeconda = c.indexOf(a.charAt(1));

            if (indexOfFirsta <= indexOfSeconda){
            c = c.substring(0, indexOfFirsta) + c.substring(indexOfFirsta +1);
            a = a.substring(1, n);
                System.out.println(a);
                System.out.println(c);                   
            return isOrderedShuffle(a, b, c);
            }

        else
            if (c.indexOf(b.charAt(0)) >= 0){
                    int indexOfFirstb = c.indexOf(b.charAt(0));
                    int indexOfSecondb = c.indexOf(b.charAt(1));

                    if (indexOfFirstb <= indexOfSecondb){
                        c = c.substring(0, indexOfFirstb) + c.substring(indexOfFirstb +1);
                        b = b.substring(1, m);
                        System.out.println(b);
                        System.out.println(c);

                    return isOrderedShuffle(a, b, c);

                }
        }

        }
    return false;         
    }       

public static void main(String[] args) {

    System.out.println(StringShuffleTest.isOrderedShuffle("abc", "def", "abedcf")); 

}

}

It helps if you break the time complexity analysis into parts. 如果您将时间复杂度分析分成几部分,则将有帮助。

We know that we remove at least one character is removed for every call to isOrderedShuffle . 我们知道,每次调用isOrderedShuffle ,都会删除至少一个字符。 For now lets assume the complexity during each call to isOrderedShuffle is C. 现在,让我们假设每次调用isOrderedShuffle的复杂度为C。

T(n) = T(n-1) + C T(n)= T(n-1)+ C

Now we need to actually figure out what C is. 现在我们需要真正弄清楚C是什么。 To do this, you want to figure out what the operation of highest complexity is in the function. 为此,您想弄清楚函数中最复杂的操作是什么。 In this case, we can look at String's indexOf function. 在这种情况下,我们可以看一下String的indexOf函数。 When calling indexOf with one character as a parameter, the time complexity is O(n) where n is the length of the string we're searching (see this answer if interested). 当调用indexOf一个字符作为参数,时间复杂度为O(n),其中n是我们正在寻找的字符串的长度(见这个答案,如果有兴趣)。 In your algorithm, the string is c. 在您的算法中,字符串为c。 So, we'll assume the length is n. 因此,我们假设长度为n。 indexOf is called a constant number of times. indexOf被称为固定次数。

C = O(n) C = O(n)

Hence, 因此,

T(n) = T(n-1) + n T(n)= T(n-1)+ n

I'll let you get this down to a closed form. 我让你把它简化为封闭形式。

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

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