<jupyter_start><jupyter_text>BFS CODE: ![bfs.png](img/bfs.png)  BFS PATH: 0 1 2 3 4 6 5<jupyter_code>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 __bfsHelper(self, sv, visited):
        q = queue.Queue()
        q.put(sv)
        
        for i in range(self.nVertices):
            if self.adjMatrix[sv][i] > 0 and visited[i] is False:
                x = q.get()
                print(x)
                visited[x] = True
                self.__bfsHelper(i, visited)                  

    def bfs(self):       # Using BFS for traversing all vertices of a graph
        q = queue.Queue()
        visited = [False for i in range(self.nVertices)]
        visited[0] = True
        q.put(0)
        while q.empty() is False:
          u = q.get()
          print(u)
          for i in range(self.nVertices):
            if self.adjMatrix[u][i] > 0 and visited[i] is False:
              q.put(i)
              visited[i] = True
    
g = Graph(7)
g.addEdge(0, 1)
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(1, 4)
g.addEdge(2, 6)
g.addEdge(4, 5)
g.addEdge(5, 6)
g.bfs()
# 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 __bfsHelper(self, sv, visited):
        q = queue.Queue()
        q.put(sv)
            
    def bfs(self):       # Using BFS for traversing all vertices of a graph
        q = queue.Queue()
        visited = [False for i in range(self.nVertices)]
        visited[0] = True
        q.put(0)
        while q.empty() is False:
          u = q.get()
          print(u, end = " ")
          for i in range(self.nVertices):
            if self.adjMatrix[u][i] > 0 and visited[i] is False:
              q.put(i)
              visited[i] = True
    
    
v, e = [int(x) for x 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)
g.bfs()<jupyter_output>4 4 
0 1
0 3
1 2
2 3
0
1
3
2
{'fraction_non_alphanumeric': 0.12374371859296482, 'fraction_numerical': 0.02920854271356784, 'mean_word_length': 1.6991525423728813, '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': '76969', 'license': 'Unlicense', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1151, 'n_words': 302, 'path': '19 Graphs - 1/19.07 BFS.ipynb', 'repo_name': 'suhassuhas/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python', 'size': '3184'}