MATH 1P67: Mathematics for Computer Science
Winter 2012
Assignment 1: Algorithms and big O
Due: Wednesday, January 25th
For this assignment, only a subset of the problems will be graded. All problems
Introduction
Algorithms
The Growth of Combinations of Functions
notation: (f + g )(x) = f (x) + g (x) and (fg )(x) = f (x)g (x)
Instructor: Babak Farzad
MATH 1P67 Mathematics for Computer Science
Intr
Example
Using structural induction, prove that if T is a full binary tree then
n(T ) 2h(T ) + 1.
47 / 118
m-ary trees
A binary tree (very common!) allows each node to have at most
two children.
48 / 1
The Growth of Functions - Lets get formal!
Instructor: Babak Farzad
MATH 1P67 Mathematics for Computer Science
The Growth of Functions - Lets get formal!
Since we focus on the runtime of the algorithm
Introduction
Algorithms
The Linear Search Algorithm
The Linear Search Algorithm.
procedure linear search(x : real number, a1 , . . . , an :
distinct real numbers)
i := 1
While (i n and x = ai )
i := i
Introduction
Algorithms
Complexity of Algorithms
Review
F G means that F O(G ) but G O(F ), and F G means
/
that F O(G ) and G O(F ). Place the following functions in
order using one of the symbols an
Introduction
Algorithms
Eciency
How does one measure algorithms?
Instructor: Babak Farzad
MATH 1P67 Mathematics for Computer Science
Introduction
Algorithms
Eciency
How does one measure algorithms?
We
Introduction
Algorithms
MATH 1P67
Mathematics for Computer Science
Instructor: Babak Farzad
Brock University
Winter 2012
Instructor: Babak Farzad
MATH 1P67 Mathematics for Computer Science
Introductio
Write the rst letter of your last name in the above box.
Assignment #
MATH 1P67
Winter 2012
Last name:
First name:
Student number:
I declare that I have read and understood Brocks regulations concerni
Full Binary Trees
For simplicity, we often work with full binary tree: each vertex has
either two children or no children.
37 / 118
Full Binary Trees
Full binary trees (and their roots) can be dened r
Recursion
1 / 118
Recursion
Recursion: a process of repeating objects in a self-similar way.
2 / 118
Recursively Dened Functions - Example
The sequence of powers of 2: an = 2n for n = 0, 1, 2, . . .
3
Quick Sort
idea:
Pick an element in the array to serve as a pivot.
Split the array into two parts: one with elements larger than
the pivot and the other with elements smaller than the pivot.
Recursive
Second-order Recurrences
A second-order recurrence is a relation of this form:
8
d1 if n = k
<
d2 if n = k + 1
T (n) =
:
c1 T (n 1) + c2 T (n 2) + f (n) if n > k + 1
If f (n) = 0 then the recurrence i
Recursive denitions of sets
A set can also be dened recursively.
Example: lets dene a set of numbers S by
Base: 3 S
Recursion: If x S and y S, then x + y S.
14 / 118
Recursive denitions of sets
A set
Recursive Algorithms
A recursive algorithm solves a problem by reducing it to an
instance of the same problem with smaller input.
Give a recursive algorithm for computing n!, when n is a positive
inte
Merge Sort
Idea
split the list into two halves
recursively sort each half
merge the two sorted sublists
mergesort(a[1, . . . , n])
Input: An array of numbers a[1 . . . n]
Output: A sorted version of t
Recursive denition of expressions
Example: all the legal numerical expressions for some programming
language. (These are all intended to be strings of characters.)
Any digit or lower-case letter is a
Well-dened function
Show that these recursively dened functions on the set of positive
integers are not well-dened.
f (n) = 1 + f (b(n + 1)/2c) for n
1 and f (1) = 1
60 / 118
Well-dened function
Show