Asymptotics II, Search Trees (Discussion 7) (Kỹ thuật phân tích thời gian chạy, cây tìm kiếm nhị phân) - Christine Zhou

UN
Uni24h
0 lượt tải
27
0

Nội dung tài liệu

Trang 2

Announcements ● Homework 2 due today, March 6! ● Homework 3 will be released on Thursday morning ○ Due Monday, March 11 ● Midterm 1 Regrade Requests due by this Friday at 11:59pm! ● SIgn up to meet with me if you’re interested! ● Resources: tinyurl.com/cs61b-christine-zhou ● Survey: tinyurl.com/cz-disc7-sp19

Trang 3

Notation: Big O, Big Omega, Big Theta ● Goal: Look at program complexity for large input ● Notations: ○ Big O - upper bound ○ Big Omega - lower bound ○ Big Theta - upper and lower bound

Trang 4

O (Big O) ● Let f(n) and g(n) be positive real numbers on inputs of size n ● f ∈ O(g) if there is a constant c > 0 s.t. f(n) <= c g(n) ● Upper bounded by g(n) when n gets significantly large. ● Bound does not have to be tight. ● True or false? ○ N2 ∈ O(N2) ○ N2 ∈ O(N500) ○ N log N ∈ O(N) ○ log N ∈ O(N2)

Trang 5

Ω (Big Omega) ● Let f(n) and g(n) be positive real numbers on inputs of size n ● f ∈ Ω(g) if there is a constant c > 0 s.t. f(n) >= c g(n) ● Lower bounded by g(n) when n gets significantly large. ● Bound does not have to be tight. ● True or false? ○ N2 ∈ Ω(N2) ○ N ∈ Ω(1) ○ N2 ∈ Ω(N3)

Trang 6

Θ (Big Theta) ● Let f(n) and g(n) be positive real numbers on inputs of size n ● f ∈ Θ(g) if there is a constant c1 > 0 and c2 > 0 s.t. ○ C1 g(n) <= f(n) <= c2 g(n) for all c1 <= c2 ● Tightly bounded by g(n) when n gets significantly large. ● • f ∈ Ω(g) and f ∈ O(g)

Trang 8

Conventions: No Constants ● Drop multiplicative constants and lower order terms ○ If our runtime is actually 2N^2 + N, we say the runtime is Theta(N^2) ● Any exponential dominates any polynomial ● Any polynomial dominates any logarithm

Trang 9

Common Asymptotic Sets ● O(1): constant ● O(log n): logarithmic ● O(sqrt(n)): square root ● O(n): linear ● O(n log n): linearithmic ● O(n^2): quadratic ● O(n^3): cubic ● O(2^n): exponential ● O(n!): factorial

Trang 10

Techniques for Analyzing Runtime ● Annotate your code ○ Write down runtimes of called functions ● Consider end conditions for loops and consequently the number of times we could run the loop ○ What are the terminating conditions for a loop? ○ Can we algebraically express the runtime of the loop? ● Branching factor ○ Do we make additional calls and if so, how many subproblems do we make?

Trang 18

Binary Search Tree ● Every key in the left subtree is less than X’s key ● Every key in the right subtree is greater than X’s key dog bag flat alf cat elf glut Binary Search Tree

Trang 19

BST Runtimes Shape of Tree Height add search “Bushy” logN Θ(logN) Θ(logN) “Spindly” N Best: Θ(1) Best: Θ(1) Worst: Θ(N) Worst: Θ(N)

Trang 20

2) Is This a BST? The following method isBSTBad is supposed to check if a given binary tree is a BST, though for some binary trees, it is returning the wrong answer. Think about an example of a binary tree for which isBSTBad fails. Let’s do this one together!

Hiển thị 20 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!