2015-12-02 552 views
0

在clasas中,我已经开始学习如何计算各种算法的运行时复杂度函数,并且发现它很困难。我正在尝试计算下面递归算法的最坏情况下的运行时间复杂度。计算递归算法的最坏情况运行时间复杂度

目前我选择我的基本操作是两个字符的索引之间的比较,它发生在if语句中。然而这个if语句是嵌套的,我不确定这是如何影响递归算法中的t(n)的。

我认为最坏的情况下运行时间复杂度是t(n) = N(N-1) = N^2 -1 or just O(n)=N^2?我认为在最坏的情况下,每个n个字符都会在外部if语句中检查,这意味着n在内部if语句中将会比较-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")); 

} 

} 

回答

1

它有助于将时间复杂度分析分解为多个部分。

我们知道,我们删除了至少一个字符,因为每次调用isOrderedShuffle都会被删除。现在让每次调用isOrderedShuffle期间承担的复杂性C.

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

现在,我们需要真正搞清楚C是什么。要做到这一点,你想弄清楚函数中最高复杂度的操作。在这种情况下,我们可以看看String的indexOf函数。当以一个字符作为参数调用indexOf时,时间复杂度为O(n),其中n是我们正在搜索的字符串的长度(如果感兴趣,请参阅this答案)。在你的算法中,字符串是c。所以,我们假设长度为n。 indexOf被称为恒定次数。

C = O(n)的

因此,

T(N)= T(N-1)+ N

我会让你得到这个下降到一个封闭的形式。