Asymptotic Complexity (Big-O Notation) CS 1037 Fundamentals of Computer Science II.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 3 Growth of Functions
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Course Introduction CS 1037 Fundamentals of Computer Science II.
Cmpt-225 Algorithm Efficiency.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
Algorithm Cost Algorithm Complexity. Algorithm Cost.
COMPSCI 102 Introduction to Discrete Mathematics.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Mathematics Review and Asymptotic Notation
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
CS 3343: Analysis of Algorithms
CS 1704 Introduction to Data Structures and Software Engineering.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
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.
Asymptotic Analysis-Ch. 3
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Asymptotic Analysis (based on slides used at UMBC)
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
ADS 1 Algorithms and Data Structures 1 Syllabus Asymptotical notation (Binary trees,) AVL trees, Red-Black trees B-trees Hashing Graph alg: searching,
Algorithm Analysis (Big O)
Scalability for Search Scaling means how a system must grow if resources or work grows –Scalability is the ability of a system, network, or process, to.
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.
CES 512 Theory of Software Systems B. Ravikumar (Ravi) Office: 141 Darwin Hall Course Web site:
Searching Topics Sequential Search Binary Search.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
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, …
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Big-O notation.
CS 3343: Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Algorithms Analysis
Analysys & Complexity of Algorithms
Analysis of Algorithms
Chapter 2.
Searching, Sorting, and Asymptotic Complexity
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Presentation transcript:

Asymptotic Complexity (Big-O Notation) CS 1037 Fundamentals of Computer Science II

What is a “Fast” Algorithm? Best case Average case Worst case Small problems Large problems 2 Best case Average case Worst case Small problems Large problems Takes the fewest seconds? Takes the fewest steps? In practice, these matter. Choice depends on: application requirements computer architecture expected input PP PP In theoretical comp. sci., algorithms ranked by: fewest # of steps, in the worst case, on large problems T

Ranking Running-Time Suppose algorithm A takes an 2 +b steps on problem of size n in worst case Suppose algorithm B takes cnlgn+d steps Which algorithm is faster in practice? – depends on a,b,c,d and typical input Which algorithm is faster in theory? – for large enough n coefficients a,b,c,d don’t matter – algorithm B therefore fundamentally faster than A 3

Example: Algorithm A vs B Force cnlgn+d to have large c... 4 B faster than A for large enough n A B

When is Algorithm A “as fast as” B? Let A(n) be # steps for A on problem size n Let B(n) be # steps for algorithm B – e.g. define functions A(n)= 4n+2, B(n)= n 2 A as fast as B if A(n) · kB(n) for all large enough n and some k > 0 4n+2 · n 2 for all n ¸ 5 4n+2 · 2n 2 for all n ¸ 3 20nlgn · n 2 for all n ¸ n time # items 5

Asymptotic Notation (“Big-O”) Let g(n) be some function of n Define O(g(n)) to be set of all functions as fast as g(n) in theoretical sense: 6 O(g(n)) = f(n) j there exist positive k, n 0 such that 0 · f(n) · kg(n) for all n ¸ n 0 f g n time # items n2n2 f(n) 2 O(n2)f(n) 2 O(n2) n0n0 n 2 O(n) n 2 O(n 2 ) n 2 2 O(n) e.g.

Asymptotic Notation (“Big-O”) Intuition: n O(n) because n+100 · 50n for n ¸ 3 or because n+100 · 2n for n ¸ 100 n 2 2 O(n) because for any k>0 eventually n 2 > kn for large n (i.e. “ n 2 slower than n ”) any constant c 2 O(n) because c · n for n ¸ c 7 f(n) 2 O(g(n)) “ f(n) no slower than kg(n) for large n ”

Asymptotic Notation (“Big-O”) Claim: any function of the form an+b with a ¸ 0 is a member of the set O(n) Proof: choose k = a+ j b j, then an+b · (a+ j b j )n 8 n ¸ 1 Claim: any an 2 +bn+c 2 O(n 2 ) if a ¸ 0 Proof: choose k = a+ j b j + j c j, then an 2 +bn+c · (a+ j b j + j c j )n 2 8 n ¸ 1 8

Asymptotic Notation (“Big-O”) Can say Insertion Sort runs in... – “quadratic time in the worst-case”, or – O(n 2 ) time... “oh of n squared time” Can say Binary Search runs in... – “logarithmic time in the worst-case”, or – O(lgn) time... “oh of log n time” Can say Counting Sort runs in... – “time linear in n and k ”, or – O(n+k) time... “oh of n plus k time” 9 this k means “max range of items”

Asymptotic Notation (“Big-O”) Claim: if f(n) 2 O(g(n)) and g(n) 2 O(h(n)) then f(n) 2 O(h(n)) Proof: We know for some n 1,n 2,k 1,k 2 > 0 0 · f(n) · k 1 g(n) 8 n ¸ n 1 0 · g(n) · k 2 h(n) 8 n ¸ n 2 and therefore 0 · f(n) · k 1 k 2 h(n) 8 n ¸ max f n 1,n 2 g 10

Big-O Notation Used Everywhere 11 DOCUMENTATION COMP. SCI. COURSES ALGORITHMS RESEARCH

More Examples of Big-O Membership n+50 6n n 2 +10n+50 n 3 ¡ 10n 2lgn+50 n+lgn+50 2nlgn+n+lgn 2 n +500n 3 c n +n c O(1) O(n) O(n 2 ) O(n 3 ) O(lgn) O(n) O(nlgn) O(2 n ) O(c n ) if c > 1 We say any constant c 2 O(1), i.e. O(1) = set of all constants Such functions are “exponential in n ” and grow extremely fast! Such algorithms take longer than age of universe to compute medium-sized problems

History of Big-O Invented by mathematician Paul Bachmann in 1894 to characterize error bounds Don Knuth popularized use for running time bounds in 1970s 13 The Art of Computer Programming, Volume 1

Knuth on Importance of Big-O “Students will be motivated to use O(n) notation for two important reasons. First, it significantly simplifies calculations because it allows us to be sloppy — but in a satisfactorily controlled way. Second, it appears in the power series 14 calculations of symbolic algebra systems like Maple and Mathematica, which today’s students will surely be using...” —Don Knuth advocating Big-O in high school, Letter to The American Mathematical Society March

END OF COURSE MATERIAL hooray!!! 15

variables if-else loops arrays pointers functions CS1036 Programming Skills Take Time! 16 effectiveness of you stuff in your brain structs classes templates sorting lists/queues trees CS1037 dictionaries hashing more… CS2210 SE2205

Big Universe of Ideas! 17 variables loops if-else arrays pointers functions exceptions structs classes templates sorting lists queues trees big-O recursion polymorphism inheritance debugging assertions iterators threads networking security optimization encapsulation searching hashing clustering heaps balanced trees caching randomized algorithms graphs matching NP-completeness computability theory finite automata approximation algorithms multisets dynamic programming compression float arithmetic symbolic computation GPUs distributed computing compilers operating systems greedy algorithms interpreters parsing databases dictionaries stacks user interfaces Java Python virtual machines C# backtracking profiling reducibility C data mining assembly file I/O proof techniques portability linear algebra rasterization references physics simulation version control concurrency combinatorics UML

Language Popularity in source:

Goals of CS 1037 Understand how to… – use data structures – implement data structures – characterize an algorithm’s performance – express computation in C++ Develop intuition about… – best data structure / algorithm for situation – good code, poor code, and dangerous code Programming experience, confidence 19

Pearls of Wisdom 20 “Anybody who comes to you and says he has a perfect language is either naive or a salesman.” —Bjarne Stroustrup at Waterloo Comp Sci Club Meeting “A program that has not been tested does not work.” —Bjarne Stroustrup

Pearls of Wisdom 21 “The psychological profiling [of a programmer] is mostly the ability to shift levels of abstraction, from low level to high level. To see something in the small and to see something in the large.” —Don Knuth “Premature optimization is the root of all evil.” —Don Knuth remember that one!!

EXAM REVIEW TIME! hooray? 22