Skip to content

Commit

Permalink
Cheatsheets for CM1035 Algorithms & Data Structures 1 (#25)
Browse files Browse the repository at this point in the history
* init directory
* add Pseudocode syntax
* add section on vectors
* add section on queues
* seperate general syntax from data structures
* add section on stacks
* explicitly construct structures
* unify documents, update README
* generate PDF
* add section on time complexity
* update chart price
* update README
  • Loading branch information
lamafab authored Mar 6, 2023
1 parent 6a010a7 commit 9548df9
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# About

Listed here is a collection of cheatsheet by topic. Those cheatsheets do not
explain the topics in depth, but rather serve as quick lookup documents.
Therefore, the course material provided by the lecturer should still be studied
and understood. Not everything that is tested at the mid-terms or final exams is
covered and the Author does not guarantee that the cheatsheets are free of
errors.

* [Pseudocode, Data Structures & Time
Complexity](./cheatsheet_pseudocode_data_structures.pdf)

# Building

_NOTE_: This step is only necessary if you chose to modify the base documents.

The base documents are written in [AsciiDoc](https://asciidoc.org/) and can be
found in the `src/` directory.

The following dependencies must be installed (Ubuntu):

```console
$ apt install -y ruby-dev wkhtmltopdf
$ gem install asciidoctor
$ chmod +x build.sh
```

To build the documents (PDF version):

```console
$ ./build.sh pdf
```

Optionally, for the HTML version:

```console
$ ./build.sh html
```

and for the PNG version:

```console
$ ./build.sh png
```

The generated output can be deleted with `./build.sh clean`.

# Disclaimer

The Presented Documents ("cheatsheets") by the Author ("Fabio Lama") are
summaries of specific topics. The term "cheatsheet" implies that the Presented
Documents are intended to be used as learning aids or as references for
practicing and does not imply that the Presented Documents should be used for
inappropriate practices during exams such as cheating or other offenses.

The Presented Documents are heavily based on the learning material provided by
the University of London, respectively the VLeBooks Collection database in the
Online Library and the material provided on the Coursera platform.

The Presented Documents may incorporate direct or indirect definitions,
examples, descriptions, graphs, sentences and/or other content used in those
provided materials. **At no point does the Author present the work or ideas
incorporated in the Presented Documents as their own.**
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Because `make` sucks.

gen_html() {
# Remove suffix and prefix
FILE=$1
OUT=${FILE%.adoc}
HTML_OUT="cheatsheet_${OUT}.html"

asciidoctor $FILE -o ${HTML_OUT}
}

# Change directory to src/ in order to have images included correctly.
cd "$(dirname "$0")/src/"

case $1 in
html)
for FILE in *.adoc
do
# Generate HTML file.
gen_html ${FILE}
done

# Move up from src/
mv *.html ../
;;
pdf)
for FILE in *.adoc
do
# Generate HTML file.
gen_html ${FILE}

# Convert HTML to PDF.
PDF_OUT="cheatsheet_${OUT}.pdf"
wkhtmltopdf \
--enable-local-file-access \
--javascript-delay 2000\
$HTML_OUT $PDF_OUT
done

# Move up from src/
mv *.pdf ../

# Cleanup temporarily generated HTML files.
rm *.html > /dev/null 2>&1
;;
png | img)
for FILE in *.adoc
do
# Generate HTML file.
gen_html ${FILE}

# Convert HTML to PNG.
IMG_OUT="cheatsheet_${OUT}.png"
wkhtmltopdf \
--enable-local-file-access \
--javascript-delay 2000\
$HTML_OUT $IMG_OUT
done

# Move up from src/
mv *.png ../

# Cleanup temporarily generated HTML files.
rm *.html > /dev/null 2>&1
;;
clean)
rm *.html > /dev/null 2>&1
rm *.png > /dev/null 2>&1
rm ../*.html > /dev/null 2>&1
rm ../*.png > /dev/null 2>&1
;;
*)
echo "Unrecognized command"
;;
esac
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
= Cheatsheet - Pseudocode, Data Structures & Time Complexity
Fabio Lama <fabio[email protected]>
:description: Module: CM1015 Computational Mathematics, started 04. April 2022
:doctype: article
:sectnums: 4
:toclevels: 4
:stem:

== General Pseudocode Syntax (Example)

Pseudocode describes the logical steps of an algorithm or process in
(mostly) plain language.

image::assets/morning_chart.png[width=550, float="right"]

. stem:[bb "function " "Morning"(tt "conscious, done, cereal")]
. stem:[" " bb "if " tt "conscious" = "TRUE" bb " then"]
. stem:[" " " " tt "maths" larr 0]
. stem:[" " " " bb "for " 1 <= i <= tt "done" bb " do"]
. stem:[" " " " " " tt "maths" larr tt "maths" + 1]
. stem:[" " " " bb "end for"]
. stem:[" " " " bb "while " tt "cereal" > 0 bb " do"]
. stem:[" " " " " " tt "cereal" larr tt "cereal" - 1]
. stem:[" " " " bb "end while"]
. stem:[" " bb "end if"]
. stem:[" " bb "return " "ready!"]
. stem:[bb "end function"]

We can now call the stem:["Morning"] function:

. stem:[tt "status" larr "Morning"("TRUE", 10, 5)]

== Data Structures

=== Vector

A **vector** is a sequentially orderer collection of elements (like an ordered
set). For example, the following vector stem:[v] is of size _three_ and
contains the elements stem:[A], stem:[B] and stem:[C]:

[stem]
++++
"new Vector " v(3) = (A, B, C)
++++

The size of the vector is **fixed**, meaning one cannot add or remove items from
it (only replace individual items).

NOTE: In some programming languages, "vector" refers to a _growable_ collection
of data, which is very different and not the case here.

==== Operations

We can do operations on vectors:

* stem:["LENGTH"\[v\]]: Returns the size of vector stem:[v].
* stem:[v\[k\]]: Returns the element of the vector stem:[v] at index
stem:[k].
* stem:[v\[k\] larr o]: Stores the element stem:[o] into the vector stem:[v]
at index stem:[k].

For example, given vector:

[stem]
++++
"new Vector " v(4) = (A, B, C, D)
++++

Then:

[stem]
++++
"LENGTH"[v] = 4\
v[2] = B\
v[2] larr Z\
v[2] = Z\
v = (A, Z, C, D)
++++

NOTE: In programming languages, the index usually starts at stem:[0],
respectively the first element of the set is indexed at stem:[0], followed by
stem:[1, 2, ...]. In our case, we start the index at stem:[1].

=== Queue

A **queue** is a data structure where elements need to "wait" before they get
processed. Elements the get processed in the order the elements were added,
respectively "first in first out" (FIFO). Queues are not fixed sized. Elements
get added to the "tail" and come out at the "head".

[stem]
++++
"new Queue " q = (A, B, C)
++++

==== Operations

We can do operations on queues:

* stem:["HEAD"\[q\]]: Returns the element at the head of the queue.
* stem:["DEQUEUE"\[q\]]: Returns the element at the head of the queue and removes
that element from the queue.
* stem:["ENQUEUE"\[o, q\]]: Adds the element stem:[o] to the tail of the queue.
* stem:["EMPTY"\[q\]]: Returns _true_ if the queue is empty or _false_ if otherwise.

For example, given queue:

[stem]
++++
"new Queue " q = (A, B, C, D)
++++

Then:

[stem]
++++
"HEAD"[q] = D\
q = (A, B, C, D)\
"DEQUEUE"[q] = D\
q = (A, B, C)\
"ENQUEUE"[Z, q]\
q = (Z, A, B, C)\
"EMPTY"[q] = "false"
++++

=== Stack

A stack is like a queue, but elements are processed in the "last in first out"
(LIFO) order.

==== Operations

* stem:["PUSH"\[o,s\]]: Adds element stem:[o] to the stack.
* stem:["TOP"\[s\]]: Returns the last inserted element from the stack.
* stem:["POP"\[s\]]: Returns the last inserted element from the stack and
removes that element from the stack.
* stem:["EMPTY"\[s\]]: Returns _true_ if the stack is empty or _false_ if
otherwise.

For example, given stack:

[stem]
++++
"new Stack " s = (A, B, C)
++++

Then:

[stem]
++++
"PUSH"[Z,s]\
s = (A, B, C, Z)\
"TOP"[s] = Z\
s = (A, B, C, Z)\
"POP"[s] = Z\
s = (A, B, C)\
"EMPTY"[s] = "false"
++++

== Time Complexity

.Source: https://youtu.be/47GRtdHOKMg
image::assets/time_complexity.jpg[align=center, width=500]

0 comments on commit 9548df9

Please sign in to comment.