CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Algorithm Analysis II Reading: Weiss, chap. 2.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts COMP-2001 L4&5 Portions.
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
The Seven Functions. Analysis of Algorithms. Simple Justification Techniques. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer Goodrich,
PSU CS 311 – Algorithm Design and Analysis Dr. Mohamed Tounsi 1 CS 311 Design and Algorithms Analysis Dr. Mohamed Tounsi
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CS2210 Data Structures and Algorithms Lecture 2:
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
Analysis of Algorithms
Introduction Data Structures & Algorithm Data Structures & Algorithm.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
CS 3343: Analysis of Algorithms
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
Iterative Algorithm Analysis & Asymptotic Notations
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
1 Dr. J. Michael Moore Data Structures and Algorithms CSCE 221 Adapted from slides provided with the textbook, Nancy Amato, and Scott Schaefer.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
COMP 2402/2002 Abstract Data Types and Algorithms Prof: Office: Office hours: Eduardo Mesa HP 5347 Tuesday and Thursday 4:30pm.
CSC401 – Analysis of Algorithms Lecture Notes 2 Asymptotic Analysis Objectives: Mathematics foundation for algorithm analysis Amortization analysis techniques.
Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
Introduction to Analysis of Algorithms CS342 S2004.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Time Complexity of Algorithms (Asymptotic Notations)
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
CSC Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
Asymptotic Notation Faculty Name: Ruhi Fatima
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
Analysis of Algorithms
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Algorithm Analysis II Reading: Weiss, chap. 2

Last Time Steps in problem solving Algorithm analysis Space complexity Time complexity Pseudo-code

Algorithm Analysis Last time: Experimental approach – problems Low level analysis – count operations Abstract even further Characterize an algorithm as a function of the “problem size” E.g. Input data = array  problem size is N (length of array) Input data = matrix  problem size is N x M

Asymptotic Notation Goal: to simplify analysis by getting rid of unneeded information (like “rounding” 1,000,001≈1,000,000) We want to say in a formal way 3n 2 ≈ n 2 The “Big-Oh” Notation: given functions f(n) and g(n), we say that f(n) is O(g(n)) if and only if there are positive constants c and n 0 such that f(n)≤ c g(n) for n ≥ n 0

Graphic Illustration f(n) = 2n+6 Conf. def: Need to find a function g(n) and a const. c such as f(n) < cg(n) g(n) = n and c = 4  f(n) is O(n) The order of f(n) is n g (n)  n c g (n)  4 n n

More examples What about f(n) = 4n 2 ? Is it O(n)? Find a c such that 4n 2 n0 50n n + 4 is O(n 3 ) Would be correct to say is O(n 3 +n) Not useful, as n 3 exceeds by far n, for large values Would be correct to say is O(n 5 ) OK, but g(n) should be as closed as possible to f(n) 3log(n) + log (log (n)) = O( ? ) Simple Rule: Drop lower order terms and constant factors

Properties of Big-Oh If f(n) is O(g(n)) then af(n) is O(g(n)) for any a. If f(n) is O(g(n)) and h(n) is O(g’(n)) then f(n)+h(n) is O(g(n)+g’(n)) If f(n) is O(g(n)) and h(n) is O(g’(n)) then f(n)h(n) is O(g(n)g’(n)) If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) is O(h(n)) If f(n) is a polynomial of degree d, then f(n) is O(n d ) n x = O(a n ), for any fixed x > 0 and a > 1 An algorithm of order n to a certain power is better than an algorithm of order a ( > 1) to the power of n log n x is O(log n), fox x > 0 – how? log x n is O(n y ) for x > 0 and y > 0 An algorithm of order log n (to a certain power) is better than an algorithm of n raised to a power y.

Asymptotic analysis - terminology Special classes of algorithms: logarithmic:O(log n) linear:O(n) quadratic:O(n 2 ) polynomial:O(n k ), k ≥ 1 exponential:O(a n ), n > 1 Polynomial vs. exponential ? Logarithmic vs. polynomial ?

Some Numbers

“Relatives” of Big-Oh “Relatives” of the Big-Oh  (f(n)): Big Omega – asymptotic lower bound  (f(n)): Big Theta – asymptotic tight bound Big-Omega – think of it as the inverse of O(n) g(n) is  (f(n)) if f(n) is O(g(n)) Big-Theta – combine both Big-Oh and Big-Omega f(n) is  (g(n)) if f(n) is O(g(n)) and g(n) is  (f(n)) Make the difference: 3n+3 is O(n) and is  (n) 3n+3 is O(n 2 ) but is not  (n 2 )

More “relatives” Little-oh – f(n) is o(g(n)) if for any c>0 there is n0 such that f(n) n0. Little-omega Little-theta 2n+3 is o(n 2 ) 2n + 3 is o(n) ?

Example Remember the algorithm for computing prefix averages - compute an array A starting with an array X - every element A[i] is the average of all elements X[j] with j < i Remember some pseudo-code … Solution 1 Algorithm prefixAverages1(X): Input: An n-element array X of numbers. Output: An n -element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. for i  0 to n - 1 do a  0 for j  0 to i do a  a + X[j] A[i]  a/(i+ 1) return array A Analyze this

Example (cont’d) Algorithm prefixAverages2(X): Input: An n-element array X of numbers. Output: An n -element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. s  0 for i  0 to n do s  s + X[i] A[i]  s/(i+ 1) return array A

Back to the original question Which solution would you choose? O(n 2 ) vs. O(n) Some math … properties of logarithms: log b (xy) = log b x + log b y log b (x/y) = log b x - log b y log b xa = alog b x log b a=log x a/log x b properties of exponentials: a (b+c) = a b a c a bc = (a b ) c a b /a c = a (b-c) b = a log a b b c = a c*log a b

Important Series Sum of squares: Sum of exponents: Geometric series: Special case when A = … + 2 N = 2 N+1 - 1

Analyzing recursive algorithms function foo (param A, param B) { statement 1; statement 2; if (termination condition) { return; foo(A’, B’); }

Solving recursive equations by repeated substitution T(n) = T(n/2) + csubstitute for T(n/2) = T(n/4) + c + csubstitute for T(n/4) = T(n/8) + c + c + c = T(n/2 3 ) + 3c in more compact form = … = T(n/2 k ) + kc“inductive leap” T(n) = T(n/2 logn ) + clogn“choose k = logn” = T(n/n) + clogn = T(1) + clogn = b + clogn = θ(logn)

Solving recursive equations by telescoping T(n) = T(n/2) + cinitial equation T(n/2) =T(n/4) + cso this holds T(n/4) = T(n/8) + cand this … T(n/8) = T(n/16) + cand this … … T(4) = T(2) + ceventually … T(2) = T(1) + cand this … T(n) = T(1) + clogn sum equations, canceling the terms appearing on both sides T(n) = θ(logn)

Problem Running time for finding a number in a sorted array [binary search] Pseudo-code Running time analysis

ADT ADT = Abstract Data Types A logical view of the data objects together with specifications of the operations required to create and manipulate them. Describe an algorithm – pseudo-code Describe a data structure – ADT

What is a data type? A set of objects, each called an instance of the data type. Some objects are sufficiently important to be provided with a special name. A set of operations. Operations can be realized via operators, functions, procedures, methods, and special syntax (depending on the implementing language) Each object must have some representation (not necessarily known to the user of the data type) Each operation must have some implementation (also not necessarily known to the user of the data type)

What is a representation? A specific encoding of an instance This encoding MUST be known to implementors of the data type but NEED NOT be known to users of the data type Terminology: "we implement data types using data structures“

Two varieties of data types Opaque data types in which the representation is not known to the user. Transparent data types in which the representation is profitably known to the user:- i.e. the encoding is directly accessible and/or modifiable by the user. Which one you think is better? What are the means provided by C++ for creating opaque data types?

Why are opaque data types better? Representation can be changed without affecting user Forces the program designer to consider the operations more carefully Encapsulates the operations Allows less restrictive designs which are easier to extend and modify Design always done with the expectation that the data type will be placed in a library of types available to all.

How to design a data type Step 1: Specification Make a list of the operations (just their names) you think you will need. Review and refine the list. Decide on any constants which may be required. Describe the parameters of the operations in detail. Describe the semantics of the operations (what they do) as precisely as possible.

How to design a data type Step 2: Application Develop a real or imaginary application to test the specification. Missing or incomplete operations are found as a side-effect of trying to use the specification.

How to design a data type Step 3: Implementation Decide on a suitable representation. Implement the operations. Test, debug, and revise.

Example - ADT Integer Name of ADTInteger Operation Description C/C++ Create Defines an identifier with an undefined value int id1; AssignAssigns the value of one integerid1 = id2; identifier or value to another integer identifier isEqualReturns true if the values associated id1 == id2; with two integer identifiers are the same

Example – ADT Integer LessThan Returns true if an identifier integer is less than the value of the second id1<id2 integer identifier Negative Returns the negative of the integer value -id1 Sum Returns the sum of two integer values id1+id2 Operation Signatures Create: identifier  Integer Assign: Integer  Identifier IsEqual: (Integer,Integer)  Boolean LessThan: (Integer,Integer)  Boolean Negative: Integer  Integer Sum: (Integer,Integer)  Integer

More examples We’ll see more examples throughout the course Stack Queue Tree And more