CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Advertisements

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 3 Growth of Functions
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
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.
Asymptotic Analysis Motivation Definitions Common complexity functions
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Elementary Data Structures and Algorithms
COMPSCI 102 Introduction to Discrete Mathematics.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
1 Big-Oh Notation CS 105 Introduction to Data Structures and Algorithms.
Mathematics Review and Asymptotic Notation
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Department of Computer Science 1 COSC 2320 Section Room 104 AH 5:30 – 7:00 PM TTh Professor Olin Johnson Office 596 PGH
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Programming Abstractions Cynthia Lee CS106X. Today’s topics:  Performance issues in recursion  Big-O performance analysis 2.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Introduction to Analysis of Algorithms CS342 S2004.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Data Structures Using C++ 2E
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Basic Concepts 2011, Fall Pusan National University Ki-Joune Li.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Programming Abstractions
Introduction to Algorithms
David Kauchak CS52 – Spring 2015
Programming Abstractions
Programming Abstractions
Searching, Sorting, and Asymptotic Complexity
CSE 373, Copyright S. Tanimoto, 2002 Asymptotic Analysis -
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007.
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: 1. Formal introduction to Big O 2. Powers of two and exponential time  Traveling Salesman problem 2

Big-O Extracting time cost from example code

A student has counted how many times we perform each line of code Do you agree with the student’s count of 3n+5? A. Yes B. No C. Other/ none/ more StatementsCost 1 float findAvg ( Vector & grades ){ 2 float sum = 0;1 3 int count = 0;1 4 while ( count < grades.size() ) {n sum += grades[count];n 6 count++;n 7 } 8 if ( grades.size() > 0 )1 9 return sum / grades.size(); 10 else1 11 return 0.0f; 12 } ALL 3n+5

A student has counted how many times we perform each line of code Do you agree with the student’s count of 3n+5? A. Yes B. No C. Other/ none/ more StatementsCost 1 float findAvg ( Vector & grades ){ 2 float sum = 0;1 3 int count = 0;1 4 while ( count < grades.size() ) {n sum += grades[count];n 6 count++;n 7 } 8 if ( grades.size() > 0 )1 9 return sum / grades.size(); 10 else1 11 return 0.0f; 12 } ALL 3n+5 Do we really care about the +5? Or the 3 for that matter?

Big-O We say a function f(n) is “big-O” of another function g(n), and write f(n) = O (g(n)) (or f(n) ∈ O (g(n))), if there are positive constants c and n 0 such that:  f(n) ≤ c g(n) for all n ≥ n 0. This is really more detail than you need to know for this course, but for some students it may help to know what underlies our approach.

Big-O We say a function f(n) is “big-O” of another function g(n), and write f(n) = O (g(n)) (or f(n) ∈ O (g(n))), if there are positive constants c and n 0 such that:  f(n) ≤ c g(n) for all n ≥ n 0. What you need to know:  O(g(n)) describes an “upper bound”—the algorithm will perform no worse than g(n)  We ignore constant factors in saying that  We ignore behavior for “small” n

Big-O Interpreting graphs

f 2 is O(f 1 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2

f 1 is O(f 2 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2

 f 2 = O(f 1 ) because f 1 is above f 2 —an “upper bound”  But also true: f 1 = O(f 2 )  We can move f 2 above f 1 by multiplying by c f1f1 f2f2 f(n) = O(g(n)), if there are positive constants c and n 0 such that f(n) ≤ c * g(n) for all n ≥ n 0.

 f 2 = O(f 1 ) because f 1 is above f 2 —an “upper bound”  But also true: f 1 = O(f 2 )  We can move f 2 above f 1 by multiplying by c  These two functions are both big-O of each other* f1f1 f2f2 f(n) = O(g(n)), if there are positive constants c and n 0 such that f(n) ≤ c * g(n) for all n ≥ n 0. * Another way of saying that is that they are big-Θ of each other (Θ is beyond the scope of this class)

f 3 is O(f 1 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2 f3f3

f 1 is O(f 3 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2 f3f3

Shortcuts for calculating Big-O analysis starting with a function characterizing the growth in cost of the algorithm

Let f(n) = 3 log 2 n + 4 n log 2 n + n  Which of the following is true? A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more

Let f(n) = n + 2n 2  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 3 ) E. Other/none/more

Let f(n) = 2 n + 14n 2 + 4n 3  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 3 ) E. Other/none/more

Let f(n) = 100  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 100 ) E. Other/none/more

Fibonacci  F(0) = 0  F(1) = 1  F(n) = F(n-1) + F(n-2) for all n > 1  Work is duplicated throughout the call tree  F(2) is calculated 3 separate times when calculating F(5)!  15 function calls in total for F(5)! Image is in the public domain.