Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- distances = {
- 'A': {'B': 5},
- 'B': {'C': 10},
- 'C': { 'D': 50, 'F': 1},
- 'D': {},
- 'F': {'B': 2},
- }
- def dixstra(graph,start,end):
- unvisited ={ i: float("inf") for i in graph}
- visited = dict()
- node= start
- unvisited[node] = 0
- while True:
- for neighbour, distance in graph[node].items():
- if neighbour not in unvisited: continue
- new_distance = distance + unvisited[node]
- if unvisited[neighbour] > new_distance:
- unvisited[neighbour] = new_distance
- visited[node]= unvisited[node]
- if node == end: break
- del unvisited[node]
- if not unvisited: break
- node = next_node(unvisited)
- return visited
- def next_node(costs):
- lowest_cost = float('inf')
- lowest_cost_node = None
- for node in costs:
- if costs[node] < lowest_cost :
- lowest_cost = costs[node]
- lowest_cost_node= node
- return lowest_cost_node
- print(dixstra(distances,"A","D"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement