CPS 100, Spring 2009 5.1 Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Spring 2015 Lecture 5: QuickSort & Selection
Complexity Analysis (Part I)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Week 2 CS 361: Advanced Data Structures and Algorithms
CPS What’s easy, hard, first? l Always do the hard part first. If the hard part is impossible, why waste time on the easy part? Once the hard part.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Algorithm analysis, searching and sorting  best vs. average vs. worst case analysis  big-Oh.
CompSci 100E 9.1 Stack: What problems does it solve?  Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Sorting.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
CSE 373: Data Structures and Algorithms
Compsci 100, Fall Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Searching Topics Sequential Search Binary Search.
CompSci 100 Prog Design and Analysis II Sept 14, 2010 Prof. Rodger CompSci 100, Fall
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
CPS Solving Problems Recursively l Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved simply.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
CPS Recurrences l Summing Numbers int sum(int n) { if (0 == n) return 0; else return n + sum(n-1); } l What is complexity? justification? l T(n)
Algorithm Analysis 1.
Complexity Analysis (Part I)
Analysis: Algorithms and Data Structures
Introduction to Algorithms
Sorting by Tammy Bailey
Tools: Solve Computational Problems
David Kauchak CS52 – Spring 2015
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Introduction to Algorithms Analysis
CS 201 Fundamental Structures of Computer Science
Recurrences (Method 4) Alexandra Stefan.
Searching, Sorting, and Asymptotic Complexity
Algorithmic complexity: Speed of algorithms
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms and implementations  It’s faster! It’s more elegant! It’s safer! It’s cooler! l We need empirical tests and analytical/mathematical tools  Given two methods, which is better? Run them to check. 30 seconds vs. 3 seconds, easy. 5 hours vs. 2 minutes, harder What if it takes two weeks to implement the methods?  Use mathematics to analyze the algorithm,  The implementation is another matter, cache, compiler optimizations, OS, memory,…

CPS 100, Spring How fast does the code run? (from Princeton 226, Wayne/Sedgewick)

CPS 100, Spring What is a list in Java? l Collection of elements, operations?  Add, remove, traverse, …  What can a list do to itself?  What can we do to a list? l Why are there different kinds of lists? Array and Linked  Useful in different applications  How do we analyze differences?

CPS 100, Spring Analyze Data Structures public double removeFirst(List list) { double start = System.currentTimeMillis(); while (list.size() != 1){ list.remove(0); } double end = System.currentTimeMillis(); return (end-start)/1000.0; } List linked = new LinkedList (); List array = new ArrayList (); double ltime = splicer.removeFirst(splicer.create(linked,100000)); double atime = splicer.removeFirst(splicer.create(array,100000)); l How much time does it take to remove the first element?

CPS 100, Spring Removing first element sizelinkarray

CPS 100, Spring Middle Index Removal public double removeMiddleIndex(List list) { double start = System.currentTimeMillis(); while (list.size() != 1){ list.remove(list.size()/2); } double end = System.currentTimeMillis(); return (end-start)/1000.0; } l What operations could be expensive here?  Explicit: size, remove  Implicit: find n th element

CPS 100, Spring Remove middle element sizelinkarray

CPS 100, Spring Quantitative Measurements of Code l Typically measure running time. Also measure memory  Other things to measure?  What about wall-clock v CPU time? Java: wall-clock l Typically change size of input/problem to validate runtime hypotheses  Not the data itself, but the number of data items  Size of string vs. number of strings in array? l Doubling hypothesis: What effect does doubling input size have on running time?  Linear: time doubles, quadratic: factor of four, …

CPS 100, Spring Different measures of complexity l Worst case  Gives a good upper-bound on behavior  Never get worse than this  Drawbacks? l Average case  What does average mean?  Averaged over all inputs? Assuming uniformly distributed random data?  Drawbacks? l Best case  Linear search, useful?

CPS 100, Spring Jaron LanierJaron Lanier ( Jaron Lanier is a computer scientist, composer, visual artist, and author. He coined the term ‘Virtual Reality’ … he co-developed the first implementations of virtual reality applications in surgical simulation, vehicle interior prototyping, virtual sets for television production, and assorted other areas "What's the difference between a bug and a variation or an imperfection? If you think about it, if you make a small change to a program, it can result in an enormous change in what the program does. If nature worked that way, the universe would crash all the time." Lanier has no academic degrees

CPS 100, Spring Notations for measuring complexity l O-notation or big-Oh: O(n 2 ) is used in most algorithmic analysis, e.g., in Compsci 130 here. It’s an upper bound in the limit  Correct to say that linear algorithm is O(n 2 ), but useful? Theta-notation or  (n 2 ) is a tight bound, solid guarantee that algorithmic analysis is exact, both upper and lower bound Omega is lower bound:  (n log n) is a lower bound for comparison based sorts  Can’t do better than that, very hard to prove l Sedgewick/Wayne uses tilde notation ~ n 2 means leading term is n squared  We’ll use this, but abuse big-Oh since we want “best” answer

CPS 100, Spring Big-Oh, O-notation: concepts & caveats l Count how many times “simple” statements execute  In the body of a loop, what matters? (e.g., another loop?)  Assume simple statements take a second, cost a penny,… What’s good, what’s bad about this assumption? Alternatives? l In a recursive method what do we do? Recurrence relations  Like a loop, but easier! (and trickier) to analyze l In real life: cache behavior, memory behavior, swapping behavior, library gotchas, things we don’t understand,…

CPS 100, Spring Multiplying and adding big-Oh l Suppose we do a linear search then we do another one  What is the complexity?  If we do 100 linear searches?  If we do n searches on a vector of size n? l What if we do binary search followed by linear search?  What are big-Oh complexities? Sum?  What about 50 binary searches? What about n searches? l What is the number of elements in the list (1,2,2,3,3,3)?  What about (1,2,2, …, n,n,…,n)?  How can we reason about this?

CPS 100, Spring Helpful formulae l We always mean base 2 unless otherwise stated  What is log(1024)?  log(xy) log(x y ) log(2 n ) 2 (log n) l Sums (also, use sigma notation when possible)  … + 2 k = 2 k+1 – 1 =  … + n = n(n+1)/2 =  a + ar + ar 2 + … + ar n-1 = a(r n - 1)/(r-1)= log(x) + log(y) y log(x) n log(2) = n 2 (log n) = n k  i=0 2i2i n  i=1 i n-1  i=0 ar i

CPS 100, Spring Why we study recurrences/complexity? l Tools to analyze algorithms l Machine-independent measuring methods l Familiarity with good data structures/algorithms l What is CS person: programmer, scientist, engineer? Brooks: scientists build to learn, engineers learn to build l Mathematics is a notation that helps in thinking, discussion, programming

CPS 100, Spring Recurrences l Summing Numbers int sum(int n) { if (0 == n) return 0; else return n + sum(n-1); } l What is complexity? justification? l T(n) = time to compute sum for n T(n) = T(n-1) + 1 T(0) = 1 l instead of 1, use O(1) for constant time  independent of n, the measure of problem size

CPS 100, Spring Solving recurrence relations l plug, simplify, reduce, guess, verify? T(n) = T(n-1) + 1 T(0) = 1 T(n) = T(n-k) + k find the pattern! Now, let k=n, then T(n) = T(0)+n = 1+n l get to base case, solve the recurrence: O(n) T(n-1) = T(n-1-1) + 1 T(n) = [T(n-2) + 1] + 1 = T(n-2)+2 T(n-2) = T(n-2-1) + 1 T(n) = [(T(n-3) + 1) + 1] + 1 = T(n-3)+3

CPS 100, Spring Complexity Practice l What is complexity of Build ? (what does it do?) ArrayList build(int n) { if (0 == n) return new ArrayList (); // empty ArrayList list = build(n-1); for(int k=0;k < n; k++){ list.add(n); } return list; } l Write an expression for T(n) and for T(0), solve.

CPS 100, Spring Recognizing Recurrences l Solve once, re-use in new contexts  T must be explicitly identified  n must be some measure of size of input/parameter T(n) is the time for quicksort to run on an n-element vector T(n) = T(n/2) + O(1) binary search O( ) T(n) = T(n-1) + O(1) sequential search O( ) T(n) = 2T(n/2) + O(1) tree traversal O( ) T(n) = 2T(n/2) + O(n) quicksort O( ) T(n) = T(n-1) + O(n) selection sort O( ) l Remember the algorithm, re-derive complexity n log n n log n n n2n2

CPS 100, Spring Sergey Brin l Simple ideas sometimes can change the world [wikipedia]  Works because of scale l Co-created pagerank (Larry Page), which evaluates links to a page and the importance of those links, based on the importance of the page from which the links come which …  Recursion?  Turtles all the way down!