130 likes | 141 Views
This article covers various topics in CS 201, including Java programming, mathematical analysis, and empirical analysis. It also includes practice problems and discussions on data structures and algorithms.
E N D
Compsci 201,Mathematical & Emprical Analysis Owen Astrachan Jeff Forbes September 29, 2017 Compsci 201, Fall 2017, Analysis
Jis for … • Java • A simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. • Jobs • Go to Eddy Cue’s talk and hopefully he’ll tell the Steve Jobs’ pen throwing story (3:30 in Penn Pavilion) • Just in Time Teaching • Questions prior to class shape active lecture Compsci 201, Fall 2017, Analysis
Stuff You Should Know • Which approach/algorithm/data structure is better? • It depends… • What is 210? • 1024 ≈ 1,000 • What is 1 + 2 + 3 + … + N? CompSci 201, Fall 2017, Analysis
Some helpful mathematics • 1 + 2 + 3 + 4 + … + N • N(N+1)/2, exactly = N2/2 + N/2 which is O(N2) why? • N + N + N + …. + N (total of N times) • N*N = N2which is O(N2) • N + N + N + …. + N + … + N + … + N (total of 3N times) • 3N*N = 3N2 which is O(N2) • 1 + 2 + 4 + … + 2N • 2N+1 – 1 = 2 x 2N – 1 which is O(2N ) • Impact of last statement on adding 2N+1 elements to a vector • 1 + 2 + … + 2N + 2N+1 = 2N+2-1 = 4x2N-1 which is O(2N) resizing + copy = total (let x = 2N)
Array vs. ArrayList • Run the code ArrayVsArrayList.java
Amortization: Expanding ArrayLists • Expand capacity of list when add() called • Calling addN times, doubling capacity as needed • Big-Oh of adding n elements? • What if we grow size by one each time?
IsomorphicWords • Consider code from the solution to IsomophicWords: • What is the input size? What does the runtime depend on? • What’s the big-Oh for the run-time? inttotal = 0; for(intj=0; j < words.length; j++) { for(intk=j+1; k < words.length; k++) { if(isomorphic(words[j],words[k])) { total+= 1; } } } returntotal; CompSci 201, Fall 2017, Analysis
Analyzing Code • Consider this method from Practice Test #3: • What is the smallest value of x such that calc(x) returns 2047? • What is the runtime complexity of calc(n)? calc(100) 127 calc(256)511 calc(511)511 calc(512)1024 publicintcalc(intn) { intsum = 0; intval = 1; while (val <= n) { sum += val; val *= 2; } returnsum; } calc(31)? calc(32)? calc(33)? calc(2n)? http://bit.ly/201-f17-0929-1 CompSci 201, Fall 2017, Analysis
Iterating through Maps Find the key in a Map whose associated value is the largest? • How to iterate through a map? • By key using Map.keySet() • By entry using Map.entrySet() • How to find the maximum value? • Map.values() & Collections.max() • How do I read and solve the problem? • What’s the big-Oh of our solution? CompSci 201, Fall 2017, Analysis
Reasoning about growth • Consider a 3-tower • How tall is a 5-tower? • How tall is a 10 tower? • How many blocks in a 5-tower? • Which best captures the height of an n-tower? http://bit.ly/201-f17-0927-2 CompSci 201, Fall 2017, Analysis
Analysis: Empirical vs. Mathematical • Empirical analysis. • Measure running times, plot, and fit curve. • Easy to perform experiments. • Model useful for predicting, but not for explaining. • Mathematical analysis. • Analyze algorithm to estimate # ops as a function of input size. • May require advanced mathematics. • Model useful for predicting and explaining. • Critical difference. Mathematical analysis is independent of a particular machine or compiler; applies to machines not yet built. • Why does algorithm complexity matter?
Test 1 Topics • Problem Solving • Tradeoffs • Java • Java syntax, conventions, control structures, and methods • Primitive types & objects • Reading from files • Decomposition, classes, interfaces, and inheritance • Comparing and hashing • Data Structures • Arrays ∓ Lists • Sets • Maps • Mathematical and Empirical Analysis • Coursework • Classwork &Discussion • Assignments: N-Body and Markov • APTs Remember to put your name on any of your six (6) double-sided letter-sized sheets Compsci 201, Fall 2017, Analysis