Function which will check if there is a path between a and b by using BFS
def Check_Path ( a , b , G ) : NEW_LINE INDENT visited = defaultdict ( bool ) NEW_LINE queue = deque ( ) NEW_LINE queue . append ( a ) NEW_LINE visited [ a ] = True NEW_LINE while queue : NEW_LINE INDENT n = queue . popleft ( ) NEW_LINE if n == b : NEW_LINE INDENT return True NEW_LINE DEDENT for i in list ( G [ n ] ) : NEW_LINE INDENT if visited [ i ] == False : NEW_LINE INDENT queue . append ( i ) NEW_LINE visited [ i ] = True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT