0% found this document useful (0 votes)
5 views5 pages

AI Lab 2 11102024 011823pm

This document outlines the lab journal for the Artificial Intelligence Lab course at Bahria University, focusing on various graph traversal techniques using BFS and DFS. It includes five tasks that require implementing algorithms in Python to explore graph structures, find shortest paths, and search hierarchical file systems. Each task has specific objectives and example graphs to guide the implementation.

Uploaded by

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

AI Lab 2 11102024 011823pm

This document outlines the lab journal for the Artificial Intelligence Lab course at Bahria University, focusing on various graph traversal techniques using BFS and DFS. It includes five tasks that require implementing algorithms in Python to explore graph structures, find shortest paths, and search hierarchical file systems. Each task has specific objectives and example graphs to guide the implementation.

Uploaded by

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

Bahria University, Lahore Campus

(Department of Computer Science)


BSCS 6-A

Lab Journal: [2]


Course: Artificial Intelligence Lab Date: _______________
Course Code: CL461
Faculty’s Name: Mehreen Tariq

Name: _____________________ Enroll No: ___________________

Evaluation of CLO Question Marks Obtained


Number Marks

CLO1: Demonstrate proficiency in developing AI 2*5


applications in LISP and Prolog 1,2,3,4,5

Total Marks 10
Objective(s):
To Understand about:

Task 1: Explore BFS traversal in directed, weighted graphs with priority-based traversal and cost calculation.
Task 2: Apply BFS to find the shortest path in a city graph with weighted edges.
Task 3: Use DFS to discover all paths in a weighted, directed graph, calculating path costs and identifying the optimal
path.
Task 4: Implement DFS to search a hierarchical file system, tracking file paths and search efficiency.
Task 5: Compute all paths and their costs in a directed, weighted graph, identifying the shortest path between two
nodes.

Lab Tasks:

Task 1: Advanced Implementation of BFS

Objective: To master BFS traversal and its application in graph-based problems using advanced structures and
constraints.

Question:

1. Write a Python program to implement BFS for a directed, weighted graph where each node has a priority level.
The algorithm should traverse the graph level-by-level but prioritize visiting nodes with higher priority first.
2. Extend the implementation to keep track of the total traversal cost during BFS.
3. Consider the graph:
python
Copy code
graph = {
'A': [('B', 2), ('C', 1)],
'B': [('D', 4), ('E', 3)],
'C': [('F', 5)],
'D': [],
'E': [('F', 1)],
'F': []
}

o Implement BFS to traverse the graph starting from node A.


o Calculate and display the cumulative cost at each visited node.

Solution [copy your code solution and output below, adjust the font accordingly]

Task 2: Shortest Path in a City Graph Using BFS

Objective: To apply BFS to find the shortest path in a weighted graph with real-world constraints.

Question:

1. Consider a city represented as a weighted graph where landmarks are nodes, and roads have travel times as
weights.
Example:

python
Copy code
city_map = {
'A': [('B', 4), ('C', 2)],
'B': [('A', 4), ('D', 1), ('E', 5)],
'C': [('A', 2), ('F', 3)],
'D': [('B', 1)],
'E': [('B', 5), ('F', 1)],
'F': [('C', 3), ('E', 1)]
}

2. Write a program using BFS to find the shortest path from node A to node F based on the travel time.
3. Display the shortest path, its cost, and the nodes visited during the search.

Solution [copy your code solution and output below, adjust the font accordingly]
Task 3: DFS with Weighted and Directed Graphs

Objective: To explore DFS traversal and its use in finding paths in complex, directed, and weighted graphs.

Question:

1. Implement DFS for a weighted, directed graph where the goal is to find all possible paths between two nodes.
Example graph:

python
Copy code
graph = {
'A': [('B', 2), ('C', 3)],
'B': [('D', 4)],
'C': [('D', 1)],
'D': [('E', 5)],
'E': [('F', 2)],
'F': []
}

2. Extend the program to calculate the total cost for each path found.
3. For the graph above, find all paths from node A to node F, along with their costs, and identify the path with the
lowest cost.

Solution [copy your code solution and output below, adjust the font accordingly]

Task 4: Searching in a File System Using DFS

Objective: To use DFS for searching within hierarchical structures like file systems with advanced constraints.

Question:
1. Write a Python program to search for a specific file, such as target.txt, in a hierarchical file system
represented as a graph:

python
Copy code
file_system = {
'root': ['folderA', 'folderB', 'file1.txt'],
'folderA': ['folderC', 'file2.txt'],
'folderB': ['file3.txt', 'target.txt'],
'folderC': ['file4.txt', 'file5.txt']
}

2. Use DFS to locate the file and return its full path (e.g., /root/folderB/target.txt).
3. Extend the program to count the total number of files visited before finding the target file.

Solution [copy your code solution and output below, adjust the font accordingly]

Task 5: Advanced Shortest Path with Costs in Directed Graphs

Objective: To implement and analyze shortest-path algorithms in directed, weighted graphs.

Question:

1. Create a directed, weighted graph with the following edges and costs:

python
Copy code
graph = {
'A': [('B', 2), ('C', 1)],
'B': [('C', 2), ('D', 5)],
'C': [('D', 1), ('F', 3)],
'D': [('C', 1), ('E', 4)],
'E': [('F', 3)],
'F': [('C', 1), ('E', 2)]
}

2. Implement a function find_path(start, end) to compute the shortest path between two nodes.
3. Extend the function to:
o Return all possible paths between the nodes.
o Calculate and display the cost for each path.
o Identify and print the path with the lowest cost.
Solution [copy your code solution and output below, adjust the font accordingly]

Attempt all tasks and get them checked by your instructor.

You might also like