22f-3386 Lab 3 6BB
22f-3386 Lab 3 6BB
[DATE]
[Company name]
[Company address]
lab number 3
Task 1:
class Graph:
def __init__(self):
self.graph[u].append(v)
while queue:
for _ in range(current_level_size):
if node == goal_node:
path = []
while node:
path.append(node)
node = parent.get(node)
queue.append(neighbor)
# Driver code
g = Graph()
g.addEdge("Home", "Products")
g.addEdge("Home", "Blogs")
start_node = "Home"
# Display Results
if bfs_path:
else:
Task 2:
from collections import defaultdict
class WebGraph:
def __init__(self):
self.site_map[source].append(destination)
while exploration_stack:
if current_page == target_page:
# Driver code
web = WebGraph()
web.add_link("Home", "Products")
web.add_link("Home", "Blogs")
start_location = "Home"
# Execute DFS
# Display results
if path_traversed:
else:
Task 3:
# Synonyms dictionary as a graph representation
synonyms = {
while stack:
if word == goal_word:
# Main Execution
if __name__ == "__main__":
# Display Results
if path:
else:
Task 4:
import json
import time
def load_dictionary(filename):
synonyms = json.load(file)
return synonyms
if current_word == goal_word:
if limit <= 0:
visited.add(current_word)
nodes_explored)
if result:
visited = set()
if result:
return result, depth, nodes_explored[0] # Return path, depth, and explored nodes
return None, max_depth, nodes_explored[0] # Return None if not found within depth limit
def main():
dictionary_file = "EnglishDictionary.txt"
graph = load_dictionary(dictionary_file)
# Perform IDDFS
if path:
# Complexity Calculation
if __name__ == "__main__":
main()
Task 5:
import heapq
# Graph representation
graph = {
visited = set()
while priority_queue:
if node == goal:
visited.add(node)
# Main Execution
if __name__ == "__main__":
if path:
else:
Task 6:
from collections import deque
import time
forward_queue = deque([start])
backward_queue = deque([goal])
forward_visited = {start}
backward_visited = {goal}
# Forward step
if forward_queue:
current_forward = forward_queue.popleft()
forward_visited.add(neighbor)
forward_parent[neighbor] = current_forward
forward_queue.append(neighbor)
# Backward step
if backward_queue:
current_backward = backward_queue.popleft()
backward_visited.add(neighbor)
backward_parent[neighbor] = current_backward
backward_queue.append(neighbor)
path = [intersection]
current = intersection
current = forward_parent[current]
path.insert(0, current)
current = intersection
current = backward_parent[current]
path.append(current)
return path
graph = {
"Blog 2": []
start_node = "Home"
start_time = time.time()
end_time = time.time()
# Output results
if path:
else:
# Calculate complexities