Esempio n. 1
0
    def find_shortest_path(self, start_vetex, end_vertex):
        """
        Return a list of vertices creating a path from the start to end vertices.

        Keyword arguments:
        start_vetex -- the starting vertex.
        end_vertex -- the ending vertex.

        Time complexity: O(V)
        Space complexity: O(V)
        """
        if end_vertex.distance == float('inf'):
            return []
        else:
            reverse = Stack()
            current_vertex = end_vertex
            reverse.push(current_vertex)
            while current_vertex != start_vetex:
                current_vertex = current_vertex.previous_vertex
                reverse.push(current_vertex)

            path = []
            while not reverse.is_empty():
                path.append(reverse.pop())

            return path
class SetOfStacks:

    LIMIT_PER_STACK = 2

    def __init__(self):
        self.main_stack = Stack()

    def pop(self):
        if self.is_empty():
            return None
        elif self._top_stack().is_empty():
            self.main_stack.pop()
            self.pop()

        return self._top_stack().pop()

    def push(self, item):
        if self.is_empty():
            self.main_stack.push(Stack())
        self._top_stack().push(item)

    def is_empty(self):
        return self.main_stack.is_empty()

    def peek(self):
        if self.is_empty():
            return None
        return self._top_stack().peek().value

    def _top_stack(self):
        return self.main_stack.peek()
Esempio n. 3
0
def ids(problem):
    """
  Implement iterative deepening search.
  """
    start = problem.get_start_state()
    depth = 1
    while True:
        s = Stack()
        visited = {}
        d_map = {}
        cur = start
        s.push(cur)
        visited[start] = None
        d_map[start] = 1
        while not s.is_empty():
            cur = s.pop()
            if problem.is_goal_state(cur):
                path = []
                while cur is not None:
                    path.append(cur)
                    cur = visited[cur]
                path.reverse()
                print len(visited)
                return path
            if d_map[cur] != depth:
                for state in problem.get_successors(cur):
                    if state not in visited or d_map[cur] + 1 < d_map[state]:
                        s.push(state)
                        visited[state] = cur
                        d_map[state] = d_map[cur] + 1
        depth += 1
Esempio n. 4
0
def dfs(problem):
    """
  Implement depth-first search.
  """
    start = problem.get_start_state()
    s = Stack()
    visited = {}
    cur = start
    s.push(start)
    visited[start] = None
    while not s.is_empty():
        cur = s.pop()
        if problem.is_goal_state(cur):
            break
        for state in problem.get_successors(cur):
            if state not in visited:
                s.push(state)
                visited[state] = cur
    path = []
    while cur is not None:
        path.append(cur)
        cur = visited[cur]
    path.reverse()
    print len(visited)
    return path
Esempio n. 5
0
def my_pow(base, exp, modulus):
    from datastructures import Stack
    powers = Stack()
    var_exp = exp
    while var_exp > 0:
        powers.push(var_exp % 2)
        var_exp = var_exp / 2
    rem = 1
    while not powers.is_empty():
        p = powers.pop()
        rem = ((base ** p) * ((rem ** 2) % modulus)) % modulus
    return rem
def sort_stack(stack):
    temp_stack = Stack()

    threshold = stack.pop()
    while not stack.is_empty():
        if stack.peek() >= threshold:
            stack_node = stack.pop()
            temp_stack.push(threshold)
            threshold = stack_node

        elif stack.peek() < threshold:
            temp_stack.push(threshold)
            threshold = stack.pop()
            while not temp_stack.is_empty() and threshold < temp_stack.peek():
                stack.push(temp_stack.pop())
            temp_stack.push(threshold)
            if not stack.is_empty():
                threshold = stack.pop()

    temp_stack.push(threshold)
    while not temp_stack.is_empty():
        stack.push(temp_stack.pop())

    return stack
                stack.push(temp_stack.pop())
            temp_stack.push(threshold)
            if not stack.is_empty():
                threshold = stack.pop()

    temp_stack.push(threshold)
    while not temp_stack.is_empty():
        stack.push(temp_stack.pop())

    return stack


if __name__ == '__main__':  # tests

    stack = Stack()
    original_values = [8, 3, 5, 9, 7, 2]
    for v in original_values:
        stack.push(v)

    sorted_stack = Stack()
    sorted_values = sorted(original_values, reverse=True)
    for v in sorted_values:
        sorted_stack.push(v)

    expected_sorted_stack = sort_stack(stack)
    while not expected_sorted_stack.is_empty() and not sorted_stack.is_empty():
        assert expected_sorted_stack.pop() == sorted_stack.pop()