Skip to content

Commit 702e300

Browse files
authored
Updated 2521 page for 23T2 (#242)
* added daniels trie problem and slides * added daneils 2521 slide to 2521 page * added short answer questions + sagas hashmap q
1 parent dc27c86 commit 702e300

File tree

14 files changed

+2133
-2061
lines changed

14 files changed

+2133
-2061
lines changed

data/articles/2521-revision-theory.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: COMP2521 Revision Theory Questions
3-
date: 2022-08-01
3+
date: 2023-07-29
44
desc: Theory based questions for the CSESoc COMP2521 Exam Revision Session.
55
tags:
66
- 'COMP2521'
@@ -9,9 +9,9 @@ author: CSESoc Education Team
99
coverPhoto: '/images/generic/alexandru-acea-XEB8y0nRRP4-unsplash.jpg'
1010
---
1111

12-
This page has 16 practice questions that cover most topics in COMP2521 22T2 and will give you a chance to test your knowledge of data structures and algorithms.
12+
This page has a set of practice questions that cover most topics in COMP2521 23T2 and will give you a chance to test your knowledge of data structures and algorithms.
1313

14-
Note that the theory questions in the real COMP2521 22T2 exam will not be multiple choice!
14+
Note that the theory questions in the real COMP2521 23T2 exam will not be multiple choice!
1515

1616
<MultiChoice>
1717
<MultiChoice.Question>

data/course-revision/1511-23T2.mdx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ Then run the following command to copy the template files:
2424
$ ~csesoc/1511-fetch
2525
```
2626

27-
## Solutions
27+
### Solutions
2828

2929
Links to sample solutions can be found at the bottom of each problem!
3030

3131
<br />
32+
33+
## Miscellaneous resources
34+
3235
<br />
33-
## [Pointers and Memory
34-
Slides](https://docs.google.com/presentation/d/1Iz8lKozedeIq5ECu0vN3ZPgnke88r2L5H7UIt9otwC8/edit?usp=sharing)
36+
37+
### [Pointers and Memory Slides]
38+
39+
(https://docs.google.com/presentation/d/1Iz8lKozedeIq5ECu0vN3ZPgnke88r2L5H7UIt9otwC8/edit?usp=sharing)
3540

3641
<figure class="video_container">
3742
<iframe

data/course-revision/2521-22T3.mdx

Lines changed: 0 additions & 28 deletions
This file was deleted.

data/course-revision/2521-23T2.mdx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: CSESoc COMP2521 Revision Practice Problems
3+
desc: Practical coding exercises to help you prepare for your COMP2521 23T2 final exam 😄
4+
course: COMP2521
5+
offering: 23T2
6+
---
7+
8+
# CSESoc COMP2521 Revision Practice Problems
9+
10+
Note:
11+
12+
For COMP2521 multiple choice theory problems, see [here](https://learn.csesoc.org.au/articles/2521-revision-theory)
13+
14+
For COMP2521 short answer theory problems, see [here](https://learn.csesoc.org.au/course-revision/2521-23T2/theory)
15+
16+
This is a set of practical programming problems designed to help you prepare for your COMP2521 23T2 final exam.
17+
18+
There are autotests for each problem on cse servers which you can use to check your solutions.
19+
20+
To start, log in to cse servers via ssh and create a new folder:
21+
22+
```bash:~
23+
$ mkdir 2521-revision
24+
$ cd 2521-revision
25+
```
26+
27+
Then run the following command to copy the template files:
28+
29+
```bash:~/2521-revision
30+
$ 2521 fetch revision
31+
```
32+
33+
<br />
34+
35+
## Miscellaneous resources
36+
37+
<br />
38+
39+
### [Time-Efficient Problem Solving Slides]
40+
41+
(https://docs.google.com/presentation/d/1OlIyQTg3afU6UOkdY4s2hpacSUsRSVGUboPcxXDEvA0/edit#slide=id.ge7f9c668d6_0_1036)
42+
43+
<figure class="video_container">
44+
<iframe
45+
src="https://docs.google.com/presentation/d/e/2PACX-1vQSdJqbTa0aI4foczJ5q3FJFaqkDzWpdDP-iVvv7zQ50jHmvU0JeOELspAFunfgMaryXlChC90Hg19t/embed?start=false&loop=false&delayms=3000"
46+
frameborder="0"
47+
width="960"
48+
height="569"
49+
allowfullscreen="true"
50+
mozallowfullscreen="true"
51+
webkitallowfullscreen="true"></iframe>
52+
</figure>
53+
54+
<br />
55+
56+
### [Implementation: Hashmap with linear probing collision resolution]
57+
58+
https://github.com/Allynixtor/comp2521-revision-session/blob/main/miscellaneous_content/hashmap_linear_probe.c
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: Chaining in Hashmaps
3+
desc: A walkthrough to implementing chaining as a collision resolution to hashmaps.
4+
class: COMP2521
5+
difficulty: 1
6+
---
7+
8+
# Chaining in Hashmaps
9+
10+
_Written by Saga Chandra from CSESoc Education_
11+
12+
This problem set will have you implement a hashing function that allow us to store strings in key pair values, and also implement the collision resolution strategy, chaining.
13+
14+
This problem set will go through a very brief and simple introduction onto how hashmaps are implemented, and why they are implemented in this way while working on the questions.
15+
16+
_Sidenote:_
17+
You can only test your code once you have completed all the tasks.
18+
19+
Hashmaps involve turning our "key", into an integer, through a hashing function. This integer can then be used as the index for an array that stores the "value". Note that this allows us to achieve O(1) insertion, lookups, and deletion as long as we don't have any collisions, that is, no two "keys" provide the same integer from the hashing function.
20+
21+
Note: Each "space" in an array is called a bucket in hashing because it can contain multiple key pair values (as we will see when we implement chaining)
22+
23+
//
24+
This will be your first task. In `HashMap.c`, write the function `string_to_int` that takes in a string, adds up all the ASCII values of the string, and returns the sum.
25+
26+
**Sample**
27+
Input:
28+
29+
```bash
30+
"hi"
31+
```
32+
33+
Output:
34+
35+
```bash
36+
209
37+
```
38+
39+
//
40+
41+
However, if we simply leave it as is, we begin to waste a lot of space.
42+
For example, if we wanted a hashmap of size two, but the hashing function provides a value of 300, we would need to initialise an array of size 300, instead of two. Therefore, after we find the hash of the key, we perform a modulo of 2 operation on the hash so that it produces either a value of 0, or 1, allowing us to create an array of size 2.
43+
44+
This method allows us to both save space while having an amazing O(1) time complexity, but it also leads to a key problem, collisions.
45+
Take for example, the hashing function that you just implemeneted that adds up all the ASCII values of the characters. We can observe that the words "dusty" and "study" would end up having the same hash value, which can lead to losing some key pair values in the hashmap.
46+
One way we can try to minimise the number of such collisions, is by considering coprime numbers. Coprime numbers tend to produce less collisions in a hashmap. But why?
47+
48+
Let's consider another hashing function for a string, that adds up the ASCII values of each character, but also adds a factor of k into each character:
49+
`hash value: k^0 * str[0] + k * str[1] + k^2 * str[2] + ... + k^n * str[n]`.
50+
What would happen if we were to modulo every hash value by the same `k`?
51+
It would result in our `hash value` being the ASCII value of `str[0]`. This means that our hash depends on only the very first character of each string. This would result in collisions for `apples, angles, anatomicophysiological` and infinitely more!
52+
Which, suffice to say, is incredibly bad.
53+
54+
Hence why we always want to be using coprime numbers, but not just any coprime numbers, we want to use prime numbers. This is because hashing functions and the hashtable itself are not necessarily written dependently, and we can't always ensure coprime values without using prime numbers.
55+
56+
Hence, the size of our hashmap should always be the next prime number from the given size.
57+
58+
//
59+
Your second task will be to write the functions `hash` and `HashMap_New` in `HashMap.c`.
60+
61+
The function `hash` signature can be altered but note that it will be used in the insertion and lookup of your HashMap.
62+
63+
The function `hash` should perform the modulo of the hash of str by a prime number and return that value.
64+
**Sample**
65+
Input:
66+
67+
```c
68+
// We are hashing the str "hi" with the prime modulo 3
69+
// Note that your hash function signature can be different
70+
hash("hi", 3)
71+
```
72+
73+
Output:
74+
75+
```bash
76+
2
77+
```
78+
79+
The function `HashMap_New` signature should not be changed.
80+
81+
`HashMap_New` takes in an integer `n` that specifies the size the user wants the new HashMap to be.
82+
83+
`HashMap_New` should initialise a HashMap that has the size of the next prime number after `n` and return the new HashMap.
84+
//
85+
86+
Moving on to the actual useful parts of a hashmap, insertion and lookup implementations.
87+
88+
FIrst off, note how bad our `hash` function is in creating collision-less hashes, as said above, anagrams produce collisions very easily using this method. Hence, in our insertion and lookup implementations, we need to consider collision resolution strategies to allow for such collisions to co-exist.
89+
90+
One method we are going to implement is called chaining.
91+
Chaining involves storing our key pair values in linked lists. Hence our buckets can hold multiple key pair values. This resolves collisions by simply inserting a new key pair value linked list node into our buckets if a collision were to ever happen.
92+
93+
For example, lets insert "cat": "one", and "act": "two" into our hashmap.
94+
95+
Initially we have an empty hashmap with four empty buckets:
96+
97+
```
98+
HashMap: | NULL | NULL | NULL | NULL |
99+
```
100+
101+
At first, when we insert "cat": "one", we can insert a linked list node that contains the key, "cat", and the value "one".
102+
103+
```
104+
HashMap: | NULL | "cat":"one" | NULL | NULL |
105+
```
106+
107+
But when we try to insert "act":"two", we can see that "act" and "cat" are anagrams and without collision resolution strategies, will result in an overwrite:
108+
109+
```
110+
HashMap: | NULL | "act":"two" | NULL | NULL |
111+
```
112+
113+
However, with chaining, we can simply create a new linked list node and append it onto the bucket.
114+
115+
```
116+
HashMap: | NULL | "cat":"one" -> "act":"two" | NULL | NULL |
117+
```
118+
119+
_Sidenotes:_
120+
Although collision resolutions do exist, we can quickly guess that they will always ruin the O(1) operations of a hashmap, which is an integral characteristic of the data structure. Consider a hashmap that collides at every collision, with chaining, we essentially have a linked list, which is not O(1) time. Hence, if for whatever reason you are writing a hashmap, it is always better to consider writing a good hash function, and ensuring that the hash function is coprime to the number of buckets, rather than relying on collision resolution strategies.
121+
122+
//
123+
Your final task will be to implement `HashMap_Insert` and `HashMap_Get` in `HashMap.c`.
124+
You should not alter either of these function's signatures.
125+
126+
`HashMap_Insert` should take in a key and a value and insert it into the hashmap using the `hash` function outlined previously. `HashMap_Insert` should also use chaining to resolve collisions in the hashmap.
127+
128+
`HashMap_Get` should take in a key and return the associated value in the hashmap. If the key does not exist in the hashmap, the string "Not Found" should be returned instead.
129+
//
130+
131+
## How will your answers be tested?
132+
133+
Once you are certain that all these components are fully working, you can test your code by compiling with `gcc hashmap_chaining.c main.c -o main` and then running `./main`
134+
.
135+
Main will initially ask for inputs `n, s`, which specifies the size of the hashmap, and the number of key value pairs you are putting into the map respectively.
136+
137+
Then it will ask for a Key, and then a Value `s` times, which should be inserted into the hash map.
138+
139+
Finally, the program will print out the entire hash map, showing each individual bucket, and its contents.
140+
141+
_Sidenote:_
142+
If the same key is inserted twice into a single hashmap, then the value of the second key should overwrite the value of the first key.
143+
144+
If the key does not exist, then `HashMap_get` should return the string "Not Found".
145+
146+
Using a hashing function that differs from what is outlined previously (adding up the ASCII values of each character) will result in different collisions and hashes, and will probably mean acing the autotests given as they are specifically catered towards the outlined `hash` to produce and test collisions.
147+
148+
## CSE Autotest
149+
150+
When you think your program is working, you can use CSE autotest to test your solution.
151+
152+
```bash:~/2521-revision/hashmap_chaining:~
153+
$ 2521 autotest hashmap_chaining
154+
```
155+
156+
## Solution
157+
158+
You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-revision-session/tree/main/problems/hashmap_chaining/solution).
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Short Answer Questions
3+
desc: Short answer questions to test your theory.
4+
class: COMP2521
5+
difficulty: 1
6+
---
7+
8+
# CSESoc COMP2521 Short Answer Practice Problems
9+
10+
## Q1
11+
12+
A and B are weighted, undirected graphs. B has the same edges as A, except the weights are increased by 3.
13+
14+
a) Do the minimal spanning trees of A and B always contain the same edges? Justify your answer.
15+
16+
b) Consider the tree of single-source shortest paths from some vertex (e.g. as produced by Dijkstra's algorithm).
17+
Does this tree always contain the same edges in both graphs? Justify your answer.
18+
19+
## Q2
20+
21+
A particular graph is stored as an array of edges, each represented as a pair of vertices.
22+
When a new edge is added, it is placed at the end of the array.
23+
For example: [{2, 3}, {1, 2}, {0, 2}, {0, 1}].
24+
25+
a) What is the time complexity of breadth first search using this representation? Give your answer in terms
26+
of V and E.
27+
28+
b) How can this representation be adjusted to improve the complexity of BFS, while maintaining the same
29+
space complexity?
30+
31+
## Q3
32+
33+
Consider an arbitrary undirected connected graph G.
34+
A graph G\* is constructed by replacing every edge of G with 4 edges and two vertices like this:
35+
36+
```
37+
G G*
38+
------ C ------
39+
/ \
40+
A ------------- B => A B
41+
\ /
42+
------ D ------
43+
```
44+
45+
Is it true that G\* has an Euler cycle?
46+
47+
## Solutions
48+
49+
You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-revision-session/blob/main/problems/theory/solution/answers.mdx).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: What's my trie?
3+
desc: Draw a trie with the minimum number of nodes, which is equal to the following trie
4+
class: COMP2521
5+
difficulty: 1
6+
---
7+
8+
# What's my trie?
9+
10+
Suppose two tries are "equal" if they contain exactly the same words.
11+
12+
Draw a trie with the minimum number of nodes, which is equal to the following trie
13+
14+
<img
15+
style={{ maxWidth: '500px' }}
16+
src="/images/comp2521-revision/trie.png"
17+
alt="Graph"
18+
width="70%"
19+
/>
20+
21+
## Solution
22+
23+
You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-revision-session/blob/main/problems/whats_my_trie/solution/trie_sol.png).
60.4 KB
Loading

0 commit comments

Comments
 (0)