60 likes | 168 Views
CS208 Coursework 1. Neil Ghani Mark Dukes Handin: Friday 25 February, 12pm, Departmental Office. Big O Notation. Question 1: If f and f’ re functions, define (f+f’) to be the function sending x to fx + f’x, ie (f + f’) x = f x + f’x
E N D
CS208 Coursework 1 Neil Ghani Mark Dukes Handin: Friday 25 February, 12pm, Departmental Office
Big O Notation • Question 1: If f and f’ re functions, define (f+f’) to be the function sending x to fx + f’x, ie (f + f’) x = f x + f’x Show that if f is O(g) and f’ is (g’), then f + f’ is O(g + g’)
More Big-O notation • Question 2: Show that the big O-notaiton is transitive. That is, show that if f is O(g) and g is O(h) then f is O(h)
Reverse1 • Question 3. What is the complexity of the reverse algorithm on lists defined by rev1 [] = [] rev1 (x:xs) = rev1 xs + [x] where [] is the empty list, (x:xs) is a list with first element x and the rest of the list is xs, [x] is a list containing only the element x and + joins two lists togther. You may assume + has linear complexity, ie is O(n)
Reverse2 • Question 4: What is the complexity of the following reverse algorithm rev2 [] = [] rev2 [x] = [x] rev2 xs = rev2 (back xs) + rev2 (front xs) where front xs is the first half of the list xs and back xs is the second half of the list xs. You may assume + is linear as are front and back
Reverse3 • Question 5: What is the complexity of the following reverse algorithm rev3 xs = rev3’ xs [] rev3’ [] ys = ys rev3’ (x:xs) ys = rev3’ xs (x:ys) As primitive operations, count the number of uses of the operation : which attaches an element to the front of a list.