Skip to content
Snippets Groups Projects
Commit 77ac330e authored by Tobias Koelling's avatar Tobias Koelling
Browse files

scaffold for complexity lecture

parent 194bb689
No related branches found
No related tags found
1 merge request!24complexity lecture
...@@ -30,7 +30,7 @@ website: ...@@ -30,7 +30,7 @@ website:
- "lectures/git/slides.qmd" - "lectures/git/slides.qmd"
- "lectures/programming-paradigms/slides.qmd" - "lectures/programming-paradigms/slides.qmd"
- "lectures/data-structures/slides.qmd" - "lectures/data-structures/slides.qmd"
# - "lectures/complexity/slides.qmd" - "lectures/complexity/slides.qmd"
# - "lectures/debugging/slides.qmd" # - "lectures/debugging/slides.qmd"
# - "lectures/good-scientific-practice/slides.qmd" # - "lectures/good-scientific-practice/slides.qmd"
# - "lectures/user-experience/slides.qmd" # - "lectures/user-experience/slides.qmd"
......
---
title: "Complexity"
author: "Tobias Kölling and Dominik Zobel"
---
# Computational complexity
## time-complexity
## space-complexity
## big-$\mathcal{O}$ notation
# specific algorithms
* sorting (e.g. bubble, quick, merge, bogo... [idea instructions](https://idea-instructions.com))
* nearest-neighbor (?)
* bisect (?) (ref to `git bisect`)
# models of computation
# an example
*(Fibonacci)*
## Fibonacci series
$$
f(0) = 0 \\
f(1) = 1 \\
f(n) = f(n-2) + f(n-1)
$$
## fib1
```{python}
#| echo: true
def fib1(n):
if n < 2: return n
return fib1(n - 2) + fib1(n - 1)
[fib1(i) for i in range(10)]
```
```{python}
#| echo: true
%%time
fib1(30)
```
## fib2
```{python}
#| echo: true
def fib2(n):
if n < 2: return n
a = 0; b = 1
for i in range(2, n + 1): a, b = b, a + b
return b
[fib2(i) for i in range(10)]
```
```{python}
#| echo: true
%%time
fib2(30)
```
## fib3
$$
\begin{pmatrix}f_{n-1}\\f_{n}\end{pmatrix} = \begin{pmatrix}0 & 1\\1 & 1\end{pmatrix} \begin{pmatrix}f_{n-2}\\f_{n-1}\end{pmatrix}
$$
## graph
```{python}
import time
import matplotlib.pylab as plt
def get_time(f):
start = time.perf_counter()
f()
end = time.perf_counter()
return end - start
x1 = list(range(30))
x2 = list(range(60))
y1 = [get_time(lambda: fib1(i)) for i in x1]
y2 = [get_time(lambda: fib2(i)) for i in x2]
plt.plot(x1, y1, label="fib1")
plt.plot(x2, y2, label="fib2")
plt.xlabel("n")
plt.ylabel("time / s")
plt.legend()
None
```
# Takeaway
## different algorithms for the same problem
Often there are different algorithms solving the same problem.
These can come with **very** different complexity ratings.
## typical tasks
For certain tasks (e.g. sorting, neighbors, averaging, or really anything...), think about what complexity class is expected.
If your program behaves differently, **change it** or **seek advice** from colleagues.
# Complexity in software design
## interdependence of components
possibly also amount of coordination and communication needed
## ways to counteract
- Abstraction
- Encapsulation
## Maintainability
_Also focus on maintainability. This lecture might or might not be the right place, but it should be useful somewhere_
# Resources
## Computational Complexity
- "Models of Computation: Exploring the Power of Computing"_ by _John Savage_, available [here](https://cs.brown.edu/people/jsavage/). Also covers some parts of computational complexity.
- Textbooks from [lecture from Stanford](https://theory.stanford.edu/~liyang/teaching/complexity.html)
## Complexity in Software Design
- "Clean Code" by _Robert Martin_ [BIS-Erdsystem](https://kataloge.hh.gbv.de/DB=1.20/XMLPRS=N/PPN?PPN=1680459244)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment