Algorithms
and
Data
Structures

Spring 2021
course
site
-->

more on O() - Thu Feb 25

Questions about anything?

SEPC reps ...

... start recording.

Today I'd like to work through some of the ideas that we started to talk about on Monday.

math review

But first I do want to review a bit of math.

What is a "log" and how do they work?

log2(128) = ?
log2(64) = ?
log2(128 * 64) = ?

Here's the basic idea:

log2(16) = 4      means      2**4 = 16

In other words, what power of 2 gives 16?

Exponents add when the numbers multiply : (2**4) * (2**8) == 2**(4+8)

That means that logs behave this way : log2(64) + log2(128) = log2(64 * 128)

An example of a log scale is decibels for volume, or frequency for music : the increases are by constant multiplicative factors, not constant additive factors.

When log(n) goes up by 1,2,3,4, then n goes up by a constant factor i.e. 2,4,8,16.

Here it is in code.

# how many times does this loop run ?

def by_halfs(n):
    loops = 0
    while n > 1:
        loops += 1
        n = n // 2
    return loops

by_halfs(128)
by_halfs(64)

Compare that with from math import log2; (log2(128), log2(64)

How about these loops?

n = 100
loopy = 0
while n > 1:
    n -= 1
    loopy += 1

n = 100
loopy = 0
while n > 1:
    n -= 1
    for m in range(100):
        loopy += 1

So ... those were O(log n), O(n), O(n**2) . The specifics will depend on what else we do in the loops, but as n gets really big, the O() tells us how many times slower the algorithm will run.

For even really big numbers, the log of that number is pretty small. So if we can find algorithms that work by reducing the size of the problem by a constant factor, then we win.

Computer speed is in the GHz range, that's \( 10^ \) = 10**9 = a billion operations per second. So we can handle a million things or operations easily; but we can't go much beyond 10**9 .

n            log2(n)
10            3
100           7   
1000         10
10000        13
100000       17
1000000      20    a million
1000000000   30    a billion

In other words, we can get from 1 up to a billion with 30 doublings.

wordsearch

We'll take one specific example and write some code together to see what's what, looking at three different algorithms :

We'll start with the attached python notebook, and work together to write down and plot all three of these.

I've uploaded three files after class: the short whiteboard thing on logs, and the expanded wordsearch files.

more math review

Other math to review and/or read about ... but we didn't get to this.

https://cs.bennington.college /courses /spring2021 /algorithms /notes /2b_analysis
last modified Thu February 25 2021 5:36 pm

attachments [paper clip]

  last modified size
TXT whiteboard_logs.pdf Thu Feb 25 2021 05:36 pm 102K
TXT wordsearch.html Wed Feb 24 2021 03:03 pm 640K
TXT wordsearch.ipynb Wed Feb 24 2021 03:03 pm 60K
TXT wordsearch_after_class.html Thu Feb 25 2021 05:36 pm 742K
TXT wordsearch_after_class.ipynb Thu Feb 25 2021 05:36 pm 141K