-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource: main.cpp
More file actions
613 lines (464 loc) · 17.9 KB
/
Source: main.cpp
File metadata and controls
613 lines (464 loc) · 17.9 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "Movie_Vertex.h"
#include "Graph.h"
#include <iostream>
#include <map>
#include <fstream>
#include <sstream>
#include <chrono>
using namespace std;
void displayMovie(Movie_Vertex movie) {
cout << "\nMovie Selected:" << endl;
cout << "Title: " << movie.getTitle() << endl;
cout << "Genre: " << movie.getGenre() << endl;
cout << "Rating: " << movie.getRating() << endl;
}
void heapify(Movie_Vertex* arr, int x, int j) {
int max = j;
int left = 2 * j + 1;
int right = 2 * j + 2;
if (arr[left].getRating() > arr[max].getRating() && left < x) {
max = left;
}
if (arr[right].getRating() > arr[max].getRating() && right < x) {
max = right;
}
if (max != j) {
swap(arr[j], arr[max]);
heapify(arr, x, max);
}
}
void createHeap(Movie_Vertex* arr, int x) {
int firstIndex = (x / 2) - 1;
for (int j = firstIndex; j >= 0; j--) {
heapify(arr, x, j);
}
}
vector<Movie_Vertex*> findRatingBDS(Movie_Vertex* arr, double min, double max, int size) {
vector<Movie_Vertex*> checkedVerts;
vector<Movie_Vertex*> queuedVerts;
vector<Movie_Vertex*> matches;
checkedVerts.push_back(&arr[0]);
queuedVerts.push_back(&arr[0]);
cout << "\nTraversing Heap...\n";
while (queuedVerts.size() > 0)
{
// Pull movie vertex to be checked
Movie_Vertex* vert = queuedVerts[0];
// Remove it from vector of vertices to check
queuedVerts.erase(queuedVerts.begin());
cout << vert->getTitle() << "->";
int idx;
// Get all edges of the currently checked movie vertex
for (int i = 0; i < size; i++)
{
if (arr[i].getTitle().compare(vert->getTitle()) == 0)
{
idx = i;
break;
}
}
// Determine if the adjacent movie has been traversed before
bool leftChecked = false;
bool rightChecked = false;
for (Movie_Vertex* movie : checkedVerts)
{
if (idx * 2 + 2 < size)
{
if (movie->getTitle().compare(arr[idx * 2 + 2].getTitle()) == 0 || idx * 2 > size)
{
rightChecked = true;
}
if (movie->getTitle().compare(arr[idx * 2 + 1].getTitle()) == 0 || idx * 2 > size)
{
leftChecked = true;
}
}
else
{
rightChecked = true;
leftChecked = true;
}
}
// Add this adjacent movie vertex to be checked since it hasn't been traversed
if (!rightChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 2]);
queuedVerts.push_back(&arr[idx * 2 + 2]);
}
if (!leftChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 1]);
queuedVerts.push_back(&arr[idx * 2 + 1]);
}
if (vert->getRating() <= max && vert->getRating() >= min)
{
matches.push_back(vert);
}
}
cout << endl;
return matches;
}
vector<Movie_Vertex*> findGenreBDS(Movie_Vertex* arr, string genre, int size) {
vector<Movie_Vertex*> checkedVerts;
vector<Movie_Vertex*> queuedVerts;
vector<Movie_Vertex*> matches;
checkedVerts.push_back(&arr[0]);
queuedVerts.push_back(&arr[0]);
cout << "\nTraversing Heap...\n";
while (queuedVerts.size() > 0)
{
// Pull movie vertex to be checked
Movie_Vertex* vert = queuedVerts[0];
// Remove it from vector of vertices to check
queuedVerts.erase(queuedVerts.begin());
cout << vert->getTitle() << "->";
int idx;
// Get all edges of the currently checked movie vertex
for (int i = 0; i < size; i++)
{
if (arr[i].getTitle().compare(vert->getTitle()) == 0)
{
idx = i;
break;
}
}
// Determine if the adjacent movie has been traversed before
bool leftChecked = false;
bool rightChecked = false;
for (Movie_Vertex* movie : checkedVerts)
{
if (idx * 2 + 2 < size)
{
if (movie->getTitle().compare(arr[idx * 2 + 2].getTitle()) == 0 || idx * 2 > size)
{
rightChecked = true;
}
if (movie->getTitle().compare(arr[idx * 2 + 1].getTitle()) == 0 || idx * 2 > size)
{
leftChecked = true;
}
}
else
{
rightChecked = true;
leftChecked = true;
}
}
// Add this adjacent movie vertex to be checked since it hasn't been traversed
if (!rightChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 2]);
queuedVerts.push_back(&arr[idx * 2 + 2]);
}
if (!leftChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 1]);
queuedVerts.push_back(&arr[idx * 2 + 1]);
}
if (vert->getGenre().compare(genre) == 0)
{
matches.push_back(vert);
}
}
cout << endl;
return matches;
}
Movie_Vertex* findTitleBDS(Movie_Vertex* arr, string title, int size) {
vector<Movie_Vertex*> checkedVerts;
vector<Movie_Vertex*> queuedVerts;
checkedVerts.push_back(&arr[0]);
queuedVerts.push_back(&arr[0]);
int count = 1;
cout << "Traversing Heap...\n";
while (queuedVerts.size() > 0)
{
// Pull movie vertex to be checked
Movie_Vertex* vert = queuedVerts[0];
// Remove it from vector of vertices to check
queuedVerts.erase(queuedVerts.begin());
cout << vert->getTitle() << "->";
int idx;
// Get all edges of the currently checked movie vertex
for (int i = 0; i < size; i++)
{
if (arr[i].getTitle().compare(vert->getTitle()) == 0)
{
idx = i;
break;
}
}
// Determine if the adjacent movie has been traversed before
bool leftChecked = false;
bool rightChecked = false;
for (Movie_Vertex* movie : checkedVerts)
{
if (idx * 2 + 2 < size)
{
if (movie->getTitle().compare(arr[idx * 2 + 2].getTitle()) == 0 || idx * 2 > size)
{
rightChecked = true;
}
if (movie->getTitle().compare(arr[idx * 2 + 1].getTitle()) == 0 || idx * 2 > size)
{
leftChecked = true;
}
}
else
{
rightChecked = true;
leftChecked = true;
}
}
// Add this adjacent movie vertex to be checked since it hasn't been traversed
if (!rightChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 2]);
queuedVerts.push_back(&arr[idx * 2 + 2]);
}
if (!leftChecked)
{
checkedVerts.push_back(&arr[idx * 2 + 1]);
queuedVerts.push_back(&arr[idx * 2 + 1]);
}
if (vert->getTitle().compare(title) == 0)
{
cout << endl;
return vert;
}
}
return nullptr;
}
int main()
{
ifstream moviesInfile("MovieDataBaseInput.txt");
ifstream ratingsInfile("MovieRatings.txt");
string tconst, title, releaseYear, genres, buffer, rating;
//skip first line of input that contains format info of file
getline(moviesInfile, buffer);
getline(ratingsInfile, buffer);
//map of vectors of movies, each vector contains movies of the same genre
//map<string, vector<Movie_Vertex>> movieGenres;
vector<Movie_Vertex> movieList;
/*going through input files line by line, still have to create graph object in order to store movie vertices*/
for (int i = 0; i < 100000; i++)
{
//get index of movie
getline(moviesInfile, tconst, '\t');
//skip title type info
getline(moviesInfile, buffer, '\t');
//get title type info
getline(moviesInfile, title, '\t');
//skip original title info
getline(moviesInfile, buffer, '\t');
//skip isAdult info
getline(moviesInfile, buffer, '\t');
//get release Year info
getline(moviesInfile, releaseYear, '\t');
//skip end year info
getline(moviesInfile, buffer, '\t');
//skip runtime info
getline(moviesInfile, buffer, '\t');
//get genres info
getline(moviesInfile, genres);
genres = genres.substr(0, genres.find(","));
//skip index info
getline(ratingsInfile, buffer, '\t');
//get the movie rating
getline(ratingsInfile, rating, '\t');
//skip number of votes info
getline(ratingsInfile, buffer);
//create movie object
Movie_Vertex movie(title, genres, stod(rating));
//push movie object into vector of movies
movieList.push_back(movie);
}
//format for user input
cout << "Welcome to the Movie Carousel!\n\n";
bool movieFound = false;
vector<Movie_Vertex> reset = movieList;
Movie_Vertex* heap[100000];
for (int i = 0; i < 100000; i++)
{
if (i < movieList.size())
{
heap[i] = &movieList.at(i);
}
}
createHeap(*heap, 100000);
while (!movieFound)
{
cout << "Search Space Size: " << movieList.size() << endl;
Graph graph("", movieList);
Graph* ptr = &graph;
cout << "\nChoose an option to start:\n\nSearch by rating (enter: 1)\nSearch by genre (enter: 2)\nSearch by name (enter: 3)\nSearch for a random movie (enter: 4)\nReset Search Space\noption:";
//stores user input choice option
string searchChoice;
getline(cin, searchChoice);
//search by Rating
if (stoi(searchChoice) == 1)
{
//upper and lower bound ranges are given by user input
string upperBound, lowerBound;
cout << "\nEnter Upperbound (0.0 - 10.0): ";
getline(cin, upperBound);
cout << "Enter Lowerbound (0.0 - 10.0): ";
getline(cin, lowerBound);
//run heap search and display time taken
auto heapStart = chrono::steady_clock::now();
findRatingBDS(*heap, stod(lowerBound), stod(upperBound), 100000);
auto heapEnd = chrono::steady_clock::now();
cout << "\nTime taken for Heap Search: " << chrono::duration_cast<chrono::nanoseconds>(heapEnd - heapStart).count() << " ns\n";
//run graph depth-first-search and display time taken
auto graphStart = chrono::steady_clock::now();
vector<Movie_Vertex*> updatedMovieList = graph.findRatingBDS(ptr, stod(lowerBound), stod(upperBound));
auto graphEnd = chrono::steady_clock::now();
cout << "\nTime taken for Graph Search: " << chrono::duration_cast<chrono::nanoseconds>(graphEnd - graphStart).count() << " ns\n";
cout << "\nMovie titles found with a rating between " << lowerBound << " - " << upperBound << ":" << endl;
//Display Updated Search Space based on Search by Genre y Rating
for (int i = 0; i < updatedMovieList.size(); i++)
{
cout << updatedMovieList.at(i)->getTitle() << endl;
}
cout << "\nWould you like to continue searching among these titles? (y / n)" << endl;
string answer;
getline(cin, answer);
//Shortens search base to movies found by the Search By Rating function
if (answer == "y")
{
for (int i = 0; i < updatedMovieList.size(); i++)
{
movieList.at(i) = *updatedMovieList.at(i);
}
unsigned int size = movieList.size();
for (int i = updatedMovieList.size(); i < size; i++)
{
movieList.pop_back();
}
}
else
{
continue;
}
}
//Search By Genre
else if (stoi(searchChoice) == 2)
{
string genre;
cout << "\nEnter Genre: ";
getline(cin, genre);
//Run Heap Search and Display time taken
auto heapStart = chrono::steady_clock::now();
findGenreBDS(*heap, genre, 100000);
auto heapEnd = chrono::steady_clock::now();
cout << "\nTime taken for Heap Search: " << chrono::duration_cast<chrono::nanoseconds>(heapEnd - heapStart).count() << " ns\n";
//Run Depth First Search on Graph and display time taken
auto graphStart = chrono::steady_clock::now();
vector<Movie_Vertex*> updatedMovieList = graph.findGenreBDS(ptr, genre);
auto graphEnd = chrono::steady_clock::now();
cout << "\nTime taken for Graph Search: " << chrono::duration_cast<chrono::nanoseconds>(graphEnd - graphStart).count() << " ns\n";
cout << "\n\nMovie titles under " << genre << ":" << endl;
//Display movies found under the Genre Specified
for (int i = 0; i < updatedMovieList.size(); i++)
{
cout << updatedMovieList.at(i)->getTitle() << endl;
}
cout << "\nWould you like to continue searching among these titles? (y / n)" << endl;
string answer;
getline(cin, answer);
//Shorten search space to the movies founder under this genre
if (answer == "y")
{
for (int i = 0; i < updatedMovieList.size(); i++)
{
movieList.at(i) = *updatedMovieList.at(i);
}
unsigned int size = movieList.size();
for (int i = updatedMovieList.size(); i < size; i++)
{
movieList.pop_back();
}
}
else
{
continue;
}
}
//Search By Name
else if (stoi(searchChoice) == 3)
{
cout << "\nEnter Movie Name:";
string movieTitle;
getline(cin, movieTitle);
//Run Heap Search and Display Time taken
auto heapStart = chrono::steady_clock::now();
findTitleBDS(*heap, movieTitle, 100000);
auto heapEnd = chrono::steady_clock::now();
cout << "\nTime taken for Heap Search: " << chrono::duration_cast<chrono::nanoseconds>(heapEnd - heapStart).count() << " ns\n";
//Run Depth First Search on Graph and Display time taken
auto graphStart = chrono::steady_clock::now();
Movie_Vertex* movie = graph.findTitleBDS(ptr, movieTitle);
auto graphEnd = chrono::steady_clock::now();
cout << "\nTime taken for Graph Search: " << chrono::duration_cast<chrono::nanoseconds>(graphEnd - graphStart).count() << " ns\n";
//If movie title is found, display its information
if (movie != nullptr)
{
displayMovie(*movie);
//If movie found is the movie that the movie wants, terminate the program
cout << "Is this the movie you wanted? (y / n)" << endl;
string answer;
getline(cin, answer);
if (answer == "y")
{
movieFound = true;
}
}
else
{
continue;
}
}
//Search For Random Movie
else if (stoi(searchChoice) == 4)
{
//Run Depth First Search and display time taken
auto graphStart = chrono::steady_clock::now();
Movie_Vertex* movie = graph.randomMovie(ptr);
auto graphEnd = chrono::steady_clock::now();
//Run Heap Search and display time taken
auto heapStart = chrono::steady_clock::now();
findTitleBDS(*heap, movie->getTitle(), 100000);
auto heapEnd = chrono::steady_clock::now();
cout << "\nTime taken for Heap Search: " << chrono::duration_cast<chrono::nanoseconds>(heapEnd - heapStart).count() << " ns\n";
cout << "\nTime taken for Graph Search: " << chrono::duration_cast<chrono::nanoseconds>(graphEnd - graphStart).count() << " ns\n";
//Display contents of Random Movie
if (movie != nullptr)
{
displayMovie(*movie);
cout << "\nIs this the movie you wanted? (y / n)" << endl;
string answer;
getline(cin, answer);
//if the movie selected is the movie wanted by the user, terminate the program
if (answer == "y")
{
movieFound = true;
}
}
else
{
continue;
}
}
//Reset Search Space
else if (stoi(searchChoice) == 5)
{
movieList = reset;
}
//Alert user that selection choice is invalid
else
{
cout << "\nInvalid Input!\n\n";
}
}
cout << "\nThanks for riding the Movie Carousel!" << endl;
return 0;
}