-
Notifications
You must be signed in to change notification settings - Fork 10
/
algorithm_visualizer.py
200 lines (146 loc) · 6.52 KB
/
algorithm_visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import imageio
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import seaborn as sns
sns.set()
import numpy as np
from tqdm import trange
import os
class alvito:
def __init__(self):
self._ims = []
self.fps = 5
self.input_shape = (0,0)
self.title = ""
self.comparisons = 0
self.track_operations = True
self.show_title = True
self.title_fontsize = 15
self.show_xlable = True
self.xlable_fontsize = 15
self.rectangle_color_1 = 'black'
self.rectangle_color_2 = 'gold'
self.rectangle_linewidth = 4
self.colormap = 'cool'
self.numbers_color = "dynamic"
self.numbers_fontsize = 15
self.dpi = 100
self.save_dir = 'gifs/'
self.custom_save_name = False
self.save_name = ''
def visulize_algorithm(self,array,i_1,i_2,i_3,ec1,ec2,ec3):
# reahsape array into its original shape
array = np.array(array).reshape(self.input_shape[0], self.input_shape[1])
# find coordinates for the rectangles
dx = 0.05
dy = dx
index=[(i_1%self.input_shape[1]+dx,i_1//self.input_shape[1]+dy),
(i_2%self.input_shape[1]+dx,i_2//self.input_shape[1]+dy),
(i_3%self.input_shape[1]+dx,i_3//self.input_shape[1]+dy)]
if self.numbers_color == 'dynamic':
annot_kws = { 'fontsize' : self.numbers_fontsize }
else:
annot_kws = { 'fontsize' : self.numbers_fontsize , 'color' : self.numbers_color }
# set plot figsize
scale = 0.8
figsize = (self.input_shape[1]*scale, self.input_shape[0]*scale)
fig, ax = plt.subplots(figsize=figsize)
# create heatmap
ax = sns.heatmap(array,
annot=True,
annot_kws = annot_kws,
fmt="d",
linewidths=2,
cbar=False,
cmap=self.colormap,
yticklabels=False,
xticklabels=False,
square=True)
# plot title
if self.show_title:
ax.set_title(self.title, fontsize=self.title_fontsize )
# plot number of comparisons
if self.show_xlable:
plt.xlabel(f'Comparisons:\n{self.comparisons}',fontsize=self.xlable_fontsize)
if self.track_operations:
# draw rectangle around tracked cells
if i_1 != -1:
ax.add_patch(Rectangle(index[0], 0.9, 0.9, fill=False, edgecolor=ec1, lw=self.rectangle_linewidth))
if i_2 != -1:
ax.add_patch(Rectangle(index[1], 0.9, 0.9, fill=False, edgecolor=ec2, lw=self.rectangle_linewidth))
if i_3 != -1:
ax.add_patch(Rectangle(index[2], 0.9, 0.9, fill=False, edgecolor=ec3, lw=self.rectangle_linewidth))
# create and save gif
if not os.path.exists('temp/'):
os.mkdir('temp/')
img_loc = 'temp/' + 'temp_image_{:d}'.format(self.comparisons+1) + '.png'
plt.savefig(img_loc, bbox_inches='tight', dpi=self.dpi)
self._ims.append(imageio.imread(img_loc))
os.remove(img_loc)
plt.close()
def bubbleSort(self, array, title="Bubble Sort"):
self.title = title
array = self._array_transform(array)
self.comparisons = 0
for passnum in trange(len(array)-1,0,-1):
for i in range(passnum):
self.comparisons += 1
if array[i]>array[i+1]:
temp = array[i]
array[i] = array[i+1]
array[i+1] = temp
self.visulize_algorithm(array,i,i+1,-1,ec1=self.rectangle_color_2,ec2=self.rectangle_color_2,ec3=self.rectangle_color_2)
else:
self.visulize_algorithm(array,i,i+1,-1,ec1=self.rectangle_color_1,ec2=self.rectangle_color_1,ec3=self.rectangle_color_1)
if self.custom_save_name:
imageio.mimsave(f'{self.save_dir}{self.save_name}.gif', self._ims, fps=self.fps)
else:
imageio.mimsave(f'{self.save_dir}/bubble_sort_comparisons_{self.comparisons}.gif', self._ims, fps=self.fps)
self._ims = []
def insertionSort(self, array, title="Insertion Sort"):
self.title = title
array = self._array_transform(array)
self.comparisons = 0
for index in trange(1,len(array)):
currentvalue = array[index]
position = index
while position>0 and array[position-1]>currentvalue:
self.comparisons += 1
array[position]=array[position-1]
position = position-1
array[position]=currentvalue
self.visulize_algorithm(array,index,position,-1,ec1=self.rectangle_color_1,ec2=self.rectangle_color_1,ec3=self.rectangle_color_1)
if self.custom_save_name:
imageio.mimsave(f'{self.save_dir}{self.save_name}.gif', self._ims, fps=self.fps)
else:
imageio.mimsave(f'{self.save_dir}insertion_sort_comparisons_{self.comparisons}.gif', self._ims, fps=self.fps)
self._ims = []
def selectionSort(self, array, title="Selection Sort"):
self.title = title
array = self._array_transform(array)
self.comparisons = 0
for fillslot in trange(len(array)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
self.comparisons += 1
self.visulize_algorithm(array,fillslot,positionOfMax,location,ec1=self.rectangle_color_2,ec2=self.rectangle_color_2,ec3=self.rectangle_color_1)
if array[location]>array[positionOfMax]:
positionOfMax = location
temp = array[fillslot]
array[fillslot] = array[positionOfMax]
array[positionOfMax] = temp
if self.custom_save_name:
imageio.mimsave(f'{self.save_dir}{self.save_name}.gif', self._ims, fps=self.fps)
else:
imageio.mimsave(f'{self.save_dir}selection_sort_comparisons_{self.comparisons}.gif', self._ims, fps=self.fps)
self._ims = []
def _array_transform(self, array):
if type(array) == list:
array = np.array(array)
if array.ndim == 1:
array = np.array([array])
self.input_shape = array.shape
# flatten the array so it can be used by the algorithm
array = array.flatten()
return array