Compsci 201, O-Notation and Maps (Interfaces too)

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
The Efficiency of Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Analysis of Algorithms
Algorithm Analysis Part of slides are borrowed from UST.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
Algorithm Analysis (Big O)
Compsci 100, Fall Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
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, …
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
TAFTD (Take Aways for the Day)
Algorithm Analysis 1.
Definition of Computer Science
Analysis: Algorithms and Data Structures
Algorithmic complexity: Speed of algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
CSC 222: Object-Oriented Programming
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
Compsci 201, Mathematical & Emprical Analysis
Program Efficiency Interested in “order of magnitude”
CS 3343: Analysis of Algorithms
Analysis of Algorithms
Efficiency (Chapter 2).
Algorithm Analysis (not included in any exams!)
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
The Efficiency of Algorithms
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
GC 211:Data Structures Algorithm Analysis Tools
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Comparing Algorithms Unit 1.2.
Lecture 5: complexity reading:
Analysys & Complexity of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Algorithmic complexity: Speed of algorithms
The Efficiency of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
CSE 373 Data Structures and Algorithms
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Algorithmic complexity: Speed of algorithms
Asymptotic complexity
Building Java Programs
slides created by Marty Stepp
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Compsci 201, Collections, Hashing, Objects
TCSS 143, Autumn 2004 Lecture Notes
Estimating Algorithm Performance
Java Basics – Arrays Should be a very familiar idea
Presentation transcript:

Compsci 201, O-Notation and Maps (Interfaces too) Owen Astrachan ola@cs.duke.edu http://bit.ly/201spring19 February 8, 2019 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps I is for … Interface LinkedList implements List Inheritance LinkedList extends AbstractSequentialList 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps PFmMaBBD Interfaces: List, Set, and Map When it makes sense to use general type Empirical and Analytical measures of efficiency Maps: API and Problem Solving Keys and Values Big-Oh and O-Notation Building a mathematical formalism with intuition 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Problems and Solutions String that occurs most in a list of strings? CountingStringsBenchmark.java, two ideas See also CountingStringsFile for same ideas https://coursework.cs.duke.edu/201spring19/classwork-spring19 Parallel arrays: word[k] occurs count[k] times Use ArrayLists: 2 “the”, 3 “fat”, 4 “fox” the fox cried fat tears 2 4 1 3 5 0 1 2 3 4 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps How does the code work? Process each string s First time words.add(s),counter.add(1) Otherwise, increment count corresponding to s c[x] += 1 ? 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

What is complexity of this code? Search for each word and … if occurs at k +1 to counter.get(k), else add at end Search complexity? O(M) when M different words One search is O(M) – what about all searches? Tracking all words. First time zero, then one, … Avoid analyzing duplicates for the moment Will take longer if we have multiple occurrences of some of M words 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps Tracking N strings Complexity of search? O(M) for M different words Complexity of words.indexOf(..) is O(M) what about all calls? 1 + 2 + … N is N(N+1)/2 O(n2) 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Should we be more precise? Adding M different words will be O(M2) 1 + 2 + … + M = M(M+1)/2 Adding duplicates: we need to be precise about adding N total words. Sometimes word will be found, still O(M) for M different words We have both M and N here, but treat M == N for easier analysis. 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Understanding O-notation This is an upper bound and in the limit Coefficients don’t matter, order of growth N + N + N + N = 4N is O(N) --- why? N*N is O(N2) – why? O(1) means independent of N, constant time In analyzing code and code fragments Account for each statement How many teams is each statement executed? 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Running times in seconds machine: 109 instructions/sec O(log N) O(N) O(N log N) O(N2) 10 3E-9 1E-8 3.3E-8 0.0000001 100 7E-9 1E-7 6.64E-7 0.0001 1,000 1E-6 0.00001 0.001 10,000 1.3E-8 0.0001329 0.102 100,000 1.7E-8 0.001661 10.008 1,000,000 0.00000002 0.0199 16.7 min 1,000,000,000 0.00000003 1.002 65.8 31.8 years This slide has errors, replaced by new version in slides 10 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps Just Say No.. When you can O(n2) Quadratic is too slow. We can do better. Our goal is linear or better. We cannot always do this, but we can with HashSet 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Example: Analyze using big-Oh What is runtime of stuff(N) How to reason about this What is return value of stuff(N) What if code changes to sum += k 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Counting for O-notation Why is O(1) complexity of sum += n Is this O(1) for any x += y ? Loop executes N times, doing O(1) per iteration Total runtime for method? O(n) 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Practice with O-Notation What is big-Oh of runtime of call calc(N) ? Number of statements executed, O(1) for 146? Use calc(16) and generalize What is big-Oh of value returned by calc(N) ? Table? N = 1, 2, 4, 8, 16 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps WOTO http://bit.ly/201spring19-feb8-1 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps

Compsci 201, Spring 2019: O-Notation + Maps Luis von Ahn Duke 2000, Math Duke Honorary Degree 2017 CEO Duolingo Macarthur Award, 2006 MIT-Lemelson Prize, 2018 Shafi is a serious theoretical computer scientist. Not much to do here but read the bullets and the quote “It’s amazing how motivating it is to sit with somebody and say, ‘What you’re doing is really important.’ I use that a lot.” 2/8/19 Compsci 201, Spring 2019: O-Notation + Maps