def BFS(self, v): """ Runs breadth-first search from the source vertex v. Returns a matrix with distances from v, or None if v does not exist. """ if v >= self.vertexCount(): return None # can't be run; vertex doesn't exist # initialize the vertex queue, the "visited" set s, and the distances from v q = MinHeap() s = set() distance = [float("inf")] * self.vertexCount() # insert the source vertex into q and set its distance as 0 q.insert(KeyValuePair(0, v)) s.add(v) distance[v] = 0 # loop through all vertices, in order of distance from v, relaxing edges out of current vertex kvp = q.extractMin() while kvp: v = kvp.value d = kvp.key # relax all edges out of current vertex v edges = self._adj[v] for e in edges: if e.dest not in s: s.add(e.dest) distance[e.dest] = d + 1 q.insert(KeyValuePair(distance[e.dest], e.dest)) # get next-lowest distance vertex kvp = q.extractMin() return distance
def test1(): minhp = MinHeap() for idx in range(10): minhp.add(idx + 1) minhp.add(20 - idx) minhp.printer() for idx in range(20): print minhp.extractMin()
def test2(): minhp = MinHeap() print minhp.isEmpty() minhp.add(1) minhp.add(1) minhp.add(1) minhp.printer() print minhp.extractMin() print minhp.extractMin()
def heapFullTest(): """ Various tests for Heap class, using both MinHeaps and MaxHeaps, including sorting and random operations. """ print("Testing MinHeap: sorting") for i in range(1, 21): if heapRandomSort(250, True): print "Test", i, "successful" else: print "Test", i, "failed" print("\nTesting MaxHeap: sorting") for i in range(1, 21): if heapRandomSort(250, False): print "Test", i, "successful" else: print "Test", i, "failed" print("\nTesting MinHeap: general") for i in range(1, 21): if heapRandomTest(250, True): print "Test", i, "successful" else: print "Test", i, "failed" print("\nTesting MaxHeap: general") for i in range(1, 21): if heapRandomTest(250, False): print "Test", i, "successful" else: print "Test", i, "failed" print("\nTesting MinHeap: other operations") ar = [1, 4, 501, -200, 32, 7, 65, -1, 20000, -34, 17] min_heap = MinHeap() min_heap.createMinHeap(ar) print min_heap.extractMin() print min_heap.extractMin() print min_heap.extractMin() max_heap = MaxHeap() max_heap.createMaxHeap(ar) print max_heap.extractMax() print max_heap.extractMax() print max_heap.extractMax() print "Max: ar", max( ar), "min_heap", min_heap.maximum(), "max_heap", max_heap.maximum() print "Min: ar", min( ar), "min_heap", min_heap.minimum(), "max_heap", max_heap.minimum()
class Calendar(object): def __init__(self): self.events = MinHeap() self.mapper = {} def add(self, record): self.events.add(record.timestamp) if record.timestamp in self.mapper: self.mapper[record.timestamp].append(record) else: self.mapper[record.timestamp] = [record] def getlatest(self): rec_time = self.events.extractMin() rec_list = self.mapper[rec_time] # Logic here decides how to handle simultaneous events. # Current Implementation - extract departure events first record = None index = -1 for idx in range(len(rec_list)): if rec_list[idx].event_type == "E": index = idx break if index < 0: record = rec_list.pop(0) else: record = rec_list.pop(index) if not len(rec_list): del self.mapper[rec_time] return record
def Dijkstra(self, v): """ Runs Dijkstra shortest-path algorithm from source v, returning the list of distances from v, or None if any edges are negative-weight. """ if self.hasNegativeWeight(): return None # Dijkstra may not run properly if any weights are negative # initialize the queue of vertices, the set s of "seen" or complete vertices, and the d list of current distances q = MinHeap() s = set() d = [float("inf")] * self.vertexCount() d[v] = 0 # insert source vertex in q; could insert all if using decreaseKey, but a basic Heap implementation doesn't support that q.insert(KeyValuePair(d[v], v)) # loop through each edge vertex once, in order of distance from v, relaxing all edges out of that vertex kvp = q.extractMin() while kvp: vert = kvp.value dist = kvp.key # only relax edges from a vertex if it has not already been visited if vert not in s: s.add(vert) # relax edges to vertices not already completed for e in self._adj[vert]: if e.dest not in s: if d[vert] + e.weight < d[e.dest]: d[e.dest] = d[vert] + e.weight q.insert( KeyValuePair(d[e.dest], e.dest) ) # could also decrease key, but would not work with basic Heap implementation # retrieve the next-lowest distance vertex kvp = q.extractMin() return d