Repository navigation
Expand file tree
/
Copy pathGrokkingAlgorithm.cpp
More file actions
339 lines (267 loc) · 11.5 KB
/
Copy pathGrokkingAlgorithm.cpp
File metadata and controls
339 lines (267 loc) · 11.5 KB
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
#include "GrokkingAlgorithm.h"
GrokkingAlgorithm::GrokkingAlgorithm(){}
GrokkingAlgorithm::~GrokkingAlgorithm(){}
void GrokkingAlgorithm::runDemoChapter_1()
{
// ========= demo Chapter 1 -- Binary Search =========
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 1 -- Binary Search " << std::endl
<< " =========================" << std::endl;
std::cout << "Original Data : " << std::endl;
PrintData(array, 12);
std::cout << "Input an integer for search: " << std::endl;
int val = 0;
std::cin >> val;
std::cout << "search result: " << GrokkingAlgorithm::BinarySearch_Ch1(array, 12, val) << std::endl << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_2()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 2 -- Selection Sort " << std::endl
<< " =========================" << std::endl;
std::list<int> lst = { 5, 3, 6, 2, 10 };
std::list<int> sortedLst;
GrokkingAlgorithm::SelectionSort_Ch2(lst, sortedLst);
std::cout << "Original List:" << std::endl;
PrintData(lst);
std::cout << "Sorted List:" << std::endl;
PrintData(sortedLst);
std::cout << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_3()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 3 -- Recursion " << std::endl
<< " =========================" << std::endl;
int num;
std::cout << "Input an integer for Fibonacci number generation(no more than 20 is preferable): ";
std::cin >> num;
std::cout << std::endl << "Fibonacci (" << num << ") is " << GrokkingAlgorithm::Recursion_Ch3(num) << std::endl << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_4()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 4 -- Quick Sort " << std::endl
<< " =========================" << std::endl;
/* Max Square */
std::cout << std::endl << "solution for Max Square " << std::endl;
std::cout << "Input two integers for width and height of a rectangle: " << std::endl;
int w, h;
std::cin >> w >> h;
std::cout << "side length of the max square: " << GrokkingAlgorithm::MaxSquare_Ch4(w, h) << std::endl;
/* Sum */
std::cout << std::endl << "solution for Sum " << std::endl;
int array[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << "Original Data: ";
PrintData(array, 9);
std::cout << "The sum is: " << GrokkingAlgorithm::Sum_Ch4(array, 9) << std::endl;
/* Quick Sort */
std::cout << std::endl << "solution for Quick Sort " << std::endl;
int unorderedArray[11] = { 25, 6, 1, 3, 8, 10, 52, 30, 15, 0, 55 };
std::cout << "Original Data: " << std::endl;
PrintData(unorderedArray, 11);
GrokkingAlgorithm::QuickSort_Ch4(unorderedArray, 11);
std::cout << "ordered array:" << std::endl;
PrintData(unorderedArray, 11);
/* Merge Sort */
std::cout << std::endl << "solution for Merge Sort " << std::endl;
int unorderedArray2[12] = { 5, 6, 100, 1, 3, 8, 10, 52, 30, 15, 0, 55 };
std::cout << "Original Data: " << std::endl;
PrintData(unorderedArray2, 12);
GrokkingAlgorithm::MergeSort_Ch4(unorderedArray2, 12);
std::cout << "ordered array:" << std::endl;
PrintData(unorderedArray2, 12);
std::cout << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_5()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 5 -- Hash Table " << std::endl
<< " =========================" << std::endl;
std::cout << "Initialize a map | <name, age>" << std::endl;
std::map<std::string, int> mapping{ { "Allen", 32 }, { "Bob", 22 }, { "Alice", 28 } };
std::map < std::string, int>::iterator iter;
std::cout << "Map is initialized as follows:" << std::endl;
for (iter = mapping.begin(); iter != mapping.end(); ++iter)
std::cout << "Name: " << iter->first << ",\tAge: " << iter->second << std::endl;
GrokkingAlgorithm::HashTable_Ch5(mapping);
std::cout << "\nAll in map are as follows:" << std::endl;
for (iter = mapping.begin(); iter != mapping.end(); ++iter)
std::cout << "Name: " << iter->first << ",\tAge: " << iter->second << std::endl;
std::cout << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_6()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 6 -- Breadth First Search (BFS) " << std::endl
<< " =========================" << std::endl;
std::cout << "initialize a personal relationship network by hash map" << std::endl;
std::map<std::string, std::vector<std::string>> relationNet{ { "you", { "alice", "bob", "claire" } },
{ "bob", { "anuj", "peggy" } }, { "alice", { "peggy" } }, { "claire", { "thom", "jonny" } },
{ "anuj", {} }, { "peggy", {} }, { "thom", { "you" } }, { "jonny", {} } };
/* assume the right one */
std::set<std::string> seller{ "jonny", "doggy", "catty" };
std::pair<bool, std::string> ret = GrokkingAlgorithm::BreadthFirstSearch_Ch6(relationNet, seller);
if (ret.first)
std::cout << "FOUND!" << std::endl << ret.second << " is a seller!" << std::endl << std::endl;
else
std::cout << "Not FOUND !" << std::endl << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_7()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 7 -- Dijkstra Algorithm " << std::endl
<< " =========================" << std::endl;
std::map<std::string, std::map<std::string, int>> graph1, graph2, graph3, graph4, graph5;
/* graph 1 */
graph1["start"]["a"] = 5;
graph1["start"]["b"] = 0;
graph1["a"]["c"] = 15;
graph1["a"]["d"] = 20;
graph1["b"]["c"] = 30;
graph1["b"]["d"] = 35;
graph1["c"]["fin"] = 20;
graph1["d"]["fin"] = 10;
/* graph 2 */
graph2["start"]["A"] = 6;
graph2["start"]["B"] = 2;
graph2["A"]["fin"] = 1;
graph2["B"]["A"] = 3;
graph2["B"]["fin"] = 5;
/* graph 3 */
graph3["start"]["b"] = 5;
graph3["start"]["c"] = 2;
graph3["b"]["d"] = 4;
graph3["b"]["e"] = 2;
graph3["c"]["b"] = 8;
graph3["c"]["e"] = 7;
graph3["d"]["fin"] = 3;
graph3["d"]["e"] = 6;
graph3["e"]["fin"] = 1;
/* graph 4 */
graph4["start"]["b"] = 10;
graph4["b"]["d"] = 20;
graph4["d"]["c"] = 1;
graph4["c"]["b"] = 1;
graph4["d"]["fin"] = 30;
/* graph 5 */
/* This is a cyclic graph with negative side, no more Dijkstra */
graph5["start"]["b"] = 2;
graph5["start"]["d"] = 2;
graph5["d"]["b"] = 2;
graph5["b"]["fin"] = 2;
graph5["b"]["e"] = 2;
graph5["e"]["d"] = -5;
graph5["e"]["fin"] = 2;
std::map<std::string, int> fin;
graph1["fin"] = graph2["fin"] = graph3["fin"] = graph4["fin"] = graph5["fin"] = fin;
std::pair<std::vector<std::string>, int> ret1, ret2, ret3, ret4, ret5;
ret1 = GrokkingAlgorithm::Dijkstra_Ch7(graph1);
ret2 = GrokkingAlgorithm::Dijkstra_Ch7(graph2);
ret3 = GrokkingAlgorithm::Dijkstra_Ch7(graph3);
ret4 = GrokkingAlgorithm::Dijkstra_Ch7(graph4);
ret5 = GrokkingAlgorithm::Dijkstra_Ch7(graph5);
size_t i = 0, dims;
std::vector<std::string>::iterator iter;
dims = ret1.first.size();
std::cout << "Shortest route:" << std::endl;
for (iter = ret1.first.begin(); i < dims - 1; ++i, ++iter)
std::cout << *iter << " <- ";
std::cout << *iter << std::endl << "Shortest distance:\t" << ret1.second << std::endl << std::endl;
dims = ret2.first.size();
std::cout << "Shortest route:" << std::endl;
for (iter = ret2.first.begin(); i < dims - 1; ++i, ++iter)
std::cout << *iter << " <- ";
std::cout << *iter << std::endl << "Shortest distance:\t" << ret2.second << std::endl << std::endl;
dims = ret3.first.size();
std::cout << "Shortest route:" << std::endl;
for (iter = ret3.first.begin(); i < dims - 1; ++i, ++iter)
std::cout << *iter << " <- ";
std::cout << *iter << std::endl << "Shortest distance:\t" << ret3.second << std::endl << std::endl;
dims = ret4.first.size();
std::cout << "Shortest route:" << std::endl;
for (iter = ret4.first.begin(); i < dims - 1; ++i, ++iter)
std::cout << *iter << " <- ";
std::cout << *iter << std::endl << "Shortest distance:\t" << ret4.second << std::endl << std::endl;
dims = ret5.first.size();
std::cout << "Shortest route:" << std::endl;
for (iter = ret5.first.begin(); i < dims - 1; ++i, ++iter)
std::cout << *iter << " <- ";
std::cout << *iter << std::endl << "Shortest distance:\t" << ret5.second << std::endl << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_8()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 8 -- GreedyAlgorithm " << std::endl
<< " =========================" << std::endl;
/* prepare data */
std::set<std::string> states_needed = { "mt", "wa", "or", "id", "nv", "ut", "ca", "az" };
std::map<std::string, std::set<std::string>> stations;
stations["kone"] = std::set < std::string > { "id", "nv", "ut" };
stations["ktwo"] = std::set < std::string > {"wa", "id", "mt"};
stations["kthree"] = std::set < std::string > {"or", "nv", "ca"};
stations["kfour"] = std::set < std::string > {"nv", "ut"};
stations["kfive"] = std::set < std::string > {"ca", "az"};
std::set<std::string> ret = GrokkingAlgorithm::GreedyAlgorithm_Ch8(states_needed, stations);
if (!ret.empty())
{
std::cout << "Solution FOUND!" << std::endl;
for (auto &elem : ret)
std::cout << elem << ", ";
std::cout << std::endl << std::endl;
}
else
std::cout << "No Solution!" << std::endl << std::endl;
return;
}
void GrokkingAlgorithm::runDemoChapter_9()
{
std::cout << "\n =========================" << std::endl
<< " Demo for Chapter 9 -- Dynamic Programming " << std::endl
<< " =========================" << std::endl;
/* Knapsack Problem */
int wt = 5;
std::map<std::string, std::pair<int, int> > info
{ { "Guitar", { 1, 1500 } }, { "Stereo", { 4, 3000 } },
{ "Laptop", { 3, 2000 } }, { "Iphone", { 1, 2000 } } };
std::pair<int, std::set<std::string>> ret = KnapsackProblem_Ch9(info, wt);
std::cout << "\nStealing things for value maximum" << std::endl
<< "The solution for Knapsack Problem with weight of " << wt << ":" << std::endl;
std::cout << "The maximum of value is: $ " << ret.first << std::endl << "The optimal solution is: ";
for (auto iter = ret.second.begin(); iter != ret.second.end(); ++iter)
std::cout << *iter << ", ";
std::cout << std::endl << std::endl;
int wt2 = 6;
std::map<std::string, std::pair<int, int>> info2
{ { "Water", { 3, 10 } }, { "Book", { 1, 3 } }, { "Food", { 2, 9 } },
{ "Jacket", { 2, 5 } }, { "Camera", { 1, 6 } } };
std::pair<int, std::set<std::string>> ret2 = KnapsackProblem_Ch9(info2, wt2);
std::cout << "Taking things to travel for value maximum" << std::endl
<< "The solution for Knapsack Problem with weight of " << wt2 << ":" << std::endl;
std::cout << "The maximum of value is: $ " << ret2.first << std::endl << "The optimal solution is: ";
for (auto iter = ret2.second.begin(); iter != ret2.second.end(); ++iter)
std::cout << *iter << ", ";
std::cout << std::endl << std::endl;
/* Longest Common Substring */
std::string str1("fished"), str2("abfisabshezzishecaahed");
std::pair<int, std::string> lcs = GrokkingAlgorithm::LongestCommonSubstring_Ch9(str1, str2);
std::cout << "Find the Longest Common Substring" << std::endl;
std::cout << "Input two strings as : \"" << str1 << "\" & \"" << str2 << "\"" << std::endl;
std::cout << "The Longest Common Substring is: \"" << lcs.second << "\"" << std::endl;
std::cout << "The length is : " << lcs.first << std::endl << std::endl;
/* Longest Common Subsequence */
std::string str3("fosh"), str4("fish");
int len = GrokkingAlgorithm::LongestCommonSubsequence_Ch9(str3, str4);
std::cout << "Find the Longest Common Subsequence" << std::endl;
std::cout << "Input two strings as : \"" << str3 << "\" & \"" << str4 << "\"" << std::endl;
std::cout << "The length of Longest Common Substring is: " << len << std::endl << std::endl;
return;
}