CS112-lecture01

27
CS112: Slides for Prof. Steinberg’s lecture 1 Lecture 1 CS112: Data Structures CS112: Data Structures Intro to course What is a data structure Asymptotic complexity and big-O

Transcript of CS112-lecture01

Page 1: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 1Lecture 1

CS112: Data StructuresCS112: Data Structures

Intro to courseWhat is a data structureAsymptotic complexity and big-O

Page 2: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 2Lecture 1

CS112: Data StructuresCS112: Data Structures• Instructor: Prof. Louis Steinberg

– office: Hill 401, 445-3581– email: [email protected]– Office hours: by appointment

• TA: Xiafong Mi

Page 3: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 3Lecture 1

CS111: Intro to ComputerCS111: Intro to ComputerScienceScience

• Course will use the computer forcommunication and instruction as muchas possible

Page 4: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 4Lecture 1

Class Web PageClass Web Page•• http://http://remusremus..rutgersrutgers..eduedu/cs112/cs112

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

– You are assumed to know anythingposted.

Page 5: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 5Lecture 1

RequirementsRequirements• CS 111 or equivalent

• Comfortable writing and debuggingprograms hundreds of lines long

• Java Language• Arrays, Linked Lists

• Determination to work hard and keep up-to-date on coursework

Page 6: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 6Lecture 1

RequirementsRequirements• Programming exams (2)

• Done in Hill Center “Cereal” labs• Written exams

• Midterm and Final

Page 7: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 7Lecture 1

TextbookTextbookData Structures: An Object-Oriented

Approach with Java, 2nd Edition by Sesh VenugopalMcGraw-Hill Primis Custom Publishing

Page 8: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 8Lecture 1

What is a data structureWhat is a data structure• A representation scheme that stores

– Multiple pieces of data– Relationships between pieces of data

• E.g,– Object– Array– Linked List

Page 9: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 9Lecture 1

What to know about a DSWhat to know about a DS• What operations can we do?• What do they cost?

– Time– Memory space

Page 10: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 10Lecture 1

How long does it takeHow 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 notdepend on these

Page 11: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 11Lecture 1

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

Page 12: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 12Lecture 1

ExampleExampleInput: double array A, int nOutput: largest number in first n elements

of Adouble big = A(0);for (int i = 1; i<n; i++){if (big < A(i)){

big = A(i)}}

Page 13: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 13Lecture 1

Choosing the operationChoosing the operationCount should model time of algorithm• Most frequent / inner loop• Most time consuming• Inherent in algorithm, not language

Page 14: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 14Lecture 1

Size of inputSize 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

Page 15: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 15Lecture 1

Count = f(size)Count = f(size)• n-1 comparisons• all inputs of same size same cost• summarize n-1 as O(n)

Page 16: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 16Lecture 1

Another ExampleAnother Example• Input: double array A, int n, double target• Output: index in first n elements of A s.t.

A(i)==targetfor (int i = 0; i<n && A(i)!= target; i++;){}• Note average vs worst cases

Page 17: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 17Lecture 1

More ExamplesMore Examplesfor (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)}}

Page 18: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 18Lecture 1

1 + 2 + 1 + 2 + …… + n + n

= n * (n + 1) / 2

Page 19: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 19Lecture 1

Recursive exampleRecursive exampleint foo(int i){ if (i == 1){

system.out.println(“foo”); } else {

foo(i-1);foo(i-1);

}}

Page 20: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 20Lecture 1

Big OBig O• Which function is bigger?

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

80 300

Page 21: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 21Lecture 1

Asymptotically fasterAsymptotically faster• Function g(n) grows asymptotically faster

than f(n) ifthere is an n0 such that for all n’>n0,g(n’) > f(n’)

n0 n’

Page 22: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 22Lecture 1

Asymptotically fasterAsymptotically faster

n0 n’

Yes No

Page 23: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 23Lecture 1

Big OBig O• Which function is bigger?

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

• What if one algorithm is run on a machinethat is twice as fast as the other?

Page 24: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 24Lecture 1

Big OBig O• f(n) is O(g(n)) if there is some constant c

such that c*g(n) grows asymptoticallyfaster than f(n)

• 3 * n2 + 7 is O(n2) because 4 * n2 growsasymptotically faster than 3 * n2 + 7

Page 25: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 25Lecture 1

Big OBig O• Informally when we say f(n) is O(g(n)) we

mean g(n) is the simplest function forwhich this is true– technically, n+4 is O(3*n2 - 9*n + 5) but we

prefer to say n+4 is O(n)

Page 26: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 26Lecture 1

Rules for Big ORules 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)

Page 27: CS112-lecture01

CS112: Slides for Prof. Steinberg’s lecture 27Lecture 1

Names for Big ONames 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