0

CODE:

def prueba(graph: 'nx.classes.graph.Graph', start: str, end: str):


  def cost(u, v):

    return graph.get_edge_data(u,v).get('weight')

  prev = {}
  dist = {}
  Q = []
  pq.heapify(Q)
  visited = set()

  for v in list(nx.nodes(graph)):

    prev[v] = 0
    dist[v] = inf
    dist[start] = 0
    nodes = dist[v], v
    pq.heappush(Q, nodes)  


  while Q != 0:

    minimum = Q[0]
    priority, element = minimum
    visited.add(minimum)

    for neighbor in dict(graph.adjacency()).get(element):
              
              path = dist[element] + cost(element, neighbor)

              if path < dist[neighbor]:
                  
                  dist[neighbor] = path
                
                  prev[neighbor] = element
                
                  if neighbor not in visited:
                      
                      visited.add(neighbor)
                      pq.heappush(Q, (dist[neighbor],neighbor))
        
                  else:
    
                      _ = pq.get((dist[neighbor],neighbor))
            
                      pq.heappush(Q, (dist[neighbor],neighbor))


    return prev

RESULT:

{'A': 0, 'B': 'D', 'C': 0, 'D': 0, 'E': 0}

Why am I getting 0 in all nodes except in the fisrt one? I do not know if is for the Q[0], because maybe it is taking all the time the same number, however there is another way to do that using the library of heapq?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Alex
  • 3
  • 1

0 Answers0