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.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Complexity Analysis (Part I)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Analysis of Recursive Algorithms
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
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.
CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
{ 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)
Iterative Algorithm Analysis & Asymptotic Notations
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
CPS 100, Spring Trees: no data structure lovelier?
Analysis of Algorithms
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
CSC 211 Data Structures Lecture 13
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
Compsci 100, Fall Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
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.
Lecture #3 Analysis of Recursive Algorithms
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Tigbur 16.1 Complexity Sorting. Complexity סימון אסימפטוטי.
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.
Algorithm Analysis 1.
Analysis of Algorithms
Analysis: Algorithms and Data Structures
Analysis of Algorithms
Introduction to Algorithms
Compsci 201 Recursion, Recurrences, & Trees
Tools: Solve Computational Problems
CS 213: Data Structures and Algorithms
Algorithm design and Analysis
Analysis of Algorithms
Searching, Sorting, and Asymptotic Complexity
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

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  If that isn’t magic, what is?  There are less than atoms in the universe.  If ordered, how long to locate a particular one?  Demo!  What is the big-Oh?

CompSci 100E 15.2 Analysis: Algorithms and Data Structures  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!  We need empirical tests and analytical/mathematical tools  Given two methods, which is better? Run them to check. o 30 seconds vs. 3 seconds, easy. 5 hours vs. 2 minutes, harder o 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,…

CompSci 100E 15.3 Recursion and recurrences  Why are some functions written recursively?  Simpler to understand, but …  Mt. Everest reasons  Are there reasons to prefer iteration?  Better optimizer: programmer/scientist v. compiler  Six of one? Or serious differences o “One person’s meat is another person’s poison” o “To each his own”, “Chacun a son gout”, …  Complexity (big-Oh) for iterative and recursive functions  How to determine, estimate, intuit

CompSci 100E 15.4 What’s the complexity of this code? // first and last are integer indexes, list is List int lastIndex = first; Object pivot = list.get(first); for(int k=first+1; k <= last; k++){ Comparable ko = (Comparable) list.get(k); if (ko.compareTo(pivot) <= 0){ lastIndex++; Collections.swap(list,lastIndex,k); }  What is big-Oh cost of a loop that visits n elements of a vector?  Depends on loop body, if body O(1) then …  If body is O(n) then …  If body is O(k) for k in [0..n) then …

CompSci 100E 15.5 FastFinder.findHelper private Object findHelper(ArrayList list, int first, int last, int kindex){ int lastIndex = first; Object pivot = list.get(first); for(int k=first+1; k <= last; k++){ Comparable ko = (Comparable) list.get(k); if (ko.compareTo(pivot) <= 0){ lastIndex++; Collections.swap(list, lastIndex,k); } Collections.swap(list, lastIndex, first); if (lastIndex == kindex) return list.get(lastIndex); if (kindex < lastIndex) return findHelper(list, first, lastIndex-1, kindex); return findHelper(list, lastIndex+1, last, kindex); }

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

CompSci 100E 15.7 Multiplying and adding big-Oh  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?  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?  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?

CompSci 100E 15.8 Helpful formulae  We always mean base 2 unless otherwise stated  What is log(1024)?  log(xy) log(x y ) log(2 n ) 2 (log n)  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) nlog(2) = n 2 (log n) = n k  i=0 2i2i n  i=1 i n-1  i=0 ar i

CompSci 100E 15.9 Recursion Review  Recursive functions have two key attributes  There is a base case, sometimes called the exit case, which does not make a recursive call  All other cases make recursive call(s), the results of these calls are used to return a value when necessary o Ensure that every sequence of calls reaches base case o Some measure decreases/moves towards base case o “Measure” can be tricky, but usually it’s straightforward  Example: sequential search in an ArrayList  If first element is search key, done and return  Otherwise look in the “rest of the list”  How can we recurse on “rest of list”?

CompSci 100E Sequential search revisited  What is complexity of sequential search? Of code below? boolean search(ArrayList list, int first, Object target) { if (first >= list.size()) return false; else if (list.get(first).equals(target)) return true; else return search(list,first+1,target); }  Why are there three parameters? Same name good idea? boolean search(ArrayList list, Object target){ return search(list,0,target); }

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

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

CompSci 100E Solving recurrence relations  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  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

CompSci 100E Complexity Practice  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(new Integer(n)); } return list; }  Write an expression for T(n) and for T(0), solve.

CompSci 100E Recognizing Recurrences  Solve once, re-use in new contexts  T must be explicitly identified  n must be some measure of size of input/parameter o 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( )  Remember the algorithm, re-derive complexity n log n n log n n n2n2