-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.hpp
374 lines (374 loc) · 16.8 KB
/
Algorithms.hpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//-- Check RKACPB Project Builder
# ifndef RKACPB
# pragma message \
"This Project is Part of 40021441054102 Projects. You are Using It as a Standalone Project"
# pragma message \
"If You Want to Compile This Project Under Support, You Must Use RKACPB from https://github.com/RamtinKosari/RKACPB"
# pragma message \
"Compiling without RKACPB Support."
# else
# pragma message \
"40021441054102 Project Configured and Initialized Successfully, Compiling Under RKACPB Support"
# endif // RKACPB
//-- Algorithms
# ifndef ALGORITHMS_OMID_SOJOODI
/**
* @file Algorithms.hpp
* @author Ramtin Kosari (ramtinkosari@gmail.com)
* @brief Algorithms Header File
* @date 2024-03-21
* @def ALGORITHMS_OMID_SOJOODI
* @brief Algorithms Header File Macro
* @details This Header File Contains All Needed Definitions and Classes for Algorithms
*/
# define ALGORITHMS_OMID_SOJOODI
/**
* @enum ENUM_ALGORITHM_ENVIRONMENT
* @brief Data Generation Methods
* @details This Enum Contains All Supported Data Generation Methods for Algorithms Class
*
* @param USE_RANDOM_DATA Use Random Data, Generates Random Data for Algorithms
* @param USE_INPUT_FILE Use Input File, Loads Data from Input File for Algorithms
*/
enum ENUM_ALGORITHMS_DATA_METHOD {
USE_RANDOM_DATA,
USE_INPUT_FILE,
};
/**
* @enum ENUM_RANDOM_DATA_SHAPES
* @brief Random Data Shapes
* @details This Enum Contains All Supported Random Data Shapes for Algorithms Class
*
* @param RANDOM_DATA_SHAPE_LEMNISCATE_OF_BERNOULLI Generates Random Data in Lemniscate of Bernoulli Shape
* @param RANDOM_DATA_SHAPE_LISSAJOUS_CURVE Generates Random Data in Lissajous Curve Shape
* @param RANDOM_DATA_SHAPE_HYPOTROCHOID Generates Random Data in Hypotrochoid Shape
* @param RANDOM_DATA_SHAPE_SPIRAL_ROAD Generates Random Data in Spiral Road Shape
* @param RANDOM_DATA_SHAPE_NAUTILUS Generates Random Data in Nautilus Shape
* @param RANDOM_DATA_SHAPE_ASTROID Generates Random Data in Astroid Shape
* @param RANDOM_DATA_SHAPE_DELTOID Generates Random Data in Deltoid Shape
* @param RANDOM_DATA_SHAPE_CLUSTER Generates Random Data in Cluster Shape
* @param RANDOM_DATA_SHAPE_ELLIPSE Generates Random Data in Ellipse Shape
* @param RANDOM_DATA_SHAPE_SATURN Generates Random Data in Saturn Planet Shape
* @param RANDOM_DATA_SHAPE_ROSE_8 Generates Random Data in Rose 8 Petal Shape
* @param RANDOM_DATA_SHAPE_ROSE_5 Generates Random Data in Rose 5 Petal Shape
* @param RANDOM_DATA_SHAPE_ROSE_4 Generates Random Data in Rose 4 Petal Shape
* @param RANDOM_DATA_SHAPE_ROSE_3 Generates Random Data in Rose 3 Petal Shape
* @param RANDOM_DATA_SHAPE_STAR_1 Generates Random Data in Star Model 1 Shape
* @param RANDOM_DATA_SHAPE_STAR_2 Generates Random Data in Star Model 2 Shape
* @param RANDOM_DATA_SHAPE_SPIRAL Generates Random Data in Spiral Shape
* @param RANDOM_DATA_SHAPE_LINE Generates Random Data in Line Shape
* @param RANDOM_DATA_SHAPE_qb Generates Random Data in qb Shape
*/
enum ENUM_RANDOM_DATA_SHAPES {
RANDOM_DATA_SHAPE_LEMNISCATE_OF_BERNOULLI,
RANDOM_DATA_SHAPE_LISSAJOUS_CURVE,
RANDOM_DATA_SHAPE_HYPOTROCHOID,
RANDOM_DATA_SHAPE_SPIRAL_ROAD,
RANDOM_DATA_SHAPE_NAUTILUS,
RANDOM_DATA_SHAPE_ASTROID,
RANDOM_DATA_SHAPE_DELTOID,
RANDOM_DATA_SHAPE_CLUSTER,
RANDOM_DATA_SHAPE_ELLIPSE,
RANDOM_DATA_SHAPE_SATURN,
RANDOM_DATA_SHAPE_ROSE_8,
RANDOM_DATA_SHAPE_ROSE_5,
RANDOM_DATA_SHAPE_ROSE_4,
RANDOM_DATA_SHAPE_ROSE_3,
RANDOM_DATA_SHAPE_STAR_1,
RANDOM_DATA_SHAPE_STAR_2,
RANDOM_DATA_SHAPE_SPIRAL,
RANDOM_DATA_SHAPE_LINE,
RANDOM_DATA_SHAPE_qb
};
/**
* @enum ENUM_CALCULATE_DATA_THETA
* @brief Calculate Data Theta
* @details This Enum Contains All Supported Theta Calculation Methods for Algorithms Class
*
* @param CALCULATE_DATA_THETA_HIGHEST Calculate Data Theta from Highest Point
* @param CALCULATE_DATA_THETA_LOWEST Calculate Data Theta from Lowest Point
* @param CALCULATE_DATA_THETA_CORNER Calculate Data Theta from Corner Point
* @param CALCULATE_DATA_THETA_MIDDLE Calculate Data Theta from Middle Point
*/
enum ENUM_CALCULATE_DATA_THETA {
CALCULATE_DATA_THETA_HIGHEST,
CALCULATE_DATA_THETA_LOWEST,
CALCULATE_DATA_THETA_CORNER,
CALCULATE_DATA_THETA_MIDDLE,
};
/**
* @def MAX_RANDOM_DATA
* @brief Maximum Amount of Random Data
* @details This Macro Defines Maximum Amount of Random Data to Generate Input Elements for Algorithms
*
* @warning Maximum Random Data Value is Set According to the Algorithm's Needs and Note that Higher Values May Cause Memory Issues and Performance Degradation
*/
# define MAX_RANDOM_DATA 3773
/**
* @enum ENUM_ALGORIHTMS_PROCESSING_METHODS
* @brief Algorithms Processing Methods
* @details This Enum Contains All Supported Processing Methods for Algorithms Class
*
* @param USE_SINGLE_THREAD Use Single Thread, Processes Algorithms with Single Thread
* @param USE_MULTI_THREADS Use Multi Threads, Processes Algorithms with Multi Threads
*/
enum ENUM_ALGORIHTMS_PROCESSING_METHODS {
USE_SINGLE_THREAD,
USE_MULTI_THREADS
};
/**
* @enum ENUM_SUPPORTED_ALGORITHMS
* @brief Supported Algorithms
* @details This Enum Contains All Supported Algorithms for Algorithms Class
*
* @note Each Algorithm Has Its Own Data and Processing Method, So You Must Define Them Before Using the Algorithm
*
* @param ALGORITHM_SEARCH_BINARY Binary Search Algorithm
* @param ALGORITHM_SORT_INSERTION Insertion Sort Algorithm
* @param ALGORITHM_SORT_SELECTION Selection Sort Algorithm
* @param ALGORITHM_SORT_BUBBLE Bubble Sort Algorithm
* @param ALGORITHM_SORT_QUICK Quick Sort Algorithm
* @param ALGORITHM_SORT_MERGE Merge Sort Algorithm
* @param ALGORITHM_SORT_HEAP Heap Sort Algorithm
* @param ALGORITHM_MIN_MAX_ARRAY Min Max Array Algorithm
* @param ALGORITHM_LARGE_NUMBERS_MULTIPLICATION Large Numbers Multiplication Algorithm
* @param ALGORITHM_MATRIX_MULTIPLICATION_STRASSEN Strassen Matrix Multiplication Algorithm
* @param ALGORITHM_MATRIX_MULTIPLICATION_SIMPLE Simple Matrix Multiplication Algorithm
* @param ALGORITHM_MATRIX_MULTIPLICATION_BLOCK Block Matrix Multiplication Algorithm
* @param ALGORITHM_MATRIX_CHAIN_MULTIPLICATION Matrix Chain Multiplication Algorithm
* @param ALGORITHM_DIJKSTRA Dijkstra Algorithm
* @param ALGORITHM_FLOYD Floyd Algorithm
* @param ALGORITHM_KNAPSACK_0_1 0/1 Knapsack Algorithm
* @param ALGORITHM_KNAPSACK_FRACTIONAL Fractional Knapsack Algorithm
* @param ALGORITHM_KNAPSACK_BRANCH_BOUNDED Branch and Bounded Knapsack Algorithm
* @param ALGORITHM_TRAVELLER_SALESMAN Traveller Salesman Algorithm
* @param ALGORITHM_MOVING_ON_GRID Moving on Grid Algorithm
* @param ALGORITHM_N_QUEENS N Queens Algorithm
* @param ALGORITHM_BFS Breadth First Search Algorithm
* @param ALGORITHM_DFS Depth First Search Algorithm
* @param ALGORITHM_HUFFMAN_CODING Huffman Coding Algorithm
* @param ALGORITHM_MINIMUM_SPANNING_TREE_PRIM Minimum Spanning Tree Prim Algorithm
* @param ALGORITHM_MINIMUM_SPANNING_TREE_KRUSKAL Minimum Spanning Tree Kruskal Algorithm
*/
enum ENUM_SUPPORTED_ALGORITHMS {
ALGORITHM_SEARCH_BINARY,
ALGORITHM_SORT_INSERTION,
ALGORITHM_SORT_SELECTION,
ALGORITHM_SORT_BUBBLE,
ALGORITHM_SORT_QUICK,
ALGORITHM_SORT_MERGE,
ALGORITHM_SORT_HEAP,
ALGORITHM_MIN_MAX_ARRAY,
ALGORITHM_LARGE_NUMBERS_MULTIPLICATION,
ALGORITHM_MATRIX_MULTIPLICATION_STRASSEN,
ALGORITHM_MATRIX_MULTIPLICATION_SIMPLE,
ALGORITHM_MATRIX_MULTIPLICATION_BLOCK,
ALGORITHM_MATRIX_CHAIN_MULTIPLICATION,
ALGORITHM_DIJKSTRA,
ALGORITHM_FLOYD,
ALGORITHM_KNAPSACK_0_1,
ALGORITHM_KNAPSACK_FRACTIONAL,
ALGORITHM_KNAPSACK_BRANCH_BOUNDED,
ALGORITHM_TRAVELLER_SALESMAN,
ALGORITHM_MOVING_ON_GRID,
ALGORITHM_N_QUEENS,
ALGORITHM_BFS,
ALGORITHM_DFS,
ALGORITHM_HUFFMAN_CODING,
ALGORITHM_MINIMUM_SPANNING_TREE_PRIM,
ALGORITHM_MINIMUM_SPANNING_TREE_KRUSKAL
};
//-- Include Needed Headers
# ifndef GRAPHICS_OMID_SOJOODI
# include "Graphics.hpp"
# endif // GRAPHICS_OMID_SOJOODI
//-- Include Sort Algorithms
# ifndef ALGORITHMS_OMID_SOJOODI_SORTS
# include "Sorts/Sorts.hpp"
# endif // ALGORITHMS_OMID_SOJOODI_SORTS
//-- Include N Queens Algorithm
# ifndef ALGORITHMS_OMID_SOJOODI_NQUEENS
# include "NQueens/NQueens.hpp"
# endif // ALGORITHMS_OMID_SOJOODI_NQUEENS
/**
* @def ALGORITHMS_LABEL
* @brief Algorithms Log Message
* @details This Macro Defines Algorithms Log Message Label
*/
# define ALGORITHMS_LABEL "\033[38;2;177;245;217m[ALGORITHMS]\033[0m "
//-- Include Needed Libraries
# include <iostream>
# include <chrono>
# include <vector>
# include <random>
/**
* @class Algorithms
* @brief Algorithms Class
* @details This Class Contains All Needed Definitions and Methods for Algorithms
*
* @param algorithm_duration Process Time of Algorithm
* @param begin_time Begin Time of Algorithm
* @param end_time End Time of Algorithm
* @param data Data Vector
*
* @see ENUM_ALGORIHTMS_PROCESSING_METHODS (\ref Algorithms.hpp "Algorithm" Processing Methods)
* @see ENUM_ALGORITHMS_DATA_METHOD (\ref Algorithms.hpp "Algorithm" Data Generation Methods)
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
* @see ENUM_SUPPORTED_ALGORITHMS (\ref Algorithms.hpp "Algorithm" Supported by the Class)
* @see MAX_RANDOM_DATA (Maximum Random Data Value)
* @see Algorithms.hpp
*/
class Algorithms {
private:
Graphics graphics;
/**
* @brief Represents the 2D Data Vector.
* @details The Variable is Used to Store the 2D Data for the Algorithm.
*/
std::vector<env::Point2D> data2D;
/**
* @brief Represents the 3D Data Vector.
* @details The Variable is Used to Store the 3D Data for the Algorithm.
*/
std::vector<env::Point3D> data3D;
/**
* @brief Represents the Starting Time Point.
* @details The Variable is Initialized in the Constructor and Used to Calculate the Processing Time of the Algorithm.
*/
std::chrono::high_resolution_clock::time_point begin_time;
/**
* @brief Represents the Ending Time Point.
* @details The Variable is Initialized in the Destructor and Used to Calculate the Processing Time of the Algorithm.
*/
std::chrono::high_resolution_clock::time_point end_time;
/**
* @brief Represents the Duration of the Algorithm.
* @details The Variable is Calculated in the Destructor and Used to Show the Processing Time of the Algorithm.
*/
std::chrono::duration<double> algorithm_duration;
/**
* @brief Represents the Data Vector.
* @details The Variable is Used to Store the Input Data for the Algorithm.
*/
std::vector<int> data;
/**
* @brief Represents the Base Index of the Data.
* @details The Variable is Used to Store the Base Index of the Data for the Algorithm.
*/
int base_index;
public:
/**
* @brief Algorithms Constructor
* @details This Constructor Initializes the Algorithm with the Given Parameters.
*
* @param algorithm The Algorithm to Use
* @param environment The Environment to Use
* @param dataMethod The Data Method to Use
* @param processingMethod The Processing Method to Use
*
* @see ENUM_SUPPORTED_ALGORITHMS (\ref Algorithms.hpp "Algorithm" Supported by the Class)
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
* @see ENUM_ALGORITHMS_DATA_METHOD (\ref Algorithms.hpp "Algorithm" Data Generation Methods)
* @see ENUM_ALGORIHTMS_PROCESSING_METHODS (\ref Algorithms.hpp "Algorithm" Processing Methods)
*/
Algorithms(
ENUM_SUPPORTED_ALGORITHMS algorithm,
ENUM_ALGORITHM_ENVIRONMENT environment,
ENUM_ALGORITHMS_DATA_METHOD data_method = USE_RANDOM_DATA,
ENUM_ALGORIHTMS_PROCESSING_METHODS processing_method = USE_SINGLE_THREAD
);
/**
* @brief Algorithms Destructor
* @details This Destructor Finalizes the Algorithm and Shows the Processing Time.
*/
~Algorithms();
/**
* @brief Generate Random 3D Environment Data
* @details This Method Generates Random 3D Environment Data for the Algorithm.
*
* @param amount The Amount of Data to Generate
* @param min The Minimum Value of Data
* @param max The Maximum Value of Data
*
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
*/
void generateRandom3DData(
int amount,
int min,
int max
);
/**
* @brief Generate Random 2D Environment Data
* @details This Method Generates Random 2D Environment Data for the Algorithm.
*
* @param amount The Amount of Data to Generate
* @param min The Minimum Value of Data
* @param max The Maximum Value of Data
*
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
*/
void generateRandom2DData(
int amount,
int min,
int max
);
/**
* @brief Generate Random Data
* @details This Method Generates Random Data for the Algorithm.
*
* @param amount The Amount of Data to Generate
* @param min The Minimum Value of Data
* @param max The Maximum Value of Data
*/
void generateRandomData(
int amount,
int min,
int max,
ENUM_ALGORITHM_ENVIRONMENT environment = ENVIRONMENT_2D,
ENUM_RANDOM_DATA_SHAPES shape = RANDOM_DATA_SHAPE_ELLIPSE,
ENUM_CALCULATE_DATA_THETA calc_theta = CALCULATE_DATA_THETA_CORNER
);
/**
* @brief Generate Chess Board
* @details This Method Generates Chess Board for the Algorithm.
*
* @param size The Size of the Chess Board
* @param environment The Environment to Use
*/
void generateChessBoardData(
int size,
ENUM_SHOW_BOX_METHODS method = SHOW_BOX_CHESS_WEIGHTED,
ENUM_ALGORITHM_ENVIRONMENT environment = ENVIRONMENT_2D
);
/**
* @brief Load Data from File
* @details This Method Loads Data from File for the Algorithm.
*
* @param file_name The File Name to Load Data from
*
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
*/
void loadDataFromFile(
std::string file_name
);
/**
* @brief Sort Algorithms
* @details This Method Contains Several Sort Algorithms to Use.
*
* @param ALGORITHM_SORT_INSERTION Insertion Sort Algorithm
* @param ALGORITHM_SORT_SELECTION Selection Sort Algorithm
* @param ALGORITHM_SORT_BUBBLE Bubble Sort Algorithm
* @param ALGORITHM_SORT_QUICK Quick Sort Algorithm
* @param ALGORITHM_SORT_MERGE Merge Sort Algorithm
* @param ALGORITHM_SORT_HEAP Heap Sort Algorithm
*/
Sorts sorts;
/**
* @brief N Queens Algorithm
* @details This Method Contains N Queens Algorithm to Use.
*
* @param ALGORITHM_N_QUEENS N Queens Algorithm
*/
NQueens nqueens;
};
# endif // ALGORITHMS_OMID_SOJOODI