#Include #Include #Include #Include: Using Namespace Int Int Int

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Εθνικό και Καποδιστριακό Πανεπιστήμιο Αθηνών - Τμήμα Φυσικής

ΠΜΣ στον Ηλεκτρονικό Αυτοματισμό


Μάθημα: Δομές Δεδομένων και Αλγόριθμοι - Project 2 2021
Θέμα: Δημιουργία R-B Tree
Καθηγητής: κ. Ρεϊσης Διονύσιος
Μαθητής: Βασίλειος Μπεζαΐτης
C:\Users\mpeza\source\repos\1\1\Source.cpp 1
1
2 #include <iostream>
3 #include <unordered_map>
4 #include <bits/stdc++.h>
5 #include "HeaderRB.h"
6
7 using namespace std;
8
9 int m, n, c;
10 #define MAX 30
11 int vertices;
12 vector<vector<int>> adj;
13
14 //methods for orthogonal list
15 struct MatrixNode
16 {
17 int _val;
18 MatrixNode* _u; // pointer to node above
19 MatrixNode* _d; // pointer to node below
20 MatrixNode* _l; // pointer to node towards left
21 MatrixNode* _r; // pointer to node towards right
22
23 // Constructor for MatrixNode
24 MatrixNode(int val = 0,
25 MatrixNode* u = nullptr,
26 MatrixNode* d = nullptr,
27 MatrixNode* l = nullptr,
28 MatrixNode* r = nullptr)
29 {
30 _val = val;
31 _u = u;
32 _d = d;
33 _l = l;
34 _r = r;
35 }
36 };
37
38 MatrixNode* insertEdge(int matrix[][30], int r, int c)
39 {
40 // an unordered_map to store the {value, pointers} pair
41 // for easy access while building the list
42 unordered_map<int, MatrixNode*> mp;
43
44 for (int i = 0; i < r; i++)
45 {
46 for (int j = 0; j < c; j++)
47 {
48 // create a newNode for each entry in the matrix
49 //MatrixNode* newNode = new MatrixNode(arr[i,j]);
50 MatrixNode* newNode = new MatrixNode(matrix[i][j]);
51 // store the pointer of the new node
52 mp[matrix[i][j]] = newNode;
53
C:\Users\mpeza\source\repos\1\1\Source.cpp 2
54 // set the up and down pointing pointers correctly
55 if (i != NULL)
56 {
57 newNode->_u = mp[matrix[i - 1][j]];
58 mp[matrix[i - 1][j]]->_d = newNode;
59 }
60
61 // similarly set the left and right pointing pointers
62 if (j != NULL)
63 {
64 newNode->_l = mp[matrix[i][j - 1]];
65 mp[matrix[i][j - 1]]->_r = newNode;
66 }
67 }
68 }
69
70 // return the start of the list
71 return mp[matrix[0][0]];
72 }
73
74 MatrixNode* deleteEdge(int matrix[][30], int r, int c, int k, int m)
75 {
76 // an unordered_map to store the {value, pointers} pair
77 // for easy access while building the list
78 unordered_map<int, MatrixNode*> mp;
79
80 for (int i = 0; i < r; i++)
81 {
82 for (int j = 0; j < c; j++)
83 {
84 // create a newNode for each entry in the matrix
85 //MatrixNode* newNode = new MatrixNode(arr[i,j]);
86 MatrixNode* newNode = new MatrixNode(matrix[i][j]);
87 // store the pointer of the new node
88 mp[matrix[i][j]] = newNode;
89
90 // set the up and down pointing pointers correctly
91 if (i != NULL)
92 {
93 newNode->_u = mp[matrix[i - 2][j]];
94 mp[matrix[i - 2][j]]->_d = newNode;
95 }
96
97 // similarly set the left and right pointing pointers
98 if (j != NULL)
99 {
100 newNode->_l = mp[matrix[i][j - 2]];
101 mp[matrix[i][j - 2]]->_r = newNode;
102 }
103 }
104 }
105
106 // return the start of the list
C:\Users\mpeza\source\repos\1\1\Source.cpp 3
107 return mp[matrix[0][0]];
108 }
109
110 //method for BFS
111 void bfs(int start)
112 {
113 // Visited vector to so that
114 // a vertex is not visited more than once
115 // Initializing the vector to false as no
116 // vertex is visited at the beginning
117 vector<bool> visited(adj.size(), false);
118 vector<int> q;
119 q.push_back(start);
120
121 // Set source as visited
122 visited[start] = true;
123
124 int vis;
125 printf("\nBFS Queue : ");
126 while (!q.empty()) {
127 vis = q[0];
128
129 // Print the current node
130
131 cout << vis << " ";
132 q.erase(q.begin());
133
134
135 // For every adjacent vertex to the current vertex
136 for (int i = 0; i < adj[vis].size(); i++) {
137 if (adj[vis][i] == 1 && (!visited[i])) {
138
139 // Push the adjacent node to the queue
140 q.push_back(i);
141
142 // Set
143 visited[i] = true;
144 }
145 }
146 }
147 printf("\n---\n");
148 }
149
150 // methods to add and delete edges to the graph
151 void addEdge(int x, int y)
152 {
153 adj[x][y] = 1;
154 adj[y][x] = 1;
155 printf("\nEdge Added");
156 printf("\n---\n");
157 }
158
159 void delEdgeVector(int x, int y)
C:\Users\mpeza\source\repos\1\1\Source.cpp 4
160 {
161 adj[x][y] = 0;
162 adj[y][x] = 0;
163
164 }
165
166 //print graph
167 void printVector(std::vector<vector<int>> const input)
168 {
169 printf("\nAdjacency Matrix\n");
170 printf("\n----------------\n");
171 for (int i = 0; i < input.size(); i++) {
172 for (int j = 0; j < input[i].size(); j++)
173 cout << input[i][j] << " ";
174 cout << endl;
175 }
176 }
177
178 //method for spanning tree
179 void BST(int arr[][30], int vertices) {
180 int edge; // number of edge
181
182 // create an array to check visited vertex
183 int* visit = new int(vertices);
184 //int *visit[vertices];
185
186 //initialise the visit array to false
187 for (int i = 0; i < vertices; i++) {
188 visit[i] = false;
189 }
190
191
192 // set number of edge to 0
193 edge = 0;
194
195 // the number of edges in minimum spanning tree will be
196 // always less than (V -1), where V is the number of vertices in
197 //graph
198
199 // choose 0th vertex and make it true
200 visit[0] = true;
201
202 int x; // row number
203 int y; // col number
204
205 // print for edge and weight
206 cout << "\n Edge";
207 cout << endl;
208 while (edge < vertices - 1) {//in spanning tree consist the V-1
number of edges
209
210 //For every vertex in the set S, find the all adjacent vertices
211 // , calculate the distance from the vertex selected.
C:\Users\mpeza\source\repos\1\1\Source.cpp 5
212 // if the vertex is already visited, discard it otherwise
213 //choose another vertex nearest to selected vertex.
214
215 int min = INT_MAX;
216 x = 0;
217 y = 0;
218
219 for (int i = 0; i < vertices; i++) {
220 if (visit[i]) {
221 for (int j = 0; j < vertices; j++) {
222 if (!visit[j] && arr[i][j]) { // not in selected
and there is an edge
223 if (min > arr[i][j]) {
224 min = arr[i][j];
225 x = i;
226 y = j;
227 }
228
229 }
230 }
231 }
232 }
233 cout << x << " ---> " << y;
234 cout << endl;
235 visit[y] = true;
236 edge++;
237 }
238
239 }
240
241 //Methods for cycle detection
242 void addEdgeCycle(vector<int> adjCycle[], int u, int v)
243 {
244 adjCycle[u].push_back(v);
245 adjCycle[v].push_back(u);
246 }
247
248 void delVectorCycle(vector<int> adjCycle[], int u, int v)
249 {
250 adjCycle[u].pop_back();
251 adjCycle[v].pop_back();
252 }
253
254 bool isCyclicConntected(vector<int> adjCycle[], int s, int V,
vector<bool>& visited)
255 {
256 // Set parent vertex for every vertex as -1.
257 vector<int> parent(V, -1);
258
259 // Create a queue for BFS
260 queue<int> q;
261
262 // Mark the current node as
C:\Users\mpeza\source\repos\1\1\Source.cpp 6
263 // visited and enqueue it
264 visited[s] = true;
265 q.push(s);
266
267 while (!q.empty()) {
268
269 // Dequeue a vertex from queue and print it
270 int u = q.front();
271 q.pop();
272
273 // Get all adjacent vertices of the dequeued
274 // vertex u. If a adjacent has not been visited,
275 // then mark it visited and enqueue it. We also
276 // mark parent so that parent is not considered
277 // for cycle.
278 for (auto v : adjCycle[u]) {
279 if (!visited[v]) {
280 visited[v] = true;
281 q.push(v);
282 parent[v] = u;
283 }
284 else if (parent[u] != v)
285 return true;
286 }
287 }
288 return false;
289 }
290
291 bool isCyclicDisconntected(vector<int> adjCycle[], int V)
292 {
293 // Mark all the vertices as not visited
294 vector<bool> visited(V, false);
295
296 for (int i = 0; i < V; i++)
297 if (!visited[i] && isCyclicConntected(adjCycle, i,
298 V, visited))
299 return true;
300 return false;
301 }
302
303
304
305
306 //Main
307 int main()
308 {
309
310
311 int arr[30][30] = { 0 };
312 int r, c, mainChoice,searchKey;
313
314 std::vector<int>* adjCycle = NULL;
315 RBTree bst;
C:\Users\mpeza\source\repos\1\1\Source.cpp 7
316
317 printf("\nMSc in Command and Computing - Data Structures and
Algorithms");
318 printf("\n----------------------------");
319 printf("\nProfessor : Mr. Dionysis REISIS");
320 printf("\nStudent : Vasileios BEZAITIS");
321 printf("\n----------------------------\n");
322 printf("\nMAIN MENU");
323 printf("\n---------\n");
324 printf("\n1. Run PROJECT 1");
325 printf("\n2. Run PROJECT 2");
326 printf("\n3. Exit");
327 printf("\n---------\n");
328 printf("\nEnter your choice : ");
329 scanf_s("%d", &mainChoice);
330
331 while (1) {
332
333 switch (mainChoice) {
334 case 1:
335 //RUN PROJECT 1
336 printf("\nData Structures and Algorithms : Project 1 - 2021
| Represent an Undirected Graph with an Adjacency Matrix,
do BFS, find Minimum Spanning Tree and Search for Circles
\n");
337 printf("\n*** PROGRAM START ***\n");
338 printf("\nEnter number of vertices (MAX=30): ");
339 scanf_s("%d", &vertices);
340
341 adjCycle = new std::vector<int>[vertices];
342
343 if (vertices > 30) {
344 printf("\nFatal Error : MAX Vertices are 30. Please Try
again. ");
345 printf("\n---------\n");
346 }
347 else {
348 r = vertices;
349 c = vertices;
350 int choice;
351
352 MatrixNode* list = NULL;
353 adj = vector<vector<int>>(vertices, vector<int>
(vertices, 0));
354
355 while (1) {
356 printf("\nMAIN MENU PROJECT 1\n");
357 printf("\n---------\n");
358 printf("\n1.Insert an edge in the Graph \n");
359 printf("2.Delete an edge from the Graph \n");
360 printf("3.Print Adjacency Matrix (Representation of
the Graph)\n");
361 printf("4.Do Breadth First Search (BFS)\n");
C:\Users\mpeza\source\repos\1\1\Source.cpp 8
362 printf("5.Find and Print the Minimum Spanning Tree
(MST)\n");
363 printf("6.Search for Circles in the Graph (Returns
yes if a circle detected)\n");
364
365 printf("9.Exit\n");
366 printf("\n---------\n");
367 printf("\nEnter your choice : ");
368 scanf_s("%d", &choice);
369 switch (choice) {
370 case 1:
371 printf("\nEnter an edge to be inserted : ");
372 scanf_s("%d %d", &m, &n);
373 if (n >= vertices || m >= vertices) {
374 printf("\nFatal Error : Please Insert a
Valid Edge");
375 printf("\n---------\n");
376 }
377 else {
378 arr[m][n] = 1;
379 arr[n][m] = 1;
380 addEdgeCycle(adjCycle, m, n);
381 addEdge(m, n);
382 }
383
384
385 //proswrina ektos gia vector
386 //list = insertEdge(arr, r, c);
387
388 break;
389 case 2:
390 printf("\nEnter an edge to be deleted : ");
391 scanf_s("%d %d", &m, &n);
392 arr[m][n] = 0;
393 arr[n][m] = 0;
394 //list = deleteEdge(arr, r, c, m, n);
395 delEdgeVector(m, n);
396 addEdgeCycle(adjCycle, m, n);
397 printf("\nEdge Deleted");
398 printf("\n---------\n");
399 break;
400 case 3:
401 printVector(adj);
402 break;
403 case 4:
404 printf("\nEnter BFS Starting Point : ");
405 scanf_s("%d", &m);
406 bfs(m);
407 break;
408 case 5:
409 printf("\nMinimum Spanning Tree : ");
410 BST(arr, vertices);
411 printf("\n---------\n");
C:\Users\mpeza\source\repos\1\1\Source.cpp 9
412 break;
413 case 6:
414 printf("\nHas the Graph Circles : ");
415 if (isCyclicDisconntected(adjCycle, vertices))
416 cout << "Yes";
417 else
418 cout << "No";
419 printf("\n---------\n");
420 break;
421 case 9:
422 printf("\n*** PROGRAM FINISHED ***\n");
423 exit(1);
424 default:
425 printf("\nFatal Error : Please Choose a Number
from Above\n");
426 printf("\n---------\n");
427 break;
428 }
429
430
431 }
432
433 }
434
435 break;
436 case 2:
437 //RUN PROJECT 2
438
439 while (1) {
440 printf
("\n*************************************************
************\n");
441 printf("Data Structures and Algorithms : Project 2 -
2021 | R-B Trees\n");
442 printf("MAIN MENU\n");
443 printf("---------\n");
444 printf("1.Create a Random RB Tree\n");
445 printf("2.Insert Node \n");
446 printf("3.Delete Node \n");
447 printf("4.Print R-B Tree \n");
448 printf("5.Search R-B Tree \n");
449 printf("9.Exit\n");
450 printf("---------\n");
451 printf("\nEnter your choice : ");
452 scanf_s("%d", &choice1);
453 switch (choice1) {
454 case 1:
455 bst.insert(8);
456 bst.insert(18);
457 bst.insert(5);
458 bst.insert(15);
459 bst.insert(17);
460 bst.insert(25);
C:\Users\mpeza\source\repos\1\1\Source.cpp 10
461 bst.insert(40);
462 bst.insert(80);
463 bst.prettyPrint();
464 printf("\nRandom RB Tree Created");
465 printf("\n---------\n");
466 break;
467 case 2:
468 printf("\nEnter a Node to be inserted : ");
469 scanf_s("%d", &m1);
470 bst.insert(m1);
471 printf("\Node Inserted");
472 printf("\n---------\n");
473 bst.prettyPrint();
474
475 break;
476 case 3:
477 printf("\nEnter a Node to be deleted : ");
478 scanf_s("%d", &m1);
479 bst.deleteNode(m1);
480 printf("\Node Deleted");
481 printf("\n---------\n");
482 bst.prettyPrint();
483
484 break;
485
486 case 4:
487 bst.prettyPrint();
488 break;
489 case 5:
490 printf("\nEnter a Node to be Searched : ");
491 scanf_s("%d", &m1);
492 searchKey = bst.searchTree(m1)->data;
493 if (searchKey > 0) {
494 printf("Node %i Found!!\n", searchKey);
495 string sColor = bst.searchTree(m1)->color ?
"RED" : "BLACK";
496 printf("Node :");
497 cout << bst.searchTree(m1)->data << "(" <<
sColor << ")" << endl;
498
499 }
500 else {
501 printf("Node not found. Please try again!");
502 }
503
504 break;
505 case 9:
506 printf("\n*** PROGRAM FINISHED ***\n");
507 exit(1);
508 default:
509 printf("\nFatal Error : Please Choose a Number from
Above\n");
510 printf("\n---------\n");
C:\Users\mpeza\source\repos\1\1\Source.cpp 11
511 break;
512 }
513
514
515 }
516
517 break;
518
519 case 3:
520 printf("\n*** PROGRAM FINISHED ***\n");
521 exit(1);
522 default:
523 printf("\nFatal Error : Please Choose a Number from Above
\n");
524 printf("\n---------\n");
525 break;
526
527 }
528
529
530
531
532 return 0;
533 }
534
535 }

You might also like