Python 3 Simple BFS


#1
def bfs(start,visited,adj_list,target):
queue = []
queue.append(start)
visited[start] = 1

while queue:
    curr_node = queue[-1]
    queue.pop()
    if curr_node==target:
        return 1
    for i in range(len(adj_list[curr_node])):
        if visited[adj_list[curr_node][i]]==0:
            if adj_list[curr_node][i]==target:
                return 1
            queue.append(adj_list[curr_node][i])
            visited[adj_list[curr_node][i]] = 1
return 0

class Solution:
    # @param A : integer
    # @param B : list of list of integers
    def solve(self, nodes, edges):
        visited = [0]*(nodes+1)
        adj_list = [[] for _ in range(nodes+1)]
        for i in range(len(edges)):
            adj_list[edges[i][0]].append(edges[i][1])

        return bfs(1,visited,adj_list,nodes)