def shortest_path(graph, sourceVertex): min_heap = PriorityQueue(True) distance = {} parent = {} for vertex in graph.all_vertex.values(): min_heap.add_task(sys.maxsize, vertex) min_heap.change_task_priority(0, sourceVertex) distance[sourceVertex] = 0 parent[sourceVertex] = None while min_heap.is_empty() is False: task = min_heap.peek_task() weight = min_heap.get_task_priority(task) current = min_heap.pop_task() distance[current] = weight for edge in current.edges: adjacent = get_other_vertex_for_edge(current, edge) if min_heap.contains_task(adjacent) is False: continue new_distance = distance[current] + edge.weight; if min_heap.get_task_priority(adjacent) > new_distance: min_heap.change_task_priority(new_distance, adjacent) parent[adjacent] = current return distance
def minimum_spanning_tree(graph): min_heap = PriorityQueue(True) vertex_to_edge = {} result = [] for vertex in graph.all_vertex.values(): min_heap.add_task(sys.maxsize, vertex) start_vertex = next(iter((graph.all_vertex.values()))) min_heap.change_task_priority(0, start_vertex) while min_heap.is_empty() is False: current = min_heap.pop_task() if (current in vertex_to_edge): spanning_tree_edge = vertex_to_edge[current] result.append(spanning_tree_edge) for edge in current.edges: adjacent = get_other_vertex_for_edge(current, edge) if min_heap.contains_task( adjacent) is True and min_heap.get_task_priority( adjacent) > edge.weight: min_heap.change_task_priority(edge.weight, adjacent) vertex_to_edge[adjacent] = edge return result
def print_ordered(self): pq = PriorityQueue() for n in self.d: prob = self.d[n] pq.add_task(n, prob) #endfor sorted = pq.sorted_queue() norms = [x for x in reversed(sorted)] for n in norms: print n, self.d[n]
def most_probable_norms(self, topN): """Computes the topN most probable norms within a suite, returning either a single norm (if there is a unique most likely norm) or all norms tied at the top""" pq = PriorityQueue() for n in self.d: prob = self.d[n] pq.add_task(n, prob) #endfor sorted_norms = pq.sorted_queue() norms = [x for x in reversed(sorted_norms)] # Check that we select either the topN, or the first ones with the same odds for (i,n) in enumerate(norms): if(i+1 == topN): tied = len(norms) > i+1 and (self.d[n] == self.d[norms[i+1]]) if (tied): topN += 1 # print "Tie between norms %s and %s with prob=%d, topN is now %d" % (n,norms[i-1],self.d[n],topN) else: break #endfor return (norms[0:topN],topN)
def most_probable_norms(self, topN): """Computes the topN most probable norms within a suite, returning either """ pq = PriorityQueue() for n in self.d: prob = self.d[n] pq.add_task(n, prob) #endfor sorted = pq.sorted_queue() norms = [x for x in reversed(sorted)] # Check that we select either the topN, or the first ones with the same odds i = 1 for n in norms[1:]: tied = (self.d[n] == self.d[norms[i - 1]]) if (i > topN): if (tied): topN += 1 # print "Tie between norms %s and %s with prob=%d, topN is now %d" % (n,norms[i-1],self.d[n],topN) else: break i += 1 #endfor return (norms[0:topN], topN)