From 2f07ccc3d7818a7dff0ad73b760145c11652b26a Mon Sep 17 00:00:00 2001 From: Maria Hartmann <lm.hartmann@gmx.de> Date: Fri, 12 Feb 2021 14:25:05 +0100 Subject: [PATCH] fixed some import issues --- pairing_heap.py | 2 +- pairing_heap_l.py | 2 +- paper-sorting-loc-new.py | 262 ++++++++++++++++----------------- paper-sorting-sep-new.py | 274 +++++++++++++++++------------------ paper-sorting-subseq-new.py | 281 +++++++++++++++++------------------- smooth_heap.py | 10 +- 6 files changed, 410 insertions(+), 421 deletions(-) diff --git a/pairing_heap.py b/pairing_heap.py index 93b4aeb..8ff4c91 100644 --- a/pairing_heap.py +++ b/pairing_heap.py @@ -20,7 +20,7 @@ class PairingHeap(PairingHeapInterface): self.countType=countType def make_heap(self): - if self.mode==0: + if self.mode==0: self.heap=PairingHeapStandard() elif self.mode==12: self.heap=SmoothHeap() diff --git a/pairing_heap_l.py b/pairing_heap_l.py index 38c02c8..bef9143 100644 --- a/pairing_heap_l.py +++ b/pairing_heap_l.py @@ -3,7 +3,7 @@ from node import Node from pairing_heap_interface import PairingHeapInterface class PairingHeapL(PairingHeapInterface): - # lazy variant of standard pairing heap (maintaining root-list and consolidating only upon extract-min) + # lazy variant of standard pairing heap (maintaining root-list and consolidating only upon extract-min) forest=[] #list storing roots of all top-level trees def __init__(self, root=None): self.forest=[] diff --git a/paper-sorting-loc-new.py b/paper-sorting-loc-new.py index 3a3d686..994df0b 100644 --- a/paper-sorting-loc-new.py +++ b/paper-sorting-loc-new.py @@ -29,148 +29,148 @@ SHADE_COLOURS = {0:'#fe4d4e', 12:'#58ab8e'} def isSorted(list0): - return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) + return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) def localizedShuffleByIndex(llist, sdev): - tuples = [] - for element in llist: - key = np.random.normal(llist.index(element) * 1.0 / len(llist), - sdev) # generate key using gaussian distribution over sorted index - tuples += [(key, element)] # store element with key - sortedTuples = sorted(tuples, key=lambda x: x[0]) # sort key-element tuples by keys - sortedList = [tup[1] for tup in sortedTuples] # discard keys - # print(sortedList) - return sortedList + tuples = [] + for element in llist: + key = np.random.normal(llist.index(element) * 1.0 / len(llist), + sdev) # generate key using gaussian distribution over sorted index + tuples += [(key, element)] # store element with key + sortedTuples = sorted(tuples, key=lambda x: x[0]) # sort key-element tuples by keys + sortedList = [tup[1] for tup in sortedTuples] # discard keys + # print(sortedList) + return sortedList def plot_avg_counts(avgCounts): - # colours from https://xkcd.com/color/rgb/ - MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html - MARKERS_LINK = {0:"o", 12:"D"} - - plt.figure('avg number of operations by heap type') - deviations = [fac * INCREMENT_LOC for fac in range(0, math.ceil(0.3 / INCREMENT_LOC), 1)] - for k in TYPES.keys(): - #print(k) - avgComps = [acounts[k] for acounts in avgCounts[0]] - maxComps = [acounts[k] for acounts in avgCounts[2]] - minComps = [acounts[k] for acounts in avgCounts[4]] - plt.plot(deviations, avgComps, color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") - plt.fill_between(deviations, minComps, maxComps, color=SHADE_COLOURS[k], alpha=.3) - avgLinks = [acounts[k] for acounts in avgCounts[1]] - maxLinks = [acounts[k] for acounts in avgCounts[3]] - minLinks = [acounts[k] for acounts in avgCounts[5]] - plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") - plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) - - #plt.title('Sorting random separable permutations', fontsize=25) - plt.xlabel('Locality parameter', fontsize=26) - plt.ylabel('Avg. number of operations / size', fontsize=26) - plt.xticks(fontsize=20) - plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points - plt.legend() - plt.grid(True) - figure = plt.gcf() # get current figure - figure.set_size_inches(16, 18) # set figure's size manually to your full screen (32x18) - plt.savefig('plots/paper-sorting-loc-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces - plt.legend(loc='best') - plt.show() + # colours from https://xkcd.com/color/rgb/ + MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html + MARKERS_LINK = {0:"o", 12:"D"} + + plt.figure('avg number of operations by heap type') + deviations = [fac * INCREMENT_LOC for fac in range(0, math.ceil(0.3 / INCREMENT_LOC), 1)] + for k in TYPES.keys(): + #print(k) + avgComps = [acounts[k] for acounts in avgCounts[0]] + maxComps = [acounts[k] for acounts in avgCounts[2]] + minComps = [acounts[k] for acounts in avgCounts[4]] + plt.plot(deviations, avgComps, color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") + plt.fill_between(deviations, minComps, maxComps, color=SHADE_COLOURS[k], alpha=.3) + avgLinks = [acounts[k] for acounts in avgCounts[1]] + maxLinks = [acounts[k] for acounts in avgCounts[3]] + minLinks = [acounts[k] for acounts in avgCounts[5]] + plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") + plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) + + #plt.title('Sorting random separable permutations', fontsize=25) + plt.xlabel('Locality parameter', fontsize=26) + plt.ylabel('Avg. number of operations / size', fontsize=26) + plt.xticks(fontsize=20) + plt.yticks(fontsize=20) + plt.rc('legend',fontsize=26) # using a size in points + plt.legend() + plt.grid(True) + figure = plt.gcf() # get current figure + figure.set_size_inches(16, 18) # set figure's size manually to your full screen (32x18) + plt.savefig('plots/paper-sorting-loc-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces + plt.legend(loc='best') + plt.show() def export_results(params, results, countType, heapTypes, filename="dijkstra"): - # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory - # each row contains randomness parameter value; plus one column containing the number of operations for each heap type - if countType == COUNT_TYPE_BOTH: - with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[0])): - row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[1])): - row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - else: - fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' - with open(fn, 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results)): - row = [params[i]] + [results[i][k] for k in TYPES.keys()] - csvwriter.writerow(row) + # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory + # each row contains randomness parameter value; plus one column containing the number of operations for each heap type + if countType == COUNT_TYPE_BOTH: + with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[0])): + row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[1])): + row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + else: + fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' + with open(fn, 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results)): + row = [params[i]] + [results[i][k] for k in TYPES.keys()] + csvwriter.writerow(row) if __name__ == "__main__": - testOutputCount = [] - avgLinksPerSize = [] - avgCompsPerSize = [] - maxLinksPerSize = [] - maxCompsPerSize = [] - minLinksPerSize = [] - minCompsPerSize = [] - - sortedInput = [] - #testInput = [] - - # ----------localised permutation inputs-------------- - # randomness parameter: standard deviation - params = [fac * INCREMENT_LOC+0.00001 for fac in range(0, math.ceil(0.3 / INCREMENT_LOC), 1)] - - for x in params: - sortedInput = [k for k in range(10000)] - avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - - for zz in range(NUMBER_TESTS): - testInput = localizedShuffleByIndex(sortedInput, x) - print(len(testInput)) - for heapType in TYPES.keys(): - linkCount = 0 - compCount = 0 - testOutput = [] - heap = PairingHeap(heapType, COUNT_TYPE_BOTH) - heap.make_heap() - for element in testInput: - node = Node(element) - (cc, lc) = heap.insert(node) - for i in range(len(testInput)): - (minNode, cc, lc) = heap.delete_min() - testOutput += [minNode.key] - compCount += cc - linkCount += lc - if isSorted(testOutput): # sanity check - #divide by size for visualization - avgCountsLinks[heapType] += (linkCount/10000) / NUMBER_TESTS - avgCountsComps[heapType] += (compCount/10000) / NUMBER_TESTS - maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/10000) - maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/10000) - minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/10000) - minCountsComps[heapType] = min(minCountsComps[heapType],compCount/10000) - else: - raise Exception("Invalid result for {}".format(TYPES[heapType])) - print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( - TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount))# diagnostics - for heapType in TYPES.keys(): - print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) - avgLinksPerSize += [avgCountsLinks] - avgCompsPerSize += [avgCountsComps] - maxLinksPerSize += [maxCountsLinks] - maxCompsPerSize += [maxCountsComps] - minLinksPerSize += [minCountsLinks] - minCompsPerSize += [minCountsComps] - plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) - export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-loc-new") + testOutputCount = [] + avgLinksPerSize = [] + avgCompsPerSize = [] + maxLinksPerSize = [] + maxCompsPerSize = [] + minLinksPerSize = [] + minCompsPerSize = [] + + sortedInput = [] + #testInput = [] + + # ----------localised permutation inputs-------------- + # randomness parameter: standard deviation + params = [fac * INCREMENT_LOC+0.00001 for fac in range(0, math.ceil(0.3 / INCREMENT_LOC), 1)] + + for x in params: + sortedInput = [k for k in range(10000)] + avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + + for zz in range(NUMBER_TESTS): + testInput = localizedShuffleByIndex(sortedInput, x) + print(len(testInput)) + for heapType in TYPES.keys(): + linkCount = 0 + compCount = 0 + testOutput = [] + heap = PairingHeap(heapType, COUNT_TYPE_BOTH) + heap.make_heap() + for element in testInput: + node = Node(element) + (cc, lc) = heap.insert(node) + for i in range(len(testInput)): + (minNode, cc, lc) = heap.delete_min() + testOutput += [minNode.key] + compCount += cc + linkCount += lc + if isSorted(testOutput): # sanity check + #divide by size for visualization + avgCountsLinks[heapType] += (linkCount/10000) / NUMBER_TESTS + avgCountsComps[heapType] += (compCount/10000) / NUMBER_TESTS + maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/10000) + maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/10000) + minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/10000) + minCountsComps[heapType] = min(minCountsComps[heapType],compCount/10000) + else: + raise Exception("Invalid result for {}".format(TYPES[heapType])) + print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( + TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount))# diagnostics + for heapType in TYPES.keys(): + print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) + avgLinksPerSize += [avgCountsLinks] + avgCompsPerSize += [avgCountsComps] + maxLinksPerSize += [maxCountsLinks] + maxCompsPerSize += [maxCountsComps] + minLinksPerSize += [minCountsLinks] + minCompsPerSize += [minCountsComps] + plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) + export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-loc-new") diff --git a/paper-sorting-sep-new.py b/paper-sorting-sep-new.py index 00b11c9..265688c 100644 --- a/paper-sorting-sep-new.py +++ b/paper-sorting-sep-new.py @@ -29,155 +29,155 @@ SHADE_COLOURS = {0:'#fe4d4e', 12:'#58ab8e'} def isSorted(list0): - return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) + return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) def plot_avg_counts(avgCounts): - # colours from https://xkcd.com/color/rgb/ - MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html - MARKERS_LINK = {0:"o", 12:"D"} - - plt.figure('avg number of operations by heap type') - for k in TYPES.keys(): - print(k) - avgComps = [acounts[k] for acounts in avgCounts[0]] - maxComps = [acounts[k] for acounts in avgCounts[2]] - minComps = [acounts[k] for acounts in avgCounts[4]] - plt.plot([2**p for p in range(4, MAXSIZE)], avgComps[3:MAXSIZE-1], color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") - plt.fill_between([2**p for p in range(4, MAXSIZE)], minComps[3:MAXSIZE-1], maxComps[3:MAXSIZE-1], color=SHADE_COLOURS[k], alpha=.3) - avgLinks = [acounts[k] for acounts in avgCounts[1]] - maxLinks = [acounts[k] for acounts in avgCounts[3]] - minLinks = [acounts[k] for acounts in avgCounts[5]] - plt.plot([2**p for p in range(4, MAXSIZE)], avgLinks[3:MAXSIZE-1], color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") - plt.fill_between([2**p for p in range(4, MAXSIZE)], minLinks[3:MAXSIZE-1], maxLinks[3:MAXSIZE-1], color=SHADE_COLOURS[k], alpha=.3) - - #plt.title('Sorting random separable permutations', fontsize=25) - plt.xlabel('Input size', fontsize=26) - plt.ylabel('Avg. number of operations / size', fontsize=26) - plt.xticks(fontsize=20) - plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points - plt.legend() - plt.grid(True) - figure = plt.gcf() # get current figure - figure.set_size_inches(16, 18) # set figure's size manually to full screen - plt.savefig('plots/paper-sorting-sep-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces - plt.legend(loc='best') - plt.show() + # colours from https://xkcd.com/color/rgb/ + MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html + MARKERS_LINK = {0:"o", 12:"D"} + + plt.figure('avg number of operations by heap type') + for k in TYPES.keys(): + print(k) + avgComps = [acounts[k] for acounts in avgCounts[0]] + maxComps = [acounts[k] for acounts in avgCounts[2]] + minComps = [acounts[k] for acounts in avgCounts[4]] + plt.plot([2**p for p in range(4, MAXSIZE)], avgComps[3:MAXSIZE-1], color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") + plt.fill_between([2**p for p in range(4, MAXSIZE)], minComps[3:MAXSIZE-1], maxComps[3:MAXSIZE-1], color=SHADE_COLOURS[k], alpha=.3) + avgLinks = [acounts[k] for acounts in avgCounts[1]] + maxLinks = [acounts[k] for acounts in avgCounts[3]] + minLinks = [acounts[k] for acounts in avgCounts[5]] + plt.plot([2**p for p in range(4, MAXSIZE)], avgLinks[3:MAXSIZE-1], color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") + plt.fill_between([2**p for p in range(4, MAXSIZE)], minLinks[3:MAXSIZE-1], maxLinks[3:MAXSIZE-1], color=SHADE_COLOURS[k], alpha=.3) + + #plt.title('Sorting random separable permutations', fontsize=25) + plt.xlabel('Input size', fontsize=26) + plt.ylabel('Avg. number of operations / size', fontsize=26) + plt.xticks(fontsize=20) + plt.yticks(fontsize=20) + plt.rc('legend',fontsize=26) # using a size in points + plt.legend() + plt.grid(True) + figure = plt.gcf() # get current figure + figure.set_size_inches(16, 18) # set figure's size manually to full screen + plt.savefig('plots/paper-sorting-sep-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces + plt.legend(loc='best') + plt.show() def export_results(params, results, countType, heapTypes, filename="dijkstra"): - # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory - # each row contains randomness parameter value; plus one column containing the number of operations for each heap type - if countType == COUNT_TYPE_BOTH: - with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[0])): - row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[1])): - row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - else: - fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' - with open(fn, 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results)): - row = [params[i]] + [results[i][k] for k in TYPES.keys()] - csvwriter.writerow(row) + # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory + # each row contains randomness parameter value; plus one column containing the number of operations for each heap type + if countType == COUNT_TYPE_BOTH: + with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[0])): + row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[1])): + row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + else: + fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' + with open(fn, 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results)): + row = [params[i]] + [results[i][k] for k in TYPES.keys()] + csvwriter.writerow(row) def separablePermutation(n): - assert (n & (n - 1) == 0) and n != 0 and n > 1, "n must be a power of two > 1" # bit magic + assert (n & (n - 1) == 0) and n != 0 and n > 1, "n must be a power of two > 1" # bit magic - def generateSepPermutation(l, r): - flip = random.random() >= 0.5 - if r - l == 1: - if flip == 0: - return [r, l] - else: - return [l, r] - else: - m = math.floor((l + r) / 2) - if flip == 0: - return generateSepPermutation(m + 1, r) + generateSepPermutation(l, m) - else: - return generateSepPermutation(l, m) + generateSepPermutation(m + 1, r) + def generateSepPermutation(l, r): + flip = random.random() >= 0.5 + if r - l == 1: + if flip == 0: + return [r, l] + else: + return [l, r] + else: + m = math.floor((l + r) / 2) + if flip == 0: + return generateSepPermutation(m + 1, r) + generateSepPermutation(l, m) + else: + return generateSepPermutation(l, m) + generateSepPermutation(m + 1, r) - return generateSepPermutation(0, n - 1) + return generateSepPermutation(0, n - 1) if __name__ == "__main__": - testOutputCount = [] - avgLinksPerSize = [] - avgCompsPerSize = [] - maxLinksPerSize = [] - maxCompsPerSize = [] - minLinksPerSize = [] - minCompsPerSize = [] - - sortedInput = [] - # testInput = [] - - # ----------separable permutation--------------------- - # parameter: length (must be power of two) - params = [2**p for p in range(1, MAXSIZE)] - - for x in params: - sortedInput = [k for k in range(x)] - avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - - for zz in range(NUMBER_TESTS): - testInput = copy.copy(sortedInput) - testInput = separablePermutation(x) - testInput[0] = -1 - for heapType in TYPES.keys(): - linkCount = 0 - compCount = 0 - testOutput = [] - heap = PairingHeap(heapType, COUNT_TYPE_BOTH) - heap.make_heap() - for element in testInput: - node = Node(element) - (cc, lc) = heap.insert(node) - for i in range(len(testInput)): - (minNode, cc, lc) = heap.delete_min() - testOutput += [minNode.key] - compCount += cc - linkCount += lc - if isSorted(testOutput): # sanity check - #divide by size for visualization - avgCountsLinks[heapType] += (linkCount/x) / NUMBER_TESTS - avgCountsComps[heapType] += (compCount/x) / NUMBER_TESTS - maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/x) - maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/x) - minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/x) - minCountsComps[heapType] = min(minCountsComps[heapType],compCount/x) - else: - raise Exception("Invalid result for {}".format(TYPES[heapType])) - print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( - TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount)) - for heapType in TYPES.keys(): - print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) - avgLinksPerSize += [avgCountsLinks] - avgCompsPerSize += [avgCountsComps] - maxLinksPerSize += [maxCountsLinks] - maxCompsPerSize += [maxCountsComps] - minLinksPerSize += [minCountsLinks] - minCompsPerSize += [minCountsComps] - plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) - export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-sep-new") + testOutputCount = [] + avgLinksPerSize = [] + avgCompsPerSize = [] + maxLinksPerSize = [] + maxCompsPerSize = [] + minLinksPerSize = [] + minCompsPerSize = [] + + sortedInput = [] + # testInput = [] + + # ----------separable permutation--------------------- + # parameter: length (must be power of two) + params = [2**p for p in range(1, MAXSIZE)] + + for x in params: + sortedInput = [k for k in range(x)] + avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + + for zz in range(NUMBER_TESTS): + testInput = copy.copy(sortedInput) + testInput = separablePermutation(x) + testInput[0] = -1 + for heapType in TYPES.keys(): + linkCount = 0 + compCount = 0 + testOutput = [] + heap = PairingHeap(heapType, COUNT_TYPE_BOTH) + heap.make_heap() + for element in testInput: + node = Node(element) + (cc, lc) = heap.insert(node) + for i in range(len(testInput)): + (minNode, cc, lc) = heap.delete_min() + testOutput += [minNode.key] + compCount += cc + linkCount += lc + if isSorted(testOutput): # sanity check + #divide by size for visualization + avgCountsLinks[heapType] += (linkCount/x) / NUMBER_TESTS + avgCountsComps[heapType] += (compCount/x) / NUMBER_TESTS + maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/x) + maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/x) + minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/x) + minCountsComps[heapType] = min(minCountsComps[heapType],compCount/x) + else: + raise Exception("Invalid result for {}".format(TYPES[heapType])) + print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( + TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount)) + for heapType in TYPES.keys(): + print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) + avgLinksPerSize += [avgCountsLinks] + avgCompsPerSize += [avgCountsComps] + maxLinksPerSize += [maxCountsLinks] + maxCompsPerSize += [maxCountsComps] + minLinksPerSize += [minCountsLinks] + minCompsPerSize += [minCountsComps] + plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) + export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-sep-new") diff --git a/paper-sorting-subseq-new.py b/paper-sorting-subseq-new.py index f21db3d..a72bc08 100644 --- a/paper-sorting-subseq-new.py +++ b/paper-sorting-subseq-new.py @@ -12,18 +12,7 @@ from node import Node from pairing_heap import PairingHeap from pairing_heap_interface import PairingHeapInterface from pairing_heap_standard import PairingHeapStandard -from variants.pairing_heap_multipass import PairingHeapMultipass -from variants.pairing_heap_ftb import PairingHeapFTB -from variants.pairing_heap_btf import PairingHeapBTF -from variants.pairing_heap_lazy import PairingHeapLazy -from variants.pairing_heap_standard_stable import PairingHeapStandardStable -from variants.pairing_heap_ftb_stable import PairingHeapFTBStable -from variants.pairing_heap_btf_stable import PairingHeapBTFStable -from variants.pairing_heap_multipass_stable import PairingHeapMultipassStable -from variants.pairing_heap_lazy_stable import PairingHeapLazyStable -from pairing_heap_smooth import PairingHeapSmooth -from variants.pairing_heap_smooth_unstable import PairingHeapSmoothUnstable -from smooth_heap_corey import SmoothHeapCorey +from smooth_heap import SmoothHeap COUNT_TYPE_BOTH = 0 COUNT_TYPE_LINKS = -1 @@ -42,150 +31,150 @@ SHADE_COLOURS = {0:'#fe4d4e', 12:'#58ab8e'} def isSorted(list0): - return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) + return all(list0[i] < list0[i + 1] for i in range(len(list0) - 1)) def generateContSortedSubseq(llist, sublen): - listcopy = copy.copy(llist) - shuffle(listcopy) - res = [] - l = 0 - while l < len(llist): - clen = random.randint(1, sublen) - sublist = copy.copy(listcopy[l:min(l + clen, len(listcopy))]) - sublist.sort() - res += sublist - l += clen - return res + listcopy = copy.copy(llist) + shuffle(listcopy) + res = [] + l = 0 + while l < len(llist): + clen = random.randint(1, sublen) + sublist = copy.copy(listcopy[l:min(l + clen, len(listcopy))]) + sublist.sort() + res += sublist + l += clen + return res def plot_avg_counts(avgCounts): - # colours from https://xkcd.com/color/rgb/ - MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html - MARKERS_LINK = {0:"o", 12:"D"} - plt.figure('avg number of operations by heap type') - deviations = [fac*INCREMENT_SUBSEQS/200 for fac in range(1, math.ceil((TEST_SIZE/5)/INCREMENT_SUBSEQS), 1)] - deviations.reverse() - for k in TYPES.keys(): - print(k) - avgComps = [acounts[k] for acounts in avgCounts[0]] - maxComps = [acounts[k] for acounts in avgCounts[2]] - minComps = [acounts[k] for acounts in avgCounts[4]] - plt.plot(deviations, avgComps, color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") - plt.fill_between(deviations, minComps, maxComps, color=SHADE_COLOURS[k], alpha=.3) - avgLinks = [acounts[k] for acounts in avgCounts[1]] - maxLinks = [acounts[k] for acounts in avgCounts[3]] - minLinks = [acounts[k] for acounts in avgCounts[5]] - plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") - plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) - - - plt.xlabel('Avg. length of sorted blocks, % of size', fontsize=26) - plt.ylabel('Avg. number of operations / size', fontsize=26) - plt.xticks(fontsize=20) - plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points - plt.legend() - plt.grid(True) - plt.gca().invert_xaxis() - figure = plt.gcf() # get current figure - figure.set_size_inches(16, 18) # set figure's size manually to full screen - plt.savefig('plots/paper-sorting-subseq-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces - plt.legend(loc='best') - plt.show() + # colours from https://xkcd.com/color/rgb/ + MARKERS_COMP = {0:"o", 12:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html + MARKERS_LINK = {0:"o", 12:"D"} + plt.figure('avg number of operations by heap type') + deviations = [fac*INCREMENT_SUBSEQS/200 for fac in range(1, math.ceil((TEST_SIZE/5)/INCREMENT_SUBSEQS), 1)] + deviations.reverse() + for k in TYPES.keys(): + print(k) + avgComps = [acounts[k] for acounts in avgCounts[0]] + maxComps = [acounts[k] for acounts in avgCounts[2]] + minComps = [acounts[k] for acounts in avgCounts[4]] + plt.plot(deviations, avgComps, color=COLOURS[k], linestyle="-", marker=MARKERS_COMP[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " comparisons") + plt.fill_between(deviations, minComps, maxComps, color=SHADE_COLOURS[k], alpha=.3) + avgLinks = [acounts[k] for acounts in avgCounts[1]] + maxLinks = [acounts[k] for acounts in avgCounts[3]] + minLinks = [acounts[k] for acounts in avgCounts[5]] + plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") + plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) + + + plt.xlabel('Avg. length of sorted blocks, % of size', fontsize=26) + plt.ylabel('Avg. number of operations / size', fontsize=26) + plt.xticks(fontsize=20) + plt.yticks(fontsize=20) + plt.rc('legend',fontsize=26) # using a size in points + plt.legend() + plt.grid(True) + plt.gca().invert_xaxis() + figure = plt.gcf() # get current figure + figure.set_size_inches(16, 18) # set figure's size manually to full screen + plt.savefig('plots/paper-sorting-subseq-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces + plt.legend(loc='best') + plt.show() def export_results(params, results, countType, heapTypes, filename="dijkstra"): - # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory - # each row contains randomness parameter value; plus one column containing the number of operations for each heap type - if countType == COUNT_TYPE_BOTH: - with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[0])): - row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results[1])): - row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] - csvwriter.writerow(row) - else: - fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' - with open(fn, 'w', newline='') as csvfile: - csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) - csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) - for i in range(len(results)): - row = [params[i]] + [results[i][k] for k in TYPES.keys()] - csvwriter.writerow(row) + # exports results of simulation as separate .csv files, one for links and one for comparisons, into /data directory + # each row contains randomness parameter value; plus one column containing the number of operations for each heap type + if countType == COUNT_TYPE_BOTH: + with open("data/" + filename + '-comps.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[0])): + row = [params[i]] + [results[0][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + with open("data/" + filename + '-links.csv', 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results[1])): + row = [params[i]] + [results[1][i][k] for k in TYPES.keys()] + csvwriter.writerow(row) + else: + fn = "data/" + filename + '-links.csv' if countType == COUNT_TYPE_LINKS else "data/" + filename + '-comps.csv' + with open(fn, 'w', newline='') as csvfile: + csvwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.values()]) + csvwriter.writerow(["randomness parameter value"] + [name for name in TYPES.keys()]) + for i in range(len(results)): + row = [params[i]] + [results[i][k] for k in TYPES.keys()] + csvwriter.writerow(row) if __name__ == "__main__": - testOutputCount = [] - avgLinksPerSize = [] - avgCompsPerSize = [] - maxLinksPerSize = [] - maxCompsPerSize = [] - minLinksPerSize = [] - minCompsPerSize = [] - - sortedInput = [] - # testInput = [] - # ----------continuous sorted subsequences inputs----- - # randomness parameter: subsequence lengths - params = [fac*INCREMENT_SUBSEQS for fac in range(1, math.ceil((TEST_SIZE/5)/INCREMENT_SUBSEQS), 1)] - params.reverse() - - for x in params: - sortedInput = [k for k in range(LIST_LEN)] - avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] - maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] - minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] - - for zz in range(NUMBER_TESTS): - testInput = generateContSortedSubseq(sortedInput, x) - #print(len(testInput)) - for heapType in TYPES.keys(): - linkCount = 0 - compCount = 0 - testOutput = [] - heap = PairingHeap(heapType, COUNT_TYPE_BOTH) - heap.make_heap() - for element in testInput: - node = Node(element) - (cc, lc) = heap.insert(node) - for i in range(len(testInput)): - (minNode, cc, lc) = heap.delete_min() - testOutput += [minNode.key] - compCount += cc - linkCount += lc - if isSorted(testOutput): # sanity check - #divide by size for visualization - avgCountsLinks[heapType] += (linkCount/LIST_LEN) / NUMBER_TESTS - avgCountsComps[heapType] += (compCount/LIST_LEN) / NUMBER_TESTS - maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/LIST_LEN) - maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/LIST_LEN) - minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/LIST_LEN) - minCountsComps[heapType] = min(minCountsComps[heapType],compCount/LIST_LEN) - else: - raise Exception("Invalid result for {}".format(TYPES[heapType])) - print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( - TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount)) - for heapType in TYPES.keys(): - print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) - avgLinksPerSize += [avgCountsLinks] - avgCompsPerSize += [avgCountsComps] - maxLinksPerSize += [maxCountsLinks] - maxCompsPerSize += [maxCountsComps] - minLinksPerSize += [minCountsLinks] - minCompsPerSize += [minCountsComps] - plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) - export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-subseq-new") + testOutputCount = [] + avgLinksPerSize = [] + avgCompsPerSize = [] + maxLinksPerSize = [] + maxCompsPerSize = [] + minLinksPerSize = [] + minCompsPerSize = [] + + sortedInput = [] + # testInput = [] + # ----------continuous sorted subsequences inputs----- + # randomness parameter: subsequence lengths + params = [fac*INCREMENT_SUBSEQS for fac in range(1, math.ceil((TEST_SIZE/5)/INCREMENT_SUBSEQS), 1)] + params.reverse() + + for x in params: + sortedInput = [k for k in range(LIST_LEN)] + avgCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + avgCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsLinks = [0 for _ in range(MAX_TYPE_KEY + 1)] + maxCountsComps = [0 for _ in range(MAX_TYPE_KEY + 1)] + minCountsLinks = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] + + for zz in range(NUMBER_TESTS): + testInput = generateContSortedSubseq(sortedInput, x) + #print(len(testInput)) + for heapType in TYPES.keys(): + linkCount = 0 + compCount = 0 + testOutput = [] + heap = PairingHeap(heapType, COUNT_TYPE_BOTH) + heap.make_heap() + for element in testInput: + node = Node(element) + (cc, lc) = heap.insert(node) + for i in range(len(testInput)): + (minNode, cc, lc) = heap.delete_min() + testOutput += [minNode.key] + compCount += cc + linkCount += lc + if isSorted(testOutput): # sanity check + #divide by size for visualization + avgCountsLinks[heapType] += (linkCount/LIST_LEN) / NUMBER_TESTS + avgCountsComps[heapType] += (compCount/LIST_LEN) / NUMBER_TESTS + maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/LIST_LEN) + maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/LIST_LEN) + minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/LIST_LEN) + minCountsComps[heapType] = min(minCountsComps[heapType],compCount/LIST_LEN) + else: + raise Exception("Invalid result for {}".format(TYPES[heapType])) + print("[{}: {}, {}/{}] \t Links: {} \t Comps: {}".format( + TYPES[heapType], x, zz+1, NUMBER_TESTS, linkCount, compCount)) + for heapType in TYPES.keys(): + print("[{}: {}, avg] \t Links: {} \t Comps: {}".format(TYPES[heapType], x, avgCountsLinks[heapType], avgCountsComps[heapType])) + avgLinksPerSize += [avgCountsLinks] + avgCompsPerSize += [avgCountsComps] + maxLinksPerSize += [maxCountsLinks] + maxCompsPerSize += [maxCountsComps] + minLinksPerSize += [minCountsLinks] + minCompsPerSize += [minCountsComps] + plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) + export_results(params, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "sorting-subseq-new") diff --git a/smooth_heap.py b/smooth_heap.py index f1edb87..2983981 100644 --- a/smooth_heap.py +++ b/smooth_heap.py @@ -3,7 +3,7 @@ from node import Node import math from pairing_heap_interface import PairingHeapInterface -class SmoothHeapCorey(PairingHeapInterface): +class SmoothHeap(PairingHeapInterface): forest=[] #list storing roots of all top-level trees not in buffer buffer=[] #decrease buffer minNode=None @@ -101,7 +101,7 @@ class SmoothHeapCorey(PairingHeapInterface): self.buffer=heap2.buffer self.size+=heap2.size if(self.minNode.key>=heap2.minNode.key): - self.minNode=heap2.minNode + self.minNode=heap2.minNode return (compCount, linkCount) def delete_min(self): @@ -187,7 +187,7 @@ class SmoothHeapCorey(PairingHeapInterface): self.minNode=self.forest[0] return (compCount, linkCount) - + def mergesort(self, llist): #standard mergesort, implemented to count comparisons properly if len(llist)<2: @@ -222,7 +222,7 @@ class SmoothHeapCorey(PairingHeapInterface): treapified = self.buffer[0] self.buffer=[] - (compCount,linkCount)=self.merge(SmoothHeapCorey(treapified)) + (compCount,linkCount)=self.merge(SmoothHeap(treapified)) return (compCount+comps, linkCount+n-1) #(n-1)links while consolidating @@ -246,7 +246,7 @@ class SmoothHeapCorey(PairingHeapInterface): else: self.listPreOrder() raise Exception("node with key {} is not in heap".format(node.key)) - + elif node.parent==None: #node is a root and has children leftChild=node.rightChild.nextSibling if leftChild.nextSibling!=leftChild: -- GitLab