Section 1.7 Comparing Algorithms: Big-O Analysis.

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

Growth-rate Functions
MATH 224 – Discrete Mathematics
Fundamentals of Python: From First Programs Through Data Structures
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Complexity Analysis (Part I)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
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.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Chapter 1 Getting Organized. Chapter 1: Getting Organized 1.1 – Software Engineering 1.2 – Object Orientation 1.3 – Classes, Objects, and Applications.
CSC 211 Data Structures Lecture 13
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Algorithm analysis, searching and sorting  best vs. average vs. worst case analysis  big-Oh.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
3.3 Complexity of Algorithms
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Sorting.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Chapter 1 Getting Organized. Waterfall Life-Cycle Model.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CSC 427: Data Structures and Algorithm Analysis
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to complexity
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Sorting by Tammy Bailey
Building Java Programs
DS.A.1 Algorithm Analysis Chapter 2 Overview
8. Comparison of Algorithms
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:

Section 1.7 Comparing Algorithms: Big-O Analysis

1.7 Comparing Algorithms: Big-O Analysis There can be more than one algorithm to solve a problem.

Counting Operations To measure the complexity of an algorithm we attempt to count the number of basic operations required to complete the algorithm To make our count generally usable we express it as a function of the size of the problem.

Counting Operations Example if N = size of array the number of operations required is N + 3 But –too dependent on programming language and counting approach –difficult if problem/algorithm is more complicated problem: return true if sum of numbers in array is > 0, false otherwise Set sum to zero while more integers Set sum to sum + next int if sum > 0 Return true else Return false

Isolate a fundamental operation Rather than count all operations, select a fundamental operation, an operation that is performed “the most”, and count it. For example, if the problem is to sort the elements of an array, count the number of times one element is compared to another element, i.e., only count comparison operations when comparing sorting algorithms.

A further simplification: Big-O Notation We measure the complexity of an algorithm as the number of times a fundamental operation is performed, represented as a function of the size of the problem. For example, an algorithm performed on an N element array may require 2N 2 + 4N + 3 comparisons. Big-O notation expresses computing time (complexity) as the term in the function that increases most rapidly relative to the size of a problem. In our example, rather than saying the complexity is 2N 2 + 4N + 3 we say it is O(N 2 ). This works just as well for most purposes and simplifies the analysis and use of the complexity measure.

Common Orders of Magnitude O(1) is called bounded time. The amount of work is not dependent on the size of the problem. O(log 2 N) is called logarithmic time. Algorithms that successively cut the amount of data to be processed in half at each step typically fall into this category. O(N) is called linear time. Adding together the elements of an array is O(N). O(N log 2 N) is called N log N time. Good sorting algorithms, such as Quicksort, Heapsort, and Mergesort presented in Chapter 10, have N log N complexity. O(N 2 ) is called quadratic time. Some simple sorting algorithms are O(N 2 ) algorithms. O(2 N ) is called exponential time. These algorithms are extremely costly.

Comparison of Growth Rates Nlog 2 NNlog 2 NN2N2 N3N3 2N2N ,09665, ,096262,144 requires 20 digits ,3842,097,152 requires 39 digits 25682,04865,53616,777,216 requires 78 digits

Three Complexity Cases Best case complexity Related to the minimum number of steps required by an algorithm, given an ideal set of input values in terms of efficiency Average case complexity Related to the average number of steps required by an algorithm, calculated across all possible sets of input values Worst case complexity Related to the maximum number of steps required by an algorithm, given the worst possible set of input values in terms of efficiency To simplify analysis yet still provide a useful approach, we usually use worst case complexity

Ways to simplify analysis of algorithms Consider worst case only –but average case can also be important Count a fundamental operation –careful; make sure it is the most used operation within the algorithm Use Big-O complexity –especially when interested in “large” problems

Sum of Consecutive Integers Algorithm Sum1 sum = 0; for (count = 1; count <= n; count++) sum = sum + count; Sum1 is O(N) Algorithm Sum2 sum = ((n + 1) * n) / 2; Sum2 is O(1)

Finding a Number in a Phone Book Algorithm Lookup1 Check first name While (unsuccessful) Check the next name Lookup1 is O(N) Algorithm Lookup2 Search area = entire book Check middle name in search area While (unsuccessful) If middle name > target name Search area = first half of search area Else Search area = second half of search area Check middle name in search area Lookup2 is O(log 2 N)