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, …

Slides:



Advertisements
Similar presentations
Algorithm Analysis.
Advertisements

Lecture: Algorithmic complexity
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Introduction to Analysis of Algorithms
Runtime Analysis CSC 172 SPRING 2002 LECTURE 9 RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Professor John Peterson
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
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.
Data Structures CS 310. Abstract Data Types (ADTs) An ADT is a formal description of a set of data values and a set of operations that manipulate the.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
(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
Asymptotic Notations Iterative Algorithms and their analysis
CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
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.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
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.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
1 Lecture 5 Generic Types and Big O. 2 Generic Data Types A generic data type is a type for which the operations are defined but the types of the items.
Data Structure Introduction.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Algorithm Analysis Part of slides are borrowed from UST.
CompSci 100e2.1 Today’s Topics l Reading:  Sedgewick & Wayne: , l Topics  APTs  Basic Collections: ArrayLists: the expandable array.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
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.
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.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Searching Topics Sequential Search Binary Search.
CompSci Inheritance and Interfaces  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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Sets Set is an unordered list of items
Analysis of Algorithms
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
From data to information
Compsci 201, Mathematical & Emprical Analysis
Algorithm Analysis (not included in any exams!)
Building Java Programs
Introduction to Data Structures
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Analysis of Algorithms
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Lecture 4: Introduction to Code Analysis
Analysis of Algorithms
Java Basics – Arrays Should be a very familiar idea
Algorithms and data structures: basic definitions
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

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, … l Write general programs to understand the abstraction, advantages? void execute(Pixmap target) { // do something } l But a dog is also a quadruped, how can we deal with this?

CPS 100e 5.2 Single inheritance in Java l A class can extend only one class in Java  All classes extend Object --- it's the root of the inheritance hierarchy tree  Can extend something else (which extends Object), why? l Why do we use inheritance in designing programs/systems?  Facilitate code-reuse (what does that mean?)  Ability to specialize and change behavior If I could change how method foo() works, bar() is ok  Design methods to call ours, even before we implement Hollywood principle: don't call us, …

CPS 100e 5.3 Comparable and Comparator l Both are interfaces, there is no default implementation  Contrast with.equals(), default implementation?  Contrast with.toString(), default? l Where do we define a Comparator?  In its own.java file, nothing wrong with that  Private, used for implementation and not public behavior Use a nested class, then decide on static or non-static Non-static is part of an object, access inner fields l How do we use the Comparator?  Sort, Sets, Maps (in the future) l Does hashing (future topic) have similar problems?

CPS 100e 5.4 Sets l Set is an unordered list of items  Items are unique! Only one copy of each item in set! l We will use two different implementations of sets TreeSet  A TreeSet is backed up by a tree structure (future topic)  Keeps items sorted (+)  Slower than HashSets ?? (-) HashSet  A HashSet is backed up by a hashing scheme (future topic)  Items not sorted – should seem to be in random order (-)  Faster than TreeSets ?? (+)

CPS 100e 5.5 Using Both ArrayList and Sets l You may want to use a set to get rid of duplicates, then put the items in an ArrayList and sort them! l Problem:  Often data comes in the form of an array  How do we go from array to ArrayList or TreeSet? l Problem:  Often we are required to return an array  How do we go from a Collection such as an ArrayList or TreeSet to an array? l Can do it the “hard” way with loops or iterators:  one item at a time l OR:

CPS 100e 5.6 Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they occur l Need more specification? l How do you do it?

CPS 100e 5.7 Possible solutions 1. Use heavy duty data structures (Knuth)  Hash tries implementation  Randomized placement  Lots o’ pointers  Several pages 2. UNIX shell script (Doug Mclroy) tr -cs “[:alpha:]” “[\n*]” < FILE | \ sort | \ uniq -c | \ sort -n -r -k 1,1 | \ head -20 Which is better?  K.I.S.?

CPS 100e 5.8 Dropping Glass Balls l Tower with N Floors l Given 2 glass balls l Want to determine the lowest floor from which a ball can be dropped and will break l How? l What is the most efficient algorithm? l How many drops will it take for such an algorithm (as a function of N)?

CPS 100e 5.9 Glass balls revisited (more balls) l Assume the number of floors is 100 l In the best case how many balls will I have to drop to determine the lowest floor where a ball will break? l In the worst case, how many balls will I have to drop? If there are n floors, how many balls will you have to drop? ( roughly )

CPS 100e 5.10 What is big-Oh about? (preview) l Intuition: avoid details when they don’t matter, and they don’t matter when input size (N) is big enough  For polynomials, use only leading term, ignore coefficients y = 3x y = 6x-2 y = 15x + 44 y = x 2 y = x 2 -6x+9 y = 3x 2 +4x The first family is O(n), the second is O(n 2 )  Intuition: family of curves, generally the same shape  More formally: O(f(n)) is an upper-bound, when n is large enough the expression cf(n) is larger  Intuition: linear function: double input, double time, quadratic function: double input, quadruple the time

CPS 100e 5.11 More on O-notation, big-Oh l Big-Oh hides/obscures some empirical analysis, but is good for general description of algorithm  Allows us to compare algorithms in the limit 20N hours vs N 2 microseconds: which is better? O-notation is an upper-bound, this means that N is O(N), but it is also O(N 2 ) ; we try to provide tight bounds. Formally:  A function g(N) is O(f(N)) if there exist constants c and n such that g(N) n cf(N) g(N) x = n

CPS 100e 5.12 Which graph is “best” performance?

CPS 100e 5.13 Big-Oh calculations from code l Search for element in an array:  What is complexity of code (using O-notation)?  What if array doubles, what happens to time? for(int k=0; k < a.length; k++) { if (a[k].equals(target)) return true; }; return false; l Complexity if we call N times on M-element vector?  What about best case? Average case? Worst case?

CPS 100e 5.14 Amortization: Expanding ArrayLists Expand capacity of list when add() called Calling add N times, doubling capacity as needed l What if we grow size by one each time? Item #Resizing costCumulative cost Resizing Cost per item Capacity After add m m+1 2 m+1 2 m+2 -2around 22 m+1

CPS 100e 5.15 Some helpful mathematics l … + N  N(N+1)/2, exactly = N 2 /2 + N/2 which is O(N 2 ) why? l N + N + N + …. + N (total of N times)  N*N = N 2 which is O(N 2 ) l N + N + N + …. + N + … + N + … + N (total of 3N times)  3N*N = 3 N 2 which is O(N 2 ) l … + 2 N  2 N+1 – 1 = 2 x 2 N – 1 which is O(2 N ) l Impact of last statement on adding 2 N +1 elements to a vector  … + 2 N + 2 N+1 = 2 N+2 -1 = 4x2 N -1 which is O(2 N ) resizing + copy = total (let x = 2 N )

CPS 100e 5.16 Running 10 6 instructions/sec NO(log N)O(N)O(N log N)O(N 2 ) , , min 100, hr 1,000, day 1,000,000, min18.3 hr318 centuries