lecture 10: class review dr john levine 52236 algorithms and complexity march 13th 2006

19
Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Upload: winfred-malone

Post on 31-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lecture 10: Class Review

Dr John Levine

52236 Algorithms and ComplexityMarch 13th 2006

Page 2: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Today’s Lecture

• More notes on Practical 1: some example graphs

• Syllabus review for this class

• Topics covered by this class and which topics are examinable

• Topics remaining to be covered: Abstract data types, hash tables, graph algorithms, NP-hard problems

• Abstract data types and an example

• Hash tables (from Mark Dunlop’s slides)

Page 3: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

More Notes on Practical 1

public class Test7 {

public static void main (String[] args) {

int sum = 0; int n = (int)Double.parseDouble(args[0]);

long startTime = System.currentTimeMillis();

for (int i=0; i<n; i++) for (int j=0; j<i*i; j++) if (j%i == 0) for (int k=0; k<j; k++) sum++;

long endTime = System.currentTimeMillis(); }}

Page 4: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

How to do it

• Find a value of n that gives a small, but non-zero time (e.g. around 50-100 milliseconds)

• Find a value of n that gives a fairly large time (depends how long you can bear to wait – about 5-10 seconds)

• Sample values of n between these two values

• Try fitting various trend lines: linear, polynomial, etc.

• Look for a high R2 value showing goodness of fit

• If you analysis says (for example) O(n3), try plotting your time values against n3 and look for a straight line

Page 5: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Learning OutcomesOn completion of the class, a student should be able:

• to implement a number of fundamental algorithms, including in particular the fundamental algorithms of searching and sorting

• to make a critical assessment of different implementations of algorithms and abstract data types

• to carry out a number of empirical studies of the performance of algorithms and abstract data types

• to appreciate a number of fundamental computational problems, and be aware of real world instances of those problems

Page 6: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Syllabus: Algorithmic Complexity

Introduction to Algorithmic Complexity: basic algorithmicclassification, with examples; the order notation (Big-oh);elementary complexity and estimation of run times; thetyranny of growth.

• Covered in specifically in Lectures 1 and 2 and Practical 1, also covered throughout the course

• Examinable

• Typical exam question: say what Big-oh is, give an analysis of some code fragments

Page 7: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Syllabus: Searching and SortingSearching and Sorting: the complexity of a range oftechniques, including the divide and conquer approach;the relative complexity of searching and sortingalgorithms; the sorting algorithms covered will includebubble sort, insertion sort, merge sort and quick sort;searching, including sequential search and the binarychop; hashing.

• Partially covered by Lectures 3 and 4

• We’ll do a quick recap of the four sorting algorithms

• We’ll look at hash tables today

• Examinable

Page 8: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Syllabus: Binary TreesBinary Trees revisited: implementations by array;expression trees; binary tree implementation of sortedlist; access times; algorithms covered include traversal,searching, balancing and deletion.

• Already covered in Programming Techniques

• We did state space search and game trees instead

• Game trees, minimax search and alpha-beta pruning are examinable, the rest is not (because last year’s students didn’t do it)

• Typical exam question: map out a simple game tree and say what move the computer should make next

Page 9: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Syllabus: Graph AlgorithmsGraphs revisited: directed and undirected graphs;representations of graphs; basic graph algorithms;applications of graphs to real world problems (forexample telecommunications, transportation systems,dependencies between objects).

• To be covered next week

• Representation of graphs, Dijkstra’s algorithm

• Application to the tube map problem

• Examinable

• Typical exam question: show application of Dijkstra’s algorithm to a real world problem

Page 10: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Syllabus: NP-Hard Problems

Permutations and Combinations: branch and bound;greedy algorithms; backtracking search; typicalproblems, for example the TSP and related problems,the knapsack problem.

• Mostly covered by our consideration of state space search and games, and also in Topics 1 and 2

• I will touch briefly on this area in my summary and revision lecture in Week 10

• Not directly examinable – see the Week 10 lecture for what you need to know in this area

Page 11: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Abstract Data Types

• Definition: a set of data values and operations on those data values which are precisely specified and independent of any particular implementation

• Example: a stack makeNewStack() returns an empty stack push(item,stack) puts item onto the top of stack top(stack) returns the top element of stack pop(stack) returns the top item and removes it

• The external user of the ADT only sees the interface, not the internal workings (representation, algorithms)

Page 12: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Example ADT: Lookup Table

• I want to create a lookup table to hold the names of my students (keys) and their birthdays (values)

• Operations: add(key,value) adds a new key-value pair lookup(key) returns the value associated with key delete(key) deletes both key and its value inOrder() show all key-value pairs in order of keys

• Example: add(“John”, 2nd December) adds John’s birthday lookup(“Fred”) returns Fred’s birthday delete(“Eric”) deletes Eric’s birthday from the table

Page 13: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: Implementation

• I can implement my lookup table in various ways: which of them is best?

• Linked list: add new items on to the front of the list, search through the list to perform lookup and deletion, sort the list to show the table in order

• Binary search tree: add new items to the tree, search the tree to perform lookup and deletion, traverse the tree to show the table in order

• Hash table: use a ‘magic function’ to map the key onto an integer and store the value at a location in memory directly computable from that integer

Page 14: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: ComplexityExercise: what is the complexity of the lookup tableoperations for the three implementations given?

You can assume the hash function is O(1).

Linked List Binary tree Hash table

add(key,value)

lookup(key)

delete(key)

inOrder()

Page 15: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: ComplexityExercise: what is the complexity of the lookup tableoperations for the three implementations given?

You can assume the hash function is O(1).

Linked List Binary tree Hash table

add(key,value) O(1)

lookup(key) O(n)

delete(key) O(n)

inOrder() O(n log n)

Page 16: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: ComplexityExercise: what is the complexity of the lookup tableoperations for the three implementations given?

You can assume the hash function is O(1).

Linked List Binary tree Hash table

add(key,value) O(1) O(log n)

lookup(key) O(n) O(log n)

delete(key) O(n) O(log n)

inOrder() O(n log n) O(n)

Page 17: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: ComplexityExercise: what is the complexity of the lookup tableoperations for the three implementations given?

You can assume the hash function is O(1).

Linked List Binary tree Hash table

add(key,value) O(1) O(log n) O(1)

lookup(key) O(n) O(log n) O(1)

delete(key) O(n) O(log n) O(1)

inOrder() O(n log n) O(n)

Page 18: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: ComplexityExercise: what is the complexity of the lookup tableoperations for the three implementations given?

You can assume the hash function is O(1).

Linked List Binary tree Hash table

add(key,value) O(1) O(log n) O(1)

lookup(key) O(n) O(log n) O(1)

delete(key) O(n) O(log n) O(1)

inOrder() O(n log n) O(n) O(k+n log n)

Page 19: Lecture 10: Class Review Dr John Levine 52236 Algorithms and Complexity March 13th 2006

Lookup Table: Evaluation

So which implementation is best?

• Linked list: simple implementation, efficient only for adding items, works even when no ordering can be defined on the keys

• Binary search tree: more involved implementation, reasonably efficient in all operations, can iterate over all keys and find a “nearest match” for a given key

• Hash table: best for adding, lookup and deletion, great for huge data sets, fairly easy to implement, but finding a good hash function can be difficult, iterating over all keys is hard as hash tables don’t preserve ordering