Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.

Slides:



Advertisements
Similar presentations
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Advertisements

Efficiency of Algorithms
The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
The Efficiency of Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
CS4413 Divide-and-Conquer
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Efficiency of Algorithms
CS107 Introduction to Computer Science
Chapter 3 The Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Algorithms and Efficiency of Algorithms February 4th.
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Efficiency of Algorithms Csci 107 Lecture 6. Last time –Algorithm for pattern matching –Efficiency of algorithms Today –Efficiency of sequential search.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Elementary Data Structures and Algorithms
Chapter 3: The Efficiency of Algorithms
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
BIT Presentation 4.  An algorithm is a method for solving a class of problems.  While computer scientists think a lot about algorithms, the term.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
1 Chapter 3: Efficiency of Algorithms Quality attributes for algorithms Correctness: It should do things right No flaws in design of the algorithm Maintainability.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 12 Recursion, Complexity, and Searching and Sorting
Chapter 19: Searching and Sorting Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
CSC 211 Data Structures Lecture 13
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
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.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Searching algorithms with simple arrays
Introduction to Search Algorithms
Sorting by Tammy Bailey
Algorithm design and Analysis
Chapter 3: The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Invitation to Computer Science 5th Edition
Presentation transcript:

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05

Invitation to Computer Science, C++ Version, Third Edition 2 Objectives In this chapter, you will learn about: Attributes of algorithms Measuring efficiency Analysis of algorithms When things get out of hand

Invitation to Computer Science, C++ Version, Third Edition 3 Introduction Desirable characteristics in an algorithm  Correctness  Ease of understanding  Elegance  Efficiency

Invitation to Computer Science, C++ Version, Third Edition 4 Attributes of Algorithms Correctness  Does the algorithm solve the problem it is designed for?  Does the algorithm solve the problem correctly? First, make it correct! Ease of understanding  How easy is it to understand or alter an algorithm?  Important for program maintenance

Invitation to Computer Science, C++ Version, Third Edition 5 Attributes of Algorithms (continued) Elegance  How clever or sophisticated is an algorithm?  Sometimes elegance and ease of understanding work at cross-purposes Efficiency  How much time and/or space does an algorithm require when executed?  Perhaps the most important desirable attribute

Invitation to Computer Science, C++ Version, Third Edition 6 Measuring Efficiency Analysis of algorithms  Study of the efficiency of various algorithms Efficiency measured as function relating size of input to time or space used For one input size, best case, worst case, and average case behavior must be considered The  notation captures the order of magnitude of the efficiency function

Invitation to Computer Science, C++ Version, Third Edition 7 Sequential Search Search for NAME among a list of n names Start at the beginning and compare NAME to each entry until a match is found

Invitation to Computer Science, C++ Version, Third Edition 8 Figure 3.1 Sequential Search Algorithm

Invitation to Computer Science, C++ Version, Third Edition 9 Sequential Search (continued) Comparison of the NAME being searched for against a name in the list  Central unit of work  Used for efficiency analysis For lists with n entries:  Best case NAME is the first name in the list 1 comparison  (1)

Invitation to Computer Science, C++ Version, Third Edition 10 Sequential Search (continued) For lists with n entries:  Worst case NAME is the last name in the list NAME is not in the list n comparisons  (n)  Average case Roughly n/2 comparisons  (n)

Invitation to Computer Science, C++ Version, Third Edition 11 Sequential Search (continued) Space efficiency  Uses essentially no more memory storage than original input requires  Very space-efficient

Invitation to Computer Science, C++ Version, Third Edition 12 Order of Magnitude: Order n As n grows large, order of magnitude dominates running time, minimizing effect of coefficients and lower-order terms All functions that have a linear shape are considered equivalent Order of magnitude n  Written  (n)  Functions vary as a constant times n

Invitation to Computer Science, C++ Version, Third Edition 13 Figure 3.4 Work = cn for Various Values of c

Invitation to Computer Science, C++ Version, Third Edition 14 Selection Sort Sorting  Take a sequence of n values and rearrange them into order Selection sort algorithm  Repeatedly searches for the largest value in a section of the data Moves that value into its correct position in a sorted section of the list  Uses the Find Largest algorithm

Invitation to Computer Science, C++ Version, Third Edition 15 Figure 3.6 Selection Sort Algorithm

Invitation to Computer Science, C++ Version, Third Edition 16 Selection Sort (continued) Count comparisons of largest so far against other values Find Largest, given m values, does m-1 comparisons Selection sort calls Find Largest n times,  Each time with a smaller list of values  Cost = n-1 + (n-2) + … = n(n-1)/2

Invitation to Computer Science, C++ Version, Third Edition 17 Selection Sort (continued) Time efficiency  Comparisons: n(n-1)/2  Exchanges: n (swapping largest into place)  Overall:  (n 2 ), best and worst cases Space efficiency  Space for the input sequence, plus a constant number of local variables

Invitation to Computer Science, C++ Version, Third Edition 18 Order of Magnitude – Order n 2 All functions with highest-order term cn 2 have similar shape An algorithm that does cn 2 work for any constant c is order of magnitude n 2, or  (n 2 )

Invitation to Computer Science, C++ Version, Third Edition 19 Order of Magnitude – Order n 2 (continued) Anything that is  (n 2 ) will eventually have larger values than anything that is  (n), no matter what the constants are An algorithm that runs in time  (n) will outperform one that runs in  (n 2 )

Invitation to Computer Science, C++ Version, Third Edition 20 Figure 3.10 Work = cn 2 for Various Values of c

Invitation to Computer Science, C++ Version, Third Edition 21 Figure 3.11 A Comparison of n and n 2

Invitation to Computer Science, C++ Version, Third Edition 22 Comparision of two extreme O(n 2 ) and O(n) algorithms Figure 3.13

Invitation to Computer Science, C++ Version, Third Edition 23 Analysis of Algorithms Multiple algorithms for one task may be compared for efficiency and other desirable attributes Data cleanup problem Search problem Pattern matching

Invitation to Computer Science, C++ Version, Third Edition 24 Data Cleanup Algorithms Given a collection of numbers, find and remove all zeros Possible algorithms  Shuffle-left  Copy-over  Converging-pointers

Invitation to Computer Science, C++ Version, Third Edition 25 The Shuffle-Left Algorithm Scan list from left to right  When a zero is found, shift all values to its right one slot to the left

Invitation to Computer Science, C++ Version, Third Edition 26 Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup

Invitation to Computer Science, C++ Version, Third Edition 27 The Shuffle-Left Algorithm (continued) Time efficiency  Count examinations of list values and shifts  Best case No shifts, n examinations  (n)  Worst case Shift at each pass, n passes n 2 shifts plus n examinations  (n 2 )

Invitation to Computer Science, C++ Version, Third Edition 28 The Shuffle-Left Algorithm (continued) Space efficiency  n slots for n values, plus a few local variables   (n)

Invitation to Computer Science, C++ Version, Third Edition 29 The Copy-Over Algorithm Use a second list  Copy over each nonzero element in turn Time efficiency  Count examinations and copies  Best case All zeros n examinations and 0 copies  (n)

Invitation to Computer Science, C++ Version, Third Edition 30 Figure 3.15 The Copy-Over Algorithm for Data Cleanup

Invitation to Computer Science, C++ Version, Third Edition 31 The Copy-Over Algorithm (continued) Time efficiency (continued)  Worst case No zeros n examinations and n copies  (n) Space efficiency  2n slots for n values, plus a few extraneous variables

Invitation to Computer Science, C++ Version, Third Edition 32 The Copy-Over Algorithm (continued) Time/space tradeoff  Algorithms that solve the same problem offer a tradeoff: One algorithm uses more time and less memory Its alternative uses less time and more memory

Invitation to Computer Science, C++ Version, Third Edition 33 The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle Time efficiency  Count examinations and swaps  Best case n examinations, no swaps  (n)

Invitation to Computer Science, C++ Version, Third Edition 34 Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup

Invitation to Computer Science, C++ Version, Third Edition 35 The Converging-Pointers Algorithm (continued) Time efficiency (continued)  Worst case n examinations, n swaps  (n) Space efficiency  n slots for the values, plus a few extra variables

Invitation to Computer Science, C++ Version, Third Edition 36 Figure 3.17 Analysis of Three Data Cleanup Algorithms

Invitation to Computer Science, C++ Version, Third Edition 37 Binary Search Given ordered data,  Search for NAME by comparing to middle element  If not a match, restrict search to either lower or upper half only  Each pass eliminates half the data

Invitation to Computer Science, C++ Version, Third Edition 38 Figure 3.18 Binary Search Algorithm (list must be sorted)

Invitation to Computer Science, C++ Version, Third Edition 39 Binary Search (continued) Efficiency  Best case 1 comparison  (1)  Worst case lg n comparisons  lg n: The number of times n may be divided by two before reaching 1  (lg n)

Invitation to Computer Science, C++ Version, Third Edition 40 Binary Search (continued) Tradeoff  Sequential search Slower, but works on unordered data  Binary search Faster (much faster), but data must be sorted first

Invitation to Computer Science, C++ Version, Third Edition 41 Figure 3.21 A Comparison of n and lg n

Invitation to Computer Science, C++ Version, Third Edition 42 Pattern Matching Analysis involves two measures of input size  m: length of pattern string  n: length of text string Unit of work  Comparison of a pattern character with a text character

Invitation to Computer Science, C++ Version, Third Edition 43 Pattern Matching (continued) Efficiency  Best case Pattern does not match at all n - m + 1 comparisons  (n)  Worst case Pattern almost matches at each point (m -1)(n - m + 1) comparisons  (m x n)

Invitation to Computer Science, C++ Version, Third Edition 44 Figure 3.22 Order-of-Magnitude Time Efficiency Summary

Invitation to Computer Science, C++ Version, Third Edition 45 When Things Get Out of Hand Polynomially bound algorithms  Work done is no worse than a constant multiple of n 2 Intractable algorithms  Run in worse than polynomial time  Examples Hamiltonian circuit Bin-packing

Invitation to Computer Science, C++ Version, Third Edition 46 When Things Get Out of Hand (continued) Exponential algorithm   (2 n )  More work than any polynomial in n Approximation algorithms  Run in polynomial time but do not give optimal solutions

Invitation to Computer Science, C++ Version, Third Edition 47 Figure 3.25 Comparisons of lg n, n, n 2, and 2 n

Invitation to Computer Science, C++ Version, Third Edition 48 Figure 3.27 A Comparison of Four Orders of Magnitude

Invitation to Computer Science, C++ Version, Third Edition 49 Summary of Level 1 Level 1 (Chapters 2 and 3) explored algorithms  Chapter 2 Pseudocode Sequential, conditional, and iterative operations Algorithmic solutions to three practical problems  Chapter 3 Desirable properties for algorithms Time and space efficiencies of a number of algorithms

Invitation to Computer Science, C++ Version, Third Edition 50 Summary Desirable attributes in algorithms:  Correctness  Ease of understanding  Elegance  Efficiency Efficiency – an algorithm’s careful use of resources – is extremely important

Invitation to Computer Science, C++ Version, Third Edition 51 Summary To compare the efficiency of two algorithms that do the same task  Consider the number of steps each algorithm requires Efficiency focuses on order of magnitude