determine if there is a path between nodes in a graph a directed graph add a new directed edge to the graph determine if there is a path from source to target using a depth first search determine if there is a path from source to target using a depth first search param visited should be an array of booleans determining if the corresponding vertex has been visited already determine if there is a path from source to target a directed graph add a new directed edge to the graph determine if there is a path from source to target using a depth first search determine if there is a path from source to target using a depth first search param visited should be an array of booleans determining if the corresponding vertex has been visited already determine if there is a path from source to target
from collections import defaultdict


class Graph:
    

    def __init__(self,vertex_count):
        self.vertex_count = vertex_count
        self.graph = defaultdict(list)
        self.has_path = False

    def add_edge(self,source,target):
        
        self.graph[source].append(target)

    def dfs(self,source,target):
        
        visited = [False] * self.vertex_count
        self.dfsutil(visited,source,target,)

    def dfsutil(self,visited,source,target):
        
        visited[source] = True
        for i in self.graph[source]:
            if target in self.graph[source]:
                self.has_path = True
                return
            if not visited[i]:
                self.dfsutil(visited,source,i)

    def is_reachable(self,source,target):
        
        self.has_path = False
        self.dfs(source,target)
        return self.has_path