0% found this document useful (0 votes)
3 views

Lab Manual 5

The document is a lab manual for the CS3151 Artificial Intelligence Lab at the University of Management and Technology, Lahore, detailing objectives, task distribution, and algorithms such as BFS, DFS, Hill Climbing, and Best First Search. It includes pseudocode, example graphs, and Python code implementations for each algorithm. Additionally, it provides exercise tasks and submission instructions for students.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Manual 5

The document is a lab manual for the CS3151 Artificial Intelligence Lab at the University of Management and Technology, Lahore, detailing objectives, task distribution, and algorithms such as BFS, DFS, Hill Climbing, and Best First Search. It includes pseudocode, example graphs, and Python code implementations for each algorithm. Additionally, it provides exercise tasks and submission instructions for students.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

University Of Management and Technology, Lahore

Lab Manual 05
CS3151-Artificial Intelligence Lab

Course Instructor Mr. Junaid Abdullah


Mansoor
Lab Instructor (s) Mr. Junaid Abdullah
Mansoor
Section V6
Semester Spring 2024
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

Hill Climbing 20 Minutes

Best first Search 20 Minutes

Exercise 60 Minutes

Online Submission 10 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.

The steps of the algorithm work as follow:

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

mark v as visited and put v into Q

while Q is non-empty

remove the head u of Q

mark and enqueue all (unvisited) neighbors of u

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 = [] # List for visited nodes.


queue = [] #Initialize a queue

def bfs(visited, graph, node): #function for BFS


CS3151: Artificial Intelligence Lab

visited.append(node)
queue.append(node)

while queue: # Creating loop to visit each node


m = queue.pop(0)
print (m, end = " ")

for neighbour in graph[m]:


if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# 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.

The DSF algorithm follows as:

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

4.1 DFS Pseudocode


The pseudocode for Depth-First Search in python goes as below: In the init() function, notice that
we run the DFS function on every node because many times, a graph may contain two different
disconnected part and therefore to make sure that we have visited every vertex, we can also run the
DFS algorithm at every node.

DFS(G, u)

u.visited = true

for each v ∈ G.Adj[u]

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' : []
}

visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): #function for dfs


if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

# 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

Here's a basic outline of the hill climbing algorithm:

• Start with an initial solution.


• Evaluate the current solution.
• Make a small change to the solution to create a neighboring solution.
• If the neighboring solution is better than the current solution, move to that solution and
repeat steps 2-4.
• If no neighboring solution is better than the current solution, terminate and return the
current solution.

5.1 Code
import random

# Define the graph with edge weights


graph = {
'5': {'3': random.randint(1, 10), '7': random.randint(1, 10)},
'3': {'2': random.randint(1, 10), '4': random.randint(1, 10)},
'7': {'8': random.randint(1, 10)},
'2': {},
'4': {'8': random.randint(1, 10)},
'8': {}
}

# Hill climbing algorithm


def hill_climbing(graph, start_node):
current_node = start_node
current_path_weight = 0

while True:
neighboring_nodes = graph.get(current_node, {})
best_neighbor = None
best_neighbor_weight = float('inf')

# Find the neighbor with the lowest weight


for neighbor, weight in neighboring_nodes.items():
if weight < best_neighbor_weight:
best_neighbor = neighbor
best_neighbor_weight = weight

# If no better neighbor found, terminate


CS3151: Artificial Intelligence Lab

if best_neighbor_weight >= current_path_weight:


break

# Move to the best neighbor


current_node = best_neighbor
current_path_weight = best_neighbor_weight

return current_node, current_path_weigh


6 Best First Search
Best First Search (BFS) is a graph traversal algorithm that explores the graph by expanding the
most promising node first. Unlike breadth-first search (BFS) and depth-first search (DFS), BFS
does not explore all possible paths from a given starting point. Instead, it focuses on the most
promising paths based on a heuristic evaluation function.

Here's a basic overview of how BFS works:

•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.

6.1 BFS Pseudocode


// Pseudocode for Best First Search
Best-First-Search(Graph g, Node start)
1) Create an empty PriorityQueue
PriorityQueue pq;
2) Insert "start" in pq.
pq.insert(start)
3) Until PriorityQueue is empty
u = PriorityQueue.DeleteMin
If u is the goal
Exit
Else
Foreach neighbor v of u
If v "Unvisited"
Mark v "Visited"
pq.insert(v)
CS3151: Artificial Intelligence Lab

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))

while not priority_queue.empty():


# Pop the node with the highest priority (lowest heuristic
value)
_, current_node = priority_queue.get()

if current_node not in visited:


# Mark the current node as visited
visited.add(current_node)
print(current_node, end=" ")

# Check neighboring nodes


for neighbor in graph[current_node]:
if neighbor not in visited:
# Calculate heuristic value (in this case, just
the node itself)
heuristic_value = int(neighbor)
# Push neighboring nodes into the priority queue
with their heuristic values
priority_queue.put((heuristic_value, neighbor))

# 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

• Apply Best first Search to following Graph

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

• To download the required file, go to File->Download .ipynb


• Only submit the .ipynb file. DO NOT zip or rar your submission file.
• Submit this file on LMS under the relevant assignment.
• Late submissions will not be accepted.

You might also like