Está en la página 1de 27

CS112: Data Structures

Intro to course What is a data structure Asymptotic complexity and big-O


CS112: Slides for Prof. Steinbergs lecture Lecture 1

CS112: Data Structures

Instructor: Prof. Louis Steinberg


office: Hill 401, 445-3581 email: lou@cs.rutgers.edu Office hours: by appointment

TA: Xiafong Mi

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

CS111: Intro to Computer Science

Course will use the computer for communication and instruction as much as possible

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

Class Web Page

http://remus.rutgers.edu/cs112

Policies Syllabus Assignments Lecture notes Recitation notes (sometimes) etc....

You are assumed to know anything posted.


Lecture 1

CS112: Slides for Prof. Steinbergs lecture

Requirements

CS 111 or equivalent
Comfortable writing and debugging programs hundreds of lines long Java Language Arrays, Linked Lists

Determination to work hard and keep upto-date on coursework

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

Requirements

Programming exams (2)


Done in Hill Center Cereal labs

Written exams
Midterm and Final

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

Textbook
Data Structures: An Object-Oriented Approach with Java, 2nd Edition by Sesh Venugopal McGraw-Hill Primis Custom Publishing

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

What is a data structure

A representation scheme that stores


Multiple pieces of data Relationships between pieces of data E.g, Object Array Linked List

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

What to know about a DS


What operations can we do? What do they cost?


Time Memory space

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

How long does it take

Problem: actual time depends on


What computer What language What compiler What programmer What input

We want a measure of time that does not depend on these


Lecture 1

CS112: Slides for Prof. Steinbergs lecture

10

Solutions

Count operations, not time Measure size of input Op count = f(input size) Among inputs of the same size, use worst or average op count Abstract away details of f: O(f)
focus on large inputs

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

11

Example
Input: double array A, int n Output: largest number in first n elements of A
double big for (int i if (big < big = = A(0); = 1; i<n; i++){ A(i)){ A(i)}}

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

12

Choosing the operation


Count should model time of algorithm Most frequent / inner loop Most time consuming Inherent in algorithm, not language

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

13

Size of input

Usually obvious measure Sometimes several equally good


eg, n x n matrix: #rows or # elements choose any but be clear which you chose

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

14

Count = f(size)

n-1 comparisons all inputs of same size same cost summarize n-1 as O(n)

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

15

Another Example
Input: double array A, int n, double target Output: index in first n elements of A s.t. A(i)==target for (int i = 0; i<n && A(i)!= target; i++;){} Note average vs worst cases

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

16

More Examples
for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ sum += A(i, j)}} for (int i = 0; i < m; i++){ for (int j = 0; j < i; j++){ sum += A(i, j)}} for (int i = 0; i < m; i++){ for (int j = i-5; j < i; j++){ sum += A(i, j)}}
CS112: Slides for Prof. Steinbergs lecture Lecture 1

17

1+2++n

= n * (n + 1) / 2
CS112: Slides for Prof. Steinbergs lecture Lecture 1

18

Recursive example
int foo(int i){ if (i == 1){ system.out.println(foo); } else { foo(i-1); foo(i-1); }}

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

19

Big O

Which function is bigger?


f(n) = n + 100 g(n) = 2 * n

80
CS112: Slides for Prof. Steinbergs lecture

300

Lecture 1

20

Asymptotically faster

Function g(n) grows asymptotically faster than f(n) if there is an n0 such that for all n>n0, g(n) > f(n)

CS112: Slides for Prof. Steinbergs lecture

n0

Lecture 1

21

Asymptotically faster

n0 Yes

n No

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

22

Big O

Which function is bigger?


f(n) = 4 * n g(n) = 2 * n

What if one algorithm is run on a machine that is twice as fast as the other?

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

23

Big O

f(n) is O(g(n)) if there is some constant c such that c*g(n) grows asymptotically faster than f(n) 3 * n2 + 7 is O(n2) because 4 * n2 grows asymptotically faster than 3 * n2 + 7

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

24

Big O

Informally when we say f(n) is O(g(n)) we mean g(n) is the simplest function for which this is true
technically, n+4 is O(3*n2 - 9*n + 5) but we prefer to say n+4 is O(n)

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

25

Rules for Big O


k is O(1)
341 is O(1)

f+g = max (O(f), O(g))


n + 1 is max( O(n), O(1)) = O(n)

k * f = O(f)
O(4*n4) = O(n4)

O(nA) < O(nB) if A < B


O(n3) < O(n4)

O(polynomial) is O(highest exponent term)


5 n4 + 44 n2 + 55 n + 12 is O(n4)

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

26

Names for Big O


O(1) is constant O(n) is linear O(n2) is quadratic O(kn) is exponential


O(kn) is bigger than any polynomial

CS112: Slides for Prof. Steinbergs lecture

Lecture 1

27

También podría gustarte