140 likes | 393 Views
Asympto t ic Notations & Pseudocode CSC I 3160 tutorial (1 st week) Office: SHB 913 Office hour: M10:00-12:00 Email: jjye@cse.cuhk.edu.hk. Ye Junjie. About the course. CSCI 3160 Design and Analysis of Algorithms Instructor : Prof. CAI Leizhen 蔡雷震 Tutor: Ye Junjie 叶俊杰 (SHB 913)
E N D
Asymptotic Notations & PseudocodeCSCI3160 tutorial (1st week)Office: SHB 913Office hour: M10:00-12:00Email: jjye@cse.cuhk.edu.hk Ye Junjie
About the course CSCI 3160 Design and Analysis of Algorithms Instructor: Prof. CAI Leizhen蔡雷震 Tutor: Ye Junjie叶俊杰 (SHB 913) Office HourM10:00 - 12:00 Course Homepage: http://www.cse.cuhk.edu.hk/csci3160/ Textbook: Algorithms (S.Dasgupta, C.Papadimitriou and U.Vazirani)
Outline Asymptotic Notations Big O natation Other notations: Ω, Θ Pseudocode Examples Sitting Plan Problem
Asymptotic notations: Big O Definition: We say f(n) = O(g(n)) if there exist constants c, n0 such that |f(n)| ≤ c |g(n)| for all n ≥ n0. Examples: n = O(2n), n = O(0.5n), n = O(n2), 10000n = O(0.0000001n2). n2 ≠ O(n), n≠ O(log n).
Asymptotic notations: Big O How to determine? f(n)=O(g(n)) means when , the order of f(n) ≤ g(n) If or constant, then f(n)=O(g(n)) Spectrum of functions… Smaller Larger Exponential functions are faster than polynomial functions Polynomial functions are faster than logarithmic functions log n = O(n) n = O(2n) O(1) … loglog n, … log n, log2 n, … n1/3, n1/2 , n, n2, n3 … 2n, 2n^2 … 22^n …
Asymptotic notations: Ω & Θ Definition: We say f(n) = Ω (g(n)) if there exist constants c, n0 such that |f(n)| ≥ c |g(n)| For all n ≥ n0 We say f(n) = Θ(g(n)) if there exist constants c1, c2, n0 such that c1 |g(n)| ≤ |f(n)| ≤ c2 |g(n)| For all n ≥ n0 Theorem: f(n) = O(g(n)) if and only if g(n) = Ω(f(n)). f(n) =Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). O means “upper bound”, Ω means “lower bound”.
Examples:O, Ω, Θ f(n)= __(g(n))
Use of Pseudocode BEFORE Composing an argument structures organization BEFORE Writing a program Pseudocode Like a flow chart: Aims: To make everything clear and readable Attend lecture If tutorial is useful yes no Attend tutorial Skip tutorial Do homework Pseudocode: TAKE_COURSE(n) 1 attend lecture 2 if tutorial is useful attend tutorial else skip tutorial 3do homework
What is Pseudocode Pseudocode is not a programming language. not C/C++, not Java, not Assembly Language, …… Pseudocode is an description of algorithm. Pseudocode is any idea of a computer program intended for human reading rather than machine reading. In this course, we use description(pseudocode) to present algorithms.
Example 1: Bubble sort • A simple sorting algorithm • Sort a list of numbers from small to large • It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
Example 1: Bubble sort • Pseudocode: • 1 BUBBLE-SORT ( A: list of numbers ) • 2 for i ← length(A) down to 1 • 3 for j ← 1 to i-1 • 4 if A [j] > A [j+1] then • 5 swap A [j] and A[j+1]) • 8 end if • 9 end for • 10 end for Prefer: There are length(A) loops. In ith loop, we swap A[j] and A[j+1] if A[j] > A[j+1] for index j from 1 to length(A)-i.
Sitting Plan Problem Sitting Plan: How to remove students(totally students) so that the minimum distance between two students is at least ? Seats in a line: linear time Seats in a loop?
Example 2 Seats in a Line: linear time Pseudocode: 1 Seat_Line ( S[1] to S[n] denote the places of n students) 2 preseat← S[1], count ← 0 3 for i← 2 to n-1 4 if S[i] – preseat =< d then 5 remove S[i], count++ 6 if count > k then 7 answer NO! 8 end if 9 else 10 preseat ← S[i] 11 end if 12 end for
Thank you! Q&A