CS 312 - Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Analysis of Algorithms
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
Chapter 3 Growth of Functions
Complexity Analysis (Part I)
Cmpt-225 Algorithm Efficiency.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Cost Algorithm Complexity. Algorithm Cost.
COMP s1 Computing 2 Complexity
COMPSCI 102 Introduction to Discrete Mathematics.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
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)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
CS 3343: Analysis of Algorithms
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Analysis of Algorithms
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Algorithm Analysis Part of slides are borrowed from UST.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Foundations of Algorithms, Fourth Edition
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
GC 211:Data Structures Algorithm Analysis Tools
CS 3343: Analysis of Algorithms
Great Theoretical Ideas in Computer Science
CS 3343: Analysis of Algorithms
Introduction to Algorithms Analysis
Data Structures Review Session
Analysis of Algorithms
Introduction to Discrete Mathematics
At the end of this session, learner will be able to:
David Kauchak cs161 Summer 2009
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Analysis of Algorithms
Presentation transcript:

CS Algorithm Analysis1 Algorithm Analysis - CS 312 Professor Tony Martinez

CS Algorithm Analysis2 Algorithm Analysis - Class Goals Analysis of Algorithms from both practical (programming) and theoretical (mathematical) points of view Efficiency of Algorithms - Time and Space Explore Fundamental Algorithmic Paradigms – Divide and Conquer – Graph Exploration – Greedy Algorithms – Dynamic Programming – Linear Programming – Local Search – Intelligent Search – Probabilistic Algorithms

CS Algorithm Analysis3 Additional Goals Develop your general problem solving and programming skills Use Visual Studio and C# Become a better person - Always remember the big picture

CS Algorithm Analysis4 Before we ask what is an Algorithm, We ask: What is Computation? Before we ask what is an Algorithm, We ask: What is Computation?

CS Algorithm Analysis5 What is Computation? What is Computation?

CS Algorithm Analysis6 Deterministic mappings What Features would we like for such computational machines Humans?, Machine vs Human Intelligence?, come to CS 478 What is Computation? What is Computation?

What is an Algorithm? No single formal definition A typical informal definition: “A finite sequence of well defined steps for solving a problem.” This definition has been influenced by the types of computational machines we have used. Is all computation done by algorithms so defined? – Sequence of operations? – Termination?

CS Algorithm Analysis8 3 Basic Questions about any Algorithm Start with a problem and then propose an algorithmic solution: 1. Is it Correct (not as obvious as it sounds)? 2. How long does it take as a function of n, the problem size? – Can also ask how much space (memory) does it require – Average, worst, and best case 3. Can we do Better? – Efficiency - Time and Space – Correctness/Accuracy – Simplicity - Intelligibility – Etc. Example - Is a certain name in a list of names of length n?

CS Algorithm Analysis9 A Bit of History Abu Jafar Muhammad ibn Musa, c – “Al Khwarizmi” (of Kwarizm) his place of origin (Uzbekistan) – Persian scholar in Baghdad – Used decimal (positional system as contrasted with Roman numerals) and proposed numerous arithmetic algorithms – Latin version of this name is Algoritmi – From this came the word Algorithm Leonardo of Pisa (Fibonacci) – Pushed the use of decimal system in Europe – Most remembered for the Fibonacci series of numbers

CS Algorithm Analysis10 Fibonacci Series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

CS Algorithm Analysis11 An Algorithm - fib1 Function for computing the n-th Fibonacci number F n function fib1(n) if n = 0: return 0 if n = 1: return 1 return fib1(n-1) + fib1(n-2)  Is it correct?  How much time does it take, as a function of n?  Can we do better?

CS Algorithm Analysis12 How long does it take Note that with each call there are two more calls (doubling each time) What is the depth of the calling tree Thus exponential time Since tree is binary, but not full it is somewhat less than, but close to O(2 n ) How bad is that?

CS Algorithm Analysis13 How long does it take Note that with each call there are two more calls (doubling each time) What is the depth of the calling tree Thus exponential time Since tree is binary, but not full it is somewhat less than, but close to O(2 n ) How bad is that? Calculating F(200) takes longer on current fastest computer than the life of the sun. Moore’s Law? Parallelism? Exponential is Intractable!

CS Algorithm Analysis14 How long does it take - Bit more Careful Assume T(n) is the time necessary to compute F(n) for fib1. What is the recurrence relation? T(n) = ? function fib1(n) if n = 0: return 0 if n = 1: return 1 return fib1(n-1) + fib1(n-2)

CS Algorithm Analysis15 How long does it take - Bit more Careful Assume T(n) is the time necessary to compute F(n) for fib1. What is the recurrence relation? T(n) = T(n-1) + T(n-2) + 3 when n > 1 function fib1(n) if n = 0: return 0 if n = 1: return 1 return fib1(n-1) + fib1(n-2)

CS Algorithm Analysis16 How long does it take - a Bit more Careful Assume T(n) is the time necessary to compute F(n) for fib1. What is the recurrence relation? T(n) = T(n-1) + T(n-2) + 3 when n > 1 Note that T(n) > F(n) F(n) ≈ n ≈ 1.6 n, where.694 are the first 3 digits of an irrational number T(n) ≈ 1.6 n We will show how to get the exact value for T(n) in this type of case in our discussion of recurrence relations in a couple of weeks

CS Algorithm Analysis17 Can we do better?

CS Algorithm Analysis18 Approximation Sometimes we can find a closed-form solution Fib(n) ≈ n ≈ 1.6 n, where.694 are the first 3 digits of an irrational number This direct calculation Fib(n) ≈ n trades off correctness for speed Approximate solution – very common for many problems as we shall see later

CS Algorithm Analysis19 Can we do better?

CS Algorithm Analysis20 Dynamic Programming Approach Store the intermediate results and avoid the doubling function fib2(n) if n=0: return 0 create an array f[0..n] f[0] = 0, f[1] = 1 for i = 2 to n: f[i] = f[i-1] + f[i-2] return f[n] Dynamic Programming approach Space complexity? - Tradeoffs Ask the questions

CS Algorithm Analysis21 Asymptotic Complexity - Big-O analysis We want to consider significant algorithm differences Independent of the particular machine Consider time complexity in terms of number of elementary operations (we will define more carefully) and as a function of input size n. Space complexity in terms of number of bits required  (n) includes: 2n, 4n+1, etc. Forms an equivalence class Are these constants important in real applications? We will focus in this class on paradigms which allow you to jump all the way into more efficient complexity classes

22 Orders of Growth – Some Complexity Classes nlog 2 nnnlog 2 nn2n2 n3n3 2n2n n!n! * * * * * * * * * * * * * * * Efficient Not Efficient

23 Orders of Growth – Some Complexity Classes nlog 2 nnnlog 2 nn2n2 n3n3 2n2n n!n! * * * * * * * * * * * * * * * Efficient Not Efficient To calibrate, there are about atoms in the solar system and atoms in the current known universe

CS Algorithm Analysis24 Asymptotic Notation Definition: given asymptotically nonnegative g(n), An asymptotically nonnegative function f(n) belongs to the set  (g(n)) if there exist positive constants c 1 and c 2 such that it can be “sandwiched” between c 1 g(n) and c 2 g(n) for sufficiently large n. Write

CS Algorithm Analysis25 Examples Give some example f(n) =  (g(n)) =  (n) How would you prove that f(n) =  (n) Note that  represents an equivalence class – if f(n) =  (g(n)) then g(n) =  (f(n)) Could we represent  (n) as  (3+n/2) – Why don't we?

CS Algorithm Analysis26 Asymptotic Notation Definition: given asymptotically nonnegative g(n),

CS Algorithm Analysis27 Asymptotic Notation Asymptotic Notation f(n) c g(n) n0n0 f(n) c g(n) n0n0 n0n0 c 1 g(n) c 2 g(n) f(n)

CS Algorithm Analysis28 Asymptotic Analysis O and Ω are sets but not equivalence classes like  O(n) includes: 2n, log(n), 4, 1/n, etc. Thus, f = O(g) is like saying f ≤ g in terms of Big-O complexity. – Thus log(n)  O(n), but n  O(log(n)) f = Ω(g) iff g = O(f), i.e. f ≥ g (Duality Rule) f =  (g) means that g = O(f) and f = O(g), i.e. f = g Examples and some guidelines – n a dominates n b for a > b: n a is a higher complexity class than n b – Any exponential dominates any polynomial (the great divide) – Which means that any polynomial dominates any logarithm Usage warning - Many say big O when they really are thinking big . However, often they are actually correct … “Sorting is …”

CS Algorithm Analysis29 The Limit Rule Max Rule: O( f(n) + g(n) ) = O( max( f(n), g(n) ) ) Some Examples f(n) = 4ng(n) = 2nTry both ways f(n) = ng(n) = n 2 Try both ways f(n) = 3n 2 - 6g(n) = 2n 2 + 4nUse max rule first f(n) = logn 2 g(n) = logn 3 f(n) = log 2 ng(n) = log 3 nlog a x = log b x/log b a f(n) = 2 n g(n) = 2 n+1

CS Algorithm Analysis30 Complexity Analysis Best case, average case, worst case – Linear search – Binary search – Usually consider worst case, unless extremely rare

CS Algorithm Analysis31 Another Example: Proving a Function is in a Particular Efficiency Class Show that Must find positive constants c 1, c 2, n 0 such that Consider one inequality at a time – for n 0 =7 we have c1<=1/14 – for n 0 =6 we have c2>1/2 Divide through by n 2 to get This proof is constructive

CS Algorithm Analysis32 Another Example Show that Suppose positive constants c 2, n 0 exist such that Can’t be true since c 2 is constant But this implies that Proof by contradiction

CS Algorithm Analysis33 Analysis Example

CS Algorithm Analysis34 Analysis Example c

CS Algorithm Analysis35 Analysis Example c(n-i)

CS Algorithm Analysis36 } Analysis Example c(n-i) rest of outer loop is constant time. b

CS Algorithm Analysis37 Analysis Example c(n-i) b Execute the loop n times for each value 1..n. Given this algorithm and model of computation, running time is given by Is this function a model of worst, best, or average case running time? } bn-1

CS Algorithm Analysis38 Analysis Example To model best case running time, let c represent the time needed to only compare T[j]<minx and not make the assignments. To model worst case running time, let c represent the time needed to compare T[j]<minx and make the assignments. What do we do to develop a model for the average case complexity? c

CS Algorithm Analysis39 Analysis Example What is the order of growth for t(n)?

CS Algorithm Analysis40 Analysis Example What is the order of growth for t(n)? t(n) is in  (n 2 )! Interpret the model--what does this mean? Will this sorting routine always perform worse than a (fictitious) solution with worst case run time of order  (n)?