CS 162 Fall 2003 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS 162 Fall 2003

Description:

How fast can you find a word in a dictionary? ... { cards[topCard ] = new Card(Card.diamond, i); cards[topCard ] = new Card(Card.club, i) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 33
Provided by: timoth90
Category:
Tags: fall

less

Transcript and Presenter's Notes

Title: CS 162 Fall 2003


1
CS 162 - Fall 2003
  • Execution Time Measurement
  • From Chapter 4 of
  • Classic Data Structures in Java

2
Why are dictionaries ordered?
  • Clearly there are different algorithms for the
    same task, and different ways of organizing
    information.
  • How fast can you find a word in a dictionary?
  • How fast could you find the same word in an
    unordered list of words?

3
Characterizing Essential Differences
  • How can we characterize the fact that looking up
    a word in a dictionary is faster than finding it
    on a list?
  • Have two people perform each, and use a
    stopwatch. (but some people are slow)
  • Try to find a better measuring device.

4
Remember Big Oh
  • From last time
  • Big-Oh captures the rate of change of execution
    time, relative to the change in input size.
  • By losing precision, can gain better
    understanding.

5
Big Oh warm up exercisesName the execution time
  • class Card
  • public Color color ()
  • if ((suit diamond) (suit heart))
  • return Color.red
  • else
  • return Color.black

6
Constant Time Functions
  • If every invocation of a function takes the same
    amount of time, regardless of any variation in
    the input, we say it is constant time, or O(1).
  • Don't care if it is 1ms, 10ms, 1/100 ms, still
    O(1).
  • Not O(3), O(400), just O(1).

7
Loops - make a deck of cards
  • Card cards new Card52
  • int topCard 0
  • for (int i 1 i lt 13 i)
  • cardstopCard new Card(Card.diamond, i)
  • cardstopCard new Card(Card.club, i)
  • cardstopCard new Card(Card.space, i)
  • cardstopCard new Card(Card.heart, i)

8
Still constant time
  • Even though its a loop, will always do the same
    actions each time it is invoked
  • Still O(1).

9
More interesting loops
  • public double minimum (double values)
  • int n values.length
  • double minValue values0
  • for (int i 1 i lt n i)
  • if (valuesi lt minValue)
  • minValue valuesi
  • return minValue

10
Many loops are O(n)
  • Here the number of executions depends upon the
    size of the array. Array that is twice as big
    will execute twice as many steps.
  • We say this is O(n).
  • Not O(i), or O(data.length), just O(n).

11
A more interesting loop
  • public boolean isPrime (int n)
  • for (int i 2 i i lt n i)
  • if (0 n i)
  • return false
  • return true

12
How many times does it loop?
  • Stops if we get to the sqrt(n) without finding a
    divisor.
  • So we say that this algorithm is O(sqrt(n))
  • But what happens if you call isPrime(100000) ?

13
Time, Worst, Best and Average
  • We say that isPrime is O(sqrt(n)) WORST CASE.
  • It is O(1) BEST CASE.
  • Usually interested in worst case (people being
    naturally pessimistic). Seldom in best case,
    often in average case, but that is tricky to
    discover.

14
Nested Loops
  • void matprod (double a, double b,
    double c)
  • int n a.length
  • for (int i 0 i lt n i)
  • for (int j 0 j lt n j)
  • cij 0.0
  • for (int k 0 k lt n k)
  • cij aik bkj

15
Independent Nested Loops
  • If the loops are independent, then can just
    multiply the running times of each
  • O(n n n), or O(n3)

16
Another interesting Loop
  • void bubbleSort (double v)
  • int n v.length
  • for (int i n-1 i gt 0 i--)
  • for (int j 0 j lt i j)
  • if (vj gt vj1)
  • double temp vj vj vj1
    vj1 temp

17
Analysis of Bubble Sort
  • Will leave the question of "is this correct" for
    another day. Today we are only asking "how fast
    does it execute"?
  • To find the biggest value takes N-1 steps. To
    find the next biggest takes N-2. To find the next
    biggest takes N-3. So on down to 1.
  • How big is 1 2 ... (N-1)

18
Sum of integers
  • As Gauss discovered in the 3rd grade, the sum of
    the values from 1 to (N-1) is N(N1)/2.
  • This is O(n2).
  • Why not O(n2n) ? More on that next lecture.

19
Other Interesting Loops
  • void insertionSort (double v)
  • int n v.length
  • for (int i 1 i lt n i)
  • double element vi
  • int j i - 1
  • while (j gt 0 element lt vj)
  • vj1 vj // slide old value up
  • j j - 1
  • vj1 element

20
Analysis of Insertion Sort
  • Outer loop is clearly O(n). But what about the
    inner loop
  • Best case is constant (what values show best case
    behavior?)
  • Worst case is O(n) (what values show worst case
    behavior?)
  • So Worst case O(n2), best case O(n).

21
Other Execution Times
  • Remember searching for a word in the dictionary?
  • How about "I'm thinking of a number between 1 and
    100".
  • (Variations - I'm allowed to lie once. I don't
    tell you the upper bound).
  • Dividing in half is a powerful idea

22
BinarySearch
  • int binarySearch (double data, double
    testValue)
  • int low 0 int high data.length
  • while (low lt high)
  • mid (low high) / 2
  • if (datamid lt testValue)
  • low mid 1
  • else
  • high mid
  • return low

23
Analysis of Binary Search
  • Think of n as the range from low to high.
  • How many times can I cut something in half?
  • Maybe easier to go the other way. If we are
    playing guess my number without an upper bound,
    how many steps until you can find an upper bound?

24
Logarithms
  • To a mathematician, a logarithm is the inverse of
    an exponential, or an integral.
  • To a computer scientist, very different
  • log(base n) - the (approximate) number of times I
    can divide by n
  • log(base 2) - the (approximate) number of times I
    can cut something in half.

25
Approximate
  • Its only approximate, because the log is a
    double, and we are interested in the intergral
    part, and might be off by 1, BUT
  • We aren't intersted in the constant factors,
    right?
  • So binary search is an O(log n) algorithm.
  • O(log n) is very fast, almost constant.

26
Adding Function Calls
  • void printPrimes (int n)
  • for (int i 2 i lt n i)
  • if (isPrime(i))
  • System.out.println("value " n
    "prime")
  • else
  • System.out.println("value " n "not
    prime")

27
Analysis of printPrimes
  • We know isPrime is O(sqrt n)
  • We know loop is executing n times,
  • so total execution is O(n sqrt n), written O(n
    sqrt n)
  • Recursive functions are harder

28
Simple Recursive functions
  • void printInt (OutputStream out, int val)
  • if (val lt 10)
  • out.write(digitChar(val))
  • else
  • printInt(val / 10)
  • out.write(digitChar(val10))

29
Recursive Functions
  • Need to determine the actual amount of work
  • For simple recursion, work is
  • (number of recursive calls) (work done on each
    recurive call).
  • Often the latter is constant, so this is
  • O(log10 n) which is just O(log n).

30
More complicated Recursion
  • void Hanoi (int n, char a, char b, char c)
  • if (n gt 0)
  • Hanoi(n-1, a, c, b)
  • System.out.println("move disk from" a "
    to" b)
  • Hanoi(n-1, c, b, a)

31
Use Mathematical Induction
  • Base case is clearly O(1) (no, it's not O(0)!)
  • Work a few cases by hand to form a hypothesis.
    Let T(n) be time to do Hanoi(N)
  • Hanoi(n) requires 2 calls on Hanoi(n-1), so T(N)
    2 T(N-1) constant
  • Simple induction proof to show that T(N) is 2n,
    so Hanoi(N) is O(2n).

32
Questions?
  • Read Chapter 4 of the data structures book.
  • Read chapter 18.
  • Programming Assignment 5 coming soon. (as is Quiz
    2).
Write a Comment
User Comments (0)
About PowerShow.com