Algorithm 哈密顿路径的时间复杂性

Algorithm 哈密顿路径的时间复杂性,algorithm,time-complexity,graph-algorithm,np,hamiltonian-cycle,Algorithm,Time Complexity,Graph Algorithm,Np,Hamiltonian Cycle,下面是使用回溯查找图中是否存在哈密顿路径的代码。根据下面的代码,时间复杂度为OV^2,其中V是顶点的总数。但哈密顿问题是NP完全问题。根据我的理解,这是一个在多项式时间n^k内无法解决的问题,其中n是输入,k是常数。我已经测试了下面的代码,工作正常。那么我的时间复杂度计算错了吗 public boolean check() { Stack<Node> nodeStack = new Stack<>(); nodeStack.add(root);

下面是使用回溯查找图中是否存在哈密顿路径的代码。根据下面的代码,时间复杂度为OV^2,其中V是顶点的总数。但哈密顿问题是NP完全问题。根据我的理解,这是一个在多项式时间n^k内无法解决的问题,其中n是输入,k是常数。我已经测试了下面的代码,工作正常。那么我的时间复杂度计算错了吗

 public boolean check() {

    Stack<Node> nodeStack = new Stack<>();
    nodeStack.add(root);
    root.setIsOnStack();

    while (!nodeStack.isEmpty()) {  

        Node currentNode = nodeStack.peek();   
        for (Entry<Node, Boolean> entry : currentNode.getNeighbourList().entrySet()) { 
            Node currentNeighbourer = entry.getKey();
            if (!currentNeighbourer.isOnStack()) { 
                if (!entry.getValue()) {  
                    nodeStack.push(currentNeighbourer); 
                    currentNeighbourer.setIsOnStack();
                    break;
                }
            } else if (currentNeighbourer == root && nodeStack.size() == noOfVertices) {  
                return true;
            }
        }
        if (currentNode == nodeStack.peek()) {
            for (Entry<Node, Boolean> entry : currentNode.getNeighbourList().entrySet()) { 
                currentNode.setNodeIsNotVisited(entry.getKey()); 
            }
            nodeStack.pop();
            currentNode.setIsNotOnStack();
            Node nodeOnTop = nodeStack.peek();
            nodeOnTop.setNodeIsVisited(currentNode);
        }
    }
    return false;
}
节点类:

  public class Node {

private final char label;
private Map<Node, Boolean> neighbourList;
private boolean isOnStack;

public Node(char label) {
    this.label = label;
    this.isOnStack = false;
    neighbourList = new LinkedHashMap<>();
}

public char getLabel() {
    return label;
}

public void addNeighbour(Node node) {
    neighbourList.put(node, false);
}

public boolean isOnStack() {
    return isOnStack;
}

public void setIsOnStack() {
    isOnStack = true;
}

public void setIsNotOnStack() {
    isOnStack = false;
}

public Map<Node, Boolean> getNeighbourList() {
    return neighbourList;
}

public void setNodeIsVisited(Node node) {
    neighbourList.replace(node, true);        
}

public void setNodeIsNotVisited(Node node) {
    neighbourList.replace(node, false);        
}

public boolean isNodeVisited(Node node) {
    return neighbourList.get(node);
}

}

告诉我们,你为什么认为它是OV^2?您的测试用例有多大?不存在hamilton路径的测试用例的输出是什么?据我所知,这是一个典型的OV^V算法。顺便说一句,回溯通常不是实现多项式时间算法的方法,除非你以这种方式修剪一些分支