foundations ii: data structures and...

39
CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor : Yusu Wang Topic 1 : Introduction and Asymptotic notation

Upload: others

Post on 08-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Foundations II: Data Structures and Algorithms

Instructor : Yusu WangTopic 1 : Introduction and

Asymptotic notation

Page 2: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Course webpagehttp://www.cse.ohio-state.edu/~yusu/courses/2331CANVAS: https://carmen.osu.edu

Office hours Tue: 11:30 am – 12:30 pm; Thu: 3:45pm – 5:00pm

Grading policy: homework: 20%, two midterms: 40%, final: 40%

CSE 2331 / 5331

Course Information

Page 3: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Exams

Midterm 1 Oct. 7th : 7 – 8:45pm, Location: KN250

Midterm 2 Nov. 4th : 7 – 8:45pm, Location: KN250

Final exam: Dec. 6th (Friday), 6:00pm--7:45pm, Room DL113

CSE 2331 / 5331

Page 4: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Exams

Midterm 1 Oct. 7th : 7 – 8:45pm, Location: KN250

Midterm 2 Nov. 4th : 7 – 8:45pm, Location: KN250

Final exam: Dec. 6th (Friday), 6:00pm--7:45pm, Room DL113

CSE 2331 / 5331

Mark you calendar.Contact me by Aug. 28 if you have schedule conflict

for any exam.

Page 5: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Notes

All homework should be submitted before or in class on the due date. Late homework during the same day receives a 10% penalty. Late homework submitted the next day receives a 30% penalty.

You may discuss homework with your classmates. It is very important that you write up your solutions individually, and acknowledge any collaboration / discussion with others.

CSE 2331 / 5331

Page 6: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

More Information

Textbook Introduction to Algorithms

by Cormen, Leiserson, Rivest and Stein(third edition)

Others CSE 2331 course notes

The Art of Computer ProgrammingBy Donald Knuth

Page 7: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Introduction

What is an algorithm? Step by step strategy to solve problems Should terminate Should return correct answer for any input instance

Page 8: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

This Course

Various issues involved in designing good algorithms What do we mean by “good”?

Algorithm analysis, language used to measure performance

How to design good algorithms Data structures, algorithm paradigms

Fundamental problems Graph traversal, shortest path etc.

CSE 2331 / 5331

Page 9: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Asymptotic Notation

CSE 2331 / 5331

Page 10: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Pseudo-code

Function Test(A, n)for i = 1 to 2n-1 do

key = A[i+1]for j = 1 to i do

A[j+1] = A[j]key = A[j+1]*2

A[i] = keyreturn key

Page 11: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Running Time

Depends on input size Depends on particular input

Worst case T(n) = max time of algorithm on any input of size n

Page 12: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Pseudo-code

Function Test(A, n)for i = 1 to 2n-1 do

key = A[i+1]for j = 1 to i do

A[j+1] = A[j]key = A[j+1]*2

A[i] = keyreturn key

c 1

c 2

T(n) = Σ ( + i ) = + n + i=2

nc 2c 1 c 3 c 5n 2 c 4

Page 13: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Asymptotic Complexity

Why shall we care about constants and lower order terms?

Should focus on the relative growth w.r.t. n Especially when n becomes large ! E.g: 0.2n1.5 v.s 500n

Page 14: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Illustration

CSE 2331 / 5331

𝑓𝑓 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛

Page 15: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Big O Notation

f(n) ∈ O(g(n)) if and only if ∃ c > 0, and n0 , so that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0

f(n) = O(g(n)) means f(n) ∈ O(g(n)) (i.e, at most) We say that g(n) is an asymptotic upper bound for f(n).

It also means:

E.g, f(n) = O(n2) if there exists c, n0 > 0 such that f(n) ≤ cn2, for all n ≥ n0.

lim ≤ cn→∞

f(n)g(n)

Page 16: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

More Examples

CSE 2331 / 5331

5𝑛𝑛2 + 6𝑛𝑛 + 8 = 𝑂𝑂 𝑛𝑛3 ? 100𝑛𝑛2 − 1000 𝑛𝑛 = 𝑂𝑂(𝑛𝑛2) ? 6𝑛𝑛3 + 7𝑛𝑛2 + 3𝑛𝑛 = 𝑂𝑂(𝑛𝑛1.5) ? 2𝑛𝑛 = 𝑂𝑂 𝑛𝑛2 ? 3lg𝑛𝑛 = 𝑂𝑂(𝑛𝑛) ? 3𝑛𝑛 = 𝑂𝑂 2𝑛𝑛 ? 3𝑛𝑛 = 𝑂𝑂 22𝑛𝑛

log3 𝑛𝑛 = 𝑂𝑂 log2 𝑛𝑛 ?

𝑛𝑛lg 𝑛𝑛

= 𝑂𝑂(𝑛𝑛)

Page 17: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Prove that 6𝑛𝑛3 + 7𝑛𝑛2 + 3𝑛𝑛 = 𝑂𝑂(𝑛𝑛1.5) Note that

6𝑛𝑛3 + 7𝑛𝑛2 + 3𝑛𝑛 ≤ 6𝑛𝑛3 + 7𝑛𝑛3 + 3𝑛𝑛3 ≤ 16𝑛𝑛3 = 4𝑛𝑛1.5

By definition, it then follows that 6𝑛𝑛3 + 7𝑛𝑛2 + 3𝑛𝑛 =𝑂𝑂(𝑛𝑛1.5)

CSE 2331 / 5331

Page 18: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Omega Ω Notation

f(n) ∈ Ω (g(n)) if and only if ∃ c > 0, and n0 , so that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0

f(n) = Ω (g(n)) means f(n) ∈ Ω (g(n)) (i.e, at least) We say g(n) is an asymptotic lower bound of f(n).

It also means: lim ≥ cn→∞

f(n)g(n)

Page 19: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Illustration

CSE 2331 / 5331

𝑓𝑓 𝑛𝑛 = Ω 𝑔𝑔 𝑛𝑛

Page 20: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

More Examples

CSE 2331 / 5331

5𝑛𝑛2 + 6𝑛𝑛 + 8 = Ω 𝑛𝑛3 ? 5𝑛𝑛2 + 6𝑛𝑛 + 8 = Ω 𝑛𝑛2 ? 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 = Ω(𝑛𝑛1.5) ? 2𝑛𝑛 = Ω 𝑛𝑛2 ? 3lg𝑛𝑛 = Ω(𝑛𝑛) ? 3𝑛𝑛 = Ω 2𝑛𝑛 ? log3 𝑛𝑛 = Ω log2 𝑛𝑛 ? 2log3 𝑛𝑛 = Ω(2log2 𝑛𝑛)

Page 21: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Theta Θ Notation

Combine lower and upper bound Means tight: of the same order 𝑓𝑓 𝑛𝑛 ∈ Θ 𝑔𝑔 𝑛𝑛 if and only if ∃ 𝑐𝑐1, 𝑐𝑐2 > 0, and 𝑛𝑛0,

such that 𝑐𝑐1𝑔𝑔 𝑛𝑛 ≤ 𝑓𝑓 𝑛𝑛 ≤ 𝑐𝑐2𝑔𝑔 𝑛𝑛 for any 𝑛𝑛 ≥ 𝑛𝑛0 𝑓𝑓 𝑛𝑛 = 𝛩𝛩 𝑔𝑔 𝑛𝑛 means 𝑓𝑓 𝑛𝑛 ∈ 𝛩𝛩 𝑔𝑔 𝑛𝑛

We say g(n) is an asymptotically tight bound for f(n). It also means:

𝑐𝑐1 ≤ lim𝑛𝑛→∞

𝑓𝑓(𝑛𝑛)𝑔𝑔(𝑛𝑛)

≤ 𝑐𝑐2

Page 22: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Prove that 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 = Θ(𝑛𝑛1.5) First note that:

6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 ≤ 6𝑛𝑛3 + 3𝑛𝑛3 ≤ 9𝑛𝑛3 = 3𝑛𝑛1.5

⇒ 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 = 𝑂𝑂(𝑛𝑛1.5)

Second note that 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 ≥ 6𝑛𝑛3 − 7𝑛𝑛2

For 𝑛𝑛 ≥ 7, 6𝑛𝑛3 − 7𝑛𝑛2 ≥ 5𝑛𝑛3 = 5 𝑛𝑛1.5

It then follows that 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 = Ω(𝑛𝑛1.5)

Putting the upper and lower bound together, we thus have 6𝑛𝑛3 − 7𝑛𝑛2 + 3𝑛𝑛 = Θ(𝑛𝑛1.5)

CSE 2331 / 5331

Page 23: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

𝑓𝑓 𝑛𝑛 = 𝑂𝑂(𝑔𝑔 𝑛𝑛 ) 𝑓𝑓 𝑛𝑛 = Ω(𝑔𝑔 𝑛𝑛 ) 𝑓𝑓 𝑛𝑛 = Θ(𝑔𝑔 𝑛𝑛 )

Page 24: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Remarks -- 1

𝑓𝑓 𝑛𝑛 = Θ(𝑔𝑔 𝑛𝑛 ) if and only if 𝑓𝑓 𝑛𝑛 = 𝑂𝑂(𝑔𝑔 𝑛𝑛 ), and 𝑓𝑓 𝑛𝑛 = Ω 𝑔𝑔 𝑛𝑛 . Insertion sort algorithm as described earlier has time complexity Θ(𝑛𝑛2).

Transpose symmetry: If 𝑓𝑓 𝑛𝑛 = 𝑂𝑂(𝑔𝑔 𝑛𝑛 ), if and only if 𝑔𝑔 𝑛𝑛 = Ω 𝑓𝑓 𝑛𝑛

Transitivity: If 𝑓𝑓 𝑛𝑛 = 𝑂𝑂(𝑔𝑔 𝑛𝑛 ) and 𝑔𝑔 𝑛𝑛 = 𝑂𝑂(ℎ 𝑛𝑛 ), then 𝑓𝑓 𝑛𝑛 = 𝑂𝑂 ℎ 𝑛𝑛 . Same for Ω and Θ

CSE 2331 / 5331

Page 25: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Remarks -- 2

It is convenient to think that Big-O is smaller than or equal to Big-Ω is larger than or equal to Big-Θ is equal

However, These relations are modulo constant factor scaling Not every pair of functions have such relations

CSE 2331 / 5331

If 𝑓𝑓 𝑛𝑛 ∉ 𝑂𝑂(𝑔𝑔 𝑛𝑛 ), this does not imply that 𝑓𝑓 𝑛𝑛 =Ω(𝑔𝑔 𝑛𝑛 ).

Page 26: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Remarks -- 3

Provide a unified language to measure the performance of algorithms Give us intuitive idea how fast we shall expect the alg. Can now compare various algorithms for the same problem

Constants hidden! O( n lg lg n ), 2n lg n

T(n) = + O (n)means T(n) = + h(n), and h(n) = O(n)

n2

n2

Page 27: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Pitfalls

O(n) + O(n) = O(n)

T(n) = n + Σ O(n) = n + O(n)

i = 1

k

OK

?

k should not depend on n ! It can be an arbitrarily large constant …

Page 28: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Yet Another Notation

Small o notation: f(n) = o(g(n)) means for any 𝑐𝑐 > 0,∃𝑛𝑛0 s.t. for all 𝑛𝑛 ≥ 𝑛𝑛0, 0 ≤ 𝑓𝑓 𝑛𝑛 < 𝑐𝑐𝑔𝑔 𝑛𝑛 . In other words,

lim = 0n→∞

f(n)g(n)

Examples: Is n - lg n = o(n) ? Is n = o (n lg n) ?

Page 29: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Yet Another Notation

Small ω notation: f(n) = ω(g(n)) means for any 𝑐𝑐 > 0,∃𝑛𝑛0 s.t. for all 𝑛𝑛 ≥𝑛𝑛0, 0 ≤ 𝑐𝑐𝑔𝑔 𝑛𝑛 < 𝑓𝑓 𝑛𝑛 . In other words,

lim = ∞n→∞

f(n)g(n)

Examples: Is n - lg n = ω(n) ? Is n = ω(n / lg n) ?

lim = 0n→∞

g(n)f(n)

or

Page 30: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Summary Asymptotic Notation

CSE 2331 / 5331

Page 31: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Another View

CSE 2331 / 5331

Page 32: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Some Special Cases

CSE 2331 / 5331

Page 33: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Example

Prove that 3 ln𝑛𝑛 = 𝑂𝑂(𝑛𝑛) This is because lim

𝑛𝑛→∞3 ln 𝑛𝑛𝑛𝑛

= 0

CSE 2331 / 5331

Page 34: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Review some basics Chapter 3.2 of CLRS See the board

CSE 2331 / 5331

Page 35: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

More Examples

𝑛𝑛3 vs. 1.0001 𝑛𝑛

𝑛𝑛3 vs. 1.0001𝑛𝑛2

𝑛𝑛𝑎𝑎 vs. 𝑏𝑏𝑛𝑛

ln(𝑛𝑛4) vs. log3 2𝑛𝑛 2lg𝑛𝑛 vs. 𝑛𝑛 lg𝑛𝑛 vs. 𝑛𝑛𝜖𝜖

𝑛𝑛 lg𝑛𝑛 vs. 𝑛𝑛 lg𝑛𝑛 𝑛𝑛 vs. log2 3𝑛𝑛

𝑛𝑛 vs. 2log2 𝑛𝑛+4

𝑛𝑛 log2 𝑛𝑛 vs. 22 log2 𝑛𝑛

CSE 2331 / 5331

Page 36: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

More Examples

lg𝑛𝑛2 vs. (lg𝑛𝑛)2 − 100 lg𝑛𝑛 1010 vs. ln𝑛𝑛 3log2 𝑛𝑛 vs. 0.5𝑛𝑛

lg 2𝑛𝑛2 + 𝑛𝑛 lg𝑛𝑛 vs lg (n3 − 100n)

𝑛𝑛 − 𝑛𝑛 lg𝑛𝑛 vs. 2𝑛𝑛2 + 𝑛𝑛 lg𝑛𝑛

CSE 2331 / 5331

In general, we can safely ignore low order terms.

Page 37: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

Hierarchy

CSE 2331 / 5331

Page 38: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

More Example

Rank the following functions by order of growth: 2𝑛𝑛−4, 𝑛𝑛 + 3 lg(3𝑛𝑛2 − 4𝑛𝑛) ,𝑛𝑛 𝑛𝑛 + 5, 𝑛𝑛 ln𝑛𝑛 , 3 ⋅ 2𝑛𝑛,

4𝑛𝑛lg 𝑛𝑛

, 𝑛𝑛 + lg𝑛𝑛

CSE 2331 / 5331

Page 39: Foundations II: Data Structures and Algorithmsweb.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec1.pdf · CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Instructor

CSE 2331 / 5331

Summary

Algorithms are useful

Example: sorting problem Insertion sort

Asymptotic complexity Focus on the growth of complexity w.r.t input size