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

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,
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9: Searching, Sorting, and Algorithm Analysis
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
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, Third Edition Additions by Shannon Steinfadt SP’05.
Chapter 2 The Algorithmic Foundations of Computer Science
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 3: The Efficiency of Algorithms
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.
Selection Sort, Insertion Sort, Bubble, & Shellsort
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.
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:
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
{ 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.
Chapter 19 Searching, Sorting and Big O
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 12 Recursion, Complexity, and Searching and Sorting
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Department of Computer Science 1 COSC 2320 Section Room 104 AH 5:30 – 7:00 PM TTh Professor Olin Johnson Office 596 PGH
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.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
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.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
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.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Introduction to Search Algorithms
Introduction to complexity
Efficiency (Chapter 2).
Chapter 3: The Efficiency of Algorithms
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
Chapter 3: The Efficiency of Algorithms
Divide and Conquer Algorithms Part I
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