9/25/24, 11:13 PM Untitled11.
ipynb - Colab
PROGRAM 1: Program to implement Uninformed
keyboard_arrow_down Search Technique:
Breadth First Search Implementation of Breadth First Search Aim: To implement BFS in
Python.
Algorithm:
Create a queue Mark each new node as visited and put that node into the queue While Queue
is non-empty: Remove the head of Queue Mark and enqueue all (unvisited) neighbors Results:
Breadth First Search was successfully implemented in Python.
1
2 #Program:
3 graph = {
4 'A' : ['B','C'],
5 'B' : ['D'],
6 'C' : ['F'],
7 'D' : ['E', 'F'],
8 'E' : [],
9 'F' : ['A']
10 }
11
12 visited = [] # Keep track of visited nodes.
13 queue = [] # Queue
14
15 def bfs(visited, graph, node):
16 visited.append(node)
17 queue.append(node)
18 while queue:
19 s = queue.pop(0)
20 print (s, end = " ")
21 for neighbour in graph[s]:
22 if neighbour not in visited:
23 visited.append(neighbour)
24 queue.append(neighbour)
25
26 # Driver Code
27 bfs(visited, graph, 'A')
A B C D F E
https://colab.research.google.com/drive/1pG6gsR3veZ9QQHNBWAbzXkI9LzsEkqBK#scrollTo=6c5bcKeIP5Nh&uniqifier=1&printMode=true 1/3
9/25/24, 11:13 PM Untitled11.ipynb - Colab
PROGRAM 2: Program to implement Uninformed
keyboard_arrow_down Search Technique:
Depth First Search Implementation of Depth First Search Aim: To implement DFS in Python.
ALGORITHM
The DFS algorithm works as follows:
Start by putting any one of the graph's vertices on top of a stack. Take the top item of the
stack and add it to the visited list. Create a list of that vertex's adjacent nodes. Add the ones
which aren't in the visited list to the top of the stack. Keep repeating steps 2 and 3 until the
stack is empty. Results:
Depth First Search was successfully implemented in Python.
1 #Program:
2 graph = {
3 'A' : ['B','C'],
4 'B' : ['D'],
5 'C' : ['F'],
6 'D' : ['E', 'F'],
7 'E' : [],
8 'F' : ['A']
9 }
10 visited = set() # Keep track of visited nodes.
11
12 def dfs(visited, graph, node):
13 if node not in visited:
14 print (node)
15 visited.add(node)
16 for neighbour in graph[node]:
17 dfs(visited, graph, neighbour)
18
19 dfs(visited, graph, 'A')
A
B
D
E
F
C
1 Start coding or generate with AI.
https://colab.research.google.com/drive/1pG6gsR3veZ9QQHNBWAbzXkI9LzsEkqBK#scrollTo=6c5bcKeIP5Nh&uniqifier=1&printMode=true 2/3
9/25/24, 11:13 PM Untitled11.ipynb - Colab
https://colab.research.google.com/drive/1pG6gsR3veZ9QQHNBWAbzXkI9LzsEkqBK#scrollTo=6c5bcKeIP5Nh&uniqifier=1&printMode=true 3/3