def __init__(self, G): self.pq = IndexMinPQ(G.V()) self.marked = [False] * G.V() # 判断顶点是不是在树上 self.distTo = [] # 存放距离树最近的边的权重 self.edgeTo = [] # 存放距离树最近的边 for v in range(G.V()): self.distTo[v] = float("inf") for v in range(G.V()): if not self.marked[v]: self.prim(G, v)
def __init__(self, G, s): self.distTo = [0.0] * G.V() self.edgeTo = [0] * G.V() for v in range(G.v): self.distTo[v] = INF self.distTo[s] = 0.0 self.pq = IndexMinPQ(G.V()) self.pq.insert(s, self.distTo[s]) while not self.pq.isEmpty(): # 因为每个点都需要松弛 v = self.pq.delMin() # 我们每次都是松弛权重最小的 # 1点所以用优先队列来退出最小权重的点 for e in G.adj(v): self.relax(e) # 松弛指定的边
def MultiWayMerge(streams): items = [] i = 0 for s in streams: items.append(IPQ.Item((i, s[0]))) del s[:1] i += 1 ipq = IPQ.IndexMinPQ(items) out = [] while not ipq.isEmpty(): out.append(ipq.minKey()) i = ipq.delMin() if len(streams[i]) > 0: ipq.insert(i, streams[i][0]) del streams[i][:1] print("out: {}".format(out))
class PrimMST(object): FLOATING_POINT_EPSILON = 1e-12 def __init__(self, G): self.pq = IndexMinPQ(G.V()) self.marked = [False] * G.V() # 判断顶点是不是在树上 self.distTo = [] # 存放距离树最近的边的权重 self.edgeTo = [] # 存放距离树最近的边 for v in range(G.V()): self.distTo[v] = float("inf") for v in range(G.V()): if not self.marked[v]: self.prim(G, v) def prim(self, G, s): self.distTo[s] = 0.0 self.pq.insert(s, self.distTo[s]) while not self.pq.isEmpty(): v = self.pq.delMin() self.scan(G, v) def scan(self, G, v): self.marked[v] = True for e in G.adj(v): w = e.other(v) if self.marked[w]: # 直接在添加的时候就避免会产生无效边 # 不会再优先队列中添加这么多的边所以比较省事 continue if e.weight() < self.distTo[w]: # 永远只保存最优的边每# # 次浏览都会更新最优化的边 self.distTo[w] = e.weight() self.edgeTo[w] = e if self.pq.contains(w): self.pq.decreaseKey(w, self.distTo[w]) else: self.pq.insert(w, self.distTo[w]) def edges(self): mst = Queue1() for v in range(len(self.edgeTo)): e = self.edgeTo[v] if e != None: mst.enqueue(e) return mst def weight(self): weight = 0.0 for e in self.edges(): weight += e.weight() return weight
def __init__(self, graph, s): self.edgeTo = [None] * graph.V self.distTo = [float("inf")] * graph.V self.pq = PQ.IndexMinPQ(graph.V) self.s = s self.distTo[s] = 0.0 self.pq.insert(s, 0.0) while (self.pq.isEmpty() != True): #print(self.pq.min()) self.relax(graph, self.pq.delMin())
class DijkstraSP(object): def __init__(self, G, s): self.distTo = [0.0] * G.V() self.edgeTo = [0] * G.V() for v in range(G.v): self.distTo[v] = INF self.distTo[s] = 0.0 self.pq = IndexMinPQ(G.V()) self.pq.insert(s, self.distTo[s]) while not self.pq.isEmpty(): # 因为每个点都需要松弛 v = self.pq.delMin() # 我们每次都是松弛权重最小的 # 1点所以用优先队列来退出最小权重的点 for e in G.adj(v): self.relax(e) # 松弛指定的边 def relax(self, e): # 松弛边 v = e.from1() # e的起点 w = e.to1() # e的终点 if self.distTo[w] > (self.disTo[v] + e.weight()): # 如果说发现原本的最短距离大那么就更新他的最短距离 self.distTo[w] = self.distTo[v] + e.weight() self.edgeTo[w] = e if self.pq.contains(w): # 往优先队列中添加值 self.pq.decreaseKey(w, self.distTo[w]) else: self.pq.insert(w, self.distTo[w]) def distTo(self, v): return self.distTo[v] def hasPathTo(self, v): return self.distTo[v] < INF def pathTo(self, v): if not self.hasPathTo(v): return None path = Stack1() # 堆栈后进先出LIFO e = self.edgeTo[v] while not e == None: path.push(e) e = self.edgeTo[e.from1()] # 一个个从指定顶点追回整个路径 return path
def DijkstraSP(self, s): self.setInfo('source',s) # source node self.setNodes('dis',Search.INFTY) # dis[v] = distance of shortest s->v path self.setNodes('back',None) # back[v] = last edge on shortest s->v path self.setNode(s,'dis',0.0) n = len(self._nodes) # relax vertices in order of distance from s pq = IndexMinPQ(n) pq.insert(s, self.getNode(s,'dis')) while not pq.isEmpty(): v = pq.delMin() for e in self.outStar(v): self.relax(pq,v,e)
def __init__(self, itemList=[]): self.ipqMin = IndexMinPQ.IndexMinPQ(itemList) self.ipqMax = IndexMaxPQ.IndexMaxPQ(itemList)