Ye junjie
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Ye Junjie PowerPoint PPT Presentation


  • 184 Views
  • Uploaded on
  • Presentation posted in: General

Asympto t ic Notations & Pseudocode CSC3160 tutorial (1 st week) Office: SHB 913 Office hour: M10:00-12:00 Email: [email protected] Ye Junjie. About the course . CSCI 3160 Design and Analysis of Algorithms Instructor : Prof. CAI Leizhen 蔡雷震 Tutor: Ye Junjie 叶俊杰 (SHB 913)

Download Presentation

Ye Junjie

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


Ye junjie

Asymptotic Notations & PseudocodeCSC3160 tutorial (1st week)Office: SHB 913Office hour: M10:00-12:00Email: [email protected]

Ye Junjie


About the course

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

Outline

Asymptotic Notations

Big O natation

Other notations: Ω, Θ

Pseudocode

Examples

Sitting Plan Problem


Asymptotic notations big o

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 o1

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

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

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

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

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

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 sort1

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

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


Ye junjie

Thank you!

Q&A


  • Login