def testHeapify(self): size = 100 array = [random.randrange(size) for i in range(size)] copy = sorted(array) heapify(array) self.checkHeapProperty(array) for elem in copy: self.assertEqual(heappop(array), elem)
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 testHeappush(self): size = 100 array = [random.randrange(size) for i in range(size)] copy = sorted(array) heap = [] for elem in array: heappush(heap, elem) self.checkHeapProperty(heap) for elem in copy: self.assertEqual(heappop(heap), elem)
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 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
def rankASs(distances, min_transcripts=3): from heap import heap, heappop # @UnresolvedImport distance_list = [] contig_list = [] for transcript in distances.keys(): contigs = list(distances[transcript].keys()) if len(contigs) >= min_transcripts: for i in range(len(contigs)): sum_dist = 0 for j in range(len(contigs)): if i != j: sum_dist += distances[transcript][contigs[i]][ contigs[j]] contig_list.append(transcript + '_' + contigs[i]) distance_list.append(sum_dist / (len(contigs) - 1)) heap_distances, heap_contigs = heap(distance_list, contig_list) return heappop(heap_distances, heap_contigs)
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
class Find_Near(): def __init__(self): self.point_list = [] def add_point(self, Point): self.point_list.append(Point) 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 nearest = [] for i in range(p): nearest.append(heappop(heap)[1]) return nearrest