Skip to content

Updated 2521 page for 23T2 #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions data/articles/2521-revision-theory.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: COMP2521 Revision Theory Questions
date: 2022-08-01
date: 2023-07-29
desc: Theory based questions for the CSESoc COMP2521 Exam Revision Session.
tags:
- 'COMP2521'
Expand All @@ -9,9 +9,9 @@ author: CSESoc Education Team
coverPhoto: '/images/generic/alexandru-acea-XEB8y0nRRP4-unsplash.jpg'
---

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.
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.

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

<MultiChoice>
<MultiChoice.Question>
Expand Down
11 changes: 8 additions & 3 deletions data/course-revision/1511-23T2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ Then run the following command to copy the template files:
$ ~csesoc/1511-fetch
```

## Solutions
### Solutions

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

<br />

## Miscellaneous resources

<br />
## [Pointers and Memory
Slides](https://docs.google.com/presentation/d/1Iz8lKozedeIq5ECu0vN3ZPgnke88r2L5H7UIt9otwC8/edit?usp=sharing)

### [Pointers and Memory Slides]

(https://docs.google.com/presentation/d/1Iz8lKozedeIq5ECu0vN3ZPgnke88r2L5H7UIt9otwC8/edit?usp=sharing)

<figure class="video_container">
<iframe
Expand Down
28 changes: 0 additions & 28 deletions data/course-revision/2521-22T3.mdx

This file was deleted.

58 changes: 58 additions & 0 deletions data/course-revision/2521-23T2.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: CSESoc COMP2521 Revision Practice Problems
desc: Practical coding exercises to help you prepare for your COMP2521 23T2 final exam 😄
course: COMP2521
offering: 23T2
---

# CSESoc COMP2521 Revision Practice Problems

Note:

For COMP2521 multiple choice theory problems, see [here](https://learn.csesoc.org.au/articles/2521-revision-theory)

For COMP2521 short answer theory problems, see [here](https://learn.csesoc.org.au/course-revision/2521-23T2/theory)

This is a set of practical programming problems designed to help you prepare for your COMP2521 23T2 final exam.

There are autotests for each problem on cse servers which you can use to check your solutions.

To start, log in to cse servers via ssh and create a new folder:

```bash:~
$ mkdir 2521-revision
$ cd 2521-revision
```

Then run the following command to copy the template files:

```bash:~/2521-revision
$ 2521 fetch revision
```

<br />

## Miscellaneous resources

<br />

### [Time-Efficient Problem Solving Slides]

(https://docs.google.com/presentation/d/1OlIyQTg3afU6UOkdY4s2hpacSUsRSVGUboPcxXDEvA0/edit#slide=id.ge7f9c668d6_0_1036)

<figure class="video_container">
<iframe
src="https://docs.google.com/presentation/d/e/2PACX-1vQSdJqbTa0aI4foczJ5q3FJFaqkDzWpdDP-iVvv7zQ50jHmvU0JeOELspAFunfgMaryXlChC90Hg19t/embed?start=false&loop=false&delayms=3000"
frameborder="0"
width="960"
height="569"
allowfullscreen="true"
mozallowfullscreen="true"
webkitallowfullscreen="true"></iframe>
</figure>

<br />

### [Implementation: Hashmap with linear probing collision resolution]

https://github.com/Allynixtor/comp2521-revision-session/blob/main/miscellaneous_content/hashmap_linear_probe.c
158 changes: 158 additions & 0 deletions data/course-revision/2521-23T2/chaining_in_hashmaps.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
title: Chaining in Hashmaps
desc: A walkthrough to implementing chaining as a collision resolution to hashmaps.
class: COMP2521
difficulty: 1
---

# Chaining in Hashmaps

_Written by Saga Chandra from CSESoc Education_

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.

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.

_Sidenote:_
You can only test your code once you have completed all the tasks.

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.

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)

//
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.

**Sample**
Input:

```bash
"hi"
```

Output:

```bash
209
```

//

However, if we simply leave it as is, we begin to waste a lot of space.
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.

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.
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.
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?

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:
`hash value: k^0 * str[0] + k * str[1] + k^2 * str[2] + ... + k^n * str[n]`.
What would happen if we were to modulo every hash value by the same `k`?
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!
Which, suffice to say, is incredibly bad.

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.

Hence, the size of our hashmap should always be the next prime number from the given size.

//
Your second task will be to write the functions `hash` and `HashMap_New` in `HashMap.c`.

The function `hash` signature can be altered but note that it will be used in the insertion and lookup of your HashMap.

The function `hash` should perform the modulo of the hash of str by a prime number and return that value.
**Sample**
Input:

```c
// We are hashing the str "hi" with the prime modulo 3
// Note that your hash function signature can be different
hash("hi", 3)
```

Output:

```bash
2
```

The function `HashMap_New` signature should not be changed.

`HashMap_New` takes in an integer `n` that specifies the size the user wants the new HashMap to be.

`HashMap_New` should initialise a HashMap that has the size of the next prime number after `n` and return the new HashMap.
//

Moving on to the actual useful parts of a hashmap, insertion and lookup implementations.

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.

One method we are going to implement is called chaining.
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.

For example, lets insert "cat": "one", and "act": "two" into our hashmap.

Initially we have an empty hashmap with four empty buckets:

```
HashMap: | NULL | NULL | NULL | NULL |
```

At first, when we insert "cat": "one", we can insert a linked list node that contains the key, "cat", and the value "one".

```
HashMap: | NULL | "cat":"one" | NULL | NULL |
```

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:

```
HashMap: | NULL | "act":"two" | NULL | NULL |
```

However, with chaining, we can simply create a new linked list node and append it onto the bucket.

```
HashMap: | NULL | "cat":"one" -> "act":"two" | NULL | NULL |
```

_Sidenotes:_
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.

//
Your final task will be to implement `HashMap_Insert` and `HashMap_Get` in `HashMap.c`.
You should not alter either of these function's signatures.

`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.

`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.
//

## How will your answers be tested?

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`
.
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.

Then it will ask for a Key, and then a Value `s` times, which should be inserted into the hash map.

Finally, the program will print out the entire hash map, showing each individual bucket, and its contents.

_Sidenote:_
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.

If the key does not exist, then `HashMap_get` should return the string "Not Found".

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.

## CSE Autotest

When you think your program is working, you can use CSE autotest to test your solution.

```bash:~/2521-revision/hashmap_chaining:~
$ 2521 autotest hashmap_chaining
```

## Solution

You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-revision-session/tree/main/problems/hashmap_chaining/solution).
49 changes: 49 additions & 0 deletions data/course-revision/2521-23T2/theory.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Short Answer Questions
desc: Short answer questions to test your theory.
class: COMP2521
difficulty: 1
---

# CSESoc COMP2521 Short Answer Practice Problems

## Q1

A and B are weighted, undirected graphs. B has the same edges as A, except the weights are increased by 3.

a) Do the minimal spanning trees of A and B always contain the same edges? Justify your answer.

b) Consider the tree of single-source shortest paths from some vertex (e.g. as produced by Dijkstra's algorithm).
Does this tree always contain the same edges in both graphs? Justify your answer.

## Q2

A particular graph is stored as an array of edges, each represented as a pair of vertices.
When a new edge is added, it is placed at the end of the array.
For example: [{2, 3}, {1, 2}, {0, 2}, {0, 1}].

a) What is the time complexity of breadth first search using this representation? Give your answer in terms
of V and E.

b) How can this representation be adjusted to improve the complexity of BFS, while maintaining the same
space complexity?

## Q3

Consider an arbitrary undirected connected graph G.
A graph G\* is constructed by replacing every edge of G with 4 edges and two vertices like this:

```
G G*
------ C ------
/ \
A ------------- B => A B
\ /
------ D ------
```

Is it true that G\* has an Euler cycle?

## Solutions

You can view the solution code to this problem [here](https://github.com/Allynixtor/comp2521-revision-session/blob/main/problems/theory/solution/answers.mdx).
23 changes: 23 additions & 0 deletions data/course-revision/2521-23T2/whats_my_trie.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: What's my trie?
desc: Draw a trie with the minimum number of nodes, which is equal to the following trie
class: COMP2521
difficulty: 1
---

# What's my trie?

Suppose two tries are "equal" if they contain exactly the same words.

Draw a trie with the minimum number of nodes, which is equal to the following trie

<img
style={{ maxWidth: '500px' }}
src="/images/comp2521-revision/trie.png"
alt="Graph"
width="70%"
/>

## Solution

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).
Binary file added public/images/comp2521-revision/trie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading