dict_graph = {} # Read the data.txt file with open('data.txt', 'r') as f: for l in f: city_a, city_b, p_cost = l.split() if city_a not in dict_graph: dict_graph[city_a] = {} dict_graph[city_a][city_b] = int(p_cost) if city_b not in dict_graph: dict_graph[city_b] = {} dict_graph[city_b][city_a] = int(p_cost) # Breadth First Search Method def BreadthFirstSearch(graph, src, dst): q = [(src, [src], 0)] visited = {src} while q: (node, path, cost) = q.pop(0) for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) q.append((temp, path + [temp], cost + graph[node][temp])) # Depth First Search Method def DepthFirstSearch(graph, src, dst): stack = [(src, [src], 0)] visited = {src} while stack: (node, path, cost) = stack.pop() for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) stack.append((temp, path + [temp], cost + graph[node][temp])) # Iterative Deepening Search Method def IterativeDeepening(graph, src, dst): level = 0 count = 0 stack = [(src, [src], 0)] visited = {src} while True: level += 1 while stack: if count <= level: count = 0 (node, path, cost) = stack.pop() for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) count += 1 stack.append((temp, path + [temp], cost + graph[node][temp])) else: q = stack visited_bfs = {src} while q: (node, path, cost) = q.pop(0) for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited_bfs: visited_bfs.add(temp) q.append((temp, path + [temp], cost + graph[node][temp])) break n = 1 print (dict_graph) print ("------------------------------------------------") while n == 1: x = input("enter the type of search you want to do \n 1.BFS 2.DFS 3.ID \n ") if x == 1: src = raw_input("Enter the source: ") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n ") print ("for BFS") print (BreadthFirstSearch(dict_graph, src, dst)) elif x == 2: src = raw_input("Enter the source: ") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n ") print ("for DFS") print (DepthFirstSearch(dict_graph, src, dst)) elif x == 3: src = raw_input("Enter the source:") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n") print ("for ID") print (IterativeDeepening(dict_graph, src, dst)) #n = input("enter 1 if you wish to continue:\n") Check these codes and fix the errors
dict_graph = {} # Read the data.txt file with open('data.txt', 'r') as f: for l in f: city_a, city_b, p_cost = l.split() if city_a not in dict_graph: dict_graph[city_a] = {} dict_graph[city_a][city_b] = int(p_cost) if city_b not in dict_graph: dict_graph[city_b] = {} dict_graph[city_b][city_a] = int(p_cost) # Breadth First Search Method def BreadthFirstSearch(graph, src, dst): q = [(src, [src], 0)] visited = {src} while q: (node, path, cost) = q.pop(0) for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) q.append((temp, path + [temp], cost + graph[node][temp])) # Depth First Search Method def DepthFirstSearch(graph, src, dst): stack = [(src, [src], 0)] visited = {src} while stack: (node, path, cost) = stack.pop() for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) stack.append((temp, path + [temp], cost + graph[node][temp])) # Iterative Deepening Search Method def IterativeDeepening(graph, src, dst): level = 0 count = 0 stack = [(src, [src], 0)] visited = {src} while True: level += 1 while stack: if count <= level: count = 0 (node, path, cost) = stack.pop() for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited: visited.add(temp) count += 1 stack.append((temp, path + [temp], cost + graph[node][temp])) else: q = stack visited_bfs = {src} while q: (node, path, cost) = q.pop(0) for temp in graph[node].keys(): if temp == dst: return path + [temp], cost + graph[node][temp] else: if temp not in visited_bfs: visited_bfs.add(temp) q.append((temp, path + [temp], cost + graph[node][temp])) break n = 1 print (dict_graph) print ("------------------------------------------------") while n == 1: x = input("enter the type of search you want to do \n 1.BFS 2.DFS 3.ID \n ") if x == 1: src = raw_input("Enter the source: ") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n ") print ("for BFS") print (BreadthFirstSearch(dict_graph, src, dst)) elif x == 2: src = raw_input("Enter the source: ") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n ") print ("for DFS") print (DepthFirstSearch(dict_graph, src, dst)) elif x == 3: src = raw_input("Enter the source:") dst = raw_input("Enter the Destination: ") while src not in dict_graph or dst not in dict_graph: print ("No such city name") src = raw_input("Enter the correct source (case_sensitive):\n") dst = raw_input("Enter the correct destination(case_sensitive):\n") print ("for ID") print (IterativeDeepening(dict_graph, src, dst)) #n = input("enter 1 if you wish to continue:\n") Check these codes and fix the errors
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
dict_graph = {}
# Read the data.txt file
with open('data.txt', 'r') as f:
for l in f:
city_a, city_b, p_cost = l.split()
if city_a not in dict_graph:
dict_graph[city_a] = {}
dict_graph[city_a][city_b] = int(p_cost)
if city_b not in dict_graph:
dict_graph[city_b] = {}
dict_graph[city_b][city_a] = int(p_cost)
# Breadth First Search Method
def BreadthFirstSearch(graph, src, dst):
q = [(src, [src], 0)]
visited = {src}
while q:
(node, path, cost) = q.pop(0)
for temp in graph[node].keys():
if temp == dst:
return path + [temp], cost + graph[node][temp]
else:
if temp not in visited:
visited.add(temp)
q.append((temp, path + [temp], cost + graph[node][temp]))
# Depth First Search Method
def DepthFirstSearch(graph, src, dst):
stack = [(src, [src], 0)]
visited = {src}
while stack:
(node, path, cost) = stack.pop()
for temp in graph[node].keys():
if temp == dst:
return path + [temp], cost + graph[node][temp]
else:
if temp not in visited:
visited.add(temp)
stack.append((temp, path + [temp], cost + graph[node][temp]))
# Iterative Deepening Search Method
def IterativeDeepening(graph, src, dst):
level = 0
count = 0
stack = [(src, [src], 0)]
visited = {src}
while True:
level += 1
while stack:
if count <= level:
count = 0
(node, path, cost) = stack.pop()
for temp in graph[node].keys():
if temp == dst:
return path + [temp], cost + graph[node][temp]
else:
if temp not in visited:
visited.add(temp)
count += 1
stack.append((temp, path + [temp], cost + graph[node][temp]))
else:
q = stack
visited_bfs = {src}
while q:
(node, path, cost) = q.pop(0)
for temp in graph[node].keys():
if temp == dst:
return path + [temp], cost + graph[node][temp]
else:
if temp not in visited_bfs:
visited_bfs.add(temp)
q.append((temp, path + [temp], cost + graph[node][temp]))
break
n = 1
print (dict_graph)
print ("------------------------------------------------")
while n == 1:
x = input("enter the type of search you want to do \n 1.BFS 2.DFS 3.ID \n ")
if x == 1:
src = raw_input("Enter the source: ")
dst = raw_input("Enter the Destination: ")
while src not in dict_graph or dst not in dict_graph:
print ("No such city name")
src = raw_input("Enter the correct source (case_sensitive):\n")
dst = raw_input("Enter the correct destination(case_sensitive):\n ")
print ("for BFS")
print (BreadthFirstSearch(dict_graph, src, dst))
elif x == 2:
src = raw_input("Enter the source: ")
dst = raw_input("Enter the Destination: ")
while src not in dict_graph or dst not in dict_graph:
print ("No such city name")
src = raw_input("Enter the correct source (case_sensitive):\n")
dst = raw_input("Enter the correct destination(case_sensitive):\n ")
print ("for DFS")
print (DepthFirstSearch(dict_graph, src, dst))
elif x == 3:
src = raw_input("Enter the source:")
dst = raw_input("Enter the Destination: ")
while src not in dict_graph or dst not in dict_graph:
print ("No such city name")
src = raw_input("Enter the correct source (case_sensitive):\n")
dst = raw_input("Enter the correct destination(case_sensitive):\n")
print ("for ID")
print (IterativeDeepening(dict_graph, src, dst))
#n = input("enter 1 if you wish to continue:\n")
Check these codes and fix the errors
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education