{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

cs1001.py , Tel Aviv University, Fall 2017-2018

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Recitation 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We talked about comlexity. First we had an intuitive intro through an exercise and then we formally defined the notion of $O$ and solved some theoretical exercises.\n", "\n", "#### Takeaways:\n", "\n", "
    \n", "
  1. The time complexity bound implies how that the running time changes as a function of the input size.
  2. \n", "
  3. Two solutions that have the same time complexity bound may differ in their running time (one can be more efficient than the other).
  4. \n", "
  5. The Big O notation \"hides\" additive and multiplicative constants
  6. \n", "
  7. Two solutions that have the same time complexity bound may differ in their running time (one can be more efficient than the other).
  8. \n", "
  9. To analyze nested loops that are dependent, use a sum ($\\sum$), with the boundaries of the external loop as the limits and the number of iterations for the inner loop in the sum itself.
  10. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise: Given an natural number $p$, how many right triangles exist with integer sized sides whose perimeter is $p$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model:\n", "\n", "How many triplets (a,b,c) are there such that:\n", "\n", "
    \n", "
  1. $a^2 + b^2 = c^2$
  2. \n", "
  3. $a + b + c = p$
  4. \n", "
  5. $a,b,c > 0$ are all integers
  6. \n", "
\n", "\n", "### In order to avoid counting a triplet twice or more, we require:\n", "\n", "
    \n", "
  1. $0 < a < b < c$
  2. \n", "
\n", "\n", "### Note:\n", "\n", "
    \n", "
  1. $a \\neq b$ and $b \\neq c$
  2. \n", "
  3. The condition $b < c$ is redundant (since we require that $a,b,c > 0$ and $a^2 + b^2 = c^2$)
  4. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When computing code complexity, we\n", "
    \n", "
  1. define the entire content of the innermost loop as an “atomic operation” which takes constant time.
  2. \n", "
  3. describe the complexity as a function of $p$ (i.e., use $p$ as the “problem size” or “input size”). \n", " Note, however, that the formal definition of “problem size” for a numeric input is the size it takes to represent the input, i.e. $n = \\log(p)$. \n", " Working with $p$ here is easier and more intuitive.
  4. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Trivial solution (v1):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def number_of_integer_right_triangles_v1(p):\n", " cnt = 0\n", " for a in range(1,p):\n", " for b in range(1,p):\n", " for c in range(1,p):\n", " if a + b + c == p and a < b and a*a + b*b == c*c:\n", " cnt += 1\n", " return cnt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analysis:\n", "\n", "$(p-1)^3$ iterations.\n", "\n", "The complexity is $O(p^3)$ (cubic complexity)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Second version (v2):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we set $a,b$ and $p$, $c$ is already defined!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def number_of_integer_right_triangles_v2(p):\n", " cnt = 0\n", " for a in range(1,p):\n", " for b in range(1,p):\n", " c = p - a - b\n", " if a < b and a*a + b*b == c*c:\n", " cnt += 1\n", " return cnt\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analysis:\n", "\n", "$(p-1)^2$ iterations.\n", "\n", "The complexity is $O(p^2)$ (squared complexity)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Third version (v3):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We require $an_0$ \n", " $|f(n)| \\leq c\\cdot|g(n)|$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Time complexity hierarchy\n", " \n", "Let $n$ denote the size of the input.\n", "The most common time complexities we encounter are :\n", "\n", "* Constant - $O(1)$\n", "* Logarithmic - $O(\\log(n))$\n", "* Root/fractional - $O(n^{1/c})$ for $c>1$\n", "* Linear - $O(n)$\n", "* Loglinear - $O(n \\log(n))$\n", "* Polynomial - $O(n^{c})$ for $c>1$\n", "* Exponential - $O(c^{n})$ for $c>1$\n", "\n", "See also this list on [Wikipedia](http://en.wikipedia.org/wiki/Time_complexity#Table_of_common_time_complexities)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prove or Disprove examples\n", "See some examples [here](http://tau-cs1001-py.wdfiles.com/local--files/recitation-logs-2017a/complexity_prove_or_disprove.pdf)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.4" } }, "nbformat": 4, "nbformat_minor": 1 }