简体   繁体   中英

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. 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. However this if-statement is nested, and i am not sure how this affects t(n) within a recursive algorithm.

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? 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.

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 . For now lets assume the complexity during each call to isOrderedShuffle is C.

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

Now we need to actually figure out what C is. 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. 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). In your algorithm, the string is c. So, we'll assume the length is n. indexOf is called a constant number of times.

C = O(n)

Hence,

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

I'll let you get this down to a closed form.

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