Dijkstra's algorithm in python
问题 I am trying to implement Dijkstra's algorithm in python using arrays. This is my implementation. def extract(Q, w): m=0 minimum=w[0] for i in range(len(w)): if w[i]<minimum: minimum=w[i] m=i return m, Q[m] def dijkstra(G, s, t='B'): Q=[s] p={s:None} w=[0] d={} for i in G: d[i]=float('inf') Q.append(i) w.append(d[i]) d[s]=0 S=[] n=len(Q) while Q: u=extract(Q,w)[1] S.append(u) #w.remove(extract(Q, d, w)[0]) Q.remove(u) for v in G[u]: if d[v]>=d[u]+G[u][v]: d[v]=d[u]+G[u][v] p[v]=u return d, p B