Skip to content

Commit 55c9d06

Browse files
authored
Merge pull request #37 from lamafab/lamafab-algos-data-ii
Cheatsheets for CM2035 Algorithms & Data Structures II
2 parents ea76782 + f2dcb6f commit 55c9d06

16 files changed

+1113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# About
2+
3+
Listed here is a collection of cheatsheet by topic. Those cheatsheets do not
4+
explain the topics in depth, but rather serve as quick lookup documents.
5+
Therefore, the course material provided by the lecturer should still be studied
6+
and understood. Not everything that is tested at the mid-terms or final exams is
7+
covered and the Author does not guarantee that the cheatsheets are free of
8+
errors.
9+
10+
* [Time and Space Complexity](./cheatsheet_time_space_complexity.pdf)
11+
* [Asymptotic Analysis](./cheatsheet_asymptotic_analysis.pdf)
12+
* [Time Complexity of Recursive Algorithms](.cheatsheet_time_complexity_recursive_algorithms.pdf)
13+
* [Comparison and Non-Comparison Sorting Algorithms](./cheatsheet_sorting_algorithms.pdf)
14+
* [Hash Tables](./cheatsheet_hash_tables.pdf)
15+
16+
**NOTE**: Those cheatsheets only cover the course material **up to the midterms**.
17+
The weeks after the midterms are not covered here.
18+
19+
# Building
20+
21+
_NOTE_: This step is only necessary if you chose to modify the base documents.
22+
23+
The base documents are written in [AsciiDoc](https://asciidoc.org/) and can be
24+
found in the `src/` directory.
25+
26+
The following dependencies must be installed (Ubuntu):
27+
28+
```console
29+
$ apt install -y ruby-dev wkhtmltopdf
30+
$ gem install asciidoctor
31+
$ chmod +x build.sh
32+
```
33+
34+
To build the documents (PDF version):
35+
36+
```console
37+
$ ./build.sh pdf
38+
```
39+
40+
Optionally, for the HTML version:
41+
42+
```console
43+
$ ./build.sh html
44+
```
45+
46+
and for the PNG version:
47+
48+
```console
49+
$ ./build.sh png
50+
```
51+
52+
The generated output can be deleted with `./build.sh clean`.
53+
54+
# Disclaimer
55+
56+
The Presented Documents ("cheatsheets") by the Author ("Fabio Lama") are
57+
summaries of specific topics. The term "cheatsheet" implies that the Presented
58+
Documents are intended to be used as learning aids or as references for
59+
practicing and does not imply that the Presented Documents should be used for
60+
inappropriate practices during exams such as cheating or other offenses.
61+
62+
The Presented Documents are heavily based on the learning material provided by
63+
the University of London, respectively the VLeBooks Collection database in the
64+
Online Library and the material provided on the Coursera platform.
65+
66+
The Presented Documents may incorporate direct or indirect definitions,
67+
examples, descriptions, graphs, sentences and/or other content used in those
68+
provided materials. **At no point does the Author present the work or ideas
69+
incorporated in the Presented Documents as their own.**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# Because `make` sucks.
4+
5+
gen_html() {
6+
# Remove suffix and prefix
7+
FILE=$1
8+
OUT=${FILE%.adoc}
9+
HTML_OUT="cheatsheet_${OUT}.html"
10+
11+
asciidoctor $FILE -o ${HTML_OUT}
12+
}
13+
14+
# Change directory to src/ in order to have images included correctly.
15+
cd "$(dirname "$0")/src/"
16+
17+
case $1 in
18+
html)
19+
for FILE in *.adoc
20+
do
21+
# Generate HTML file.
22+
gen_html ${FILE}
23+
done
24+
25+
# Move up from src/
26+
mv *.html ../
27+
;;
28+
pdf)
29+
for FILE in *.adoc
30+
do
31+
# Generate HTML file.
32+
gen_html ${FILE}
33+
34+
# Convert HTML to PDF.
35+
PDF_OUT="cheatsheet_${OUT}.pdf"
36+
wkhtmltopdf \
37+
--enable-local-file-access \
38+
--javascript-delay 2000\
39+
$HTML_OUT $PDF_OUT
40+
done
41+
42+
# Move up from src/
43+
mv *.pdf ../
44+
45+
# Cleanup temporarily generated HTML files.
46+
rm *.html > /dev/null 2>&1
47+
;;
48+
png | img)
49+
for FILE in *.adoc
50+
do
51+
# Generate HTML file.
52+
gen_html ${FILE}
53+
54+
# Convert HTML to PNG.
55+
IMG_OUT="cheatsheet_${OUT}.png"
56+
wkhtmltopdf \
57+
--enable-local-file-access \
58+
--javascript-delay 2000\
59+
$HTML_OUT $IMG_OUT
60+
done
61+
62+
# Move up from src/
63+
mv *.png ../
64+
65+
# Cleanup temporarily generated HTML files.
66+
rm *.html > /dev/null 2>&1
67+
;;
68+
clean)
69+
rm *.html > /dev/null 2>&1
70+
rm *.png > /dev/null 2>&1
71+
rm ../*.html > /dev/null 2>&1
72+
rm ../*.png > /dev/null 2>&1
73+
;;
74+
*)
75+
echo "Unrecognized command"
76+
;;
77+
esac
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
= Cheatsheet - Asymptotic Analysis
2+
Fabio Lama <fabio[email protected]>
3+
:description: Module: CM2035 Algorithms and Data Structures II, started April 2024
4+
:doctype: article
5+
:sectnums: 4
6+
:toclevels: 4
7+
:stem:
8+
9+
== About
10+
11+
Asymptotic analysis is an alternative way of describing the time or memory
12+
requirements of an algorithm.
13+
14+
== Big O Notation
15+
16+
Big O notation stem:[O(x)] defines a set of functions that act as an **upper bound**
17+
stem:[g(N)] for stem:[T(N)]. Formally defined as:
18+
19+
stem:[T(N)] is stem:[O(g(N))] if there exist positive
20+
constants stem:[c] and stem:[n_0] such that:
21+
22+
[stem]
23+
++++
24+
T(N) <= c xx g(N) " for all " N > n_0
25+
++++
26+
27+
Note that there can be **multiple functions** stem:[g_x(N)] that act as **an upper bound**
28+
for stem:[T(N)]. Additionally, do notice that it's **not necessary** that
29+
stem:[c xx g(N)] is equal to or greater than stem:[T(N)] for all values of
30+
stem:[N].
31+
32+
image::assets/big_o_notation.png[align=center, width=300]
33+
34+
For example, consider:
35+
36+
[stem]
37+
++++
38+
T(N) = 10 N^2 + 15N + 5\
39+
g(N) = N^2\
40+
c = 1
41+
++++
42+
43+
Here, stem:[c xx g(N)] is never greater than stem:[T(N)], because there is no
44+
solution for:
45+
46+
[stem]
47+
++++
48+
10 N^2 + 15N + 5 <= 1 xx N^2
49+
++++
50+
51+
However, consider:
52+
53+
[stem]
54+
++++
55+
c = 25
56+
++++
57+
58+
In case of stem:[N = 1] we get:
59+
60+
[stem]
61+
++++
62+
10 xx 1^2 + 15 xx 1 + 5 <= 25 xx 1^2\
63+
= 10 + 15 + 5 <= 25\
64+
= 30 <= 25
65+
++++
66+
67+
Which is false. However, for stem:[N = 2] we get:
68+
69+
[stem]
70+
++++
71+
10 xx 2^2 + 15 xx 2 + 5 <= 25 xx 2^2\
72+
= 40 + 30 + 5 <= 100\
73+
= 75 <= 100
74+
++++
75+
76+
Which is true. Therefore:
77+
78+
[stem]
79+
++++
80+
T(N) " is " O(N^2) " because"\
81+
T(N) <= 25 xx g(N) " for all " N >= 2
82+
++++
83+
84+
There choice for stem:[c] **is arbitrary**, as long as it satisfies the conditions.
85+
86+
== Omega Notation
87+
88+
The Omega notation stem:[Omega(x)] defines a set of functions that act as a
89+
**lower bound** stem:[g(N)] for (stem:[T(N)]). Formally defined as:
90+
91+
stem:[T(N)] is stem:[Omega(g(N))] if there exist positive constants stem:[c] and
92+
stem:[n_0] such that:
93+
94+
[stem]
95+
++++
96+
T(N) >= c xx g(N) " for all " N > n_0
97+
++++
98+
99+
Similarly to the Big O notation, there can be **multiple functions** stem:[g_x(N)]
100+
that act as **a lower bound** for stem:[T(N)] and it's **not necessary** that
101+
stem:[c xx g(N)] is equal to or less than stem:[T(N)] for all values of
102+
stem:[N], but only for the larger values.
103+
104+
image::assets/omega_notation.png[align=center, width=300]
105+
106+
== Theta Notation
107+
108+
The Theta notation stem:[Theta(x)] defines a **single function** that acts as
109+
both an **upper and lower bound** for (stem:[T(N)]). Formally defined as:
110+
111+
stem:[T(N)] is stem:[Theta(g(N))] if there exist positive constants stem:[c_1],
112+
stem:[c_2] and stem:[n_o] such that both those conditions hold true:
113+
114+
[stem]
115+
++++
116+
T(N) >= c_1 xx g(N) " for all " N > n_0\
117+
T(N) <= c_2 xx g(N) " for all " N > n_0
118+
++++
119+
120+
Alternatively:
121+
122+
[stem]
123+
++++
124+
c_1 xx g(N) <= T(N) <= c_2 xx g(N) " for all " N > n_0
125+
++++
126+
127+
image::assets/theta_notation.png[align=center, width=300]
128+
129+
As already noted, Theta notation has **only one function**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
= Cheatsheet - Hash Tables
2+
Fabio Lama <fabio[email protected]>
3+
:description: Module: CM2035 Algorithms and Data Structures II, started April 2024
4+
:doctype: article
5+
:sectnums: 4
6+
:toclevels: 4
7+
:stem:
8+
9+
== About
10+
11+
A hash table is a data structure that maps keys to values using a hash function. This function transforms the input (key) into a fixed-size integer, which serves as an index in an array, enabling fast data retrieval. Hash tables are widely used due to their average stem:[Theta(1)] time complexity for both insertion and lookup operations.
12+
13+
== Search Algorithms Complexity
14+
15+
WARNING: This table assumes no collisions in the hash table.
16+
17+
|===
18+
|Name |Worst case (time) |Best case (time)| Worst case (space) |Best case (space)
19+
20+
|Linear Search
21+
|stem:[Theta(N)]
22+
|stem:[Theta(1)]
23+
|stem:[Theta(1)]
24+
|stem:[Theta(1)]
25+
26+
|Binary Search (iterative)
27+
|stem:[Theta(log N)]
28+
|stem:[Theta(1)]
29+
|stem:[Theta(1)]
30+
|stem:[Theta(1)]
31+
32+
|Binary Search (recursive)
33+
|stem:[Theta(log N)]
34+
|stem:[Theta(1)]
35+
|stem:[Theta(log N)]
36+
|stem:[Theta(1)]
37+
38+
|Direct Addressing
39+
|stem:[Theta(1)]
40+
|stem:[Theta(1)]
41+
|stem:[Theta(k)]
42+
|stem:[Theta(k)]
43+
44+
|Hash Table
45+
|stem:[Theta(1)]
46+
|stem:[Theta(1)]
47+
|stem:[Theta(M)]
48+
|stem:[Theta(M)]
49+
|===
50+
51+
Where stem:[k] is the maximum possible key value and stem:[M] is the size of the hash table.
52+
53+
== Hash Tables
54+
55+
Hash tables use an index of an array to represent a number. Consider an array of
56+
size 7 and the follwing, simple hash function:
57+
58+
[stem]
59+
++++
60+
h(x) = x mod 7
61+
++++
62+
63+
For example, lets say stem:[x = 11] and we compute:
64+
65+
[stem]
66+
++++
67+
h(11) = 11 mod 7 = 4
68+
++++
69+
70+
This means the number 11 is stored in the array at index 4. Knowing the index,
71+
the search for that number is very fast. However, given that the size of this
72+
array is very small, **the number of collisions can be very high**, depending on
73+
number of inputs.
74+
75+
For example, both 11 and 18 would be stored at index 4:
76+
77+
[stem]
78+
++++
79+
h(11) = 11 mod 7 = 4\
80+
h(18) = 18 mod 7 = 4
81+
++++
82+
83+
If necessary, the hash table can be **extended** with a larger array, but this
84+
requires rehashing all the existing elements. Alternatively, the method of
85+
**linear probing** can be applied to use the next available index in case of a
86+
collision (note that this information must be stored somewhere). Or, each index
87+
can be a pointer to a separate, nested table, which is a **separate chaining** method.
88+
89+
The best case and worst case complexity for hash tables must consider those
90+
collision handling methods as well, as in how it behaves with no collisions at
91+
all and with all elements colliding, respectively.

0 commit comments

Comments
 (0)