Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute...

18
Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 [email protected]

Transcript of Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute...

Page 1: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Data StructuresGiri Narasimhan

Office: ECS 254A Phone: x-3748 [email protected]

Page 2: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Hello! u  What are we here for?

q  COP 3530 q  It is a required course, but a pivotal one for BS-CS

u  What is this course about? q  Data Structures & Algorithm Analysis •  Teaches you to plan before you act •  Helps prior to coding to design the algorithm in a disciplined,

systematic manner •  Helps analyze and compare algorithms before implementation •  Helps implement the most efficient programs •  Simple to increasingly complex data structures & algorithms

COP 3530: DATA STRUCTURES 8/22/16

Page 3: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

General Information u  Course Website: https://users.cs.fiu.edu/~giri/teach/

3530Fall2016.html

u  Moodle Site: Soon!

u  See Course website for q  Syllabus q  course objectives and learning outcomes q  prerequisites and co-requisites q  Required text q  All policies, rules and regulations, including •  Assignment Submission Policy •  Cheating Policy •  attendance standards

8/22/16 COP 3530: DATA STRUCTURES

Page 4: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Evaluation u  Programming Assignments 40%

u  In-class quizzes 15%

u  Exams 40%

u  Class Participation 5%

8/22/16 COP 3530: DATA STRUCTURES

Page 5: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode: Prelude to Code u  Structured, indented code

u  Free format

u  Skip formal syntax, declarations, and other details

8/22/16 COP 3530: DATA STRUCTURES

Page 6: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 1: Find kth largest among N numbers

Select-Kth-Largest (k, A) Sort(A) in decreasing order Report A[k]

u 3 considerations: Correctness? Time? Memory?

8/22/16 COP 3530: DATA STRUCTURES

Page 7: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 2: Report all Prime numbers between 1 and N

ReportAllPrimes (N)

For j = 2 to N do For k = 2 to j-1 do

If (k is a factor of j) then Report j is not a prime and process next j

Report j is a prime

u 3 considerations: Correctness? Time? Memory?

8/22/16 COP 3530: DATA STRUCTURES

u  A prime number has only two divisors: 1 and itself

Page 8: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 3: Max Contiguous Subsequence Sum

MaxSubseqSum(A)

For every possible subsequence of A

compute Sum of subsequence

keep track of highest subsequence sum

8/22/16 COP 3530: DATA STRUCTURES

MaxSubseqSum(A)

Initialize maxSum to 0

N := size(A)

For i = 1 to N do

For j = i to N do

Initialize thisSum to 0

for k = i to j do

add A[k] to thisSum

if (thisSum > maxSum) then

update maxSum

u  3 considerations: Correctness? Time? Memory?

Page 9: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Time Complexity Analysis u  Pseudocode is enough for prelim time & memory analysis

u  Time Complexity Analysis can: q  Give you rough estimate of time q  Give you a sense of growth of time complexity with input

size (Asymptotics) q  Help you to compare two or more algorithms in a machine-

independent manner

u  Time Complexity Analysis cannot: q  Inform you about correctness or memory usage q  Account for machine-dependent, PL-dependent, and

programmer-dependent differences

8/22/16 COP 3530: DATA STRUCTURES

Page 10: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Why not do Timing experiments

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

0 50 100

Input Size

Tim

e (

ms)

u  Implementations take time

u  Experiments cannot test all inputs

u  Average-Case vs Worst-Case Analysis

u  Results may be machine-dependent

8/22/16 COP 3530: DATA STRUCTURES

Page 11: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Asymptotic Running Time u  To compute asymptotic running time,

q  Consider the worst-case scenario q  Consider the worst-case scenario & count number of steps

as a function of length of input q  Eliminate all terms except the dominant term(s) q  Eliminate constants where possible q  Simplify expression where possible q  What remains is typically the asymptotic running time in

big-Oh notation

8/22/16 COP 3530: DATA STRUCTURES

Page 12: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 1: Find kth largest among N numbers

Select-Kth-Largest (k, A) Sort(A) in decreasing order Report A[k]

u 3 considerations: Correctness? Time? Memory?

u Time ≈ time for sorting

8/22/16 COP 3530: DATA STRUCTURES

Page 13: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 2: Report all Prime numbers between 1 and N

ReportAllPrimes (N)

For j = 2 to N do For k = 2 to j-1 do

If (k is a factor of j) then Report j is not a prime and process next j

Report j is not a prime

u 3 considerations: Correctness? Time? Memory?

u Time ≈ N2

8/22/16 COP 3530: DATA STRUCTURES

u  A prime number has only two divisors: 1 and itself

Page 14: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Pseudocode 3: Max Contiguous Subsequence Sum

MaxSubseqSum(A)

For every possible subsequence of A

compute Sum of subsequence

keep track of highest subsequence sum

8/22/16 COP 3530: DATA STRUCTURES

MaxSubseqSum(A)

Initialize maxSum to 0

For i = 1 to N-1 do

For j = i to N-1 do

Initialize thisSum to 0

for k = i to j do

add A[k] to thisSum

if (thisSum > maxSum) then

update maxSum

u  Time ≈ N3

Page 15: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Some Growth Rates

8/22/16 COP 3530: DATA STRUCTURES

Page 16: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

8/22/16 COP 3530: DATA STRUCTURES

Page 17: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Review your exponents, logs, series

Exponents

u XAXB = XA+B

u XA ÷ XB = XA-B

u  (XA)B = XAB

u  Fine points q  XN + XN = 2XN ≠ X2N q  XN * XN = X2N

q  2N + 2N = 2N+1

Logarithms

u  log xy = y log x

u  log xy = log x + log y

u  log log n = log(log n)

u  logk n = (log n)k

u  logyx= log x – log y

u  logbx = logax / logab

8/22/16 COP 3530: DATA STRUCTURES

Page 18: Data Structuresgiri/teach/3530/f16/Lectures/Lec1-Intro.pdf · Asymptotic Running Time ! To compute asymptotic running time, " Consider the worst-case scenario " Consider the worst-case

Advantages of Asymptotic Analysis & Big-Oh Notation

u  Allows for rough measure of running time

u  Abstracts main features of code without focusing on details of implementation or hardware or language or environment

u  Tells us how time complexity scales with input size

u  Allows for a quick high-level comparison of algorithms

8/22/16 COP 3530: DATA STRUCTURES