def shortDist(Graph, start_node) : # both dijkstra_dist_1 and dijkstra_dist_2 are dict # 'node':short_distance # dijkstra_dist_1 is the one with finished nodes # dijkstra_dist_2 is the one with nodes to be calculated dijkstra_dist_1 = {str(start_node):0} dijkstra_dist_2 = {str(node):1000000.0 for node in Graph[0] if node != start_node} current_node = start_node current_dist = 0.0 count = 0 my_heap = heap() for node in dijkstra_dist_2 : my_heap.pushheap(heapElement(node,dijkstra_dist_2[node])) while True : if len(G[1][current_node-1]) == 0 : break if len(dijkstra_dist_2) == 0 : break for [node_i, dist_i] in G[1][current_node-1] : if str(node_i) in dijkstra_dist_2 : if current_dist + dist_i < dijkstra_dist_2[str(node_i)] : try : if current_node == 144 and node_i == 68 : print 'before delete' print my_heap.checkheap() print my_heap.size print my_heap.position['68'] #print my_heap.h[1].key, my_heap.h[1].value my_heap.delKey(str(node_i)) if current_node == 144 and node_i == 68 : print 'after delete', my_heap.checkheap() my_heap.pushheap(heapElement(str(node_i),current_dist + dist_i)) dijkstra_dist_2[str(node_i)] = current_dist + dist_i except : print 'error', current_node, node_i, current_dist + dist_i #my_heap.printheap() sys.exit(1) current_element = my_heap.popheap() current_node = int(current_element.key) current_dist = current_element.value del dijkstra_dist_2[str(current_node)] if current_dist > 1000000 - 1 : break dijkstra_dist_1[str(current_node)] = current_dist print 'count: ', count, (current_node, current_dist) count += 1 if count > 10000 : break return dijkstra_dist_1
from heap import * h = heap() def heapify(): n = len(h.arr) n = n//2 while(n>1): h.sink(h.arr[n]) n = n-1 def printMax(): n = len(h.arr) while(n>1): h.arr[1], h.arr[n-1] = h.arr[n-1], h.arr[1] h.sink(h.arr[1]) n = n-1 print(h.arr) t = list(input().split(' ')) for i in t: h.insert(i) #heapify() printMax()
def __init__(self): S= [] self._S = heap(S) # this heap will contain the stack elements self._p = -1 # instance variable for setting priority for each element
from heap import * import sys myheap = heap() myheap.appendheap('145', 648.0) myheap.appendheap('11', 1913.0) myheap.appendheap('129', 676.0) myheap.appendheap('173', 2069.0) myheap.appendheap('150', 2183.0) myheap.appendheap('80', 982.0) myheap.appendheap('103', 1826.0) myheap.appendheap('30', 4612.0) myheap.appendheap('49', 2437.0) myheap.appendheap('107', 2540.0) myheap.appendheap('111', 2535.0) myheap.appendheap('35', 998.0) myheap.appendheap('170', 2620.0) myheap.appendheap('151', 5835.0) myheap.appendheap('102', 6916.0) myheap.appendheap('155', 5547.0) myheap.appendheap('55', 6399.0) myheap.appendheap('141', 6896.0) myheap.appendheap('125', 2909.0) myheap.appendheap('158', 4575.0) myheap.appendheap('169', 2776.0) myheap.appendheap('189', 3610.0) myheap.appendheap('192', 7321.0) myheap.appendheap('27', 1222.0) myheap.appendheap('198', 1724.0) myheap.appendheap('47', 9988.0) myheap.appendheap('179', 1000000.0)
def __init__(self): Q = [] self._Q = heap(Q) # this heap will contain the queue elements self._p = -1 # instance variable for setting priority for each element
from heap import * left_heap = heap('desc') right_heap = heap('asc') a = [] m = [] i = 0 count = 0 for line in open('Median.txt'): i += 1 item = int(line.strip()) # the first element if i == 1: left_heap.pushheap(heapElement(str(i), item)) m.append(item) continue # check if the current value is bigger than the largest of the left heap # if larger, then push to the left (odd) or right (even) and move the right root to left it needed # if not larger, either push the left (odd) or move root of left to right and push to left if item > left_heap.h[1].value: if i % 2 == 0: right_heap.pushheap(heapElement(str(i), item)) else: # if value > right root, move right root if item > right_heap.h[1].value: tmp = right_heap.popheap() right_heap.pushheap(heapElement(str(i), item)) left_heap.pushheap(tmp) # if left < value < right, insert to left
def __init__(self): S = [] self._S = heap(S) # this heap will contain the stack elements self._p = -1 # instance variable for setting priority for each element
import heap import random import time time=time.time random=random.random heap=heap.heap x=input("enter the number of elements to be stored in the array") list=[] for i in range(0,x): list.append(random()) heap1=heap() for i in list: heap1.insert(i) start=time() while not heap1.is_empty(): heap1.extract_max() print "time taken is "+str(time()-start)
import heap import random import time time = time.time random = random.random heap = heap.heap x = input("enter the number of elements to be stored in the array") list = [] for i in range(0, x): list.append(random()) heap1 = heap() for i in list: heap1.insert(i) start = time() while not heap1.is_empty(): heap1.extract_max() print "time taken is " + str(time() - start)
from heap import * array = [18, 25, 19, 14, 7, 9, 3, 16, 8, 100] # get length of array array_length = len(array) heap_length = array_length heap1 = heap(array, array_length, heap_length) """ call max_heapify on each element of array from mid to start because we know A[n/2 .. n] are leaves. So A[0 .. (n/2-1)] are root nodes to other nodes for sure. we ran algorithm backwards because it is most efficient to use it bottom up then top down. Top down will need more no of swaps and eventually will produce wrong result. """ # arrange the array into heap which means you should know what are the # properties of heap google it reader heap1.build_max_heap() for i in range((array_length - 1), 0, -1): # 0th or root element is max so swap max with last element and exclude last element from heap_size # by decrementing it heap1.array[0], heap1.array[i] = heap1.array[i], heap1.array[0] heap1.heap_size = heap1.heap_size - 1 # 0th element of array is out of order and malforming the heap so again heapify it heap1.max_heapify(0) #heap1.max_heapify(1) print(heap1.array)
from heap import * heap1 = heap('asc') print heap1.size heap1.pushheap(heapElement('a',2)) heap1.pushheap(heapElement('b',7)) heap1.pushheap(heapElement('c',5)) heap1.pushheap(heapElement('d',4)) heap1.pushheap(heapElement('e',6)) heap1.pushheap(heapElement('f',8)) a = heap1.size heap1.h[1].value = 1 print heap1.checkheap() for i in range(a) : heap1.popheap().printElement()
def auto(fix, PT, EVENT, mobility, shuffle, E, C, L, G, INITIAL): m, n = len(fix), PT - sum(fix) X = heap(m, n) test = [] for _ in X: get = [fix[__] + _[__] for __ in range(m)] test.append(get) for deposit in test: max_P, counter = 0, 0 print '================================' if shuffle: if sum(mobility) == 0: all_kind = [[randint(300, 700) for _ in range(PT)] for __ in range(200)] else: all_kind = set(list(permutations(mobility))) else: all_kind = [mobility] for team_speed in all_kind: passing, total = 0, 0 if shuffle: counter += 1 for __ in range(1000): #run times team = [] for i, times in enumerate(deposit): for _ in range(times): team.append([INITIAL[i], EVENT[INITIAL[i]][0]]) LOCKER = L ENEMY = [1 for _ in range(E)] CHANGER = [0 for _ in range(C)] GATE = [[False, []] for _ in range(G)] PASS = [True for _ in range(PT)] speed_change = [{ 'tornado': [False, False], 'spiderweb': [False, False] } for _ in range(PT)] ending = False while not ending: for i in range(PT): if PASS[i]: move(team[i], team_speed[i], speed_change[i]) while team[i][1] <= 0 and not ending: trial = team[i][0] number = alphabet[trial[1]] TYPE = trial[0] if TYPE == 'S': selector(trial, team[i], EVENT, len(EVENT[trial][1])) elif TYPE == 'E': enemy(trial, team[i], EVENT, ENEMY, number) elif TYPE == 'C': changer(trial, team[i], EVENT, CHANGER, number, len(EVENT[trial][1])) elif TYPE == 'T': teleport(trial, team[i], EVENT) elif TYPE == 'L': locker(trial, team[i], EVENT, LOCKER, number) elif TYPE == 'B': button(trial, team[i], EVENT, PASS, GATE, alphabet[EVENT[trial][1][1][1]]) elif TYPE == 'G': gate(trial, team[i], EVENT, PASS, GATE, number, i) elif TYPE == 'A' or TYPE == 'D': speed(trial, team[i], EVENT, speed_change[i], TYPE) else: ending = True if ending or not any(PASS): break if sum(ENEMY) == 0: passing += 1. total += 1. if passing / total * 100. > max_P: counter = 0 max_P = passing / total * 100. print 'MAX!!!:', deposit, team_speed, max_P, '%' Prob = passing / total * 100. if Prob == 0. or Prob == 100.: break if counter > 40: #if u can't get a higher Prob for * times,change a new set print 'shimakaze:o soi~' break
def help(): print("help - Prints this list \n" "makenull - Clears the heap \n" "insert <integer> - Inserts the number into the heap\n" "min - Prints the current min on the heap \n" "inorder - Prints heap in inorder\n" "preorder - Prints heap in preorder \n" "postorder - Prints heap in postorder \n" "deletemin - Removes min from the heap \n" "sort - Calls deletemin repeatedly to print out sorted numbers \n" "exit - Exits the program (also Crtl-D exits) ") if __name__ == '__main__': heap = heap() print("Welcome to the Heap") print("The List of Commands is below, type help to see them again.") print("help - Prints this list") print("makenull - Clears the heap") print("insert <integer> - Inserts the number into the heap") print("min - Prints the current min on the heap") print("inorder - Prints heap in inorder \n" "preorder - Prints heap in preorder\n" "postorder - Prints heap in postorder\n" "deletemin - Removes min from the heap\n" "sort - Calls deletemin repeatedly to print out sorted numbers\n" "exit - Exits the program (also Crtl-D exits)") while True: # check until the exit or Ctrl+D is entered try: