Skip to content

Commit 7b2a640

Browse files
authored
COMP2521 revision theory patches (#261)
* fix and improve 2521 revision theory content * small fixes to type errors and use of next/link * fix explanation images
1 parent 4a9418f commit 7b2a640

22 files changed

+199
-112
lines changed

components/Navbar.tsx

+6-13
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,21 @@ export default function Navbar() {
153153
</Flex>
154154
<NavContainer isOpen={isOpen}>
155155
{navItems.map((navItem, idx) => (
156-
<Link key={idx} href={`/${navItem.path}`}>
156+
<Link
157+
key={idx}
158+
href={`/${navItem.path}`}
159+
legacyBehavior
160+
passHref>
157161
<Text
158-
as="a"
162+
as={'a'}
159163
css={{ color: '$slate12', cursor: 'pointer', whiteSpace: 'nowrap' }}
160164
onClick={() => { setIsOpen(false); console.log('hi') }}>
161165
{navItem.title}
162-
163166
</Text>
164167
</Link>
165168

166169
)
167170
)}
168-
{/* <Text
169-
as="a"
170-
size="label-lg"
171-
css={{
172-
color: '$slate11',
173-
userSelect: 'none',
174-
cursor: 'not-allowed'
175-
}}>
176-
About
177-
</Text> */}
178171
</NavContainer>
179172
<Box css={{
180173
display: isOpen ? "block" : "none",

data/articles/2521-revision-theory.mdx

+122-50
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
1717
<MultiChoice.Question>
1818
## Question 1 - Minimum Spanning Tree
1919

20-
Given this graph, what is the total weight of the minimum spanning tree?
20+
Given this graph, find the total weight of the minimum spanning tree.
2121

2222
<Centerer>
2323
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/4-mst.png" alt="Graph" width="70%" />
@@ -53,7 +53,9 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
5353
and depth-first traversal on a graph.
5454

5555
Show what would be printed by the following calls to
56-
these functions on this graph:
56+
these functions on this graph.
57+
58+
When visiting neighbouring vertices, assume they are visited in ascending order.
5759

5860
<Centerer>
5961
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/bft-dft.png" alt="Graph" width="70%" />
@@ -135,8 +137,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
135137
<MultiChoice.Question>
136138
## Question 3 - Time complexity
137139

138-
What is the time complexity of the following function?
139-
140+
Find the Big-O time complexity of the following function in terms of n where n is the number of elements in the `nums` array.
140141
```c:print_nums.c
141142
void print_nums(int nums[]) {
142143
for (int i = 0; i < 100; i++) {
@@ -148,43 +149,51 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
148149
</MultiChoice.Question>
149150
<MultiChoice.Answer isCorrect>
150151
O(1)
151-
<MultiChoice.Explanation content="This loops time will always be constant as have defined how many times it will be executed."/>
152+
<MultiChoice.Explanation content="The loop runs a definitive number of times (100 times) no matter how large the `nums` array might be. So the function runs in O(100) which simplifies to O(1)."/>
152153
</MultiChoice.Answer>
153-
<MultiChoice.Answer isCorrect>
154+
<MultiChoice.Answer isCorrect={false}>
154155
O(100)
155-
<MultiChoice.Explanation content="O(100) is equivalent to O(1). This loops time will always be constant as have defined how many times it will be executed."/>
156+
<MultiChoice.Explanation content="In Big-O notation, constant factors should be omitted."/>
156157
</MultiChoice.Answer >
157-
<MultiChoice.Answer content="O(n)" />
158-
<MultiChoice.Answer content="O(n ^ 2)" />
158+
159+
<MultiChoice.Answer>
160+
O(n)
161+
<MultiChoice.Explanation content="The loop inside the function does not execute in the order of n times. No matter how many elements in the array, the loop only iterates 100 times!" />
162+
</MultiChoice.Answer>
163+
<MultiChoice.Answer content="O(n ^ 2)" />
159164
</MultiChoice>
160165

161166
<MultiChoice>
162167
<MultiChoice.Question>
163168
## Question 4 - Euler paths
164169

165-
Does an Euler path exist for this graph?
166-
170+
Does an Euler path/circuit exist for this graph?
171+
167172
<Centerer>
168173
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/1-euler.png" alt="Graph" width="70%" />
169174
</Centerer>
170175

171176
</MultiChoice.Question>
172177

173-
<MultiChoice.Answer content="Yes"/>
174178
<MultiChoice.Answer
175-
content="No"
176-
isCorrect>
179+
isCorrect content="Has Euler path, has Euler circuit">
177180
<MultiChoice.Explanation>
178-
An Euler Path does not exist for this graph.
181+
An Euler path is a path that uses every edge of a graph exactly once.
182+
An Euler circuit is a cycle that uses every edge of a graph exactly once.
183+
184+
How to determine if a graph has an Euler path/circuit without actually finding the path/circuit:
179185

180-
It is known that a graph has an Euler path if and only if:
181-
- It is connected
182-
- It has exactly two vertices that have an odd degree
186+
A graph has an Euler path if and only if:
187+
- It is connected
188+
- It has exactly zero or exactly two vertices that have an odd degree
183189

184-
The given graph satisfies the first criteria as it is connected.
190+
A graph has an Euler circuit if and only if:
191+
- It is connected
192+
- It has exactly zero vertices that have an odd degree ie all vertices have an even degree
185193

186-
However, it does not satisfy the second criteria, as none of the vertices have
187-
an odd degree. In general terms, the degree of a vertex is the number of edges
194+
Think about why. What degree should the head, tail, and internal vertices have in order to form an Euler path/circuit?
195+
196+
The degree of a vertex is the number of edges
188197
connecting it. Looking at this graph, we see:
189198

190199
<table >
@@ -217,13 +226,17 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
217226
</tr>
218227
</table>
219228

220-
And realise that none of the nodes/vertices have an odd degree. Hence the second
221-
criteria is unfufilled.
222-
223-
Hence, it is concluded that an Euler path does not exist for this graph.
229+
Notice that none of the nodes/vertices have an odd degree. So an Euler circuit does exist for this graph. And by extension so does an Euler path (as an Euler circuit is a special case of an Euler path where the start and end vertices are the same).
224230
</MultiChoice.Explanation>
225231

226232
</MultiChoice.Answer>
233+
<MultiChoice.Answer content="Has Euler path, has no Euler circuit"/>
234+
<MultiChoice.Answer content="Has no Euler path, has Euler circuit">
235+
<MultiChoice.Explanation>
236+
If a graph has an Euler circuit, that also counts as an Euler path as an Euler circuit is a special case of an Euler path where the start and end vertices are the same.
237+
</MultiChoice.Explanation>
238+
</MultiChoice.Answer>
239+
<MultiChoice.Answer content="Has no Euler path, has no Euler circuit"/>
227240
</MultiChoice>
228241

229242
<MultiChoice>
@@ -240,7 +253,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
240253
<MultiChoice.Explanation content="Selection sort performs equally in all cases." />
241254
</MultiChoice.Answer>
242255
<MultiChoice.Answer content="Naive quicksort" isCorrect>
243-
<MultiChoice.Explanation content="Naive quicksort will be consistently slow with sorted and reverse inputs at it chooses the least efficient pivot, but quicker for random inputs as the pivot is more wisely chosen." />
256+
<MultiChoice.Explanation content="Naive quicksort will be consistently slow with sorted and reverse inputs at it chooses the least efficient pivot, but quicker for random inputs as the naive pivot is more likely to split the partitions more equally." />
244257
</MultiChoice.Answer>
245258
<MultiChoice.Answer content="Merge sort">
246259
<MultiChoice.Explanation content="Merge sort has the same time complexity for all cases." />
@@ -251,36 +264,71 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
251264
<MultiChoice.Question>
252265
## Question 6 - Time complexity
253266

254-
What is the time complexity of this algorithm?
267+
Find the time complexity of this function in Big-O notation in terms of `n`.
255268

256269
```c:question2.c
257-
void function(int n) {
270+
# Assume n is the size of the dynamically allocated array `arr`
271+
int function(int n, int* arr) {
272+
for (int i = 0; i < n; i++) {
273+
for (int j = 0; j < i; j++) {
274+
arr[i] += 1;
275+
}
276+
}
277+
278+
int total = 0;
258279
for (int i = 0; i < n; i++) {
259-
int *a = calloc(n, sizeof(int));
260-
printf("Hello!\n");
261-
free(a);
280+
total += arr[i];
262281
}
282+
return total;
263283
}
264284
```
265285

266286
</MultiChoice.Question>
267287
<MultiChoice.Answer content="O(n)" />
268288
<MultiChoice.Answer content="O(n^2)" isCorrect>
269-
<MultiChoice.Explanation content="Calloc is an O(n) operation. As it is nested in a for loop of O(n), the time complexities multiply, resulting in O(n^2)." />
289+
<MultiChoice.Explanation>
290+
The first loop has a nested loop that iterates 0 times, then 1 time, then 2 times, etc. up to `n - 1` times.
291+
```c:
292+
for (int i = 0; i < n; i++) {
293+
for (int j = 0; j < i; j++) {
294+
arr[i] += 1;
295+
}
296+
}
297+
```
298+
This results in a total of 0 + 1 + 2 + ... + n - 1 = n(n - 1) / 2 = O(n^2) operations (using formula for arithmetic sum).
299+
300+
The second loop iterates n times.
301+
302+
```c:
303+
for (int i = 0; i < n; i++) {
304+
total += arr[i];
305+
}
306+
```
307+
308+
Since O(n^2) dominates O(n), the function's overall time complexity is O(n^2).
309+
</MultiChoice.Explanation>
310+
270311
</MultiChoice.Answer>
271-
<MultiChoice.Answer content="O(n + m)" />
272-
<MultiChoice.Answer content="O(n * log(n))" />
312+
<MultiChoice.Answer content="O(n^2 + n)">
313+
<MultiChoice.Explanation>
314+
In Big-O notation, we focus on the dominant term, which represents the most significant factor affecting the growth rate of time complexity as the input size increases.
315+
316+
In this case, the dominant term is O(n^2), so we can ignore O(n) term which becomes insignificant for larger n.
317+
</MultiChoice.Explanation>
318+
319+
</MultiChoice.Answer>
320+
<MultiChoice.Answer content="O(n log(n))" />
273321
</MultiChoice>
274322

275323
<MultiChoice>
276324
<MultiChoice.Question>
277325
## Question 7 - Time complexity
278326

279-
What is the time complexity of the following function? You may assume that `n` is positive.
327+
Find the time complexity of this function in Big-O notation in terms of `n`.
280328

281329
```c:halve.c
330+
# Assume n >= 0
282331
void halve(int n) {
283-
printf(""called halve(%d)\n"", n);
284332
if (n == 0) {
285333
return;
286334
}
@@ -304,9 +352,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
304352
<MultiChoice.Question>
305353
## Question 8 - Time complexity
306354
307-
What is the time complexity of the following function? You may assume the values of `n` and `m` passed in are positive.
355+
Find the time complexity of this function in Big-O notation in terms of `n` and `m`.
308356
309357
```c:rem.c
358+
# Assume n > m > 0
310359
int rem(int n, int m) {
311360
while (n >= m) {
312361
n -= m;
@@ -322,7 +371,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
322371
<MultiChoice.Answer isCorrect>
323372
O(n / m)
324373
<MultiChoice.Explanation>
325-
This function is used to find the remainder of a division, hence the number of steps will be how many times `m` goes into `n`.
374+
This function is used to find the remainder after dividing `n` by `m`. The number of steps will be how many times `m` goes into `n`.
326375
</MultiChoice.Explanation>
327376
</MultiChoice.Answer>
328377
</MultiChoice>
@@ -342,36 +391,54 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
342391
<Centerer>
343392
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-a.png" alt="Graph" width="100%" />
344393
</Centerer>
345-
<MultiChoice.Explanation content="Just inserts the 45 doing a BST insert without balancing." />
394+
<MultiChoice.Explanation content="In an AVL tree we must maintain the height-balance imperative after each insertion -- every node must be height-balanced. I.e. for each node, the difference in height of it's left and right subtrees must be less than 2." />
346395
</MultiChoice.Answer>
347396
<MultiChoice.Answer>
348397
<Centerer>
349398
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-b.png" alt="Graph" width="100%" />
350399
</Centerer>
351-
<MultiChoice.Explanation content="Inserts the 45 directly after the 43 and pushes the 67 down, then also rearranges 67's children." />
400+
<MultiChoice.Explanation content="We cannot insert the 45 directly as the right child of 43 and push the 67 out of place." />
352401
</MultiChoice.Answer>
353402
<MultiChoice.Answer>
354403
<Centerer>
355404
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-c.png" alt="Graph" width="100%" />
356405
</Centerer>
357-
<MultiChoice.Explanation content="Right rotation about 67. This is part 1 of the solution." />
406+
<MultiChoice.Explanation content="Right rotation about 67 from the original tree. This is part of the solution, but not complete as the tree is still unbalanced (at node 43)." />
358407
</MultiChoice.Answer>
359408
<MultiChoice.Answer isCorrect>
360409
<Centerer>
361-
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-d.png" alt="Graph" width="100%" />
410+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-e.png" alt="Graph" width="100%" />
362411
</Centerer>
363412
<MultiChoice.Explanation>
364-
After inserting 45, 43 becomes unbalanced (2 left vs 4 right).
413+
Firstly we insert the new node 45 into it's correct position in the binary search tree.
365414
366-
Also, its right child is left-heavy, so before doing a left rotation, we do a right rotation.
367-
</MultiChoice.Explanation>
415+
<Centerer>
416+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-explanation-1.png" alt="Graph" width="100%" />
417+
</Centerer>
418+
419+
To maintain the height-balance imperative of an AVL tree, we unwind along the path from the new node back up to the root, checking the balance of each node. We find that the node 43 is unbalanced:
368420
369-
</MultiChoice.Answer>
370-
<MultiChoice.Answer>
371421
<Centerer>
372-
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-e.png" alt="Graph" width="100%" />
422+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-explanation-2.png" alt="Graph" width="100%" />
423+
</Centerer>
424+
425+
Since the new node 45 went to the right of 43, then to the left of 67, we need to do a double rotation to balance the tree. We first do a right rotation on 67, then a left rotation on 43:
426+
427+
<Centerer>
428+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-explanation-3.png" alt="Graph" width="100%" />
373429
</Centerer>
374-
<MultiChoice.Explanation content="Some other balanced tree" />
430+
431+
<Centerer>
432+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-explanation-4.png" alt="Graph" width="100%" />
433+
</Centerer>
434+
435+
<Centerer>
436+
<img style={{maxWidth: '500px'}} src="/images/comp2521-revision/q10-avl-explanation-5.png" alt="Graph" width="100%" />
437+
</Centerer>
438+
439+
Node 55 becomes the new root and our AVL tree is once again fully height-balanced after the insertion of 45.
440+
</MultiChoice.Explanation>
441+
375442
</MultiChoice.Answer>
376443
</MultiChoice>
377444
@@ -598,9 +665,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
598665
<MultiChoice.Question>
599666
## Question 16 - Time complexity (Challenge)
600667
601-
What is the time complexity of the following function? You may assume the value of `n` is positive.
668+
What is the time complexity of the following function?
602669
603670
```c:print_pairs.c
671+
# Assume n > 0
604672
void print_pairs(int n) {
605673
for (int i = 1; i <= n; i++) {
606674
for (int j = 1; j <= n; j += i) {
@@ -618,6 +686,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
618686
We firstly know that we will iterate through the outside loop `n` times, that gives us our `n` value.
619687
620688
The number of iterations of the inside loop is reduced on every iteration of `i`, hence the number of inside loop iterations is being reduced in a logarithmic fashion.
689+
690+
The outer loop runs `n` times. The inner loop runs `n, n/2, n/3, n/4, ...` times which is known as a <a href="https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)" target="_blank">harmonic series</a>. The total number of iterations is `n + n/2 + n/3 + ... + n/n = n * (1 + 1/2 + 1/3 + ... + 1/n)`. The series 1 + 1/2 + 1/3 + ... + 1/n is the nth harmonic number, and as n approaches infinity, the nth harmonic number approaches log(n). Therefore, the overall time complexity is O(n * log(n)).
691+
692+
Note: you are not expected to know about harmonic series for the COMP2521 exam.
621693
</MultiChoice.Explanation>
622694
623695
</MultiChoice.Answer>

data/articles/ppc-backend-1.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,4 @@ In our <Link href="/articles/ppc-backend-2">next article</Link> we will look at
172172
next="/articles/ppc-backend-2"
173173

174174
nextName="Backend Project Tutorial 2 - MongoDB"
175-
>
176-
</ArticleButtonContainer>
175+
/>

data/articles/ppc-backend-2.mdx

+1-3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,4 @@ nextName="Backend Project Tutorial 3 - CRUD Opearations"
134134

135135
prev="/articles/ppc-backend-1"
136136
prevName="Backend Project Tutorial 1 - Creating a simple Express.js backend"
137-
>
138-
</ArticleButtonContainer>
139-
137+
/>

data/articles/ppc-backend-3.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,4 @@ nextName="Backend Project Tutorial 4 - Users and authentication"
9797

9898
prev="/articles/ppc-backend-2"
9999
prevName="Backend Project Tutorial 2 - MongoDB"
100-
>
101-
</ArticleButtonContainer>
100+
/>

data/articles/ppc-backend-4.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -384,5 +384,4 @@ nextName="Backend Project Tutorial 5 - Deployment"
384384

385385
prev="/articles/ppc-backend-3"
386386
prevName="Backend Project Tutorial 3 - CRUD Operations"
387-
>
388-
</ArticleButtonContainer>
387+
/>

data/articles/ppc-backend-5.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,4 @@ That's about it for our backend tutorial. To recap, we have set up an Express ba
5353

5454
prev="/articles/ppc-backend-4"
5555
prevName="Backend Project Tutorial 4 - Users and authentication"
56-
>
57-
</ArticleButtonContainer>
56+
/>

0 commit comments

Comments
 (0)