Abstract Data Types (ADTs) Data Structures The Java Collections API

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CSE Lecture 3 – Algorithms I
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
The Efficiency of Algorithms
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
COMP s1 Computing 2 Complexity
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
© 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.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
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.
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.
CSC 211 Data Structures Lecture 13
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Data Structure Introduction.
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
Introduction to Analysis of Algorithms CS342 S2004.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
Searching Topics Sequential Search Binary Search.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Algorithm Analysis 1.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Introduction to Analysis of Algorithms
Searching – Linear and Binary Searches
CSC 222: Object-Oriented Programming
Introduction to complexity
Introduction to Analysis of Algorithms
Big-Oh and Execution Time: A Review
Building Java Programs
Algorithm design and Analysis
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”),
Algorithm Efficiency, Big O Notation, and Javadoc
Data Structures Review Session
Lecture 5: complexity reading:
Building Java Programs
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
Algorithms and data structures: basic definitions
Presentation transcript:

Algorithm Efficiency, Big O Notation, ADT’s, and Role of data Structures Abstract Data Types (ADTs) Data Structures The Java Collections API Reading: L&C 3rd ed: 2.1-2.4, 3.1, 3.3, 2nd ed: 1.6-1.7, 3.1

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

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

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<N; i++) for (int j=0; j<N; j++) counts[i][j] = 0; The length of time the algorithm takes to execute still depends on the value of N

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

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(n2) Let T(n) be a function that formulates the time an algorithm needs to be completed, where n is the parameter that specifies the size of the problem, we say that the algorithm is O(T(n)) [or the algorithm has the time-complexity of O(T(n))].

Big-O Notation Big-O notation measures how fast the the running time of the algorithm grows with increase in the size of the problem , not how long will it take for our algorithm to run as a function of the size of the problem. Therefore, We only include the fastest growing term and ignore any multiplying by or adding of constants. Since they are not dependent on the size of the problem. If our time growth function has multiple terms dependent on the problem size n, we only take the dominating term as the Big-O measure. Example

Seven Growth Functions Eight functions O(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  n2 Cubic  n3 Exponential  2n Factorial  n!

Growth Rates Compared n=1 n=2 n=4 n=8 n=16 n=32 1 logn 2 3 4 5 n 8 16 2 3 4 5 n 8 16 32 nlogn 24 64 160 n2 256 1024 n3 512 4096 32768 2n 65536 4294967296 n! 40320 2.09e+13 2.63e+35

Big-O for a Problem O(T(n)) for a problem means there is some O(T(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

Data Structures That brings up the topic of the Data structure on which the algorithm operates. Data Structure is a particular way of organizing the data in computer memory so that it can be used efficiently.

Role of Data Structures 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. As an example, publishers offer dictionaries with the words listed in alphabetical order to minimize the length of time it takes us to look up a word.

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 or list.

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]) return true; // found it return false; //did not find it.

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 Like the old parlor game “Twenty Questions” Algorithm is O(log2n), where n is size of array

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]) return true; // found it else if (value < list[(min+max)/2]) max = (min+max)/2 - 1; min = (min+max)/2 + 1; } return false; // didn’t find it

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 designing an algorithm for processing it efficiently. In fact we can not separate one task from another.

Abstract Data Types (ADT’s) A data type is a set of values and operations that can be performed on those values. The Java primitive data types (e.g. int) have values and operations defined in Java itself. An Abstract Data Type (ADT) is a (usually more sophisticated) data type that has values and operations that are not defined in the language itself. Instead, in Java, an ADT is implemented using a class or an interface.

Abstract Data Types (ADT’s) The code for Arrays.sort is designed to sort an array of Comparable objects: public static void sort (Comparable [ ] data) The Comparable interface defines an ADT There are no objects of Comparable “class” There are objects of classes that implement the Comparable interface. Arrays.sort only uses methods defined in the Comparable interface, i.e. compareTo().

ADT’s and Data Structures Data structures are used to implement an Abstract Data Type. A data structure is used to: to organize the data that the ADT is encapsulating. The type of data structure should be hidden by the API (the methods) of the ADT. Interface (ADT) Class that uses an ADT Class that implements an ADT Data Structure

Collections A collection is a typical example of Abstract Data Type. A collection is a data type that contains and allows access to a group of objects. The Collection ADT is the most general form of ADTs designed for containing/accessing a group of objects. We have more specific forms of Collection ADTs which describe the access “strategy” that models that collection: A Set is a group of things without any duplicates A Stack is the abstract idea of a pile of things, LIFO A Queue is the abstract idea of a waiting line, FIFO A List is an indexed group of things

The Java Collections API The classes and interfaces in the Java Collections Library are named to indicate the underlying data structure and the abstract Data type. For example, the ArrayList we studied in CS110 uses an underlying array as the data structure for storing its objects and implements its access model as a list However, from the user’s code point of view, the data structure is hidden by the API.