<jupyter_start><jupyter_text>Check whether v1 and v2 vertices has a path in between them<jupyter_code># This format was asked in Coding Ninjas Assignment:-
import queue
class Graph:
    def __init__(self, nVertices):
        self.nVertices = nVertices
        self.adjMatrix = [[0 for i in range(nVertices)] for j in range(nVertices)]
       
    
    def addEdge(self, v1, v2):
        self.adjMatrix[v1][v2] = 1
        self.adjMatrix[v2][v1] = 1
        
        
    def removeEdge(self, v1, v2):
        if self.containEdge(v1, v2) is False:
            return
        self.adjMatrix[v1][v2] = 0
        self.adjMatrix[v2][v1] = 0
        
        
    def containsEdge(self, v1, v2):
        return True if self.adjMatrix[v1][v2] > 0 else False
        
    def __str__(self):
        return str(self.adjMatrix)
    
    
    def hasPathHelper(self, v1, v2, visited):

        if self.adjMatrix[v1][v2] > 0:
            return True
        
        visited[v1] = True

        for i in range(self.nVertices):
            if self.adjMatrix[v1][i] > 0  and visited[i] is False:
                if self.hasPathHelper(i, v2, visited):
                    return True         
                
        return False    
    
    def hasPath(self, v1, v2):
        visited = [False] * 7
        return self.hasPathHelper(v1, v2, visited)        
g = Graph(7)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(0, 3)

g.addEdge(1, 5)
g.addEdge(2, 4)
g.addEdge(3, 6)

g.addEdge(4, 6)


print(g.hasPath(1, 3))
print(g.hasPath(5, 6))
print(g.hasPath(1, 6))
print(g.hasPath(0, 5))
print(g.hasPath(2, 3))
# Coding Ninjas Assignment:
class Graph:
    def __init__(self, nVertices):
        self.nVertices = nVertices
        self.adjMatrix = [[0 for i in range(nVertices)] for j in range(nVertices)]
       
    
    def addEdge(self, v1, v2):
        self.adjMatrix[v1][v2] = 1
        self.adjMatrix[v2][v1] = 1
        
    def hasPathHelper(self, v1, v2, visited):

        if self.adjMatrix[v1][v2] > 0:
            return True
        
        visited[v1] = True
        
        for i in range(self.nVertices):
            if self.adjMatrix[v1][i] > 0  and visited[i] is False:
                if self.hasPathHelper(i, v2, visited):
                    #visited[v2] = True
                    return True
             
        return False
    
    
    def hasPath(self, v1, v2, n):
        visited = [False] * n
        return self.hasPathHelper(v1, v2, visited)
                
v, e = [int (i) for i in input().split()[:2]]
g = Graph(v)
for i in range(e):
    a, b = [int(x) for x in input().split()[:2]]
    g.addEdge(a, b)
    
v1, v2 = [int (r) for r in input().split()[:2]]
if g.hasPath(v1, v2, v):
    print("true")
else:
    print("false")<jupyter_output>6 3
5 3 
0 1
3 4
0 3
false
{'fraction_non_alphanumeric': 0.1262135922330097, 'fraction_numerical': 0.037756202804746494, 'mean_word_length': 1.8918918918918919, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '329261', 'license': 'Unlicense', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1021, 'n_words': 265, 'path': '19 Graphs - 1/19.08 Has Path.ipynb', 'repo_name': 'suhassuhas/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python', 'size': '2781'}