简体   繁体   English

Java:如何计算程序的时间复杂度?

[英]Java: How to calculate time complexity of a program?

I have the following program. 我有以下程序。 In a case where it has multiple functions, do I combine the time complexity of each or just take the highest order time complexity out of all of them? 在具有多个功能的情况下,我要合并每个功能的时间复杂度还是仅从所有功能中取最高阶的时间复杂度?

I believe the find() has a time complexity of n and isCompound has a time complexity of n has well. 我相信find()的时间复杂度为n,而isCompound的时间复杂度为n。 Is that correct? 那是对的吗?

Thank you and will be sure to vote up and accept answer. 谢谢您,一定会投票支持并接受答案。

private static String[] find(String[] array) {
    Set<String> words = new LinkedHashSet<>(Arrays.asList(array));
    Set<String> otherWords = new HashSet<>(words);
    for (Iterator<String> i = words.iterator(); i.hasNext(); ) {
        String next = i.next();
        otherWords.remove(next);
        if (isCompound(next, otherWords)) {
            i.remove();
        } else {
            otherWords.add(next);
        }
    }
}

private static boolean isCompound(String string, Set<String> otherWords) {
    if (otherWords.contains(string)) {
        return true;
    }
    for (String word : otherWords) {
        if (string.startsWith(word)) {
            return isCompound(string.replaceAll("^" + word, ""), otherWords);
        }
        if (string.endsWith(word)) {
            return isCompound(string.replaceAll(word + "$", ""), otherWords);
        }
    }
    return false;
}

There is nothing like time complexity of a program. 没有什么比程序的时间复杂度更高了。 We calculate time complexity for algorithms or, in the context of programming, for individual (atomic) functions. 我们计算算法的时间复杂度,或者在编程的情况下,计算单个(原子)函数的时间复杂度。

We benchmark programs (which may consist of multiple functions) by measuring their running time in tools like profilers. 我们通过在分析器等工具中测量程序的运行时间来对程序(可能由多个功能组成)进行基准测试。 Imagine if a program contains hundreds of source files, how can you expect to calculate its time complexity? 想象一下,如果一个程序包含数百个源文件,您如何期望计算它的时间复杂度?

To analyze the complexities for find and isCompound , we certainly need to know the complexities for the functions called inside them, like otherWords.remove(next) , otherWords.add(next) , string.replaceAll("^" + word, "") or otherWords.contains(string) . 要分析findisCompound的复杂性,我们当然需要知道在其中调用的函数的复杂性,例如otherWords.remove(next)otherWords.add(next)string.replaceAll("^" + word, "")otherWords.contains(string)

If you know for certain what their complexities are, then we can compute the complexities for your functions. 如果您确定它们的复杂性是什么,那么我们可以为您的函数计算复杂性。 And even if you calculate all the complexities, that too would be an approximation made for very large inputs. 即使您计算了所有复杂度,也将是非常大的输入的近似值。 So, you decide what you really wish to calculate. 因此,您可以决定真正要计算的内容。

EDIT: To calculate the complexities for your program, I suggest you break down each of the library function you called and try to analyze them. 编辑:为了计算程序的复杂度,建议您分解调用的每个库函数并尝试对其进行分析。 For instance, since otherWords is a HashSet , we can infer that otherWords.contains(string) (lookup operation in a hash table) might take O(1) (big-O) time. 例如,由于otherWordsHashSet ,我们可以推断出otherWords.contains(string) (在哈希表中进行查找操作)可能需要O(1) (big-O)时间。

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

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