CS322Week 11 - Friday
Last time
What did we talk about last time? Graph isomorphism Trees
Questions?
Logical warmup
You are the despotic ruler of an ancient empire Tomorrow is the 25th anniversary of your reign You have 1,000 bottles of wine you were planning to open for the
celebration Your Grand Vizier uncovered a plot to murder you:
10 years ago, a rebel worked in the vineyard that makes your wine He poisoned one of the 1,000 bottles you are going to serve tonight and
has been waiting for revenge The rebel died an accidental death, and his papers revealed his plan, but
not which bottle was poisoned The poison exhibits no symptoms until death Death occurs within ten to twenty hours after consuming even the
tiniest amount of poison You have over a thousand slaves at your disposal and just under 24
hours to determine which single bottle is poisoned You have a few hundred prisoners, sentenced to death Can you use just the prisoners and risk no slaves? If so, what's the smallest number of prisoners you can risk and still
be sure to find the bottle?
Finishing Trees
Parse trees
A grammar for a formal language (such as we will discuss next week or the week after) is made up of rules that allow non-terminals to be turned into other non-terminals or terminals
For example:1. <sentence> <noun phrase><verb phrase>2. <noun phrase> <article><noun> |
<article><adjective><noun>3. <verb phrase> <verb><noun phrase>4. <article> a | an | the5. <adjective> funky6. <noun> DJ | beat7. <verb> plays | spins
Make a parse tree corresponding to the sentence, "The DJ plays a funky beat"
Describing trees
Any tree with more than one vertex has at least one vertex of degree 1
If a vertex in a tree has degree 1 it is called a terminal vertex (or leaf)
All vertices of degree greater than 1 in a tree are called internal vertices (or branch vertices)
A property of trees
For any positive integer n, a tree with n vertices must have n – 1 edges
Prove this by mathematical inductionHint: Any tree with 2 or more nodes
has a vertex of degree 1. What happens when you remove that vertex?
Rooted trees
In a rooted tree, one vertex is distinguished and called the root
The level of a vertex is the number of edges along the unique path between it and the root
The height of a rooted tree is the maximum level of any vertex of the tree
The children of any vertex v in a rooted tree are all those nodes that are adjacent to v and one level further away from the root than v
Two distinct vertices that are children of the same parent are called siblings
If w is a child of v, then v is the parent of w If v is on the unique path from w to the root, then v is
an ancestor of w and w is a descendant of v
Rooted tree example
Consider the following tree, rooted at 0
What is the level of 5? What is the level of 0? What is the height of this tree? What are the children of 3? What is the parent of 2? What are the siblings of 8? What are the descendants of 3?
3
0
5
21
7 9
6
4
8
Binary trees
A binary tree is a rooted tree in which every parent has at most two children
Each child is designated either the left child or the right child
In a full binary tree, each parent has exactly two children
Given a parent v in a binary tree, the left subtree of v is the binary tree whose root is the left child of v
Ditto for right subtree
Binary tree applications
As we all know from data structures, a binary tree can be used to make a data structure that is efficient for insertions, deletions, and searches
But, it doesn't stop there! We can represent binary arithmetic with a
binary tree Make a binary tree for the expression ((a –
b)∙c) + (d/e) The root of each subtree is an operator Each subtree is either a single operand or another
expression
Full Binary Tree Theorem 1
If k is a positive integer and T is a full binary tree with k internal vertices, then T has a total of 2k + 1 vertices and has k + 1 terminal vertices
Prove it!Hint: Induction isn't needed. We
just need to relate the number of non-terminal nodes to the number of terminal nodes
Full Binary Tree Theorem 2
If T is a perfect binary tree with height h, then it has 2h+1 – 1 vertices
Prove it using induction!
Generalizing that result
If T is a binary tree with t terminal vertices and height h, then t 2h
Prove it using strong induction on the height of the tree
Hint: Consider cases where the root of the tree has 1 child and 2 children separately
Graphing Functions
Graphing functions
I am assuming that you have been graphing functions since about 6th or 7th grade
Still, the formal definition of a graph of a function is: Let f be a real-valued function of a real
variable The graph of f is the set of all points (x,
y) in the Cartesian coordinate plane such that x is in the domain of f and y = f(x)
Power functions
In computer science, we are often interested in power functions of x written pa(x) where pa(x) = xa where x and a are nonnegative
Power functions are the building blocks of polynomial functions
Graph the following on the same coordinate axes p0
p1/2
p1
p2
Discontinuous functions
Recall the definition of the floor of x: x = the largest integer that is less than
or equal to x Graph f(x) = x Defining functions on integers
instead of real values affects their graphs a great deal
Graph p1(x) = x, x R Graph f(n) = n, n N
Multiples of functions
There is a strong visual (and of course mathematical) correlation a function that is the multiple of another function
Examples: g(x) = x + 2 2g(x) = 2x + 4
Given f graphed below, sketch 2f
- 6 - 5 - 4 - 3 - 2 - 1 1 2 3 4 5 6
21
-1-2
Absolute value
Consider the absolute value function f(x) = |x|
Left of the origin it is constantly decreasing
Right of the origin it is constantly increasing
- 6 - 5 - 4 - 3 - 2 - 1
1 2 3 4 5 6
21
-1-2
Increasing and decreasing functions
We say that f is decreasing on the set S iff for all real numbers x1 and x2 in S, if x1 < x2, then f(x1) > f(x2)
We say that f is increasing on the set S iff for all real numbers x1 and x2 in S, if x1 < x2, then f(x1) < f(x2)
We say that f is an increasing (or decreasing) function iff f is increasing (or decreasing) on its entire domain
Clearly, a positive multiple of an increasing function is increasing
Virtually all running time functions are increasing functions
Asymptotic Notation (Big Oh)Student Lecture
Asymptotic Notations
Growth of functions
Mathematicians worry about the growth of various functions
They usually express such things in terms of limits, maybe with derivatives
We are focused primarily on functions that bound running time spent and memory consumed
We just need a rough guide We want to know the order of the
growth
Definitions
Let f and g be real-valued functions defined on the same set of nonnegative real numbers
f is of order at least g, written f(x) is (g(x)), iff there is a positive A R and a nonnegative a R such that A|g(x)| ≤ |f(x)| for all x > a
f is of order at most g, written f(x) is O(g(x)), iff there is a positive B R and a nonnegative b R such that |f(x)| ≤ B|g(x)| for all x > b
f is of order g, written f(x) is (g(x)), iff there are positive A, B R and a nonnegative k R such that A|g(x)| ≤ |f(x)| ≤ B|g(x)| for all x > k
Using the notation
Express the following statements using appropriate notation: 10|x6| ≤ |17x6 – 45x3 + 2x + 8| ≤ 30|x6|, for x > 2
Justify the following:
is (x)
0,1
)92(1515
xx
xxx
6,151
)92(15
xxx
xx
1)92(15
xxx
Properties of -, O-, and - notation
Let f and g be real-valued functions defined on the same set of nonnegative real numbers
1. f(x) is (g(x)) and f(x) is O(g(x)) iff f(x) is (g(x))2. f(x) is (g(x)) iff g(x) is O(f(x))3. f(x) is (f(x)), f(x) is O(f(x)), and f(x) is (f(x))4. If f(x) is O(g(x)) and g(x) is O(h(x)) then f(x) is
O(h(x))5. If f(x) is O(g(x)) and c is a positive real, then cf(x)
is O(g(x))6. If f(x) is O(h(x)) and g(x) is O(k(x)) then f(x) + g(x)
is O(G(x)) where G(x) = max(|h(x)|,|k(x)|) for all x7. If f(x) is O(h(x)) and g(x) is O(k(x)) then f(x)g(x) is
O(h(x)k(x))
Orders of functions
If 1 < x, then x < x2
x2 < x3
… So, for r, s R, where r < s and x >
1, xr < xs
By extension, xr is O(xs)
Proving bounds
Prove a bound for g(x) = (1/4)(x – 1)(x + 1) for x R
Prove that x2 is not O(x) Hint: Proof by contradiction
Polynomials
Let f(x) be a polynomial with degree n f(x) = anxn + an-1xn-1 + an-2xn-2 … + a1x + a0
By extension from the previous results, if an is a positive real, then f(x) is O(xs) for all integers s n f(x) is (xr) for all integers r ≤ n f(x) is (xn)
Furthermore, let g(x) be a polynomial with degree m g(x) = bmxm + bm-1xm-1 + bm-2xm-2 … + b1x + b0
If an and bm are positive reals, then f(x)/g(x) is O(xc) for real numbers c > n - m f(x)/g(x) is not O(xc) for all integers c > n - m f(x)/g(x) is (xn - m)
Upcoming
Next time…
More on asymptotic notation
Reminders
Keep reading Chapter 11 Keep working on Assignment 9
Due next Friday