def test_sort(): ''' Test sorting (heap sort) of priority queues. ''' q = PriorityQueue([5, 1, 2, 4, 6, 3]) assert q == [1, 4, 2, 5, 6, 3] assert q.sort() == [1, 2, 3, 4, 5, 6]
def dijkstra(graph, start, end=None): nodes = graph.get_nodes() # initialize distance dictionary dist = {node: float('Inf') for node in nodes} dist[start] = 0 # initialize predecessor dictionary pred = {node: None for node in nodes} # initialize prio queue for "unvisited nodes nodes_nonfinal = PriorityQueue() for node in nodes: nodes_nonfinal.add_task(task=node, priority=dist[node]) # main computation loop while nodes_nonfinal.not_empty(): u = nodes_nonfinal.pop_task() for adj in graph.get_node_neighbours(u): if nodes_nonfinal.contains_task(adj): temp = dist[u] + float(graph.get_default_weights((u, adj))[0]) if temp < dist[adj]: pred[adj] = u nodes_nonfinal.add_task(task=adj, priority=temp) # if an end node was specified, the corresponding shortest path shall be # computed and displayed if end is not None: path, path_sum = shortest_path(graph, pred, end) #print '#' * 50 #print 'Path: ', path #print 'Weight: ', path_sum return path else: get_shortest_path_tree(graph, pred, start)
def __init__(self): self._lock = threading.Lock() self._modified = threading.Condition(self._lock) self._queue = PriorityQueue() self._next_id = 1 self._should_stop = False self._loop_thread = ProfiledThread(target=self._the_loop, name_prefix='DelayedExecutor') self._loop_thread.start()
def __init__(self, input_maze=None): is_text = type(input_maze) == str if is_text: converter = TextToMatrixMazeConverter(input_maze) converter = MazeToGraphConverter(converter.maze, converter.start_cell) else: converter = MazeToGraphConverter(input_maze) self.vertices = converter.graph_vertices self.edge_weights = converter.graph_edge_weights self.adjacency_list = converter.graph_adjacency_list self.edge_weights = converter.graph_edge_weights self.start_v = converter.start_cell self.exit_v = converter.exit_cell self.dist = dict() self.prev = dict() self.queue = PriorityQueue() self.shortest_path = self.shortest_exit_path()
def main(self, s): self.dists[s] = 0 pq = PriorityQueue() for u in range(self.n): pq.insert(float('inf'), u) pq.decrease_key(s, 0) while pq: _, u = pq.extract() for v in self.G[u]: if self.relax(u, v, self.G[u][v]): pq.decrease_key(v, self.dists[v])
def huffman(C): n = len(C) Q = PriorityQueue(C, MinHeap) for i in range(n - 1): x = Q.extract_min() y = Q.extract_min() z = Node(x.key() + y.key()) z.left = x z.right = y Q.insert(z) return Q.extract_min()
def prim(G): n = G.num_vertices() parents = [-1] * n pq = PriorityQueue() for u in G: pq.insert(float('inf'), u) pq.decrease_key(u, 0) while pq: _, u = pq.extract() for v in G[u]: if v in pq and G[u][v] < pq[v]: parents[v] = u pq.decrease_key(v, G[u][v]) mst = set() for u, v in enumerate(parents): if v >= 0: mst.add((u, v)) return mst
def add(self, obj, tm=None): if isinstance(obj, tuple): obj, tm = obj if obj not in self: PriorityQueue.add(self, obj, tm or time.time())
def add(self, obj, value, tm=None): if obj not in self: PriorityQueue.add(self, obj, (tm or time.time(), value))
class DelayedExecutor(object): def __init__(self): self._lock = threading.Lock() self._modified = threading.Condition(self._lock) self._queue = PriorityQueue() self._next_id = 1 self._should_stop = False self._loop_thread = ProfiledThread(target=self._the_loop, name_prefix='DelayedExecutor') self._loop_thread.start() def _cancel(self, id): with self._lock: if id not in self._queue: # because of user-space-race with _the_loop return False if self._queue.front()[0] == id: self._modified.notify() self._queue.pop_by_key(id) return True def add(self, callback, deadline=None, timeout=None): if timeout is not None: deadline = time.time() + timeout elif deadline is None: raise ValueError("You must specify deadline or timeout") with self._lock: id = self._next_id self._next_id += 1 self._queue.add(id, (deadline, callback)) if self._queue.front()[0] == id: self._modified.notify() ret = lambda : self._cancel(id) # TODO use weak self ret.id = id return ret schedule = add def stop(self): with self._lock: self._should_stop = True self._modified.notify() self._loop_thread.join() def _the_loop(self): while not self._should_stop: with self._lock: while not(self._should_stop or self._queue): self._modified.wait() if self._should_stop: return id, (deadline, callback) = self._queue.front() now = time.time() if now < deadline: self._modified.wait(deadline - now) if self._should_stop: return if not self._queue or self._queue.front()[0] != id: continue self._queue.pop_front() try: if callback.__code__.co_argcount: callback(id) else: callback() except: logging.exception("Failed to execute %s" % callback) del callback
def prim(graph, start_node): queue = PriorityQueue() parent = {} mst = [] mst_sum = 0 for node in graph.get_nodes(): queue.add_task(task=node, priority=float('Inf')) parent[node] = None # put first node in the queue queue.add_task(task=start_node, priority=0) while queue.not_empty(): cheapest_node = queue.pop_task() if parent[cheapest_node] is not None: temp_weight = float(graph.get_default_weights((cheapest_node, parent[cheapest_node]))[0]) mst.append((temp_weight, (cheapest_node, parent[cheapest_node]))) mst_sum += temp_weight for adj_node in graph.get_node_neighbours(cheapest_node): edge_weight = float(graph.get_default_weights((cheapest_node, adj_node))[0]) if queue.contains_task(adj_node) and edge_weight < queue.get_priority(adj_node): parent[adj_node] = cheapest_node queue.add_task(task=adj_node, priority=edge_weight) print "Prim Weight: ", mst_sum return mst
def add(self, pck): if pck not in self: PriorityQueue.add(self, pck, getattr(pck, priorAttr, 0))
from heap import Node, PriorityQueue q = PriorityQueue([5, 4, 3]) def test_basic(): assert q.min == 3 assert q.size == 3 assert q == [3, 5, 4] def test_insert(): ''' Testing `insert` method, which inserts a new element into the queue and reorders the underlying MinHeap if necessary. ''' q.insert(2) assert q.min == 2 assert q.size == 4 assert q == [2, 3, 4, 5] q.insert(1) assert q.min == 1 assert q.size == 5 assert q == [1, 2, 4, 5, 3] def test_relations(): ''' Testing parent and child relations among elements.
self.g = g # list of event secuences Ids class PriorityQueue_tuple(object): def __init__(self, l, c_, ci, cj): self.l = l self.c_ = c_ self.ci = ci self.cj = cj ############### containers #####################3 cluster_list = dict() ### Stores the clusters formed priorityQueue_dict = dict( ) ### Maps the priorityQueue elements to Tuple objects q = PriorityQueue([]) ### Priority Queue #############//////////////################## def add_cluster(c): global counter_cluster_id cluster_list[counter_cluster_id] = c counter_cluster_id += 1 return counter_cluster_id - 1 def remove_cluster(ckey): cluster_list.pop(ckey) ## check def getNextLabel():
class DijkstrasMazeSolver(object): """Gets shortest maze exit path with help of Dijkstras algorithm. It converts 0/1 matrix maze to edge weighted graph representation (due to MazeToGraphConverter) and then finds shortest path. Also it uses Heap data structure to quickly get minimums. """ infinity = 10 ** 10 def __init__(self, input_maze=None): is_text = type(input_maze) == str if is_text: converter = TextToMatrixMazeConverter(input_maze) converter = MazeToGraphConverter(converter.maze, converter.start_cell) else: converter = MazeToGraphConverter(input_maze) self.vertices = converter.graph_vertices self.edge_weights = converter.graph_edge_weights self.adjacency_list = converter.graph_adjacency_list self.edge_weights = converter.graph_edge_weights self.start_v = converter.start_cell self.exit_v = converter.exit_cell self.dist = dict() self.prev = dict() self.queue = PriorityQueue() self.shortest_path = self.shortest_exit_path() def shortest_exit_path(self): self.count_shortest_paths(self.start_v, self.exit_v) length = self.dist[self.exit_v] path = self.get_path(self.exit_v) return path, length def get_path(self, v, path=()): path = (v,) + path if self.prev[v] and v != self.start_v: return self.get_path(self.prev[v], path) else: return path def count_shortest_paths(self, source, dest): self.dist[source] = 0 self.prev[source] = None for v in self.vertices: if v != source: self.dist[v] = self.infinity self.prev[v] = None else: self.queue.insert((v, self.dist[v])) while self.queue: u = self.queue.pop_min()[0] if u == dest: break for v in self.adjacency_list[u]: edge = frozenset((u, v)) alt = self.dist[u] + self.edge_weights[edge] if alt < self.dist[v]: self.dist[v] = alt self.prev[v] = u if v not in self.queue: self.queue.insert((v, alt))
def test_sanity(self): ilist = [2, 4, 1, 9, 7, 5, 3, 2, 3] olist = [1, 2, 2, 3, 3, 4, 5, 7, 9] # heap test from heap import Heap h = Heap(deepcopy(ilist)) h.sanity() self.assertListEqual(h.sort(), olist) # min heap h = Heap(deepcopy(ilist), htype='min') h.sanity() self.assertListEqual(h.sort(), olist[::-1]) # big array test array = range(500) random.shuffle(array) h = Heap(array) h.sanity() self.check_sorted(h.sort()) # priority queue from heap import PriorityQueue q = PriorityQueue(deepcopy(ilist)) q.sanity() # get top test top = q.find_top() self.assertEqual(top, max(ilist)) self.assertEqual(top, q.pop_top()) q.sanity() self.assertNotEqual(top, q.find_top()) # update key test q.update_key(3, 10) # upwards q.sanity() q.update_key(8, 0) # downwards q.sanity() # insert key test q.insert_key(15) q.sanity() q.insert_key(-1) q.sanity() q.insert_key(4) q.sanity()
#@ <int> cj: Id of Cluster j class PriorityQueue_tuple(object): def __init__(self, l, c_, ci, cj): self.l = l self.c_ = c_ self.ci = ci self.cj = cj ############### containers ##################### # Maps Ids to clusters cluster_list = dict() # Maps PriorityQueue elements to PriorityQueue_Tuple Objects priorityQueue_dict = dict() # Main Priority Queue for MinDL algorithm <heap> q = PriorityQueue([]) # -- Adds a new cluster (c) to cluster_list -- #@<Cluster> c: new cluster object #@<int> returns: new cluster's id def add_cluster(c): global counter_cluster_id cluster_list[counter_cluster_id] = c counter_cluster_id += 1 return counter_cluster_id - 1 # -- Removes the cluster identified by Id cKey from cluster_list -- #@<int> ckey: Id or key of cluster c def remove_cluster(ckey):
def add(self, key, value, t=None): if key not in self: PriorityQueue.add(self, key, (t or time.time(), value))
def test_node_heaping(): ''' Test priority queues with nodes as elements. ''' a = Node(label='a', msg="boom!", priority=1) b = Node(label='b', msg="hi", priority=2) c = Node(label='c', msg="ok", priority=3) d = Node(label='d', msg="oh", priority=4) q = PriorityQueue([b, c, d]) assert q.min == b assert q.min.msg == 'hi' assert q.min.label == 'b' assert q == [b, c, d] q.insert(a) assert q.min == a assert q.min.msg is 'boom!' assert q.min.label == 'a' assert q == [a, b, d, c] assert q.delete('c') == c assert q.sort() == [a, b, d] assert q.min == a assert q.min.label == 'a' min = q.shift() assert min == a assert min.label == 'a' assert q.sort() == [b, d] assert q.min == b assert q.min.label == 'b' q = PriorityQueue([d, c, b, a]) assert [a, b, c, d] == q.sort() assert [a, b, c, d] == [q.shift() for x in range(q.size)] assert q.size == 0 assert q == [] from itertools import permutations nodes = [a, b, c, d] for perm in permutations(nodes): q = PriorityQueue(perm) assert [a, b, c, d] == q.sort() assert [a, b, c, d] == [q.shift() for x in range(q.size)] assert q.size == 0 assert q == []
class DijkstrasMazeSolver(object): """Gets shortest maze exit path with help of Dijkstras algorithm. It converts 0/1 matrix maze to edge weighted graph representation (due to MazeToGraphConverter) and then finds shortest path. Also it uses Heap data structure to quickly get minimums. """ infinity = 10**10 def __init__(self, input_maze=None): is_text = type(input_maze) == str if is_text: converter = TextToMatrixMazeConverter(input_maze) converter = MazeToGraphConverter(converter.maze, converter.start_cell) else: converter = MazeToGraphConverter(input_maze) self.vertices = converter.graph_vertices self.edge_weights = converter.graph_edge_weights self.adjacency_list = converter.graph_adjacency_list self.edge_weights = converter.graph_edge_weights self.start_v = converter.start_cell self.exit_v = converter.exit_cell self.dist = dict() self.prev = dict() self.queue = PriorityQueue() self.shortest_path = self.shortest_exit_path() def shortest_exit_path(self): self.count_shortest_paths(self.start_v, self.exit_v) length = self.dist[self.exit_v] path = self.get_path(self.exit_v) return path, length def get_path(self, v, path=()): path = (v, ) + path if self.prev[v] and v != self.start_v: return self.get_path(self.prev[v], path) else: return path def count_shortest_paths(self, source, dest): self.dist[source] = 0 self.prev[source] = None for v in self.vertices: if v != source: self.dist[v] = self.infinity self.prev[v] = None else: self.queue.insert((v, self.dist[v])) while self.queue: u = self.queue.pop_min()[0] if u == dest: break for v in self.adjacency_list[u]: edge = frozenset((u, v)) alt = self.dist[u] + self.edge_weights[edge] if alt < self.dist[v]: self.dist[v] = alt self.prev[v] = u if v not in self.queue: self.queue.insert((v, alt))