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 / 118
m-ary trees
A binary tree (very common!) allows each
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 + 1
If i n then location := i
else location := 0
(loca
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 and between each pair of the
functions.
9n ,
log2 (n),
n3
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 can time how long it takes a computer.
Instructor: Bab
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
Introduction
Algorithms
The Growth of Combinations of Funct
Introduction
Algorithms
MATH 1P67
Mathematics for Computer Science
Instructor: Babak Farzad
Brock University
Winter 2012
Instructor: Babak Farzad
MATH 1P67 Mathematics for Computer Science
Introduction
Algorithms
Sakai
The course is oered on Sakai.
You ca
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 concerning
academic integrity and that the work submitted herew
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 are weighted equally.
1. Describe an algorithm in pseud
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 recursively as
follows:
A single vertex is a full binary
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 / 118
Recursively Dened Functions - Example
The sequen
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.
Recursively repeat the algorithm for both halves of the original
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 is homogeneous.
Example: Fibonacci numbers fn = fn
1
+ f
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 can also be dened recursively.
Example: lets dene 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
integer.
Review:
Basis Step: F(0) = 1
Inductive Step: F(n)
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 this array
if n > 1 then
merge(mergesort(a[1 . . . bn/2c
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 numerical expression.
If X and Y are numerical expressi
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 that these recursively dened functions on the set of po
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 algorithms based on the
input size, we consider functions from t