def find_nearest(self, center, p): if len(self.point_list) < p: raise 'Not enough points stored' elif len(self.point_list) == p: return self.point_list heap = [] for point in self.point_list: distance = self.calculate_distance(point, center) heappush(heap, (distance, point)) distance , point
def maxGasStationDistance(stations, K): distances = [] n = len(stations) for i in range(n-1): d = stations[i+1] - stations[i] heappush(distances, [-d, 1, d]) for j in range(K): item = heappop(distances) prio, num, curr = item num += 1 heappush(distances, [-(curr/num), num, curr]) return -distances[0][0]
def sortGraph(graph): indegree = {node: 0 for node in graph} for c in graph: for neighbor in graph[c]: indegree[neighbor] += 1 q = [node for node in graph if indegree[node] == 0] heapify(q) tpOrder = '' while q: cur = heappop(q) tpOrder = tpOrder + cur for nc in graph[cur]: indegree[nc] = indegree[nc] - 1 if indegree[nc] == 0: heappush(tpOrder, nc) if len(tpOrder) == len(graph): return True return False
def get_shortest_path(start, end, vlist): # distances使用字典的方式保存每一个顶点到start点的距离 distances = {} # 从start到某点的最优路径的前一个结点 # eg:start->B->D->E,则previous[E]=D,previous[D]=B,等等 previous = {} # 用来保存图中所有顶点的到start点的距离的优先队列 # 这个距离不一定是最短距离 nodes = [] for vertex in vlist: if vertex == start: distances[vertex] = 0 # 将start点的距离初始化为0 heap.heappush(nodes, [0, vertex]) # nodes.append([0,vertex]) elif vertex in g.neighboringVertices(start): distances[vertex] = length(start, vertex) # 把与star点相连的结点距离start点的距离初始化为对应的道路长度 heap.heappush(nodes, [length(start, vertex), vertex]) # nodes.append([length(start, vertex), vertex]) previous[vertex] = start else: distances[vertex] = 9999 # 把与start点不直接连接的结点距离start的距离初始化为9999 heap.heappush(nodes, [9999, vertex]) # nodes.append([9999, vertex]) previous[vertex] = None shortest_path = [1] lenPath = 0 while nodes: smallest = heap.heappop(nodes)[1] # 取出队列中最小距离的结点 # smallest = nodes.pop()[1] if smallest == end: shortest_path = [] lenPath = distances[smallest] temp = smallest #print('previous[temp]:',temp) while (temp != start) : shortest_path.append(temp) temp = previous[temp] shortest_path.append(temp) # 将start点也加入到shortest_path中 if distances[smallest] == 9999: # 所有点不可达 break # 遍历与smallest相连的结点,更新其与结点的距离、前继节点 for neighbor in g.neighboringVertices(smallest): dis = distances[smallest] + length(smallest, neighbor.getLabel()) if dis < distances[neighbor.getLabel()]: distances[neighbor.getLabel()] = dis previous[neighbor.getLabel()] = smallest # 更新与smallest相连的结点的前继节点 for node in nodes: if node[1] == neighbor.getLabel(): node[0] = dis # 更新与smallest相连的结点到start的距离 break heap.heapify(nodes) return shortest_path, lenPath
def get_shortest_path(start, end, vlist): distances = {} previous = {} nodes = [] for vertex in vlist: if vertex == start: distances[vertex] = 0 heap.heappush(nodes, [0, vertex]) elif vertex in h.neighboringVertices(start): distances[vertex] = length2(start, vertex) heap.heappush(nodes, [length2(start, vertex), vertex]) previous[vertex] = start else: distances[vertex] = inf heap.heappush(nodes, [inf, vertex]) previous[vertex] = None shortest_path = [1] lenPath = 0 while nodes: smallest = heap.heappop(nodes)[1] # smallest = nodes.pop()[1] if smallest == end: shortest_path = [] lenPath = distances[smallest] temp = smallest while (temp != start): shortest_path.append(temp) temp = previous[temp] shortest_path.append(temp) if distances[smallest] == inf: break for neighbor in h.neighboringVertices(smallest): dis = distances[smallest] + length2(smallest, neighbor.getLabel()) if dis < distances[neighbor.getLabel()]: distances[neighbor.getLabel()] = dis previous[neighbor.getLabel()] = smallest for node in nodes: if node[1] == neighbor.getLabel(): node[0] = dis break heap.heapify(nodes) return shortest_path, lenPath
medians = [stream[0], small[0]] for a in stream[2:]: # if k is odd, then mk is ((k+1)/2)th smallest number if len(small) == len(large): if a < max(small): medians.append(max(small)) heap._heappush_max(small, a) elif a > min(large): medians.append(min(large)) largemin = heap.heappushpop(large, a) heap._heappush_max(small, largemin) else: medians.append(a) heap._heappush_max(small, a) # if k is even, then mk is the (k/2)th else: if a < max(small): m1 = heap._heappushpop_max(small, a) heap.heappush(large, m1) medians.append(max(small)) elif a > min(large): medians.append(max(small)) heap.heappush(large, a) else: medians.append(max(small)) heap.heappush(large, a) print "result: ", sum(medians)