コード例 #1
0
def test_peek():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    actual = q.peek()
    expected = "apple"
    assert actual == expected
コード例 #2
0
def test_peek_post_dequeue():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    q.dequeue()
    actual = q.peek()
    expected = "bananas"
    assert actual == expected
コード例 #3
0
def test_dequeue_when_full():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.dequeue()
    expected = "apples"
    assert actual == expected
コード例 #4
0
def test_enqueue_two():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.peek()
    expected = "apples"
    assert actual == expected
コード例 #5
0
def test_dequeue():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    actual = q.dequeue()
    expected = "apple"
    assert actual == expected
コード例 #6
0
    def _has_path_bfs(self, visited, start_id, target_id):
        """Search Graph looking for node with target_id, starting at a given
        node (id_1), using breadth first search.

        Parameters
        ----------
        visited : dict
            Contains mapping from ids to bool indicating if they have been
            visited (True) or not (False). Format:
                id : Node
        start_id : str
            Id of node where search starts
        target_id : str
            Id of target node that is being searched for

        Returns
        -------
        bool
            True if target_id found, False otherwise
        """
        # Use Queue to store ids of nodes to be visited next
        nodes_to_visit = Queue()

        # Mark starting node as visited and add to Queue
        visited[start_id] = True
        nodes_to_visit.add(start_id)

        # Keep visiting nodes until none left to visit (Queue empty)
        while not nodes_to_visit.is_empty():

            # Extract node at front of Queue
            current_node_id = nodes_to_visit.remove()
            current_node = self.nodes[current_node_id]

            # Check to see if current node id matches target_id
            if current_node_id == target_id:
                return True

            # Check through current_node's neighbours
            for node_id in current_node.neighbour_ids:

                # Ensure node not already visited
                if not visited[node_id]:

                    # Mark node as visited and add to Queue
                    visited[node_id] = True
                    nodes_to_visit.add(node_id)

        # If no match found after search completes, return False
        return False
コード例 #7
0
def test_exhausted():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    q.dequeue()
    q.dequeue()
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
コード例 #8
0
def test_is_empty():
    q = Queue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
コード例 #9
0
def test_dequeue_when_empty():
    q = Queue()
    with pytest.raises(InvalidOperationError):
        q.dequeue()
コード例 #10
0
def test_enqueue():
    q = Queue()
    q.enqueue("apple")
    actual = q.front.value
    expected = "apple"
    assert actual == expected
コード例 #11
0
def test_peek_when_empty():
    q = Queue()
    with pytest.raises(InvalidOperationError):
        q.peek()
def is_route_between_nodes(graph, id_a, id_b):
    """Check to see if there is a route between nodes with id a and id b, in a
    directed graph

    Uses 2 simultaneous breadth first searches from id a and id b

    Parameters
    ----------
    graph : Graph
        Directed graph to search
    id_a : any
        Id of node at one end of route we are trying to find. Must be hashable,
        so can be used as key in a dictionary.
    id_b : any
        Id of node at other end of route we are trying to find. Must be
        hashable, so can be used as key in a dictionary.
    """
    # Set up dict of nodes visited by search from node with id a, and b
    visited_by_a = {k: False for k in graph.nodes.keys()}
    visited_by_b = {k: False for k in graph.nodes.keys()}

    # Use Queue to store tuple: (id of node to visit, bool that is True if node
    # is part of node a's search, False if part of node b's search
    nodes_to_visit = Queue()

    # Mark starting nodes as visited, and add to queue
    visited_by_a[id_a] = True
    nodes_to_visit.add((id_a, True))
    visited_by_b[id_b] = True
    nodes_to_visit.add((id_b, False))

    # Keep visiting nodes until none left to visit (Queue empty)
    while not nodes_to_visit.is_empty():

        # Extract node at front of Queue
        current_node_id, is_on_search_a = nodes_to_visit.remove()
        current_node = graph.nodes[current_node_id]

        # Check through current_node's neighbours
        for node_id in current_node.neighbour_ids:

            # Ensure node not already visited by relevant search
            if is_on_search_a and not visited_by_a[node_id]:
                # Check if node has b's id
                if node_id == id_b:
                    return True
                # Mark node as visited and add to Queue
                visited_by_a[node_id] = True
                nodes_to_visit.add((node_id, is_on_search_a))
            else:
                # Check if node has a's id
                if current_node_id == id_a:
                    return True
                # Mark node as visited and add to Queue
                visited_by_b[node_id] = True
                nodes_to_visit.add((node_id, is_on_search_a))

    # If no match found after search completes, return False
    return False