AI Lab 2 11102024 011823pm
AI Lab 2 11102024 011823pm
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:
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': []
}
Solution [copy your code solution and output below, adjust the font accordingly]
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]
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]
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]