# Ye Junjie - PowerPoint PPT Presentation

1 / 14

Asympto t ic Notations & Pseudocode CSC3160 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)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Ye Junjie

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

Asymptotic Notations & PseudocodeCSC3160 tutorial (1st week)Office: SHB 913Office hour: M10:00-12:00Email: jjye@cse.cuhk.edu.hk

Ye Junjie

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

(a) f(n) = n-100 ; g(n) = n-200

(b) f(n) = n1/2 ; g(n) = n2/3

(c) f(n) =100n + log n ; g(n) = n + (log n)2

(d) f(n) = n1.01 ; g(n) = n log2n

### 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 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

### Sitting Plan Problem

Sitting Plan: How to remove k students(totally n students) so that the minimum distance between two students is at least d?

Seats in a Line: linear time

Seats in a loop: hints O(n^2) O(dn) ?

General model: vertex cover

### 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

8 end if

9 else

10 preseat ← S[i]

11 end if

12 end for

Thank you!

Q&A