Foundations of Algorithms, Fourth Edition

Slides:



Advertisements
Similar presentations
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Foundations of Algorithms, Fourth Edition
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
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 Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Iterative Algorithm Analysis & Asymptotic Notations
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of.
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
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.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Theoretical analysis of time efficiency
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Introduction to Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
Analysis of Algorithms
Reasons to study Data Structures & Algorithms
Introduction to Algorithms
What is an Algorithm? Algorithm Specification.
Algorithm design and Analysis
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Efficiency Chapter 10.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Programming and Data Structure
Reasons to study Data Structures & Algorithms
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Algorithms and data structures: basic definitions
Presentation transcript:

Foundations of Algorithms, Fourth Edition Richard Neapolitan, Kumarss Naimipour Updated by Richard P. Simpson Chapter 1 Algorithms: Efficiency, Analysis, and Order

What is a problem A problem is a question to which we seek an answer. Examples We want to rearrange a list of numbers in numerical order. (sort) Determine whether the number x is in a list S of n numbers. What is the 25 Fibonacci number?

What is an instance of a problem? An instance of a problem is a specific assignment of the parameters that define the problem. For example in the case of sorting n numbers we need to be giving the n numbers in specific order and n the number of values to sort. This creates the specific case we are interested in.

What is an Algorithm? In mathematics and computer science, an algorithm (from Algoritmi, the Latin form of Al-Khwārizmī) is an effective method expressed as a finite list of well-defined instructions for calculating a function IE a step by step solution to the problem. In computer systems, an algorithm is basically an instance of logic written in software by software developers to be effective for the intended "target" computer(s), in order for the target machines to produce output from given input (perhaps null).

Sequential Search Problem: Is the key x in the array S of n keys? Inputs(parameters): integer n, array of keys indexed from 1 to n (0 to n-1 ?) Outputs: location, 0 if not in S void seqsearch(int n, const int S[], int x, index& location) { location = 1; while(location<=n && S[location]!=x) location++; if (location >n)location=0; }

Matrix Multiplication void matrixmult(int n, const int A[][],const int B[][], int c[][]); { index i,j,k; for(i=1; i<=n; i++) for(j=1, j<=n; j++){ C[i][j]= 0; for(k=1; k<=n; k++) C[i][j] = C[i][j] + C[i][k]* B[k][j]; }

Searching Arrays Sequential Search Binary Search Recursive ( be able to write this !) Non Recursive (in book) A problem can solved using a lot of different algorithms. These may vary in efficiency and or complexity(we will discuss this later) See table 1.1

Recursive Fibonacci See Wolfram MathWorld Discussion 1 2 3 5 8 13 21 34 55 89 . . . f(0) = 0, f(1)=1, f(n)=f(n-1)+f(n-2)

Recursive Solution int fib( int n) { if(n<=1) return n; else return fib(n-1) + fib(n-2); }

The recursive algorithm hits all these nodes! Is this efficient??

Iterative Version int fib2 ( int n) { int I; Fills array from left to right. int f[0..n]; very efficient! f[0]=0; if (n > 0) 0 1 2 3 4 5 6 7 8 f[1]=1; n-2 n-1 n for (i=2; i<=n; i++) f[i] = f[i-1] + f[i-2]; } return f[n]; SEE Table 1.2 1 1 2 3 5 8

Analysis of Algorithms Complexity Analysis This is a measure of the amount of work done by an algorithm as a function of its input data size. IE it’s a function of n. Efficiency I use this term in a very specific way. If two different algorithms having the same complexity are run on the same data set the execution time will probably be different. The faster algorithm is more efficient than the other one.

Types of complexity Worst Case ( our main focus) Best Case Average Case Every Case (i.e. Best = Worst)

Average case complexity Sequential Search Suppose we have an array of n items and would like to do a sequential search for the value x. Also assume that the value x can be in any location with equal probability (ie 1/n) 𝐴 𝑛 = 𝑘=1 𝑛 (𝑘× 1 𝑛 ) = 1 𝑛 × 𝑘=1 𝑛 𝑘 = 1 𝑛 × 𝑛(𝑛+1) 2 = 𝑛+1 2 See the analysis for the possibility that x is not in the array. p22

Complexity Classes recall that n is the data set size! Constant ( 1, 3, 9, 232, etc) Linear ( n, 2n, 3n-2, 21n+100 etc) Quadratic (n2,2n2-3,4n2-3n+23, etc) Cubic ( n3, 4n3+3n2-2n+7, etc) Etc NOTE: The leading term of the polynomial is the most important term from a growth perspective.

Complexity Classes The complexity class of cubic polynomials is represented by the notation Ɵ(n3) Note that Ɵ(n3) is a set of functions. Common complexity sets include Ɵ(lg n) Ɵ(2n) Ɵ(n) Ɵ(n!) Ɵ(n lg n) etc Ɵ(n2)

Figure 1.3: Growth rates of some common complexity functions.

Doubling the Data Size If an algorithm is Ɵ(n2) and the data set is doubled what happens to the execution time? Specificly assume that we have 2n items Hence Ɵ((2n)2)=Ɵ(4n2) = 4Ɵ(n2) Four times as long! What about cubics?

Big O (memorize) This is not a suggestion Definition For a given complexity function f(n), O(f(n)) is the set of functions g(n) for which there exists some positive real constant c and some nonnegative integer N such that for all n≥N, g(n) ≤ c × f(n)

Showing that 5 𝑛 2 +3𝑛−6∈𝜃( 𝑛 2 ) We need to find a c and a N that will make the following inequality true 5 𝑛 2 +3𝑛−6≤𝑐 𝑛 2 What would a good choice for c be? 5 𝑛 2 +3𝑛−6≤6 𝑛 2 3𝑛−6≤ 𝑛 2 0≤ 𝑛 2 −3𝑛+6 We can solve this or just guess. A solution : c=3 and c=6 works.

Greater than quadratics Big O , Big Ω, Big θ Greater than quadratics N3,2n Ω(n2) All quadratics Θ( n2 ) Less than quadratics nlgn,n,lg O(n2)

Figure 1.4: Illustrating "big O", Ω and Θ

Figure 1.5: The function n2 + 10n eventually stays beneath the function 2n2

Another way of looking at it Figure 1.6: The sets O (n2), Ω (n2)and Θ (n2). Some exemplary members are shown.

Logarithm Rules The logarithm to the base b of x denoted logbx is defined to that number y such that by = x logb(x1*x2) = logb x1 + logb x2 logb(x1/x2) = logb x1 - logb x2 logb xc = c logbx logbx > 0 if x > 1 logbx = 0 if x = 1 logbx < 0 if 0 < x < 1

Additional Rules For all real a>0, b>0 , c>0 and n logb a = logca / logc b logb (1/a) = - logb a logb a = 1/ logab a logb n = n logb a

Theorem: log(n!)(nlogn) Case 1 nlogn  O(log(n!)) log(n!) = log(n*(n-1)*(n-2) * * * 3*2*1) = log(n*(n-1)*(n-2)**n/2*(n/2-1)* * 2*1 => log(n/2*n/2* * * n/2*1 *1*1* * * 1) = log(n/2)n/2 = n/2 log n/2  O(nlogn) Case 2 log(n!)  O(nlogn) log(n!) = logn + log(n-1) + log(n-2) + . . . Log(2) + log(1) < log n + log n + log n . . . + log n = nlogn

The Little o Theorem: If log(f)o(log(g)) and lim g(n) =inf as n goes to inf then f o(g) Note the above theorem does not apply to big O for log(n2) O(log n) but n2 O(n) Application: Show that 2n  o(nn) Taking the log of functions we have log(2n)=nlog22 and log( nn) = nlog2n. Hence Implies that 2n  o(nn)

Theorem: L’Hospitals Rule

Homework 1.1 problem 7 1.3 problem 14 1.4 problem 15, 19