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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
|
# Learning Profile for Assignment #3
## Computer Science 272: Data Structures and Algorithms
* Name: Mo Khan
* Student ID: 3431709
* https://github.com/mokhan/comp-272/tree/master/src/03/README.md
## Question 1
### Problem Statement
Illustrate that the nodes of any AVL tree T can be
colored "red" and "black" so that T becomes a
red-black tree.
```plaintext
AVL Tree Red-Black Tree
(20:3) (20:b)
/ \ --> / \
(15:2) (30:2) (15:b) (30:b)
/ \ \ / \ \
(10:1) (17:1) (35:1) (10:r) (17:r) (35:r)
```
* perform pre order traversal
* assign colour of Red/Black node based on height of each AVL node
```plaintext
Step 1:
(20:b)
Step 2:
(20:b)
/
(15:b)
Step 3:
(20:b)
/
(15:b)
/
(10:r)
Step 4:
(20:b)
/
(15:b)
/ \
(10:r) (17:r)
Step 5:
(20:b)
/ \
(15:b) (30:b)
/ \
(10:r) (17:r)
Step 6:
(20:b)
/ \
(15:b) (30:b)
/ \ \
(10:r) (17:r) (35:r)
```
### Description of the Code
The function `avl_tree_to_rb_tree` provides an
implementation of this. To accomplish this
the code makes two passes down the tree. The
first pass it used to build a clone of the
AVL tree as a Red-Black tree with all nodes coloured
black. The second pass traverses the red-black
tree and applies the appropriate colour to
each node in the function `change_colour`.
If the height of the left subtree is less
than the height of hte right subtree or
the height is odd then the left child is
coloured black otherwise red.
The same is applied to the right subtree.
```c
change_colour(tree->left, left_height < right_height || is_odd(left_height) ? black : red);
change_colour(tree->right, right_height < left_height || is_odd(right_height) ? black : red);
```
## Question 2
### Problem Statement
Illustrate that via AVL single rotation, any binary search tree T1 can be
transformed into another search tree T2 (with the same items).
Left rotation:
```plaintext
(10) (20)
\ / \
(20) -> (10) (30)
\
(30)
```
Right rotation:
```plaintext
(30) (20)
/ / \
(20) --> (10) (30)
/
(10)
```
Left-Right rotation:
```plaintext
(30) (20)
/ / \
(10) -> (10) (30)
\
(20)
```
Right-Left rotation:
```plaintext
(10) (20)
\ / \
(30) --> (10) (30)
/
(20)
```
Give an algorithm to perform this transformation using O(N log N) rotation on average.
See `./avl_tree.c`.
## Question 3
### Problem Statement
Suppose you are given two sequences S1 and S2 of `n` elements, possibly
containing duplicates, on which a total order relation is defined.
1. Describe an efficient algorithm for determining if S1 and S2 contain the same set of elements.
Since S1 and S2 has a total order relation defined this means
that the data is sorted in both sequences.
To tell if the S1 and S2 contain the same set of elements we can use two pointers
to walk through each item in each sequence one step at a time to compare the values
at each index to ensure they are a match. As soon as we detect a mismatch
we know that the sequences do not contain the same set of elements. If we can
iterate to the end of both sequences at the same time then we have a match.
1. Analyze the running time of this method.
The time complexity is dependent on the size of `n` elements and is
therefore a linear time algorithm `O(n)`.
The space complexity is constant, `O(1)`, because only two pointers
are needed to walk through both sequences. The amount of space required
to perform this algorithm does not change as the input size of `n` changes.
## Question 4
### Problem Statement
Given sequence 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, sort the sequence using the
following algorithms, and illustrate the details of the execution of the
algorithms:
a. merge-sort algorithm.
```plaintext
[3,1,4,1,5,9,2,6,5,3,5,]
[3,1,4,1,5,9,]
[3,1,4,]
[3,1,]
[3,]
[3,1,]
[1,3,4,]
[1,3,4,1,5,9,]
[1,3,4,1,5,]
[1,3,4,1,]
[1,3,4,1,5,]
[1,3,4,1,5,9,]
[1,1,3,4,5,9,2,6,5,3,5,]
[1,1,3,4,5,9,2,6,5,]
[1,1,3,4,5,9,2,6,]
[1,1,3,4,5,9,2,]
[1,1,3,4,5,9,2,6,]
[1,1,3,4,5,9,2,6,5,]
[1,1,3,4,5,9,2,5,6,3,5,]
[1,1,3,4,5,9,2,5,6,3,]
[1,1,3,4,5,9,2,5,6,3,5,]
```
b. quick-sort algorithm.
* Choose a partitioning strategy you like to pick a pivot element from the sequence.
* Analyze how different portioning strategies may impact on the performance of the sorting algorithm.
For choosing a pivot I chose to use the value in the last element of the sequence.
Alternative, strategies include choosing a random pivot in each sub-sequence.
Using the last item in the sub-sequence as the pivot:
```plaintext
[3,1,4,1,5,9,2,6,5,3,]
[3,1,4,1,2,]
[1,1,]
[1,]
[]
[1,]
[1,1,]
[1,1,2,3,4,]
[1,1,2,]
[1,1,2,3,3,]
[1,1,2,3,3,4,5,6,5,9,]
[1,1,2,3,3,4,]
[1,1,2,3,3,4,5,5,5,9,]
[1,1,2,3,3,4,5,5,]
[1,1,2,3,3,4,5,5,5,6,]
```
## Question 5
### Problem Statement
Given the graph shown below, answer the following questions:
1. Illustrate the sequence of vertices of this graph visited using depth-first search traversal starting at vertex `g`.
1. Illustrate the sequence of vertices of this graph visited using breadth-first search traversal starting at vertex `b`.
1. Illustrate adjacency list representation and adjacency matrix representation, respectively, for this graph.
* What are the advantages and disadvantages of those two representations?
1. Describe an algorithm to find in the graph a path illustrated below that goes through every edge exactly once in each direction.
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
#### Depth First Traversal
Order: g, h, o, p, l, k, n, i, m, j, f, c, d, b, a, e
1. [g]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
2. [g, h]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
3. [g, h, o]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(*)---(p)
```
4. [g, h, o, p]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(*)---(*)
```
5. [g, h, o, p, l]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (k) / (*)
| \ | / |
| \ |/ |
(m) \(n)---(*)---(*)
```
6. [g, h, o, p, l, k]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (*) / (*)
| \ | / |
| \ |/ |
(m) \(n)---(*)---(*)
```
7. [g, h, o, p, l, k, n]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(i)---(j)/ (*) / (*)
| \ | / |
| \ |/ |
(m) \(*)---(*)---(*)
```
8. [g, h, o, p, l, k, n, i]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(*)---(j)/ (*) / (*)
| \ | / |
| \ |/ |
(m) \(*)---(*)---(*)
```
9. [g, h, o, p, l, k, n, i, m]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(*)---(j)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
10. [g, h, o, p, l, k, n, i, m, j]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
11. [g, h, o, p, l, k, n, i, m, j, f]
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
12. [g, h, o, p, l, k, n, i, m, j, f, c]
```plaintext
(a)---(b)---(*)---(d)
| \ / /
| \ / /
(e) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
13. [g, h, o, p, l, k, n, i, m, j, f, c, d]
```plaintext
(a)---(b)---(*)---(*)
| \ / /
| \ / /
(e) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
14. [g, h, o, p, l, k, n, i, m, j, f, c, d, b]
```plaintext
(a)---(*)---(*)---(*)
| \ / /
| \ / /
(e) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
15. [g, h, o, p, l, k, n, i, m, j, f, c, d, b, a]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(e) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
16. [g, h, o, p, l, k, n, i, m, j, f, c, d, b, a, e]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
#### Breadth First Traversal
Order: [b, a, f, c, e, j, d, i, g, m, n, h, k, o, p, l]
1. [b]
```plaintext
(a)---(*)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
2. [b, a]
```plaintext
(*)---(*)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
3. [b, a, f]
```plaintext
(*)---(*)---(c)---(d)
| \ / /
| \ / /
(e) \(*)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
4. [b, a, f, c]
```plaintext
(*)---(*)---(*)---(d)
| \ / /
| \ / /
(e) \(*)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
5. [b, a, f, c, e]
```plaintext
(*)---(*)---(*)---(d)
| \ / /
| \ / /
(*) \(*)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
6. [b, a, f, c, e, j]
```plaintext
(*)---(*)---(*)---(d)
| \ / /
| \ / /
(*) \(*)/ (g)/--(h)
| | / | /
| | / | /
(i)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
7. [b, a, f, c, e, j, d]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (g)/--(h)
| | / | /
| | / | /
(i)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
8. [b, a, f, c, e, j, d, i]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (g)/--(h)
| | / | /
| | / | /
(*)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
9. [b, a, f, c, e, j, d, i, g]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(h)
| | / | /
| | / | /
(*)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
10. [b, a, f, c, e, j, d, i, g, m]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(h)
| | / | /
| | / | /
(*)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(*) \(n)---(o)---(p)
```
11. [b, a, f, c, e, j, d, i, g, m, n]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(h)
| | / | /
| | / | /
(*)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(*) \(*)---(o)---(p)
```
12. [b, a, f, c, e, j, d, i, g, m, n, h]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (k) / (l)
| \ | / |
| \ |/ |
(*) \(*)---(o)---(p)
```
13. [b, a, f, c, e, j, d, i, g, m, n, h, k]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (l)
| \ | / |
| \ |/ |
(*) \(*)---(o)---(p)
```
14. [b, a, f, c, e, j, d, i, g, m, n, h, k, o]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (l)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(p)
```
15. [b, a, f, c, e, j, d, i, g, m, n, h, k, o, p]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (l)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
16. [b, a, f, c, e, j, d, i, g, m, n, h, k, o, p, l]
```plaintext
(*)---(*)---(*)---(*)
| \ / /
| \ / /
(*) \(*)/ (*)/--(*)
| | / | /
| | / | /
(*)---(*)/ (*) / (*)
| \ | / |
| \ |/ |
(*) \(*)---(*)---(*)
```
#### Adjacency List
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
(a) -> [b, e, f]
(b) -> [a, f]
(c) -> [b, d, f]
(d) -> [c, g]
(e) -> [a, i]
(f) -> [a, c, j]
(g) -> [d, h, j, k]
(h) -> [g, o]
(i) -> [e, j, m, n]
(j) -> [f, g, i]
(k) -> [g, o]
(l) -> [p]
(m) -> [i]
(n) -> [i, o]
(o) -> [k, n, p]
(p) -> [l, o]
```
Good:
* Space efficient because no space is wasted for edges that do not exist.
Bad:
* A lookup to determine if two vertexes are connected requires a linear time lookup due to the size of the list for a single edge. `O(n)`.
#### Adjacency Matrix
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
```plaintext
-----------------------------------
| |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|
|a|0|1|0|0|1|1|0|0|0|0|0|0|0|0|0|0|
|b|1|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|
|c|0|1|0|1|0|1|0|0|0|0|0|0|0|0|0|0|
|d|0|0|1|0|0|0|1|0|0|0|0|0|0|0|0|0|
|e|1|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|
|f|1|0|1|0|0|0|0|0|0|1|0|0|0|0|0|0|
|g|0|0|0|1|0|0|0|1|0|1|1|0|0|0|0|0|
|h|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|
|i|0|0|0|0|1|0|0|0|0|1|0|0|1|1|0|0|
|j|0|0|0|0|0|1|1|0|1|0|0|0|0|0|0|0|
|k|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|
|l|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|
|m|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|
|n|0|0|0|0|0|0|0|0|1|0|0|0|0|0|1|0|
|o|0|0|0|0|0|0|0|0|0|0|1|0|0|1|0|1|
|p|0|0|0|0|0|0|0|0|0|0|0|1|0|0|1|0|
-----------------------------------
```
Good:
* constant time lookup to see if two vertexes are connected `O(1)`
Bad:
* space inefficient `O(n^2)`
An adjacency matrix might be a better choice when space is less important
than fast lookups. An adjacency list may be a better choice if space is
a higher priority concern than time.
#### Traverse Every Edge
To traverse every edge in both directions we can use an adjacency matrix
and iterate through every cell in the matrix. If the cell contains a 1 to
indicate an edge than we know that we can traverse from the edge at
that row and column. Both directions will be represented in different cells
in the matrix.
When we visit each cell in the matrix we can flip the 1 to a 0 to ensure that
we do not revisit a visited edge.
1. Start at any vertex
1. Iterate through list of edges.
1. If the vertex on the other end of the edge has not been visited yet then visit it and loop until all edges are exhausted for the vertex.
1. Remove the edge from the matrix when visiting a node
1. Backtrack to previous vertex, and remove the edge.
1. Visit any edge where you can backtrack safely.
An example of this algorithm can be found in `./matrix.c` with accompanying tests in `./matrix_test.c`.
The graph to traverse is:
```plaintext
(a)---(b)---(c)---(d)
| \ / /
| \ / /
(e) \(f)/ (g)/--(h)
| | / | /
| | / | /
(i)---(j)/ (k) / (l)
| \ | / |
| \ |/ |
(m) \(n)---(o)---(p)
```
We can build a matrix that will look like the following:
```bash
| |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|
|a|0|1|0|0|1|1|0|0|0|0|0|0|0|0|0|0|
|b|1|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|
|c|0|1|0|1|0|1|0|0|0|0|0|0|0|0|0|0|
|d|0|0|1|0|0|0|1|0|0|0|0|0|0|0|0|0|
|e|1|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|
|f|1|0|1|0|0|0|0|0|0|1|0|0|0|0|0|0|
|g|0|0|0|1|0|0|0|1|0|1|1|0|0|0|0|0|
|h|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|
|i|0|0|0|0|1|0|0|0|0|1|0|0|1|1|0|0|
|j|0|0|0|0|0|1|1|0|1|0|0|0|0|0|0|0|
|k|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|
|l|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|
|m|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|
|n|0|0|0|0|0|0|0|0|1|0|0|0|0|0|1|0|
|o|0|0|0|0|0|0|0|0|0|0|1|0|0|1|0|1|
|p|0|0|0|0|0|0|0|0|0|0|0|1|0|0|1|0|
```
The order of traversal will be:
```plaintext
->(a)->(b)->(c)->(d)->(g)->(h)->(o)->(k)->(g)->(j)->(f)->(a)->(e)->(i)->(m)-
|
|---------------------------------------------------------------------------
->(i)->(n)->(o)->(p)->(l)->(p)->(o)->(n)->(i)->(j)->(i)->(e)->(a)->(f)->(c)-
|
|---------------------------------------------------------------------------
->(f)->(j)->(g)->(k)->(o)->(h)->(g)->(d)->(c)->(b)->(a)
```
After the traversal the matrix will have zero edges.
```bash
| |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|
|a|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|b|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|c|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|d|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|e|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|f|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|g|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|h|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|i|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|j|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|k|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|l|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|m|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|n|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|o|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
|p|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
```
## Question 6
### Problem Statement
Why does the method `remove(x)` in the `RedBlackTree` implementation
perform the assignment `u:parent = w:parent?`
Shouldn’t this already be done by the call to `splice(w)`?
It is possible that more than one rotation needs to occur so assigning the new parent
is necessary.
```java
boolean remove(T x)
{
Node<T> u = findLast(x);
if (u == nil || compare(u.x, x) != 0)
return false;
Node<T> w = u.right;
if (w == nil) {
w = u;
u = w.left;
} else {
while (w.left != nil)
w = w.left;
u.x = w.x;
u = w.right;
}
splice(w);
u.colour += w.colour;
u.parent = w.parent;
removeFixup(u);
return true;
}
void removeFixup(Node<T> u) {
while (u.colour > black) {
if (u == r) {
u.colour = black;
} else if (u.parent.left.colour == red) {
u = removeFixupCase1(u);
} else if (u == u.parent.left) {
u = removeFixupCase2(u);
} else {
u = removeFixupCase3(u);
}
}
if (u != r) {
Node<T> w = u.parent;
if (w.right.colour == red && w.left.colour == black) {
flipLeft(w);
}
}
}
```
Source [Open Data Structures](https://www.aupress.ca/app/uploads/120226_99Z_Morin_2013-Open_Data_Structures.pdf)
## Question 7
### Problem Statement
Implement the `remove(u)` method, that removes the node `u` from a
`MeldableHeap`. This method should run in `O(log n)` expected time.
```java
class MeldableHeap {
Node<T> merge(Node<T> h1, Node<T> h2) {
if (h1 == nil) return h2;
if (h2 == nil) return h1;
if (compare(h2.x, h1.x) < 0) return merge(h2, h1);
if (rand.nextBoolean()) {
h1.left = merge(h1.left, h2);
h1.left.parent = h1;
} else {
h1.right = merge(h1.right, h2);
h1.right.parent = h1;
}
return h1;
}
boolean add(T x) {
Node<T> u = newNode();
u.x = x;
r = merge(u, r);
r.parent = nil;
n++;
return true;
}
T remove() {
T x = r.x;
r = merge(r.left, r.right);
if (r != nil) r.parent = nil;
n--;
return x;
}
}
```
[Source](https://www.aupress.ca/app/uploads/120226_99Z_Morin_2013-Open_Data_Structures.pdf)
An implementation of `meldable_heap_remove(u)` can be found in `./meldable_heap.c`.
## Question 8
### Problem Statement
Prove that a binary tree with `k` leaves has height at least `log k`.
The proof can be derived with the following.
Suppose we have a function `h` that takes input `k`
and returns a tree with `k` leaves.
For each positive natural number we can
assert that the height of the tree must be greater
than or equal to `log2(k)`.
```plaintext
for each positive natural number
assert(height(h(k)) >= log2(k))
```
An example test is provided in `btree_test.c` that
asserts that this holds true for the first
500 positive integers.
```c
for (int k = 0; k < 500; ++k)
assert_that(btree_height(btree_generate(k)) >= log2(k), is_equal_to(true));
```
|