CS350
Lecture Notes - 1

Happy Birthday!

Birthday_Paradox.png

Are there two people in this room with the same birthday?

How might we go about answering this question?

  for each person in the room:
    find each's birthday on the calendar
      if the calendar day is marked
        return that day
      else
        mark that day
  return false
  for each person in the room:
    write each's birthday on the whiteboard
    for other people in the room:
      if other's birthday is on the whiteboard
        return birthday
  return false

Which of these algorithms is better?

Are there other algorithms that solve this problem?

Complexity:

Efficient procedures for solving large scale problems

Scalability

Why study algorithms?


Administrative Details

On Syllabus:
David Lu
Office: TBA (Feng Liu's old office 120-09)
email: dlu@pdx.edu
Course webpage: https://davidjlu.github.io/CS350/
Will use D2L as a grade book, so you can access and keep track of your grades through the quarter.
TA: Neil Babson
Office Hours: Tues/Thurs 2-3PM in fishbowl
email: nbabson@pdx.edu

Go over syllabus

Look at schedule.
Schedule is tentative.

Winter weather policy!

Schedule Philosophy and Textbook

Textbook is grouped by design strategy

Group by problem type - CLRS (Algorithms Bible)

Levitin is supposed to be much more readable than CLRS

Schedule and Assessments:

Homeworks
* 4 problem sets
* 2 will feature implementing some algorithms and testing
Midterm
Final - Date?


Intro to Algorithms

"An algorithm is a sequence of unambiguous instructions for solving a problem, i.e. for obtaining a required output for any legitimate input in a finite amount of time." (Levitin 1.1)

Another Example

Find Duplicates
Input: a length n array, A, of integers.
Output: true if the array contains a duplicate and false otherwise

  for i <- 1 ... n
    for j <- 1 ... n
      if i !=j and A[i] = A[j] then
        return true
  return false

How do we know whether this algorithm correctly solves the problem for any legitimate input?

Asymptotic Analysis

Idea: Count basic operations -- the operation contributing most to the running time

Analysis - Find Duplicates
C(n)=i=1nj=1n2C(n) = \sum_{i=1}^{n} \sum_{j=1}^{n} 2

(why 2?)

=2i=1nj=1n1= 2 \sum_{i=1}^{n} \sum_{j=1}^{n} 1
=2i=1nn= 2 \sum_{i=1}^{n} n
=2n2= 2n^2

Units for measuring running time
Let copc_{op} be the execution time of an algorithm's basic operation, and let C(n)C(n) be the number of times this operation needs to be executed for the algorithm. Then we can estimate the running time T(n)T(n) of a program implementing this algorithm by the formula

T(n)copC(n)T(n) \approx c_{op}C(n)

Less Stupid Find Duplicates
Input: a length n array, A, of integers.
Output: true if the array contains a duplicate and false otherwise

  for i <- 1...n-1
    for j <- i+1...n
      if A[i] = A[j] then
        return true

Is this a correct algorithm to solve the problem?

Analysis - Less Stupid Find Duplicates
C(n)=i=1n1j=i+1n1C(n) = \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} 1
=i=1n1ni= \sum_{i=1}^{n-1} n-i
=i=1n1i= \sum_{i=1}^{n-1} i
=n(n1)2= \frac{n(n-1)}{2}
=12(n2n)= \frac{1}{2}(n^2-n)
12n2\approx \frac{1}{2}n^2

What happens if we double the input size?

T(2n)T(n)=copC(2n)copC(n)\frac{T(2n)}{T(n)} = \frac{c_{op}C(2n)}{c_{op}C(n)}
=12(2n)212n2= \frac{\frac{1}{2}(2n)^2}{\frac{1}{2}n^2}
=4n2n2= \frac{4n^2}{n^2}
=4= 4

About 4 times longer!

Orders of growth

Orders of growth
Also factorial
Exponential and above is HARD!
Growth


For Wed read sections 2.1 and 2.2.
I will prepare HW1 and place it in the assignments tab on the course website.