21
“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

Thought for the Day

  • Upload
    maire

  • View
    19

  • Download
    1

Embed Size (px)

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

Citation preview

Page 1: 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

Thought for the Day

Page 2: 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

Page 3: Thought for the Day

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

Page 4: Thought for the Day

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

Page 5: Thought for the Day

Uses of the Hash Table Classes

• SSame interface (Dictionary) as the ListDictionary class

• Similar applications– more efficient– But: unordered

Page 6: Thought for the Day

Dictionary ADT Summary

ADT Advantages DisadvantagesList-Dictionary

OrderedFlexible size

Slow access

Internal-HashTable

Fast access UnorderedFixed size

External-HashTable

Fast accessFlexible size

Unordered

Page 7: Thought for the Day

OOSection 3Section 3

Algorithm AnalysisAlgorithm Analysis

Big-O

Page 8: Thought for the Day

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

Page 9: Thought for the Day

Analysing Algorithms

• Many algorithms may appear simple and efficient

• This may not be true in fact!

Page 10: Thought for the Day

Example

• Solving simultaneous equations

• Cramer’s Rule

• Calculate the determinant

• For n equations, it takes n! operations

Page 11: Thought for the Day

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!

Page 12: Thought for the Day

Example (cont.)

• Another approach is needed!

• The tri-diagonal method

• Needs about 10n operations

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

Page 13: Thought for the Day

Implications

• Need to choose algorithms very carefully

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

also be important

Page 14: Thought for the Day

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!

Page 15: Thought for the Day

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

Page 16: Thought for the Day

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

Page 17: Thought for the Day

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

Page 18: Thought for the Day

Input Data (cont.)

• Often the number of data items

• But not always!

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

Page 19: Thought for the Day

Input Data (cont.)

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

Page 20: Thought for the Day

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)

Page 21: Thought for the Day