The Efficiency of Algorithms

Slides:



Advertisements
Similar presentations
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Analysys & Complexity of Algorithms Big Oh Notation.
Introduction to Analysis of Algorithms
The Efficiency of Algorithms
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
The Efficiency of Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm Analysis (Big O)
Program Performance & Asymptotic Notations CSE, POSTECH.
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:
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
© 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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis (Big O)
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
The Efficiency of Algorithms Chapter 9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
Chapter 2 Algorithm Analysis
Mathematical Foundation
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
Introduction to Search Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to complexity
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Data Structures Using The Big-O Notation 1.
Algorithm Analysis CSE 2011 Winter September 2018.
DATA STRUCTURES Introduction: Basic Concepts and Notations
Complexity Analysis.
CS 3343: Analysis of Algorithms
Computation.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Algorithms Analysis
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
Chapter 2.
The Efficiency of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
CSC 427: Data Structures and Algorithm Analysis
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
The Efficiency of Algorithms
Design and Analysis of Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
The Efficiency of Algorithms
Analysis of Algorithms
Algorithm Efficiency and Sorting
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Presentation transcript:

The Efficiency of Algorithms Chapter 4 Adapted from Pearson Education, Inc.

Contents Motivation Measuring an Algorithm’s Efficiency Counting Basic Operations Best, Worst, and Average Cases Big Oh Notation The Complexities of Program Constructs Picturing Efficiency The Efficiency of Implementations of the ADT Bag An Array-Based Implementation A Linked Implementation Comparing the Implementations Adapted from Pearson Education, Inc.

Objectives Assess efficiency of given algorithm Compare expected execution times of two methods Given efficiencies of algorithms Adapted from Pearson Education, Inc.

Motivation - Even a simple program can be inefficient Three algorithms for computing ∑ i Trace each with n=5 and time yourself Trace each with n=10 and time yourself What do you notice after doubling n? When doubling n The time remained fixed When doubling n The time was doubled When doubling n The time was quadrupled Adapted from Pearson Education, Inc.

Measuring an Algorithm’s Efficiency Complexity Space requirements Time requirements Other issues for best solution Generality of algorithm Programming effort Problem size – number of items program will handle Growth-rate function Adapted from Pearson Education, Inc.

Counting Basic Operations Linearly related to n Quadratically related to n Independent of n Adapted from Pearson Education, Inc.

Counting Basic Operations Linearly related to n Quadratically related to n Independent of n Adapted from Pearson Education, Inc.

(a) Typical growth-rate functions – (b) Effect of doubling the size of n – (c) An example time to process 106 items @ 1MIPS (b) (c) Adapted from Pearson Education, Inc.

Best, Worst, and Average Cases Some algorithms depend only on size of data set Other algorithms depend on nature of the data Best case search when item at beginning Worst case when item at end Average case somewhere between Adapted from Pearson Education, Inc.

Big Oh Notation A function f(n) is of order at most g(n) that is, We can say f(n) = (n2+n)/2 is of order g(n) = n2 f(n) has a Big Oh of n2  O(n2) This means, with c=1 and N=1 (n2+n)/2 ≤ 1∙ n2 for all n ≥ 1 A function f(n) is of order at most g(n) that is, f(n) is O(g(n)) — if : A positive real number c and positive integer N exist such that f(n) ≤ c ∙ g(n) for all n ≥ N. That is, c ∙ g(n) is an upper bound on f(n) when n is sufficiently large. Adapted from Pearson Education, Inc.

Big Oh Identities O(k g(n)) = O(g(n)) for a constant k O(g1(n)) + O(g2(n)) = O(g1(n) + g2(n)) O(g1(n)) x O(g2(n)) = O(g1(n) x g2(n)) O ( g1(n) + g2(n) + . . . + gm(n) ) = O ( max[ g1(n), g2(n), . . ., gm(n) ] ) = max [ O(g1(n)), O(g2(n)), . . . , O(gm(n)) ] Adapted from Pearson Education, Inc.

Big Oh Identities O(k g(n)) = O(g(n)) for a constant k O(g1(n)) + O(g2(n)) = O(g1(n) + g2(n)) O(g1(n)) x O(g2(n)) = O( g1(n) x g2(n) ) O ( g1(n) + g2(n) + . . . + gm(n) ) = O (max[ g1(n), g2(n), . . ., gm(n) ] ) = max[O(g1(n)), O(g2(n)), . . . , O(gm(n)) ] O(3 g(n)) = O(g(n)) O(n) + O(3n2) = O(n + 3n2) O(n) x O(3n2) = O(n x 3n2) O (n + 3n2 + 5logn+n ) = O ( max[ n, 3n2, 5logn+n ] ) = max [ O(n), O(3n2), O(5logn+n) ] = O(n+n2) = O( max(n, n2)) = O(n2) = O(3n3) = O(n3) = O(2n+3n3+5logn) = max(O(n),O(n3),O(logn)) = O(n3) Adapted from Pearson Education, Inc.

Figure 4-6 An O(n) algorithm Adapted from Pearson Education, Inc.

Figure 4-7 An O(n2) algorithm Adapted from Pearson Education, Inc.

Figure 4-8 Another O(n2) algorithm Adapted from Pearson Education, Inc.

Complexities of program constructs Time Complexity Consecutive segments If that choses between if- part and else-part A loop that iterates m times Add growth-rates  max O(cond) + max(O(if-part),O(else-part)) m * (O(cond) + O(body)) Adapted from Pearson Education, Inc.

Array Based Implementation (fixed size) Adding an entry to a bag, an O(1) method, Adapted from Pearson Education, Inc.

Array Based Implementation (fixed size) Adding an entry to a bag, an O(1) method, O(1) O(1) +O(1) = O(1) O(1) Max(O(1),O(1)) = O(1) O(1) O(1) O(1) O(1) +O(1) +O(1) = O(1) Adapted from Pearson Education, Inc.

Array Based Implementation Searching for an entry, O(1) best case, O(n) worst or average case  O(n) method overall Adapted from Pearson Education, Inc.

Array Based Implementation Searching for an entry, O(1) best case, O(n) worst or average case  O(n) method overall The size of the bag, is the size of the problem  numberOfEntries is our n This loop executes numberOfEntries times, i.e. n times O(1) n * (O(1) + O(1)) = n * O(1) = O(n) O(1) O(1) +O(1) =O(1) O(1) O(1) O(1) O(1) +O(n) +O(1) = O(n) Adapted from Pearson Education, Inc.

A Linked Implementation Adding an entry to a bag, an O(1) method, O(1) +O(1) = O(1) Adapted from Pearson Education, Inc.

A Linked Implementation Searching a bag for a given entry, O(1) best case, O(n) worst case  O(n) overall Adapted from Pearson Education, Inc.

A Linked Implementation Searching a bag for a given entry, O(1) best case, O(n) worst case  O(n) overall The size of the bag, is the size of the problem This loop executes n times O(1) n * (O(1)+O(1)) = n * O(1) = O(n) O(1) O(1) O(1) O(1) +O(n) +O(1) = O(n) Adapted from Pearson Education, Inc.

Time efficiencies of the ADT bag operations Adapted from Pearson Education, Inc.

End Chapter 4 Adapted from Pearson Education, Inc.