You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -135,8 +137,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
135
137
<MultiChoice.Question>
136
138
## Question 3 - Time complexity
137
139
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.
140
141
```c:print_nums.c
141
142
void print_nums(int nums[]) {
142
143
for (inti=0; i<100; i++) {
@@ -148,43 +149,51 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
148
149
</MultiChoice.Question>
149
150
<MultiChoice.AnswerisCorrect>
150
151
O(1)
151
-
<MultiChoice.Explanationcontent="This loops time will always be constant as have defined how many times it will be executed."/>
152
+
<MultiChoice.Explanationcontent="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)."/>
152
153
</MultiChoice.Answer>
153
-
<MultiChoice.AnswerisCorrect>
154
+
<MultiChoice.AnswerisCorrect={false}>
154
155
O(100)
155
-
<MultiChoice.Explanationcontent="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.Explanationcontent="In Big-O notation, constant factors should be omitted."/>
156
157
</MultiChoice.Answer >
157
-
<MultiChoice.Answercontent="O(n)" />
158
-
<MultiChoice.Answercontent="O(n ^ 2)" />
158
+
159
+
<MultiChoice.Answer>
160
+
O(n)
161
+
<MultiChoice.Explanationcontent="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!" />
isCorrectcontent="Has Euler path, has Euler circuit">
177
180
<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:
179
185
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
183
189
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
185
193
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
188
197
connecting it. Looking at this graph, we see:
189
198
190
199
<table >
@@ -217,13 +226,17 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
217
226
</tr>
218
227
</table>
219
228
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).
224
230
</MultiChoice.Explanation>
225
231
226
232
</MultiChoice.Answer>
233
+
<MultiChoice.Answercontent="Has Euler path, has no Euler circuit"/>
234
+
<MultiChoice.Answercontent="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.Answercontent="Has no Euler path, has no Euler circuit"/>
227
240
</MultiChoice>
228
241
229
242
<MultiChoice>
@@ -240,7 +253,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
240
253
<MultiChoice.Explanationcontent="Selection sort performs equally in all cases." />
<MultiChoice.Explanationcontent="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.Explanationcontent="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." />
244
257
</MultiChoice.Answer>
245
258
<MultiChoice.Answercontent="Merge sort">
246
259
<MultiChoice.Explanationcontent="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
251
264
<MultiChoice.Question>
252
265
## Question 6 - Time complexity
253
266
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`.
255
268
256
269
```c:question2.c
257
-
voidfunction(int n) {
270
+
# Assume n is the size of the dynamically allocated array `arr`
271
+
intfunction(int n, int* arr) {
272
+
for (inti=0; i<n; i++) {
273
+
for (intj=0; j < i; j++) {
274
+
arr[i] +=1;
275
+
}
276
+
}
277
+
278
+
inttotal=0;
258
279
for (inti=0; i<n; i++) {
259
-
int *a=calloc(n, sizeof(int));
260
-
printf("Hello!\n");
261
-
free(a);
280
+
total += arr[i];
262
281
}
282
+
return total;
263
283
}
264
284
```
265
285
266
286
</MultiChoice.Question>
267
287
<MultiChoice.Answer content="O(n)" />
268
288
<MultiChoice.Answer content="O(n^2)" isCorrect>
269
-
<MultiChoice.Explanationcontent="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 (inti=0; i<n; i++) {
293
+
for (intj=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 (inti=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
+
270
311
</MultiChoice.Answer>
271
-
<MultiChoice.Answercontent="O(n + m)" />
272
-
<MultiChoice.Answercontent="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))" />
273
321
</MultiChoice>
274
322
275
323
<MultiChoice>
276
324
<MultiChoice.Question>
277
325
## Question 7 - Time complexity
278
326
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`.
280
328
281
329
```c:halve.c
330
+
# Assumen>=0
282
331
voidhalve(intn) {
283
-
printf(""calledhalve(%d)\n"", n);
284
332
if (n == 0) {
285
333
return;
286
334
}
@@ -304,9 +352,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
304
352
<MultiChoice.Question>
305
353
## Question 8 - Time complexity
306
354
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`.
308
356
309
357
```c:rem.c
358
+
# Assumen>m>0
310
359
intrem(intn, intm) {
311
360
while (n >=m) {
312
361
n-=m;
@@ -322,7 +371,7 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
322
371
<MultiChoice.Answer isCorrect>
323
372
O(n / m)
324
373
<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`.
326
375
</MultiChoice.Explanation>
327
376
</MultiChoice.Answer>
328
377
</MultiChoice>
@@ -342,36 +391,54 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
<MultiChoice.Explanationcontent="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." />
<MultiChoice.Explanationcontent="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)." />
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:
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:
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
+
375
442
</MultiChoice.Answer>
376
443
</MultiChoice>
377
444
@@ -598,9 +665,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
598
665
<MultiChoice.Question>
599
666
## Question 16 - Time complexity (Challenge)
600
667
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?
602
669
603
670
```c:print_pairs.c
671
+
# Assumen>0
604
672
voidprint_pairs(intn) {
605
673
for (inti=1; i <=n; i++) {
606
674
for (intj=1; j<=n; j+=i) {
@@ -618,6 +686,10 @@ Note that the theory questions in the real COMP2521 23T3 exam will not be multip
618
686
We firstly know that we will iterate through the outside loop `n` times, that gives us our `n` value.
619
687
620
688
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.
0 commit comments