diff --git a/src/teaching/cse-250/2022fa/index.erb b/src/teaching/cse-250/2022fa/index.erb index 9d6ef673..d9e60ef5 100644 --- a/src/teaching/cse-250/2022fa/index.erb +++ b/src/teaching/cse-250/2022fa/index.erb @@ -217,7 +217,7 @@ schedule: special: "No Class: Labor Day" dow: Mon - date: 09/07/22 - topic: Runtime Analysis + topic: Runtime Analysis - Intuitions due: AI Quiz dow: Wed section_a: @@ -226,33 +226,44 @@ schedule: section_b: slides: slide/3b-FunctionAnalysis.html - date: 09/09/22 - topic: Runtime Analysis (contd...) + topic: Runtime Analysis - Formalism dow: Fri due: PA0 + section_a: + slides: slide/4b-AsymptoticNotation.html section_b: slides: slide/4b-AsymptoticNotation.html - week: 3 lectures: - date: 09/12/22 - topic: Collections, Sequences, ADTs dow: Mon + topic: Runtime Analysis - Formalism (contd...) + topic: + section_a: + slides: slide/4b-AsymptoticNotation.html + section_b: + slides: slide/4b-AsymptoticNotation.html - date: 09/14/22 - topic: Array Lists + topic: Runtime Analysis - Examples dow: Wed + section_b: + slides: slide/6b-AsymptoticExamples.html - date: 09/16/22 - topic: Linked Lists + topic: Collections, Sequences, ADTs dow: Fri due: PA1 + section_b: + slides: slide/7b-ADTsandSequences.html - week: 4 lectures: - date: 09/19/22 - topic: Recursion Intro + topic: Collections, Sequences, ADTs (contd...) dow: Mon - date: 09/21/22 - topic: Divide and Conquer, Tail Recursion + topic: Collections, Sequences, ADTs (contd...) dow: Wed - date: 09/23/22 - topic: Recursion (contd...) + topic: Recursion Intro dow: Fri due: WA0 - week: 5 diff --git a/src/teaching/cse-250/2022fa/slide/4b-AsymptoticNotation.erb b/src/teaching/cse-250/2022fa/slide/4b-AsymptoticNotation.erb index dd2b0bb0..fab574bc 100644 --- a/src/teaching/cse-250/2022fa/slide/4b-AsymptoticNotation.erb +++ b/src/teaching/cse-250/2022fa/slide/4b-AsymptoticNotation.erb @@ -1,7 +1,7 @@ --- template: templates/cse250_2022_slides.erb title: Asymptotic Notation -date: Sept 7, 2022 +date: Sept 7 and 12, 2022 textbook: "Ch. 7.3-7.4" --- @@ -342,6 +342,17 @@ textbook: "Ch. 7.3-7.4"

Problem: What if $g(n)$ doesn't bound $f(n)$ from both above and below?

+
+

+    if input = 1:
+      /* take 1 step */
+    else:
+      /* take n steps */
+  
+ +

Schroedinger's Code: Simultaneously like $f_1(n) = 1$ and $f_2(n) = n$

+
+

Upper, Lower Bounds

diff --git a/src/teaching/cse-250/2022fa/slide/6b-AsymptoticExamples.erb b/src/teaching/cse-250/2022fa/slide/6b-AsymptoticExamples.erb new file mode 100644 index 00000000..f23bfefd --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/6b-AsymptoticExamples.erb @@ -0,0 +1,241 @@ +--- +template: templates/cse250_2022_slides.erb +title: Asymptotic Notation Examples +date: Sept 14, 2022 +textbook: "Ch. 7.3-7.4" +--- + +
+

Big-_ Notation Recap

+ +
+
Big-ϴ
+
Growth Functions in the same complexity class.
+
If $f(n) \in \Theta(g(n))$, then an algorithm that takes $f(n)$ steps is exactly as fast as one that takes $g(n)$ steps.
+ +
Big-O
+
Growth Functions in the same or smaller complexity class.
+
If $f(n) \in O(g(n))$, then an algorithm that takes $f(n)$ steps is as fast as or faster than one that takes $g(n)$ steps.
+ +
Big-Ω
+
Growth Functions in the same or bigger complexity class.
+
If $f(n) \in \Omega(g(n))$, then an algorithm that takes $f(n)$ steps is as slow as or slower than one that takes $g(n)$ steps.
+
+
+ +
+

Common Runtimes

+ +
+
Constant Time: $\Theta(1)$
+
e.g., $T(n) = c$ (runtime is independent of $n$)
+ +
Logarithmic Time: $\Theta(\log(n))$
+
e.g., $T(n) = c\log(n)$ (for some constant $c$)
+ +
Linear Time: $\Theta(n)$
+
e.g., $T(n) = c_1n + c_0$ (for some constants $c_0, c_1$)
+ +
Quadratic Time: $\Theta(n^2)$
+
e.g., $T(n) = c_2n^2 + c_1n + c_0$
+ +
Polynomial Time: $\Theta(n^k)$ (for some $k \in \mathbb Z^+$)
+
e.g., $T(n) = c_kn^k + \ldots + c_1n + c_0$
+ +
Exponential Time: $\Theta(c^n)$ (for some $c \geq 1$)
+
+
+ +
+

Constants vs Asymptotics

+
+ +
+

Constant-Factor Speedups

+ +

+    for(i ← 0 until n) { /* do work */ }
+  
+ +
+ +
+

$c$ and $n_0$

+ +

Compare $T_1(n) = 100n$ vs $T_2(n) = n^2$

+ +
+ +
+

$c$ and $n_0$

+ +

Asymptotically slower runtimes can be better.

+ +
+ +
+

... but from now on, if $T_2(n)$ is in a bigger complexity class, then $T_1(n)$ is better/faster/stronger.

+
+ +
+

Examples

+
+ +
+

Bubble Sort

+

+    bubblesort(seq: Seq[Int]):
+  1.  n ← seq length
+  2.  for i ← n-2 to 0, by -1:
+  3.    for j ← i to n-1:
+  4.      if seq(j+1) < seq(j):
+  5.        swap seq(j) and seq(j+1)
+  
+

What is the runtime complexity class of Bubble Sort?

+
+ +
+

Summation Rules

+ +
    +
  1. $\sum_{i=j}^{k}c = (k - j + 1)c$
  2. + +
  3. $\sum_{i=j}^{k}(cf(i)) = c\sum_{i=j}^{k}f(i)$
  4. + +
  5. $\sum_{i=j}^{k}(f(i) + g(i)) = \left(\sum_{i=j}^{k}f(i)\right) + \left(\sum_{i=j}^{k}g(i)\right)$
  6. + +
  7. $\sum_{i=j}^{k}(f(i)) = \left(\sum_{i=\ell}^{k}(f(i))\right) - \left(\sum_{i=\ell}^{j-1}(f(i))\right)$ (for any $\ell < j$)
  8. + +
  9. $\sum_{i=j}^{k}f(i) = f(j) + f(j+1) + \ldots + f(k-1) + f(k)$
  10. + +
  11. $\sum_{i=j}^{k}f(i) = f(j) + \ldots + f(\ell - 1) + \left(\sum_{i=\ell}^k f(i)\right)$ (for any $j < \ell \leq k$)
  12. + +
  13. $\sum_{i=j}^{k}f(i) = \left(\sum_{i=j}^{\ell}f(i)\right) + f(\ell+1) + \ldots + f(k)$ (for any $j \leq \ell < k$)
  14. + +
  15. $\sum_{i=1}^{k}i = \frac{k(k+1)}{2}$
  16. + +
  17. $\sum_{i=0}^{k}2^i = 2^{k+1}-1$
  18. + +
  19. $n! \leq c_sn^n$ is a tight upper bound (Sterling: Some constant $c_s$ exists)
  20. +
+
+ +
+

Bubble Sort

+

+    bubblesort(seq: Seq[Int]):
+  1.  n ← seq length
+  2.  for i ← n-2 to 0, by -1:
+  3.    for j ← i to n-1:
+  4.      if seq(j+1) < seq(j):
+  5.        swap seq(j) and seq(j+1)
+  
+

Note: We can ignore the exact number of steps required by any step in the algorithm, as long as we know its complexity

+

Can we safely say this algorithm is $\Theta(n^2)$?

+
+ +
+

Bubble Sort (for Mutable Sequences)

+

+    def sort(seq: mutable.Seq[Int]): Unit =
+    {
+      val n = seq.length
+      for(i <- n - 2 to 0 by -1; j <- i to n)
+      {
+        if(seq(n) < seq(j))
+        {
+          val temp = seq(j+1)
+          seq(j+1) = seq(j)
+          seq(j) = temp
+        }
+      }
+    }
+  
+
+ +
+

Bubble Sort (for Immutable Sequences)

+

+    def sort(seq: Seq[Int]): Seq[Int] =
+    {
+      val newSeq = seq.toArray
+      val n = seq.length
+      for(i <- n - 2 to 0 by -1; j <- i to n)
+      {
+        if(newSeq(n) < newSeq(j))
+        {
+          val temp = newSeq(j+1)
+          newSeq(j+1) = newSeq(j)
+          newSeq(j) = temp
+        }
+      }
+      return newSeq.toList
+    }
+  
+
+ +
+

Searching Sequences

+

+    def indexOf[T](seq: Seq[T], value: T, from: Int): Int =
+    {
+      for(i <- from until seq.length)
+      {
+        if(seq(i).equals(value)) { return i }
+      }
+      return -1
+    }
+  
+
+ +
+

Searching Sequences

+

+    def count[T](seq: Seq[T], value: T): Int =
+    {
+      var count = 0; 
+      var i = indexOf(seq, value, 0)
+      while(i != -1)
+      {
+        count += 1;
+        i = indexOf(seq, value, i+1)
+      }
+      return count
+    }
+  
+
+ +
+

Searching Sorted Sequences

+ +

... with $O(1)$ access to elements ('random access')

+ + + +

What if no random access is available?

+
\ No newline at end of file diff --git a/src/teaching/cse-250/2022fa/slide/7b-ADTsandSequences.erb b/src/teaching/cse-250/2022fa/slide/7b-ADTsandSequences.erb new file mode 100644 index 00000000..969a449e --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/7b-ADTsandSequences.erb @@ -0,0 +1,196 @@ +--- +template: templates/cse250_2022_slides.erb +title: Asymptotic Notation Examples +date: Sept 16, 2022 +textbook: "Ch. 7.1, 1.7.2" +--- + + + +
+

Sequences

+ +
+
The Fibonacci Sequence
+
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
+ +
Characters of a String
+
'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'
+ +
Lines of a file.
+
+
+ +
+

What can you do with a Sequence?

+ + +
+ +
+

Abstract Data Types (ADTs)

+ + +
+ +
+

The Seq ADT

+ +
+
apply(idx: Int): [A]
+
Get the element (of type A) at position idx.
+ +
iterator: Iterator[A]
+
Get access to
    view
all elements in the seq, in order, once.
+ +
length: Int
+
Count the number of elements in the seq.
+
+
+ +
+

The mutable.Seq ADT

+ +
+
apply(idx: Int): [A]
+
Get the element (of type A) at position idx.
+ +
iterator: Iterator[A]
+
Get access to
    view
all elements in the seq, in order, once.
+ +
length: Int
+
Count the number of elements in the seq.
+ +
insert(idx: Int, elem: A): Unit
+
Insert an element at position idx with value elem.
+ +
remove(idx: Int): A
+
Remove the element at position idx and return the removed value.
+
+
+ +
+

... so how do we implement it?

+
+ +
+

RAM (CSE 220 crossover)

+ + +
+ +
+

RAM

+ +
+
new T()
+
Find some unused part of memory big enough to fit a T, mark it used, and return the address of that location.
+
+ +
+

+      var arr = new Array[Int](50)
+    
+ allocates $50 * 4 = 200$ bytes of memory (Java Int is 4 bytes). +
+ +

If arr is at address $a$, where should you look for arr(19)? arr(55)?

+
+ +
+

Array[T] : Seq[T]

+ +

An Array of $n$ items of type T:

+ + + +
+ +
+

Array[T] : Seq[T]

+ +

How do we implement...

+
+
apply(idx: Int): [A]
+
Get the element (of type A) at position idx.
+ +
length: Int
+
Count the number of elements in the seq.
+ +
insert(idx: Int, elem: A): Unit
+
Insert an element at position idx with value elem.
+ +
remove(idx: Int): A
+
Remove the element at position idx and return the removed value.
+
+
+ +
+

Insert and remove don't make sense on arrays...

+

Idea Reserve extra space in the array!

+
+ +
+

ArrayBuffer[T] : Buffer[T]

+ +

An ArrayBuffer of $n$ items of type T:

+ + + +
+ +
+

... to be continued

+
\ No newline at end of file diff --git a/src/teaching/cse-250/2022fa/slide/graphics/04b/class-bounds.svg b/src/teaching/cse-250/2022fa/slide/graphics/04b/class-bounds.svg index cd696ec8..7d9efa6f 100644 --- a/src/teaching/cse-250/2022fa/slide/graphics/04b/class-bounds.svg +++ b/src/teaching/cse-250/2022fa/slide/graphics/04b/class-bounds.svg @@ -2,9 +2,9 @@ + transform="translate(-19.088647,-26.515478)"> + + f(n) + chighg(n) + clowg(n) + n0 + diff --git a/src/teaching/cse-250/2022fa/slide/graphics/07b/adt_opaque_box.svg b/src/teaching/cse-250/2022fa/slide/graphics/07b/adt_opaque_box.svg new file mode 100644 index 00000000..4a7af677 --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/graphics/07b/adt_opaque_box.svg @@ -0,0 +1,175 @@ + + + + + + + + + + + + + + + + + + Read 'n'th item + Read Everything + Update 'n'th item + + + + + Data are in an opaque box(no specified layout) + + + + Access goverened by semantics (what)not implementation (how) + + + + diff --git a/src/teaching/cse-250/2022fa/slide/graphics/07b/array.svg b/src/teaching/cse-250/2022fa/slide/graphics/07b/array.svg new file mode 100644 index 00000000..5fdf6e72 --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/graphics/07b/array.svg @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + n + |T| + a[0] + a[1] + a[2] + a[3] + a[4] + ... + + + diff --git a/src/teaching/cse-250/2022fa/slide/graphics/07b/arraybuffer.svg b/src/teaching/cse-250/2022fa/slide/graphics/07b/arraybuffer.svg new file mode 100644 index 00000000..b6ae6eb6 --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/graphics/07b/arraybuffer.svg @@ -0,0 +1,253 @@ + + + + + + + + + + + + + + + + n + |T| + a[0]orNone + ... + + u + a[1]orNone + a[2]orNone + a[3]orNone + a[4]orNone + + + diff --git a/src/teaching/cse-250/2022fa/slide/graphics/07b/ram.svg b/src/teaching/cse-250/2022fa/slide/graphics/07b/ram.svg new file mode 100644 index 00000000..50ea9eee --- /dev/null +++ b/src/teaching/cse-250/2022fa/slide/graphics/07b/ram.svg @@ -0,0 +1,3214 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + RAM + + + 0100100001100101011011000110110001101111... + + + + + 01001000 01100101 01101100 01101100 01101111 ... + + + + + + + + + H + e + l + l + o + + + + + + + + Array + + + fixed size elements + + + + + fixed number of elements + + + + +