CS112-lecture01
-
Upload
manan-shah -
Category
Documents
-
view
27 -
download
0
Transcript of 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
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
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
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.
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
CS112: Slides for Prof. Steinberg’s lecture 6Lecture 1
RequirementsRequirements• Programming exams (2)
• Done in Hill Center “Cereal” labs• Written exams
• Midterm and Final
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
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
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
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
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
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)}}
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
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
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)
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
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)}}
CS112: Slides for Prof. Steinberg’s lecture 18Lecture 1
1 + 2 + 1 + 2 + …… + n + n
= n * (n + 1) / 2
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);
}}
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
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’
CS112: Slides for Prof. Steinberg’s lecture 22Lecture 1
Asymptotically fasterAsymptotically faster
n0 n’
Yes No
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?
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
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)
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)
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