def bidirectional(self, start, start2):
        moves, moves2 = [], []

        stack = LinkedList.DoublyLinkedList()
        stackCopy = LinkedList.DoublyLinkedList()
        stack.insertEnd(start, None, None)
        stackCopy.insertEnd(start, None, None)
        visited = [start]

        stack2 = LinkedList.DoublyLinkedList()
        stackc2 = LinkedList.DoublyLinkedList()
        stack2.insertEnd(start2, None, None)
        stackc2.insertEnd(start2, None, None)
        visited2 = [start2]

        while stack.empty() is not None and stack2.empty() is not None:
            currentMove = stack.removeStart()
            currentMove2 = stack2.removeStart()

            nextMoves = Expansion.getNextMoves(currentMove.data)
            for i in range(len(nextMoves)):
                newMove = nextMoves[i]
                if newMove not in visited:
                    stack.insertEnd(newMove, currentMove, None)
                    stackCopy.insertEnd(newMove, currentMove, None)
                    visited.append(newMove)
                    if newMove in visited2:
                        while currentMove2.prevPointer is not None:
                            currentMove2 = currentMove2.prevPointer
                        while currentMove2.data != newMove:
                            currentMove2 = currentMove2.nextPointer
                        if currentMove2.parent is None:
                            moves2.append(currentMove.data)
                        while currentMove2.parent is not None:
                            currentMove2 = currentMove2.parent
                            moves2.append(currentMove2.data)
                        moves1 = stackCopy.tree()
                        return "Bidirecional solution: " + str(
                            list(moves1[::-1]) + moves2)

            nextMoves = Expansion.getNextMoves(currentMove2.data)
            for i in range(len(nextMoves)):
                newMove = nextMoves[i]
                if newMove not in visited2:
                    stack2.insertEnd(newMove, currentMove2, None)
                    stackc2.insertEnd(newMove, currentMove2, None)
                    visited2.append(newMove)
                    if newMove in visited:
                        while currentMove.prevPointer is not None:
                            currentMove = currentMove.prevPointer
                        while currentMove.nextPointer is not None and currentMove.data != newMove:
                            currentMove = currentMove.nextPointer
                        if currentMove.parent is None:
                            moves2.append(currentMove.data)
                        while currentMove.parent is not None:
                            currentMove = currentMove.parent
                            moves2.append(currentMove.data)
                        moves1 = stackc2.tree()
                        return "Bidirecional solution: " + str(
                            list(moves2[::-1]) + moves1)
예제 #2
0
    def iterativeDepth(self, start, target):
        limitDepth = int(input("Enter the limit depth: "))
        currentDepth = 0

        queue = LinkedList.DoublyLinkedList()
        stack = LinkedList.DoublyLinkedList()
        copyStack = LinkedList.DoublyLinkedList()
        stack.insertEnd(start, None, 0)
        copyStack.insertEnd(start, None, 0)
        visited = [start]

        while not stack.empty() or not queue.empty():
            if stack.empty():
                while not queue.empty():
                    queueMove = queue.removeEnd()
                    stack.insertEnd(queueMove.data, queueMove.parent,
                                    queueMove.level)
                    copyStack.insertEnd(queueMove.data, queueMove.parent,
                                        queueMove.level)
                    visited.append(queueMove.data)
                limitDepth += 2
                print("\nSolution not found.\nNext limit depth: ", limitDepth)

            currentMove = stack.removeEnd()
            currentDepth = currentMove.level + 1
            nextMoves = Expansion.getNextMoves(currentMove.data)

            for i in range(len(nextMoves) - 1, -1, -1):
                newMove = nextMoves[i]
                flag = True
                if newMove in visited:
                    flag = False
                    visitedIndex = visited.index(newMove)
                    head = copyStack.head
                    while head.data != newMove:
                        head = head.nextPointer
                    if currentDepth < head.level:
                        visited.pop(visitedIndex)
                        flag = True
                if flag:
                    if newMove == target:
                        copyStack.insertEnd(newMove, currentMove, currentDepth)
                        moves = copyStack.tree()
                        return "Iterative depth solution: " + str(moves[::-1])

                    if currentDepth < limitDepth:
                        stack.insertEnd(newMove, currentMove, currentDepth)
                        copyStack.insertEnd(newMove, currentMove, currentDepth)
                        visited.append(newMove)
                    else:
                        queue.insertEnd(newMove, currentMove, currentDepth)
    def breadthFirst(self, start, target):
        queue = LinkedList.DoublyLinkedList()
        copyQueue = LinkedList.DoublyLinkedList()
        queue.insertEnd(start, None, None)
        copyQueue.insertEnd(start, None, None)
        visited = [start]

        while queue.empty() is not None:
            currentMove = queue.removeStart()
            nextMoves = Expansion.getNextMoves(currentMove.data)

            for i in range(len(nextMoves)):
                newMove = nextMoves[i]
                if newMove not in visited:
                    queue.insertEnd(newMove, currentMove, None)
                    copyQueue.insertEnd(newMove, currentMove, None)
                    visited.append(newMove)
                    if newMove == target:
                        moves = copyQueue.tree()
                        return "Breadth-first solution: " + str(moves[::-1])
    def depthFirst(self, start, target):
        stack = LinkedList.DoublyLinkedList()
        copyStack = LinkedList.DoublyLinkedList()
        stack.insertEnd(start, None, None)
        copyStack.insertEnd(start, None, None)
        visited = [start]

        while stack.empty() is not None:
            currentMove = stack.removeEnd()
            nextMoves = Expansion.getNextMoves(currentMove.data)
            
            for i in range(len(nextMoves)-1, -1, -1):
                newMove = nextMoves[i]
                if newMove not in visited:
                    stack.insertEnd(newMove, currentMove, None)
                    copyStack.insertEnd(newMove, currentMove, None)
                    visited.append(newMove)
                    if newMove == target:
                        moves = copyStack.tree()
                        return "Depth-first solution: " + str(moves[::-1])
    def limitedDepth(self, start, target):
        limitDepth = int(input("Enter the limit depth: "))
        stack = LinkedList.DoublyLinkedList()
        copyStack = LinkedList.DoublyLinkedList()
        stack.insertEnd(start, None, 0)
        copyStack.insertEnd(start, None, 0)
        visited = [start]

        while stack.empty() is not None:
            currentMove = stack.removeEnd()

            if currentMove == False:
                return "Solution not found."
            currentDepth = currentMove.level + 1
            if currentDepth <= limitDepth:
                nextMoves = Expansion.getNextMoves(currentMove.data)
                for i in range(len(nextMoves) - 1, -1, -1):
                    newMove = nextMoves[i]
                    flag = True
                    if newMove in visited:
                        flag = False
                        visitedIndex = visited.index(newMove)
                        head = copyStack.head
                        while head.data != newMove:
                            head = head.nextPointer
                        if currentDepth < head.level:
                            visited.pop(visitedIndex)
                            flag = True
                    if flag:
                        stack.insertEnd(newMove, currentMove, currentDepth)
                        copyStack.insertEnd(newMove, currentMove, currentDepth)
                        visited.append(newMove)
                        if newMove == target:
                            moves = copyStack.tree()
                            return "Limited depth-first solution: " + str(
                                moves[::-1])