# **************************************************************************
# Question 4.1 - Route Between Nodes
#
# Problem:
#   Design an algorithm to find whether two nodes are connected in a 
#   directed graph
#
# Solution: O(N)
#   We run a breath first search algorithm from the first node to search
#   for a path to the second node.
# **************************************************************************

from collections import deque


def is_connected(node1, node2):
    queue = deque()
    queue.append(node1)

    while queue:
        node = queue.popleft()

        if node == node2:
            return True

        for neighbor in node.neighbors:
            if not neighbor.visited:
                neighbor.visited = True
                queue.append(neighbor)

    return False
