Computer Science 101 Efficiency and Complexity Analysis.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Efficiency of Algorithms
Lecture: Algorithmic complexity
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.
Fundamentals of Python: From First Programs Through Data Structures
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
Analysys & Complexity of Algorithms Big Oh Notation.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Efficiency of Algorithms
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
CS107 Introduction to Computer Science
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Cmpt-225 Algorithm Efficiency.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Elementary Data Structures and Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
1 Complexity Lecture Ref. Handout p
Program Performance & Asymptotic Notations CSE, POSTECH.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1.2. Comparing Algorithms. Learning outcomes Understand that algorithms can be compared by expressing their complexity as a function relative to the size.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
DR. Gatot F. Hertono, MSc. Design and Analysis of ALGORITHM (Session 2)
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Computer Science 101 A Survey of Computer Science Timing Problems.
3.3 Complexity of Algorithms
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis (Big O)
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
Searching Topics Sequential Search Binary Search.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Comp 245 Data Structures Analysis of Algorithms. Purpose A professional programmer must be able to determine how efficient his code is. Efficiency, as.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Analysis of Algorithms
Computer Science 112 Fundamentals of Programming II
Lesson Objectives Aims Understand the following: The big O notation.
Building Java Programs
Introduction to Data Structures
Chapter 3: The Efficiency of Algorithms
Analysys & Complexity of Algorithms
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Algorithmic Complexity
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
Presentation transcript:

Computer Science 101 Efficiency and Complexity Analysis

Things to Desire in an Algorithm Correctness Maintainability Efficiency

Measuring Efficiency Empirical - use clock to get actual running times for different inputs Problems: –Different machines have different running times –Some running times are so long that it is impractical to check them

Measuring Efficiency Analytical - use pencil and paper to determine the abstract amount of work that a program does for different inputs Advantages: –Machine independent –Can predict running times of programs that are impractical to run

Complexity Analysis Pick an instruction that will run most often in the code Determine the number of times this instruction will be executed as a function of the size of the input data Focus on abstract units of work, not actual running time

Example: Search for Largest We focus on the comparison ( > ) inside the loop and ignore the other instructions. for a list of length 1, zero comparisons for a list of length 2, one comparison. for list of length N, N - 1 comparisons set Largest 1 set Current to 2 while Current <= N do if A(Current) > A(Largest) then set Largest to Current increment Current

Big-O Notation Big-O notation expresses the amount of work a program does as a function of the size of the input O(n) stands for order of magnitude n, or order of n for short Search for the largest is O(n), where n is the size of the input

Common Orders of Magnitude ConstantO(k) LogarithmicO(log 2 n) LinearO(n) QuadraticO(n 2 ) ExponentialO(k n )

Common Orders of Magnitude n O(log 2 n) O(n) O(n 2 )O(2 n ) digits!

Recall that search for a value required exactly 3N + 3 steps in the worst case As N gets very large, the difference between N and N + K becomes negligible (where K is a constant) As N gets very large, the difference between N and N / K or N * K also becomes negligible Use the highest degree term in a polynomial and drop the others (N 2 – 2N + 2)  N 2 Approximations

Example Approximations n O(n) O(n) + 2 O(n 2 ) O(n 2 ) + n

Analysis of Selection Sort The main loop runs approximately N times Thus, the search for largest algorithm runs N times The loop within the ith run of search for largest runs N - i times

Analysis of Selection Sort Overall, the number of comparisons performed by the search for the largest value within selection sort is N N N = (N 2 – N)/2  N 2 Selection sort is a quadratic algorithm

Other Algorithms (Worst Cases) Search Search for largestO(?) Sequential search for a target valueO(?) Binary search for a target valueO(?) Sorting Bubble sortO(?) Quick sortO(?)