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.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CSE Lecture 3 – Algorithms I
Algorithm Analysis.
Lecture: Algorithmic complexity
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.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
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
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Bigointro1 Algorithm Analysis & Program Testing An introduction.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
Introduction to Analysis of Algorithms
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
Mathematics Review and Asymptotic Notation
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis (Big O)
Searching Topics Sequential Search Binary Search.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
CS 302 Data Structures Algorithm Efficiency.
Analysis of Algorithms
Introduction to complexity
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Complexity Analysis.
Building Java Programs
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Efficiency, Big O Notation, and Javadoc
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Algorithm Efficiency Chapter 10.
Data Structures Review Session
Algorithm Efficiency and Sorting
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
slides created by Ethan Apter
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
Algorithm Efficiency and Sorting
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

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 being manipulated are not defined. Generic programming: –Technique used to solve the “Code Bloat” problem. That is by taking into account all possible data types of the method’s parameters. –Generic Program provides a data type “placeholder” that is filled in by a pair of angle brackets <>

3 Generic type example Previous method call: Swap (ref num1, ref num2);

4 Timing Tests

5 Exercise

6 Algorithm Efficiency Let’s look at the following algorithm for initializing the values in an array: final int N = 500; int [] counts = new int[N]; for (int i=0; i<counts.length; i++) counts[i] = 0; The length of time the algorithm takes to execute depends on the value of N

7 Algorithm Efficiency In that algorithm, we have one loop that processes all of the elements in the array Intuitively: –If N was half of its value, we would expect the algorithm to take half the time –If N was twice its value, we would expect the algorithm to take twice the time That is true and we say that the algorithm efficiency relative to N is linear

8 Algorithm Efficiency Let’s look at another algorithm for initializing the values in a different array: final int N = 500; int [] [] counts = new int[N][N]; for (int i=0; i<counts.length; i++) for (int j=0; j<counts[i].length; j++) counts[i][j] = 0; The length of time the algorithm takes to execute still depends on the value of N

9 Algorithm Efficiency However, in the second algorithm, we have two nested loops to process the elements in the two dimensional array Intuitively: –If N is half its value, we would expect the algorithm to take one quarter the time –If N is twice its value, we would expect the algorithm to take quadruple the time That is true and we say that the algorithm efficiency relative to N is quadratic

10 Big-O Notation We use a shorthand mathematical notation to describe the efficiency of an algorithm relative to any parameter n as its “Order” or Big-O –We can say that the first algorithm is O(n) –We can say that the second algorithm is O(n 2 ) For any algorithm that has a function g(n) of the parameter n that describes its length of time to execute, we can say the algorithm is O(g(n)) We only include the fastest growing term and ignore any multiplying by or adding of constants

11 Seven Growth Functions Seven functions g(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n): –Constant  1 –Logarithmic  log n –Linear  n –Log Linear  n log n –Quadratic  n 2 –Cubic  n 3 –Exponential  2 n

12 Growth Rates Compared n=1n=2n=4n=8n=16n= logn n nlogn n2n n3n n2n

13 Big-O for a Problem O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the problem Don’t assume that the specific algorithm that you are currently using is the best solution for the problem There may be other correct algorithms that grow at a smaller rate with increasing n Many times, the goal is to find an algorithm with the smallest possible growth rate

14 Role of Data Structures That brings up the topic of the structure of the data on which the algorithm operates If we are using an algorithm manually on some amount of data, we intuitively try to organize the data in a way that minimizes the number of steps that we need to take Publishers offer dictionaries with the words listed in alphabetical order to minimize the length of time it takes us to look up a word

15 Role of Data Structures We can do the same thing for algorithms in our computer programs Example: Finding a numeric value in a list If we assume that the list is unordered, we must search from the beginning to the end On average, we will search half the list Worst case, we will search the entire list Algorithm is O(n), where n is size of array

16 Role of Data Structures Find a match with value in an unordered list int [] list = {7, 2, 9, 5, 6, 4}; for (int i=0; i<list.length, i++) if (value == list[i]) statement; // found it // didn’t find it

17 Role of Data Structures If we assume that the list is ordered, we can still search the entire list from the beginning to the end to determine if we have a match But, we do not need to search that way Because the values are in numerical order, we can use a binary search algorithm Algorithm is O(log 2 n), where n is size of array

18 Role of Data Structures Find a match with value in an ordered list int [] list = {2, 4, 5, 6, 7, 9}; int min = 0, max = list.length-1; while (min <= max) { if (value == list[(min+max)/2]) statement; // found it else if (value < list[(min+max)/2]) max = (min+max)/2 - 1; else min = (min+max)/2 + 1; } // didn’t find it

19 Order of Magnitude of a Function

20 Names of Orders of Magnitude

21 Role of Data Structures The difference in the structure of the data between an unordered list and an ordered list can be used to reduce algorithm Big-O This is the role of data structures and why we study them We need to be as clever in organizing our data efficiently as we are in figuring out an algorithm for processing it efficiently

22 Garbage collector (GC) is a form of automatic memory management The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program Benefits –Garbage collection frees the programmer from manually dealing with memory reallocation. As a result, certain categories of bugs are eliminated or substantially reduced:

for(int i = 0; i < n; i *= 2) { cout << i << endl;} 'i' is increased by 2*itself each run for(int i = 0; i < n; i++) { //linear for(int j = 1; j < n; j *= 2}} 23