Data Structures CS 310. Abstract Data Types (ADTs) An ADT is a formal description of a set of data values and a set of operations that manipulate the.

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

Analysis of Algorithms
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
1 TCSS 342, Winter 2005 Lecture Notes Course Overview, Review of Math Concepts, Algorithm Analysis and Big-Oh Notation Weiss book, Chapter 5, pp
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
The Efficiency of Algorithms
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Asymptotic Notations Iterative Algorithms and their analysis
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
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.
ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithm Analysis O Ω.
Recap Introduction to Algorithm Analysis Different Functions Function’s Growth Rate Three Problems Related to Algorithm Running Time Find Minimum in an.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
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.
Algorithm Analysis (Big O)
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Searching Topics Sequential Search Binary Search.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Algorithm Analysis 1.
Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Programming and Data Structure
Analysis of Algorithms
Presentation transcript:

Data Structures CS 310

Abstract Data Types (ADTs) An ADT is a formal description of a set of data values and a set of operations that manipulate the values. An ADT does not specify how the type should be implemented.

ADT: A concrete example Values –minimum –maximum –precision –Infinity –undefined Operators & Properties –add –subtract –multiply –divide –rounding mode (4 modes supported) IEEE floating point 754 An ADT frequently implemented in hardware

ADTs – Why bother? Abstraction Encapsulation Reusability Ability to change the implementation without changing programs using the ADT.

ADTs and languages An ADT can be implemented in any language, even assembly. ADTs can be particularly elegant in object oriented languages. –Why?

Is one algorithm better than another? How should we judge?

Comparing algorithms Maintainability Readability Elegance Complexity analysis

How long does it take for an algorithm to run? –Is runtime a good indication? –How does the algorithm behave as the number of items N increases? How much memory does the algorithm use? –Should we measure in bytes? –Behavior with respect to N?

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Sample analysis Maximum Contiguous subsequence Given a list of integers, return the subsequence that produces the maximum sum. Examples:  2, 3, 5, -33, 5, 6, 1, 3, 3  5, 6, 1, 3, 3  -2, -5, -8, -2, -17, -33  empty  3, -7, 13, -3, 10, 20, -50, 6, 3  13, -3, 10, 20

Brute force solution Copyright © 2006 Pearson Addison-Wesley. All rights reserved

How many operations? We estimated that O(N 3 ) operations Could we make this a little more precise? We need a helping hand from combinatorics

If we have 3 items, how many ways can we choose two of them?

combinatorics In general, we can determine how many possible ways i items can be chosen from N with:

Our first proof

How many times exactly? We know that 1 and N are constants, so we need to think about the number of ways to choose i, j, and k. They vary between 1 and N.

A first attempt Suppose we were to choose 3 integers from 1 to N. We know that this would be This is almost right, but why not?

A refinement k can be either equal to i or j Suppose we add two additional choices –low –high giving us N+2 choices When we select low or high: –low  k=i –high  k=j What does it mean when we pick both low and high?

Number of operations contd Now, when we choose 3 from N+2 choices, we have the exact number of iterations through the loop.

O(N 2 ) Copyright © 2006 Pearson Addison-Wesley. All rights reserved

O(N) – Linear time For the quadratic algorithm, we simply changed the inner most loop of the cubic algorithm from linear O(N) time to constant O(1) time. To make a linear time algorithm, we will need to be a bit more clever…

Clever observation 1 (Theorem 5.2) Let A i,j denote the sequence from i to j. Let S i,j denote the sum of A i,j. If A i,j has S i,j j, then A i,q is not a maximum subsequence.

Clever observation 2 All contiguous subsequences that border the maximum contiguous subsequence must be negative or zero. Let’s prove it…

Clever observation 3 Theorem 5.3 For any i, let A i,j be the first sequence with S i,j < 0. Then for any i≤p≤j and p≤q, A p,q is either: –not a maximum contiguous subsequence –equal to a previously seen maximum contiguous subsequence.

Clever observation 3 Remember – A i,j is the first sequence whose sum S i,j < 0 –i≤p≤j and p≤q case 1: p=i Then S p,q = S i,q but this is < 0

Clever observation 3 case 2: i<p<q≤j A p,q cannot be a max contiguous subsequence.

Clever observation 3 case 3 i<p≤j<q

O(N) algorithm Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Big-Oh, Big-Omega, Big-Theta, and Little-Oh Copyright © 2006 Pearson Addison-Wesley. All rights reserved Note: We usually are finding the growth of the function with respect to N which is Θ(F(N)), but we usually abuse the notation and write O(F(N))

Big Oh notation in style… Earlier, we showed that our cubic algorithm had operations. Nonetheless, we write that it is O(N 3 ). The lower order terms are ignored as they are dominated by the N 3 and we ignore constant scale factors.

Review - logarithms For any base B and N > 0, Note that log B B K = K. Which base should we use for complexity analysis?

logs and complexity analysis Which base should we use for complexity analysis? It doesn’t matter! (See theorem 5.4, p. 181) For convenience, we always use base 2.

Fun with logarithms Repeated doubling –Given X = 1, how many times t must X be doubled before X >= N?

Fun with logarithms Repeated halving X = N while X > 1 { X = X / 2 } How many times will the loop execute?

Repeated halving Try to prove this at home…

Harmonic numbers The N th harmonic number is defined as By theorem 5.5 (p. 183 Weiss)

log N tools We now have three tools at our disposal to recognize log N phenomenon in our programs: –repeated doubling –repeated halving –harmonic series

Static searching A static search occurs when we look for an element of an abstract data type and do not modify the data structure. For now, we will consider searching for integers in arrays.

Linear search found = false; done = false; i = 0; while (! done && ! found) { if (a[i] == search_value){ found = true; } else { i = i+1; if (i >= a.length) { done = true; }

Complexity analysis of our search Failure case – What does it cost if we don’t find anything? Worst case – What if we do find something, but it is the last item we try? Average case – What happens on average?

Binary search Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Empirical runtime of binary search or how to check your results… Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Interpolation search Make a good guess about the next location based on the min and max values of the array. Assumes that numbers are uniformly distributed (they are spread randomly between the min and max values). Average case: O(log log N) Worst case: O(N)

Things to remember big-oh analysis is true for some N 0 such that N>N 0. big-oh analysis doesn’t take the physical devices into account: –e.g. disk and RAM access times are dramatically different Worst case usually easier to determine than average case.

Common errors Total time in consecutive loops does not affect the big-oh estimate other than the the size of the biggest loop. Pay attention to how many times the loop iterates. A loop could be log N, N 2, etc. Remember to drop the low order terms and constants: 5N 3 +4N 2 +8N  O(N 3 )

Common errors continued Remember, big-oh is an upper bound. You cannot say that T(N) > O(N 3 ) as by definition O(N 3 ) implies that there are less than order N 3 operations. Write T(N)=O(N 3 )

Complexity recap O (big-oh)  upper bound: order ≤ stated bound Ω (big-omega)  lower bound: order ≥ stated bound Θ (big-theta)  is exactly of order: order = stated bound o (little-oh)  is less than order: order < stated bound Remember, we usually compute big-theta and write big-oh. Sigh…