Algorithm Analysis (CSC 301 2015F) : EBoards

CSC301.01 2015F, Class 10: An Introduction to Trees


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit

Academic

Peer

Questions

Hash Tables: Semi-Formal Analysis

With a good hash function, set is expected O(1); worst case is O(n), where n is the number of slots in the hash table.

Assumption one: Hash function distributes things appropriately. For any input, there are equal odds that it goes in any of the n slots.

Assumption two: There are no more than n/2 values in the hash table. (Alternately: n values in the hash table, at least 2n slots in the hash table.)

Assumption three: Probed hash table.

First time we look in the hash table: 50% we find an empty slot

Average number of steps

    1*n/2 + 2*n/4 + 3*n/8 + 4*n/16 + 5*n/32 + ...
    Average = ^/n
    1*1/2 + 2/4 + 3/8 + 4/16 + 5/32 + ...

    Sum from k = 1 to infinity of k/2^k

    1/2 + 1/2 + 3/8 + 4/16 + 5/32 + ...
    1 + 3/8 + 4/16 + 5/32 + ...
    1 + 10/16 + 5/32 + ...
    1 + 25/32 + 6/64 + ...
    1 + 56/64 + 7/128
    1 + 119/128 + 8/256

    1 + (2^k-(k+2))/2^k

Never reaches 2, although gets arbitrarily close

The stuff before the ellipses 1 + 56/64 + 7/128...

Hash Functions and Hash Tables: Other Applications

I have a string, big, of length n. I have another string, small, of length k < n. Does small appear as a substring of big?

What's your strategy?

What's a really bad input for your strategy?

Strategy O

Strategy H

With the cool Skiena hash function, what do we know about hash(substring(str, 0, k)) vs hash(substring(str, 1, k+1))

So we can compute the n different hash codes in O(n + k) time = O(n) time

But ... we have to use a hash table. And if we have to create a hash table, We're using a lot of extra space! (Probably O(n) space.)

There's often a time/space tradeoff in your algorithms

Trees: The Basics

A tree is "a graph with no cycles"

A tree is "a collection of values such that with a hierarchial relationship"

Each value may have one or more children (child values) and at most one parent value. The relationship is symmetric relationship: if a is a child of b, then b is a parent of a.

Goal for Monday: Work on terminology and notation; Think about traversal; Think about balancing techniques

Balancing Trees