def driver(): heapA = MinHeap() heapB = MinHeap() dictA = {} dictB = {} with open(sys.argv[1]) as f: n = int(f.readline().strip()) for _ in range(n): in_data = f.readline().strip().split() ip, tier, time = in_data[0], in_data[1], int(in_data[2]) if tier == "A": heapA.insert(time) if time not in dictA.keys(): dictA[time] = [] dictA[time].append(ip) elif tier == "B": heapB.insert(time) if time not in dictB.keys(): dictB[time] = [] dictB[time].append(ip) serve_requests(heapA, dictA) serve_requests(heapB, dictB)
def test_peek_at_smallest(self): numbers = [11, 322, 3, 199, 29, 7, 1, 18, 76, 4, 2, 47, 123] h = MinHeap(numbers) self.assertEqual(h.peek(), 1) self.assertEqual(len(h), len(numbers)) self.assertEqual(len(numbers), 13) i = MinHeap(BIG_NUMBERS) self.assertEqual(i.peek(), 46) self.assertEqual(len(i), len(BIG_NUMBERS))
def test_pop_from_heap(self): numbers = [11, 322, 3, 199, 29, 7, 1, 18, 76, 4, 2, 47, 123] h = MinHeap(numbers) self.assertEqual(h.pop(), 1) self.assertEqual(len(h), len(numbers)-1) self.assertEqual(h.pop(), 2) self.assertEqual(h.pop(), 3) self.assertEqual(h.pop(), 4) self.assertEqual(len(h), len(numbers)-4) self.assertEqual(h.pop(), 7) self.assertEqual(h.pop(), 11) self.assertEqual(len(h), len(numbers)-6) i = MinHeap(BIG_NUMBERS) self.assertEqual(i.pop(), 46)
def __init__(self, N, policy='SJF'): self.N = N # number of timesteps to schedule self.running = None self.clock = 0 # the current timestep being scheduled self.policy = policy # instantiate the readyQueue, which may be a FIFO or MinHeap self.readyQuene = MinHeap() # you may need additional queues for # - tasks that have been added but not released yet # - tasks that have been completed # - the Gantt chart self.notReadyQuene = MinHeap() self.completed_quene = MinHeap() self.ganttChart = MinHeap()
def dijkstra(graph, s, e): """Simple dijkstra algo to work out shortest path to an end node""" pq = MinHeap() # Set the distance for the start node to zero then build heap s.set_distance(0) pq.build_heap([(v.get_distance(),v) for v in graph]) while not pq.is_empty(): cv = pq.del_min() current_v = cv[1] current_v.set_as_visited() if current_v.get_key() == e.get_key(): break else: for v in current_v.adj: new_dist = current_v.get_distance() + current_v.get_weight(v) if new_dist < v.get_distance(): v.set_distance(new_dist) v.set_pred(current_v) pq.decrease_val(v, new_dist) # if queue empties before end is found it means there are no paths from source to end if pq.is_empty() == True: print 'No path to exists' else: shortest(e)
def dsp(graph: list, s, t) -> int: """ Find shortest path from two vertices via Dijkstra's shortest-path algorithm. Args: s: source vertex t: the target vertex Returns: Shortest path from `s` to `t` vertices """ if s == t: return 0 # Vertices seen so far visited = [] heap = MinHeap([(10000000, v) for v in range(1, len(graph))]) heap.update_min(0, s) while not heap.is_empty(): dist, u = heap.pop() visited.append(u) if u == t: return dist for v, d in graph[u]: if v not in visited: heap.update_min(dist + d, v)
class TestMinHeap(unittest.TestCase): min_heap = MinHeap() def test_a_empty_heap(self): with self.assertRaises(IndexError): self.min_heap.peek() def test_b_add(self): self.min_heap.add(10) self.min_heap.add(4) self.min_heap.add(15) self.assertEqual(4, self.min_heap.peek()) self.assertListEqual([4, 10, 15, 0, 0, 0, 0, 0, 0, 0], self.min_heap._heap) def test_c_remove(self): self.min_heap.remove() self.assertListEqual([10, 15, 15, 0, 0, 0, 0, 0, 0, 0], self.min_heap._heap) def test_d_add_remove(self): self.min_heap.add(20) self.min_heap.add(0) self.min_heap.add(30) self.assertListEqual([0, 10, 20, 15, 30, 0, 0, 0, 0, 0], self.min_heap._heap) self.min_heap.remove() self.min_heap.remove() self.min_heap.add(2) self.min_heap.add(4) self.min_heap.add(1) self.min_heap.add(-3) self.assertEqual(-3, self.min_heap.peek()) self.assertListEqual([-3, 4, 1, 30, 15, 20, 2, 0, 0, 0], self.min_heap._heap)
def __init__(self, filename): self.H1 = MaxHeap([]) self.H2 = MinHeap([]) self.median = [] self.input_stream = [] self.load(filename) self.feed()
def setUp(self): vals = list(range(1, 1000)) random.shuffle(vals) self.H = MinHeap() '''insert random values into structure''' for i in vals: self.H.insertNode(i, i)
def test_push_onto_heap(self): numbers = [11, 322, 3, 199, 29, 7, 1, 18, 76, 4, 2, 47, 123] i = MinHeap(BIG_NUMBERS) i.push(17) self.assertEqual(i.peek(), 17) i.push(24) self.assertEqual(i.pop(), 17) self.assertEqual(i.pop(), 24) self.assertEqual(i.pop(), 46) h = MinHeap(numbers) h.push(6) self.assertEqual(len(h), len(numbers)+1) self.assertEqual(h.pop(), 1) self.assertEqual(h.pop(), 2) self.assertEqual(h.pop(), 3) self.assertEqual(h.pop(), 4) self.assertEqual(h.pop(), 6)
def dijkstra(aGraph, start, queue="FibHeap"): # Set the distance for the start node to zero start.set_distance(0) # Put the tuple pair into the priority queue unvisited_queue = [(v.get_distance, v) for v in aGraph] pyheap = False #building and rebuilding this queue is different can eventually just change background code minheap = False if queue == "FibHeap": print("Using Fiobonnaci Heap Structure...\n") # Initialize new FibHeap obj = FibHeap() elif queue == "MinHeap": #print("Using Heapq Data Structure From Python...\n") minheap = True obj = MinHeap() elif queue == "Binomial": #print("Using Binomial Heap Structure...\n") obj = BinomialHeap() #Build the heap for index, item in enumerate(unvisited_queue): obj.insertNode(item[1].get_distance(), item) for next in item[1].adjacent: obj.insertNode(item[1].get_weight(next) + item[1].get_distance(), next) #store in tree based on weights while (len(unvisited_queue)): min, key = obj.extractMin() current = key[1] current.set_visited() #set vertex to visited # now visit adj nodes for next in current.adjacent: #if already visited just skip if next.visited: continue new_dist = current.get_distance() + current.get_weight(next) #check if new_dist is smaller if new_dist < next.get_distance(): next.set_distance(new_dist) next.set_previous(current) #print( 'updated : current = %s next = %s new_dist = %s' % (current.get_id(), next.get_id(), next.get_distance())) # Empty and Rebuild heap obj.emptyHeap() unvisited_queue = [(v.get_distance, v) for v in aGraph if not v.visited] for index, item in enumerate(unvisited_queue): obj.insertNode(item[1].get_distance(), item) for next in item[1].adjacent: obj.insertNode(item[1].get_weight(next) + item[1].get_distance(), next) #store in tree based on weights
def test_key_function(self): fruits = ['Watermelon', 'blueberry', 'lime', 'Lemon', 'pear', 'loquat'] fruits_heap = MinHeap(fruits, key=str.lower) self.assertEqual(fruits_heap.peek(), 'blueberry') fruits_heap.push('Apple') fruits_heap.push('jujube') self.assertEqual(fruits_heap.pop(), 'Apple') self.assertEqual(fruits_heap.pop(), 'blueberry') self.assertEqual(fruits_heap.pop(), 'jujube') self.assertEqual(fruits_heap.pop(), 'Lemon')
def __init__(self, eval_functions): """ Initialize the MinHeap and the evaluation functions. """ self.trees = [] self.heaps = [] self.clauseDeleted = [] self.eval_functions = eval_functions self.counter = 0 for i in range(0, len(eval_functions.eval_descriptor)): self.trees.append(MinHeap())
def make_tree(freq_table): trees = MinHeap() trees.add(1, TreeLeafEndMessage()) for (symbol, freq) in freq_table.items(): trees.add(freq, TreeLeaf(symbol)) while len(trees) > 1: (rfreq, right) = trees.pop_min() (lfreq, left) = trees.pop_min() trees.add(lfreq + rfreq, TreeBranch(left, right)) (totalfreq, tree) = trees.pop_min() return tree
def get_graph_from_file(filename): graph_heap = MinHeap() with open(filename, 'r') as f: for line in f: node = Node() split_line = line.split() node.g_num = int(split_line[0]) #index label keeps track of where this node is in the heap's array node.index_label = graph_heap.size for edge in split_line[1:]: target, length = edge.split(',') node.edges.append({'target':int(target), 'length':int(length)}) graph_heap.insert(node) return graph_heap
def prim(G): h = MinHeap() G.cost[0] = 0 ll = [node(i) for i in range(G.V)] heapq.heapify(ll) T = [] while len(ll) > 0: u = heapq.heappop(ll).v for v in G.adj[u]: if G.cost[v] > G.weight[u][v]: G.cost[v] = G.weight[u][v] G.prev[v] = u print(len(ll)) heapq.heapify(ll) # for i in range(G.V-heapq.): # heapq.heappop() T = [] for i in range(G.V): if G.prev[i] is not None: T.append((G.prev[i],i)) print(T) # def prim(G): # H = MinHeap() # G.cost[0] = 0 # H.build_heap([node(i) for i in range(G.V)]) # while H.size > 0: # u = H.extract_min().v # print("min: ", u,end=": ") # for v in G.adj[u]: # if G.cost[v] > G.weight[u][v]: # print(v, end=", ") # G.prev[v] = u # G.cost[v] = G.weight[u][v] # #H.updatePriority(v) # # print('\n',H.heap) # H.build_heap([node(i) for i in range(G.V)]) # print() # T = [] # for i in range(G.V): # if G.prev[i] is not None: # T.append((G.prev[i],i)) return T
def test_faster_than_sorting(self): with Timer() as sort_timer: sorted(MANY_BIG_NUMBERS) heap = MinHeap(MANY_BIG_NUMBERS) with Timer() as min_heap_timer: heap.push(150) heap.push(950) heap.push(400) heap.push(760) heap.push(280) heap.push(870) heap.push(330) heap.push(1000) heap.push(50) heap.push(500) items = [heap.pop() for _ in range(10)] self.assertEqual(len(items), 10) self.assertLess(min_heap_timer.elapsed, sort_timer.elapsed)
def find_path_astar(start, stop): visited = [] unvisited = [start] parent = {start: "None"} gscore = {start: 0} #fscore = {start:1/links_in_common(start,stop)} fscore = MinHeap() fscore.insert((start, distance_heuristic(start, stop))) #print(fscore) global article_data while len(unvisited) > 0: curr = fscore.extract()[0] print(ancestor_chain(parent, curr)) if curr == stop: unwrap_path(parent, start, stop) break try: unvisited.remove(curr) visited.append(curr) curr_links = get_page_links(curr)["parse"]["links"] for l in curr_links: try: if l["exists"] == "": link = l["*"] if (is_article(link)) and (link not in visited): tentative_gscore = gscore[curr] + 1 if link not in unvisited: unvisited.append(link) elif tentative_gscore >= gscore[link]: continue parent[link] = curr gscore[link] = tentative_gscore #fscore[link] = gscore[link] + (1/links_in_common(link,stop)) f = gscore[link] + distance_heuristic(link, stop) fscore.insert((link, f)) except: continue #print(fscore.array) #print("{0} of {1}".format(fscore.size,fscore.capacity)) except: continue del article_data[curr]
def __iter__(self): # this enables converting the task into a tuple() type so that # the priority queue can just cast it to tuple before comparison. # it depends on the policy if (self.scheme == 'FCFS'): t = (self.release, self.last_dispatched_time) # example, but you may want a secondary # priority for tie-breaker. if so, just add them to the tuple. elif (self.scheme == 'SJF'): # shortest job first # tuple that defines priority in terms of "job length" # or is it really job length? t = (self.cpuBurst, self.last_dispatched_time) elif (self.scheme == 'RR'): # round robin # define round robin priority if you use a MinHeap; # or you could just use a FIFO. t = MinHeap() t.put((self.last_dispatched_time, self.remaining_time)) else: raise ValueError("Unknown scheme %s" % self.scheme) for i in t: yield i
def least_cost_path(graph, coord, start, dest, cost): '''This function takes in any graph supplied by the user, by default the only graph built by the script is the edmonton roads graph. This function can also take in any cost function of two variables, start and destination. ''' heap = MinHeap() # create a min heap heap.add(0, (start, start)) # dist, (vertice, prior vertice) reached = { } # dictionary of vertices reached with their shortest cost and prior vertice while not heap.isempty(): # run the program until the heap is empty curr_dist, (curr_vert, pri_vert) = heap.pop_min( ) # pop the min cost vertice and explore it, aka extract minimum distance runner for v in graph.neighbours(curr_vert): temp_dist = curr_dist + cost(coord, curr_vert, v) # Find new distance if ( v in reached and temp_dist < reached[v][1] ) or v not in reached: # Check if the key has been used already reached[v] = (curr_vert, temp_dist ) # Previous Vertice, distance if v != dest: heap.add(temp_dist, (v, curr_vert)) min_path = [] # Trace back the shortest path if dest in reached: # First check if there is even a path min_path.insert(0, dest) curr_id = reached[dest][0] prior_id = dest while prior_id != start: # trace back until we reach the start from dest min_path.insert(0, curr_id) # keep adding prior vert to front of list prior_id = curr_id if prior_id not in reached: return min_path curr_id = reached[prior_id][0] return min_path, reached[dest]
def split(self, x, y): """Find (n_class-1) best thresholds for a feature x and corresponding labels y. Args: x: a feature. y: the labels. Returns: a list of the best thresholds(x-value). """ heap = MinHeap(self.n_class) sort_index = sorted(range(len(x)), key=lambda i: x[i]) x_sort = x[sort_index] y_sort = y[sort_index] thres_i, ig = self.split_by_entropy(x_sort, y_sort, sort=False) heap.push((tuple(range(len(y))), thres_i), -ig) while heap.size < self.n_class and heap.minimum() < 0: (piece, thres), ig = heap.pop() piece1, piece2 = list(piece[:thres]), list(piece[thres:]) thres_i1, ig1 = self.split_by_entropy(x_sort[piece1], y_sort[piece1], sort=False) thres_i2, ig2 = self.split_by_entropy(x_sort[piece2], y_sort[piece2], sort=False) heap.push((piece1, thres_i1), -ig1) heap.push((piece2, thres_i2), -ig2) return x_sort[sorted([item[0][0][-1] for item in heap.items()])[:-1]]
def setUp(self): self.h = MinHeap(elements=[2, 4, 5, 12, 13, 6, 10])
def setUpClass(self): self.H = MinHeap() self.data = [('A',7),('B',3),('C',4),('D',1),('E',6),('F',5),('G',2)]
def shortestpath(tilemap, startnode, goalnode): ''' Find the shortestpath between the startnode and the goalnode Input Arguments: tilemap: the tile map object startnode: the location of start tile on the map (xtile, ytile) goalnode: the location of the goal tile on the map (xtile, ytile) Note: startnode and goalnode must be walkable (in the tilemap.legaltileLoc set) Return: path: A shortest path list containing the tile locations from start tile to goal tile ''' # reached dict, key: v_to, value: (actualcost, v_from) reached = dict() # explorenode minheap, key: totalcost, value: (actualcost, v_from, v_to) # totalcost = actualcost + heuristiccost explorenode = MinHeap() explorenode.add(0, (0, startnode, startnode)) # The node to be explored, key: v_to, value: (actualcost, v_from) explorenodedict = dict() explorenodedict[startnode] = (0, startnode) while len(explorenode) > 0: # Get the node with minimun total cost totalcost, checknode = explorenode.pop_min() actualcost, v_from, v_to = checknode if v_to in reached: # node has already been reached continue # Add v_to to reached dict reached[v_to] = (actualcost, v_from) # Remove v_to from explorenodedict del explorenodedict[v_to] if v_to == goalnode: # reached goal break # Explore the neighbour nodes of v_to for neighbournode in nodeneighbour(tilemap, v_to): if neighbournode in reached: # Don't do anything if the neighbournode has already been reached continue # Check if the neighbournode is in the explorenode dict if neighbournode in explorenodedict: # Check if the new actualcost (v_to's actualcost + 1) is less than the old actualcost if explorenodedict[neighbournode][0] > (reached[v_to][0] + 1): # Update the actualcost and v_from for neighbournode explorenodedict[neighbournode] = (reached[v_to][0] + 1, v_to) newtotalcost = reached[v_to][0] + 1 + heuristiccost( neighbournode, goalnode) # Add the neighbournode with newtotalcost to minheap explorenode.add( newtotalcost, (reached[v_to][0] + 1, v_to, neighbournode)) else: # Add neighbournode to explorenodedict explorenodedict[neighbournode] = (reached[v_to][0] + 1, v_to) # Add neighbournode to explorenode minheap totalcost = reached[v_to][0] + 1 + heuristiccost( neighbournode, goalnode) explorenode.add(totalcost, (reached[v_to][0] + 1, v_to, neighbournode)) # The path from start to dest path = [] if goalnode in reached: path.append(goalnode) while reached[goalnode][1] != startnode: path.append(reached[goalnode][1]) goalnode = reached[goalnode][1] if goalnode != startnode: path.append(startnode) path.reverse() return path
print('{0:<{1}}'.format(rmapping[i], offset), end='') print() for i in range(N): if costs[i] == 10000: print('inf', end=' ') else: print('{},{}'.format(costs[i], rmapping[parent[i]]), end=' ') print() # dijkstra part parent = [i for i in range(N)] visited = [False for _ in range(N)] costs = [10000 for _ in range(N)] heap = MinHeap() # for maintaining min cost element #initalization costs[src] = 0 heap.insert((0, src)) NN = '' while not heap.empty(): cw, cur = heap.poll() visited[cur] = True for v, w in graph[cur]: if not visited[v] and costs[v] > costs[cur] + w: costs[v] = costs[cur] + w heap.insert((costs[v], v)) parent[v] = cur
#used to find top 10 job and states from count import Counting from minheap import MinHeap import sys #get the count for every job and state count = Counting(sys.argv[1]) job_count, state_count, sum_count = count.count() #find the top_10 jobs job_heap = MinHeap(10) job_fhandle = open(sys.argv[2],'w') for key,value in job_count.items(): job_heap.add(key,value) job_fhandle.write('TOP_OCCUPATIONS;NUMBER_CERTIFIED_APPLICATIONS;PERCENTAGE\n') result = dict() for i in range(min(10,len(job_count))): key,value = job_heap.extract() result[key.lstrip('""').rstrip('""')] = value result = sorted(result.items(), key = lambda item:item[0]) result.sort(key = lambda x:x[1], reverse = True) for item in result: key = item[0] value = item[1] p = round(value / sum_count * 100.0, 1) s = key + ';' + str(value) + ';' + str(p) + '%' + '\n' job_fhandle.write(s) #find the top_10 states state_heap = MinHeap(10) state_fhandle = open(sys.argv[3],'w')
def setUp(self): self.heap = MinHeap()
def test_heapify_after_init(self): elements = [2, 4, 5, 6, 10, 12, 13] h = MinHeap() self.assertEqual(h.h, []) h._heapify(elements) self.assertEqual(h.h, [2, 4, 5, 6, 10, 12, 13])
def setUp(self): self.h = MinHeap( elements=[(4, 101), (12, 9), (13, 5), (2, 1), (6, 1001), (5, 45), (10, 121)])
def test_pop_empty(self): h = MinHeap() self.assertIsNone(h.pop()) self.assertEqual(h.h, [])