                              PRACTICAL NO:2
                       BREADTH-FIRST-SEARCH
GRAPH 1
GRAPH:
CODE:
from collections import deque
graph = {
    'A': ['B', 'C'],
    'B': ['A', 'D', 'E'],
    'C': ['A', 'F'],
    'D': ['B'],
    'E': ['B', 'F'],
    'F': ['C', 'E']
}
def bfs(graph, start):
    visited = {start} 
    queue = deque([start])
    while queue:
        node = queue.popleft()
        print(node, end=" ")
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor) 
                queue.append(neighbor)
bfs(graph, 'A')
OUTPUT:
GRAPH 2
GRAPH:
CODE:
from collections import deque
graph = {
    1:[2],
    2:[3,6,8],
    3:[2,6,4,7],
    4:[3,6],
    5:[6],
    6:[4,5,3,2],
    7:[3],
    8:[2]
}
def bfs(graph, start):
    visited = {start} 
    queue = deque([start])
    while queue:
        node = queue.popleft()
        print(node, end=" ")
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor) 
                queue.append(neighbor)
bfs(graph, 1)
OUTPUT:
GRAPH 3
GRAPH:
CODE:
from collections import deque
graph = {
    1:[2,7,8],
    2:[3,6],
    3:[4,5],
    4:[3],
    5:[3],
    6:[2],
    7:[1],
    8:[9,12],
    9:[10,11],
    10:[],
    11:[],
    12:[]
}
def bfs(graph, start):
    visited = {start} 
    queue = deque([start])
    while queue:
        node = queue.popleft()
        print(node, end=" ")
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor) 
                queue.append(neighbor)
bfs(graph, 1)
OUTPUT: