#python
#python
Вопрос:
В настоящее время я работаю над проектом, где мне задали следующий вопрос: «Объясните, как график является абстракцией проблемы», Может кто-нибудь помочь понять эту концепцию, пожалуйста?
У меня также есть следующий фрагмент кода, который работает отлично, но я хочу изменить его, добавив ending_ver, чтобы вернуть кратчайший путь между заданными версиями
import heapq
def dijkstras(grh, str_ver):
distances = {vertex: float('infinity') for vertex in grh}
distances[str_ver] = 0
pq = [(0, str_ver)]
while len(pq) > 0:
current_distance, current_vertex = heapq.heappop(pq)
#noodes can get added to the priority q multiple times
#we only process a vertex the first time we remove it from the priority Q
if current_distance > distances[current_vertex]:
continue
for neighbor, weight in grh[current_vertex].items():
distance = current_distance weight
#Only consider this new path if its better than any
#path we've already found
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances