Lab Manual 5
Lab Manual 5
Lab Manual 05
CS3151-Artificial Intelligence Lab
Table of Contents
1 Objectives ..................................................................................................................................2
2 Task Distribution .......................................................................................................................3
3 BFS ............................................................................................................................................3
3.1 BFS Pseudocode .................................................................................................................4
3.2 Graph ..................................................................................................................................4
3.3 Code....................................................................................................................................4
4 DFS ............................................................................................................................................5
4.1 DFS Pseudocode .................................................................................................................6
4.2 Graph ..................................................................................................................................6
4.3 Code....................................................................................................................................7
5 Hill Climbing .............................................................................................................................7
5.1 Code....................................................................................................................................8
6 Best First Search ........................................................................................................................9
6.1 BFS Pseudocode .................................................................................................................9
6.2 Graph ................................................................................................................................ 10
6.3 Code..................................................................................................................................10
7 Exercise ....................................................................................................................................11
8 Submission Instructions ...........................................................................................................12
1 Objectives
After performing this lab, students shall be able to understand implementation search algorithms
in:
• DFS
• BFS
CS3151: Artificial Intelligence Lab
• Hill climbing
• Best first Search
2 Task Distribution
Total Time 150 Minutes
BFS 20 Minutes
DFS 20 Minutes
Exercise 60 Minutes
3 BFS
Before learning the python code for Breadth-First and its output, let us go through the algorithm
it follows for the same. We can take the example of Rubik’s Cube for the instance. Rubik’s Cube
is seen as searching for a path to convert it from a full mess of colors to a single color. So
comparing the Rubik’s Cube to the graph, we can say that the possible state of the cube is
corresponding to the nodes of the graph and the possible actions of the cube is corresponding to
the edges of the graph.
As breadth-first search is the process of traversing each node of the graph, a standard BFS
algorithm traverses each vertex of the graph into two parts: 1) Visited 2) Not Visited. So, the
purpose of the algorithm is to visit all the vertex while avoiding cycles.
BFS starts from a node, then it checks all the nodes at distance one from the beginning node,
then it checks all the nodes at distance two, and so on. So as to recollect the nodes to be visited,
BFS uses a queue.
1. Start by putting any one of the graph’s vertices at the back of the queue.
2. Now take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add those which are not within the visited
list to the rear of the queue.
4. Keep continuing steps two and three till the queue is empty.
CS3151: Artificial Intelligence Lab
Many times, a graph may contain two different disconnected parts and therefore to make sure
that we have visited every vertex, we can also run the BFS algorithm at every node
3.1 BFS Pseudocode
The pseudocode for BFS in python goes as below:
create a queue Q
while Q is non-empty
3.2 Graph
Given Below is the graph which we use for coding
3.3 Code
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited.append(node)
queue.append(node)
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
In the above code, first, we will create the graph for which we will use the breadth-first search.
After creation, we will create two lists, one to store the visited node of the graph and another one
for storing the nodes in the queue.
After the above process, we will declare a function with the parameters as visited nodes, the graph
itself and the node respectively. And inside a function, we will keep appending the visited and
queue lists.
Then we will run the while loop for the queue for visiting the nodes and then will remove the same
node and print it as it is visited.
At last, we will run the for loop to check the not visited nodes and then append the same from the
visited and queue list
4 DFS
Before learning the python code for Depth-First and its output, let us go through the algorithm it
follows for the same. The recursive method of the Depth-First Search algorithm is implemented
using stack. A standard Depth-First Search implementation puts every vertex of the graph into one
in all 2 categories: 1) Visited 2) Not Visited. The only purpose of this algorithm is to visit all the
vertex of the graph avoiding cycles.
1. We will start by putting any one of the graph's vertex on top of the stack.
2. After that take the top item of the stack and add it to the visited list of the vertex.
3. Next, create a list of that adjacent node of the vertex. Add the ones which aren't in the
visited list of vertexes to the top of the stack.
4. Lastly, keep repeating steps 2 and 3 until the stack is empty.
CS3151: Artificial Intelligence Lab
DFS(G, u)
u.visited = true
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
4.2 Graph
.
CS3151: Artificial Intelligence Lab
4.3 Code
# Using a Python dictionary to act as an adjacency list
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
In the above code, first, we will create the graph for which we will use the depth-first search. After
creation, we will create a set for storing the value of the visited nodes to keep track of the visited
nodes of the graph.
After the above process, we will declare a function with the parameters as visited nodes, the graph
itself and the node respectively. And inside the function, we will check whether any node of the
graph is visited or not using the “if” condition. If not, then we will print the node and add it to the
visited set of nodes.
Then we will go to the neighboring node of the graph and again call the DFS function to use the
neighbor parameter.
At last, we will run the driver code which prints the final result of DFS by calling the DFS the first
time with the starting vertex of the graph.
5 Hill Climbing
Hill climbing is a heuristic search algorithm used for mathematical optimization problems. It's a
simple iterative algorithm that starts with an arbitrary solution to a problem, then iteratively
makes small changes to the solution to gradually improve it, until no further improvements can
be made.
CS3151: Artificial Intelligence Lab
5.1 Code
import random
while True:
neighboring_nodes = graph.get(current_node, {})
best_neighbor = None
best_neighbor_weight = float('inf')
•BFS maintains a priority queue (often implemented using a heap data structure) to store
the nodes of the graph.
• The algorithm starts with an initial node and adds it to the priority queue.
• At each iteration, BFS selects the node with the highest priority from the priority queue.
• BFS expands the selected node by adding its neighboring nodes to the priority queue.
• The priority of each neighboring node is determined by a heuristic evaluation function.
• The process continues until a goal node is found or the priority queue becomes empty.
BFS is often used in search problems where the search space is too large to explore exhaustively.
By focusing on the most promising paths first, BFS can efficiently find solutions to these problems.
Mark u "Examined"
End procedur
6.2 Graph
Consider the Following Graph
6.3 Code
from queue import PriorityQueue
graph = {
'5': [('3', 2), ('7', 1)],
'3': [('2', 3), ('4', 2)],
'7': [('8', 1)],
'2': [],
'4': [('8', 2)],
'8': []
}
# Best First Search function
def best_first_search(graph, start_node):
visited = set() # Set to store visited nodes
priority_queue = PriorityQueue() # Priority queue to store nodes
based on heuristic value
CS3151: Artificial Intelligence Lab
# Push the start node into the priority queue with a heuristic
value of 0
priority_queue.put((0, start_node))
# Driver code
print("Following is the Best First Search:")
best_first_search(graph, '5')
7 Exercise
• Apply the BFS and DFS to following Graph
CS3151: Artificial Intelligence Lab
8 Submission Instructions
Always read the submission instructions carefully.
• Rename your Jupyter notebook to your roll number and download the notebook as .ipynb
extension.
CS3151: Artificial Intelligence Lab