1 / 34

CS322

Week 11 - Friday. CS322. 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

hakan
Download Presentation

CS322

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 11 - Friday CS322

  2. Last time • What did we talk about last time? • Graph isomorphism • Trees

  3. Questions?

  4. 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?

  5. Finishing Trees

  6. 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: • <sentence>  <noun phrase><verb phrase> • <noun phrase>  <article><noun> | <article><adjective><noun> • <verb phrase>  <verb><noun phrase> • <article>  a | an | the • <adjective>  funky • <noun>  DJ | beat • <verb>  plays | spins • Make a parse tree corresponding to the sentence, "The DJ plays a funky beat"

  7. 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)

  8. A property of trees • For any positive integer n, a tree with n vertices must have n – 1 edges • Prove this by mathematical induction • Hint: Any tree with 2 or more nodes has a vertex of degree 1. What happens when you remove that vertex?

  9. 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

  10. 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? 0 3 4 1 2 5 6 7 9 8

  11. 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

  12. 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

  13. 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

  14. 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!

  15. 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

  16. Graphing Functions

  17. 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)

  18. 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

  19. 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

  20. 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 2 1 -6 -5 -4 -3 -2 -1 -1 -2 1 2 3 4 5 6

  21. 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 2 1 -6 -5 -4 -3 -2 -1 -1 -2 1 2 3 4 5 6

  22. Increasing and decreasing functions • We say that f is decreasing on the set Siff 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 Siff 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 ifff 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

  23. Asymptotic Notation (Big Oh) Student Lecture

  24. Asymptotic Notations

  25. 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

  26. 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) isO(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

  27. 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)

  28. Properties of -, O-, and - notation • Let f and g be real-valued functions defined on the same set of nonnegative real numbers • f(x) is (g(x)) and f(x) is O(g(x)) ifff(x) is (g(x)) • f(x) is (g(x)) iffg(x) is O(f(x)) • f(x) is (f(x)), f(x) is O(f(x)), and f(x) is (f(x)) • If f(x) is O(g(x)) and g(x) is O(h(x)) then f(x) is O(h(x)) • If f(x) is O(g(x)) and c is a positive real, then cf(x) is O(g(x)) • 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 x • 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))

  29. 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)

  30. 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

  31. 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)

  32. Upcoming

  33. Next time… • More on asymptotic notation

  34. Reminders • Keep reading Chapter 11 • Keep working on Assignment 9 • Due next Friday

More Related