Skip to content
Snippets Groups Projects
Commit ca19b8a5 authored by Maria Hartmann's avatar Maria Hartmann
Browse files

code cleanup

parent 67c0aa0b
Branches
No related tags found
No related merge requests found
......@@ -25,47 +25,25 @@ COUNT_TYPE_COMPS = -2
TYPES = {21: "Pairing", 22: "Smooth"}
MAX_TYPE_KEY = max(TYPES.keys())
#COLOURS = ['xkcd:fire engine red', 'xkcd:dusty orange', 'xkcd:clear blue', 'xkcd:cool green',
# 'xkcd:macaroni and cheese', 'xkcd:fire engine red', 'xkcd:dusty orange', 'xkcd:clear blue',
# 'xkcd:cool green', 'xkcd:macaroni and cheese', 'xkcd:bright sky blue', 'xkcd:bright sky blue', 'xkcd:green',
# 'xkcd:ochre', 'xkcd:sea blue', 'xkcd:sea green', 'xkcd:sea blue', 'xkcd:warm grey',
# 'xkcd:bright sky blue', 'xkcd:bright sky blue']
LINETYPES = 5 * ['-'] + 5 * ['--'] + ['--', '-'] + ['--', '--', '--', '--', '--', '--', '-', '-']
FIG_LABELS = ["comparisons", "links"]
MAX_TYPE_KEY = max(TYPES.keys())
COLOURS = {21:'xkcd:fire engine red', 22:'xkcd:sea green'}
SHADE_COLOURS = {21: '#fe4d4e', 22: '#58ab8e'}
NUMBER_TESTS = 10 # number of tests to run
TEST_SIZE = 500 # ,6000,7000,8000,9000,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000
TEST_SIZE = 500
EDGE_PROBABILITY = 0.05
WEIGHT_RANGE = 10000
def plot_avg_counts_old(avgCounts):
# colours from https://xkcd.com/color/rgb/
linetypes = 5 * ["-"] + 5 * ["--"] + ["--", "-"] + ["-, -"]
plt.figure('Dijkstra with variable connectivity')
for k in TYPES.keys():
avgComps = [acounts[k] for acounts in avgCounts[0]]
plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgComps, color=COLOURS[k], linestyle='-',
label=TYPES[k] + " comparisons")
avgLinks = [acounts[k] for acounts in avgCounts[1]]
plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgLinks, color=COLOURS[k], linestyle='--',
label=TYPES[k] + " links")
plt.grid(True)
plt.legend(loc='best')
plt.show()
def plot_avg_counts(avgCounts):
"""generates and saves plot of results"""
# colours from https://xkcd.com/color/rgb/
MARKERS_COMP = {21:"o", 12:"d", 22:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html
MARKERS_LINK = {21:"o", 12:"D", 22:"D"}
plt.figure('avg number of operations in Dijkstra\'s algorithm')
deviations = [factor * EDGE_PROBABILITY for factor in range(1, 21, 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]]
......@@ -77,7 +55,6 @@ def plot_avg_counts(avgCounts):
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('Edge probability', fontsize=26)
plt.ylabel('Avg. number of operations / size', fontsize=26)
plt.xticks(fontsize=20)
......@@ -85,7 +62,6 @@ def plot_avg_counts(avgCounts):
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-dijkstra-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces
......@@ -143,26 +119,25 @@ if __name__ == "__main__":
for _ in range(NUMBER_TESTS):
# some nice graph generators here: https://networkx.github.io/documentation/stable/reference/generators.html
# initialize input graph
graph = nx.fast_gnp_random_graph(TEST_SIZE, x)
# graph = nx.random_regular_graph(10, 1000)
for (u, v) in graph.edges():
for (u, v) in graph.edges(): # assign weights
graph.edges[u, v]['w'] = random.randint(1, WEIGHT_RANGE)
for heapType in TYPES.keys():
for v in graph.nodes():
graph.nodes[v]['v'] = False # "visited" marker
linkCount = 0
compCount = 0
vertex2qnode = {}
vertex2qnode = {} # mapping graph nodes to heap nodes
dist = [888888888 for _ in range(len(graph.nodes()))]
prev = [None for _ in range(len(graph.nodes()))]
heap = PairingHeap(heapType, COUNT_TYPE_BOTH)
heap.make_heap()
source = graph.nodes()[0]
dist[0] = 0
# Dijkstra's algorithm
dist[0] = 0 # start node
for idx, v in enumerate(graph.nodes()):
#qnode = Node(dist[idx])
qnode = Node(dist[v])
qnode.vertex = v
vertex2qnode[v] = qnode
......@@ -180,18 +155,16 @@ if __name__ == "__main__":
s))
u = minNode.vertex
uk = minNode.key
#print(uk)
#print('extracted {}'.format(minNode.key))
graph.nodes[u]['v'] = True # minNode has been visited
for idx, v in enumerate(graph.neighbors(u)):
alt = uk + graph.edges[u, v]['w']
if alt < dist[v] and not graph.nodes[v]['v']:
(cc, lc) = heap.decrease_key(vertex2qnode[v], dist[v] - alt)
#print('decreased from {} to {}'.format(dist[v],alt))
linkCount += lc
compCount += cc
dist[v] = alt
prev[v] = u
# track avg. results
avgCountsLinks[heapType] += (linkCount / NUMBER_TESTS)/TEST_SIZE
avgCountsComps[heapType] += (compCount / NUMBER_TESTS)/TEST_SIZE
maxCountsLinks[heapType] = max(maxCountsLinks[heapType], linkCount/TEST_SIZE)
......@@ -212,6 +185,6 @@ if __name__ == "__main__":
maxCompsPerSize += [maxCountsComps]
minLinksPerSize += [minCountsLinks]
minCompsPerSize += [minCountsComps]
# plot_avg_counts([avgCompsPerSize, avgLinksPerSize])
plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize])
export_results(xs, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "dijkstra")
......@@ -4,7 +4,11 @@ as priority queue in Dijkstra's algorithm. Algorithm is run on randomly generate
10-regular graphs of variable size.
Results are stored as .csv files in ../data folder and plots of results in ../plots"""
import networkx as nx
import random
import matplotlib.pyplot as plt
import psutil
import csv
import os, sys, inspect
# ensuring imports from parent directory work
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
......@@ -12,11 +16,6 @@ parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
from node import Node
from pairing_heap import PairingHeap
import networkx as nx
import random
import matplotlib.pyplot as plt
import psutil
import csv
COUNT_TYPE_BOTH = 0
COUNT_TYPE_LINKS = -1
......@@ -24,9 +23,7 @@ COUNT_TYPE_COMPS = -2
TYPES = {21: "Pairing", 22: "Smooth"}
MAX_TYPE_KEY = max(TYPES.keys())
LINETYPES = 5 * ['-'] + 5 * ['--'] + ['--', '-'] + ['--', '--', '--', '--', '--', '--', '-', '-']
FIG_LABELS = ["comparisons", "links"]
MAX_TYPE_KEY = max(TYPES.keys())
# colours from https://xkcd.com/color/rgb/
COLOURS = {21:'xkcd:fire engine red', 22:'xkcd:sea green'}
......@@ -38,26 +35,12 @@ EDGE_PROBABILITY = 0.05
WEIGHT_RANGE = 10000
def plot_avg_counts_old(avgCounts):
plt.figure('Dijkstra with variable connectivity')
for k in TYPES.keys():
avgComps = [acounts[k] for acounts in avgCounts[0]]
plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgComps, color=COLOURS[k], linestyle='-',
label=TYPES[k] + " comparisons")
avgLinks = [acounts[k] for acounts in avgCounts[1]]
plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgLinks, color=COLOURS[k], linestyle='--',
label=TYPES[k] + " links")
plt.grid(True)
plt.legend(loc='best')
plt.show()
def plot_avg_counts(avgCounts):
MARKERS_COMP = {21:"o", 12:"d", 22:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html
MARKERS_LINK = {21:"o", 12:"D", 22:"D"}
plt.figure('avg number of operations in Dijkstra\'s algorithm')
deviations = [ 10+round(TEST_SIZE*20*factor * EDGE_PROBABILITY) for factor in range(1, 21, 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]]
......@@ -74,10 +57,8 @@ def plot_avg_counts(avgCounts):
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.rc('legend', fontsize=26) # using a size in points
#plt.gca().invert_xaxis()
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-dijkstra2-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces
......@@ -135,13 +116,11 @@ if __name__ == "__main__":
for _ in range(NUMBER_TESTS):
# some nice graph generators here: https://networkx.github.io/documentation/stable/reference/generators.html
##graph = nx.fast_gnp_random_graph(100, 0.1)
# initializing input
graph = nx.random_regular_graph(10,10+round(TEST_SIZE*x*20))
##graph = nx.random_regular_graph(10,100)
for (u, v) in graph.edges():
graph.edges[u, v]['w'] = random.randint(1, WEIGHT_RANGE)
for heapType in TYPES.keys():
#print('---')
for v in graph.nodes():
graph.nodes[v]['v'] = False # "visited" marker
linkCount = 0
......@@ -153,15 +132,13 @@ if __name__ == "__main__":
heap = PairingHeap(heapType, COUNT_TYPE_BOTH)
heap.make_heap()
source = graph.nodes()[0]
# Dijkstra's algorithm
dist[0] = 0
for idx, v in enumerate(graph.nodes()):
# qnode = Node(dist[idx])
qnode = Node(dist[v])
qnode.vertex = v
vertex2qnode[v] = qnode
(cc, lc) = heap.insert(qnode)
#print('inserted key {}',qnode.key)
linkCount += lc
compCount += cc
......@@ -175,18 +152,17 @@ if __name__ == "__main__":
s))
u = minNode.vertex
uk = minNode.key
#print(uk)
#print('extracted {}'.format(minNode.key))
graph.nodes[u]['v'] = True # minNode has been visited
for idx, v in enumerate(graph.neighbors(u)):
alt = uk + graph.edges[u, v]['w']
if alt < dist[v] and not graph.nodes[v]['v']:
(cc, lc) = heap.decrease_key(vertex2qnode[v], dist[v] - alt)
#print('decreased from {} to {}'.format(dist[v],alt))
linkCount += lc
compCount += cc
dist[v] = alt
prev[v] = u
# tracking avg. results
TSIZE = 10+round(TEST_SIZE*x*20)
avgCountsLinks[heapType] += (linkCount / NUMBER_TESTS)/TSIZE
avgCountsComps[heapType] += (compCount / NUMBER_TESTS)/TSIZE
......@@ -208,6 +184,6 @@ if __name__ == "__main__":
maxCompsPerSize += [maxCountsComps]
minLinksPerSize += [minCountsLinks]
minCompsPerSize += [minCountsComps]
# plot_avg_counts([avgCompsPerSize, avgLinksPerSize])
plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize])
export_results(xs, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "dijkstra")
......@@ -21,14 +21,8 @@ from pairing_heap import PairingHeap
COUNT_TYPE_BOTH = 0
COUNT_TYPE_LINKS = -1
COUNT_TYPE_COMPS = -2
MAXSIZE = 17
NUMBER_TESTS = 10 # number of tests to run
TEST_SIZES = [j for j in range(MAXSIZE)]
LIST_LEN = 10000 # number of elements in test list
TEST_SIZE = 10000 # number of elements in test list
STEP_SIZE = 100
INCREMENT_LOC = 0.01
INCREMENT_SUBSEQS = 100
TYPES = {0: "Pairing", 12: "Smooth"}
MAX_TYPE_KEY = max(TYPES.keys())
COLOURS = {0: 'xkcd:fire engine red', 12: 'xkcd:green'}
......@@ -47,7 +41,6 @@ def localizedShuffleByIndex(llist, sdev):
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
......@@ -59,7 +52,6 @@ def plot_avg_counts(avgCounts):
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]]
......@@ -71,7 +63,6 @@ def plot_avg_counts(avgCounts):
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)
......@@ -126,7 +117,6 @@ if __name__ == "__main__":
minCompsPerSize = []
sortedInput = []
#testInput = []
# ----------localised permutation inputs--------------
# randomness parameter: standard deviation
......
......@@ -57,7 +57,6 @@ def plot_avg_counts(avgCounts):
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)
......@@ -131,7 +130,6 @@ if __name__ == "__main__":
minCompsPerSize = []
sortedInput = []
# testInput = []
# ----------separable permutation---------------------
# parameter: length (must be power of two)
......@@ -147,7 +145,6 @@ if __name__ == "__main__":
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():
......
......@@ -23,12 +23,9 @@ from pairing_heap import PairingHeap
COUNT_TYPE_BOTH = 0
COUNT_TYPE_LINKS = -1
COUNT_TYPE_COMPS = -2
MAXSIZE = 17
NUMBER_TESTS = 20 # number of tests to run
TEST_SIZES = [j for j in range(MAXSIZE)]
LIST_LEN = 10000 # number of elements in test list
TEST_SIZE = 10000 # number of elements in test list
STEP_SIZE = 100
INCREMENT_SUBSEQS = 100
TYPES = {0: "Pairing", 12: "Smooth"}
MAX_TYPE_KEY = max(TYPES.keys())
......@@ -129,7 +126,6 @@ if __name__ == "__main__":
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)]
......@@ -146,7 +142,6 @@ if __name__ == "__main__":
for zz in range(NUMBER_TESTS):
testInput = generateContSortedSubseq(sortedInput, x)
#print(len(testInput))
for heapType in TYPES.keys():
linkCount = 0
compCount = 0
......
......@@ -8,8 +8,6 @@ from random import shuffle
import matplotlib.pyplot as plt
import copy
import csv
from node import Node
from pairing_heap import PairingHeap
import os, sys, inspect
......@@ -17,17 +15,14 @@ import os, sys, inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
from node import Node
from pairing_heap import PairingHeap
COUNT_TYPE_BOTH = 0
COUNT_TYPE_LINKS = -1
COUNT_TYPE_COMPS = -2
MAXSIZE = 18
NUMBER_TESTS = 5 # number of tests to run
TEST_SIZES = [j for j in range(MAXSIZE)] #
LIST_LEN = 10000 # number of elements in test list
TEST_SIZE = 10000 # number of elements in test list
STEP_SIZE = 100
INCREMENT_LOC = 0.005
INCREMENT_SUBSEQS = 100
TYPES = {0: "Pairing", 12: "Smooth"}
MAX_TYPE_KEY = max(TYPES.keys())
COLOURS = {0: 'xkcd:fire engine red', 12: 'xkcd:green'}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment