Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象列表上KMP算法的时间效率评估_Java_Algorithm_Pattern Matching_Big O_Knuth Morris Pratt - Fatal编程技术网

Java 对象列表上KMP算法的时间效率评估

Java 对象列表上KMP算法的时间效率评估,java,algorithm,pattern-matching,big-o,knuth-morris-pratt,Java,Algorithm,Pattern Matching,Big O,Knuth Morris Pratt,我在程序中实现了KMP模式搜索算法来搜索对象表。我想知道如何评估我的职能的时间效率 我读了一些资料,说KMP算法的时间复杂度是O(n)[不包括空间复杂度] 因此,我将遍历从1到N项的对象列表,搜索每个项的模式匹配。一旦有一场比赛,我将打破循环,但这并不真正影响我的评价(我想) 既然我迭代我的所有项目,我的大O符号是:O(n^2),因为需要O(n)来找到匹配项,O(n)来迭代我的所有项目 以下是我的代码,以获得一些见解: while(itr.hasNext()){ //M is the l

我在程序中实现了KMP模式搜索算法来搜索对象表。我想知道如何评估我的职能的时间效率

我读了一些资料,说KMP算法的时间复杂度是O(n)[不包括空间复杂度]

因此,我将遍历从1到N项的对象列表,搜索每个项的模式匹配。一旦有一场比赛,我将打破循环,但这并不真正影响我的评价(我想)

既然我迭代我的所有项目,我的大O符号是:O(n^2),因为需要O(n)来找到匹配项,O(n)来迭代我的所有项目

以下是我的代码,以获得一些见解:

while(itr.hasNext()){
    //M is the length of our pattern

    i = 0; //index of text to be searched
    j = 0; //index of our pattern

    txt  = itr.next().toString();  //Store the key inside txt string
    N = txt.length(); //length of our text to be search
    
    //Check if the searchText is equal or less than key in the dictionary
    //If our searchText is more than the key length, there is no use of searching
    if(M <= N){
        while (i < N) {
            //Check if the searchText.charAt equals to txt.charAt
            //Increase i,j if matches to compare next character(s)
            if (searchText.charAt(j) == txt.charAt(i)) { 
                j++; 
                i++; 
            }else{ //If the chars at our pattern and text does not match
                if (j != 0) //if it's not the first index of our pattern
                    j--; //reduce one index
                else
                    i++; //otherwise move onto the next index of our text to be searched
            }
            
            //Check whether the length of the searchText equals to the match counter
            //It means that the searchKey exists in our dictionary
            if (j == M) { 
                System.out.println((String.format("%-35s", txt)) + get((K)txt));
                counter++;      //Holds the number of entries found
                j--; 
                break;          //No need to look anymore since there's a match
            }
        }
    }
}
while(itr.hasNext()){
//M是我们图案的长度
i=0;//要搜索的文本索引
j=0;//我们模式的索引
txt=itr.next().toString();//将密钥存储在txt字符串中
N=txt.length();//要搜索的文本的长度
//检查searchText是否等于或小于字典中的键
//如果我们的searchText超过了密钥长度,那么搜索就没有用了

如果(M如果列表中有n个项目&平均每个元素大小为M,则搜索时间复杂度为O(mn)。KMP的搜索时间复杂度为O(M)对于每个元素。您需要将其乘以n。

如何定义
j
next
呢?我假定
next
是KMP部分匹配表,
j
是该表的索引。KMP算法本身具有最坏情况和平均时间复杂度
O(txt的长度)
。如果对
itr
的每个元素运行该算法,程序将具有最坏情况下的时间复杂度
O((itr长度)*max(itr中的txt长度))
和平均时间复杂度
O((itr长度)*平均(itr中的txt长度))
。中断循环只会影响最佳情况下的时间复杂度(模式立即出现在第一个
txt
)中,即
O(搜索文本的长度)