thought for the day

Post on 18-Mar-2016

17 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Thought for the Day. “Sense shines with a double luster when it is set in humility. An able and yet humble man is a jewel worth a kingdom.” – William Penn. table. key. key. key. value. value. value. External Hash Table: Iterators. Slightly more complicated: need to work through array - PowerPoint PPT Presentation

TRANSCRIPT

“Sense shines with a double luster when it is set in humility. An able and yet humble man is a jewel worth a kingdom.”– William Penn

Thought for the Day

External Hash Table: Iterators

• Slightly more complicated:– need to work through array– and work through linked

lists

table ...

keyvalue

keyvalue

keyvalue

The HashTableIterator Classprivate class HashTableIterator ... { private int index; private EntryNode<K, V> nextEntry; public HashTableIterator () { for (index = 0; index < table.length; index++) if (table[index] != null) break; // First non-empty bucket if (index < table.length) // Have data nextEntry = table[index]; } // constructor public Pair<K, V> get () { return nextEntry; } // get ...} // class HashTableIterator

public void next () { nextEntry = nextEntry.next; if (nextEntry == null) // Look for next non-empty bucket while (++index < table.length) { if (table[index] != null) // Found more data { nextEntry = table[index]; break; } } } // next

public boolean atEnd () { return index >= table.length; } // atEnd

Uses of the Hash Table Classes

• SSame interface (Dictionary) as the ListDictionary class

• Similar applications– more efficient– But: unordered

Dictionary ADT Summary

ADT Advantages DisadvantagesList-Dictionary

OrderedFlexible size

Slow access

Internal-HashTable

Fast access UnorderedFixed size

External-HashTable

Fast accessFlexible size

Unordered

OOSection 3Section 3

Algorithm AnalysisAlgorithm Analysis

Big-O

Chapter 8: Big-O

• Objectives– Introduce algorithm analysis

– Study methods for measuring algorithm efficiency and thus comparing algorithms

– Consider some common results

– Show some simple applications of the techniques of algorithm analysis

Analysing Algorithms

• Many algorithms may appear simple and efficient

• This may not be true in fact!

Example

• Solving simultaneous equations

• Cramer’s Rule

• Calculate the determinant

• For n equations, it takes n! operations

Example (cont.)

• If n = 30 (a very small set of equations)

n! = 30! = 3 × 1032

• Computers are very fast• Assume 109 operations per second (1GHz)

Time taken = 1016 years

Longer than the estimated life of the universe!

Example (cont.)

• Another approach is needed!

• The tri-diagonal method

• Needs about 10n operations

If n = 30, time taken = 10-7 seconds

Implications

• Need to choose algorithms very carefully

• This example focussed on time– other resources (memory, disk space, etc.) may

also be important

Algorithmic Complexity

• Not how difficult it is to understand!

• How it behaves with respect to time (or other resources)

• Example: we say that Cramer’s Rule has a complexity of n!

Algorithmic Complexity

• Need to measure complexity in a way that is independent of external factors– compiler optimisations, speed of computers,

etc.

• Find some important operation(s)– Often comparisons, or data exchanges– Count them

Use to Compare Algorithms

• Algorithm 1– 20 comparisons– 30 exchanges

• Algorithm 2– 100 comparisons– 150 exchanges

• On Apple II

(1MHz)– 30s

• On Pentium 4 (3GHz)– 3s

The Impact of the Input Data

• Vitally important

• Must compare “apples with apples”• But we don’t want to get into specific

details!• Use some abstract measure of data• Example:

– Cramer’s Rule: n equations

Input Data (cont.)

• Often the number of data items

• But not always!

• Example: text searching algorithms– length of search string x length of document

Input Data (cont.)

• Often three cases we need to consider:– average case– best case– worst case

Big-O Notation

• Or order notation

• Assume we can find a function giving the number of operations:

f(n) = 3.5n2 + 120n + 45

Dominant term

Order n2

or more simply:

O(n2)

top related