def get_neighbors_link_cost(source_id, network_topology):
nodes = network_topology['nodes']
# get network topology links
links = network_topology['links'].keys()
#print(links)
# get the link costs in network topology
link_cost = network_topology['links']
#print(link_cost)
# make an empty dictionary for neighbor_link_cost
#neighbor_link_cost = None
neighbor_link_cost = {}
# add the cost to the source_id as zero
#neighbor_link_cost[None] = None
neighbor_link_cost[source_id] = 0
#print(neighbor_link_cost)
# go through the link
for link in links:
# if source id is not in the link, skip it
if source_id not in link:
continue
# get the neighbor information and cost for this neighbor
neighbor = link[0] if source_id == link[1] else link[1]
#neighbor_link_cost[None] = None
#neighbor_link_cost[neighbor] = 3
neighbor_link_cost[neighbor] = link(source_id,neighbor)
#neighbor_link_cost[neighbor] = link_cost.get('z','x')
#print(neighbor_link_cost[neighbor])
return neighbor_link_cost
network_topology = {'nodes': set(['z','x','y','w','v','u','t']),
'links': {('z','x'): 8,
('z','y'): 12,
('x','y'): 6,
('x','w'): 6,
('x','v'): 3,
('y','t'): 7,
('y','v'): 8,
('v','t'): 4,
('v','w'): 4,
('v','u'): 3,
('t','u'): 2,
('w','u'): 3
}
}
source_id = 'v'
get_neighbors_link_cost(source_id, network_top)
Ожидаемый результат должен быть:
Expected output: {'v': 0, 'x': 3, 'y': 8, 't': 4, 'w': 4, 'u': 3}
Какой должна быть эта строка, чтобы получить ожидаемый результат? Заранее спасибо