Chapter 3: The Efficiency of Algorithms Invitation to Computer Science,

Similar documents
Chapter 3: The Efficiency of Algorithms

Chapter 3: The Efficiency of Algorithms. Invitation to Computer Science, C++ Version, Third Edition

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

ANALYSIS OF ALGORITHMS. ANALYSIS OF ALGORITHMS IB DP Computer science Standard Level ICS3U

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Intro to Algorithms. Professor Kevin Gold

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

CSCI 104 Log Structured Merge Trees. Mark Redekopp

PRAM Divide and Conquer Algorithms

Introduction to the Analysis of Algorithms. Algorithm

CS 103 Unit 8b Slides

Questions. 6. Suppose we were to define a hash code on strings s by:

Lecture 5: Running Time Evaluation

Lecture 2: Getting Started

Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Algorithm Analysis. Big Oh

ECE 2574: Data Structures and Algorithms - Big-O Notation. C. L. Wyatt

CS Data Structures and Algorithm Analysis

Sorting. Bubble Sort. Selection Sort

Analysis of Algorithms

4.4 Algorithm Design Technique: Randomization

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Lecture 3: Sorting 1

Theory and Algorithms Introduction: insertion sort, merge sort

Introduction to Algorithms 6.046J/18.401J

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Recall from Last Time: Big-Oh Notation

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Data Structures and Algorithms CSE 465

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Introduction to Computers & Programming

Introduction to Algorithms 6.046J/18.401J/SMA5503

O(n): printing a list of n items to the screen, looking at each item once.

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

COSC 311: ALGORITHMS HW1: SORTING

Data Structures and Algorithms Sorting

CSI33 Data Structures

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Chapter 2: Complexity Analysis

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Scientific Computing. Algorithm Analysis

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

(6-2) Efficiency of Algorithms D & D Chapter 20. Instructor - Andrew S. O Fallon CptS 122 (July 11, 2018) Washington State University

Computational Complexity: Measuring the Efficiency of Algorithms

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Collection of priority-job pairs; priorities are comparable.

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

CS 103 Lecture 4 Slides

Lecture 6 Sorting and Searching

UNIT 1 ANALYSIS OF ALGORITHMS

Basic Data Structures (Version 7) Name:

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

Computer Science 1000: Part #2. Algorithms

Parallel Convex Hull using MPI. CSE633 Fall 2012 Alex Vertlieb

ASYMPTOTIC COMPLEXITY

Algorithm Analysis Part 2. Complexity Analysis

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Algorithms and Data Structures

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Lecture 8: Mergesort / Quicksort Steven Skiena

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithm Analysis and Design

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Sorting. Order in the court! sorting 1

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Computer Algorithms. Introduction to Algorithm

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CS:3330 (22c:31) Algorithms

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Lecture 8 13 March, 2012

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Introduction to Algorithms

Chapter 7 Sorting. Terminology. Selection Sort

Lecture Notes for Chapter 2: Getting Started

Introduction to Algorithms 6.046J/18.401J

Computational biology course IST 2015/2016

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Transcription:

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science,

Objectives In this chapter, you will learn about Attributes of algorithms Measuring efficiency Analysis of algorithms When things get out of hand 2

Objectives In this chapter, you will learn about Attributes of algorithms Measuring efficiency Analysis of algorithms When things get out of hand 3

Introduction Desirable characteristics in an algorithm Correctness Ease of understanding (clarity) Elegance Efficiency Invitation to Computer Science, 5th Edition 4

Attributes of Algorithms Correctness Does the algorithm solve the problem it is designed for? Does the algorithm solve the problem correctly? Ease of understanding (clarity) How easy is it to understand or alter the algorithm? Important for program maintenance 5

Attributes of Algorithms (continued) Elegance How clever or sophisticated is the algorithm? Sometimes elegance and ease of understanding work at cross-purposes Efficiency How much time and/or space does the algorithm require when executed? Extremely desirable attribute of an algorithm 6

Measuring Efficiency Analysis of algorithms Study of the efficiency of various algorithms Efficiency measured as a 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 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 8

Figure 3.1 Sequential Search Algorithm 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) 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) 11

Sequential Search (continued) Space efficiency Uses essentially no more memory storage than original input requires Very space efficient 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 13

Figure 3.4 Work = cn for Various Values of c 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 15

Figure 3.6 Selection Sort Algorithm 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) + + 2 + 1 = n(n-1)/2 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 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 ) 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 ) 20

Figure 3.10 Work = cn 2 for Various Values of c 21

Figure 3.11 A Comparison of n and n 2 22

Analysis of Algorithms Multiple algorithms for one task may be compared for efficiency and other desirable attributes Data cleanup problem Search problem Pattern matching 23

Data Cleanup Algorithms Given a collection of numbers, find and remove all zeros Possible algorithms Shuffle-left Copy-over Converging-pointers 24

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 25

Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup 26

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 -n shifts plus n examinations Θ(n 2 ) 27

The Shuffle-Left Algorithm (continued) Space efficiency A constant number of extra memory locations is needed Space efficient 28

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) 29

Figure 3.15 The Copy-Over Algorithm for Data Cleanup 30

The Copy-Over Algorithm (continued) Time efficiency (continued) Worst case No zeros n examinations and n copies Θ(n) Space efficiency A lot of extra memory space is required Less space efficient 31

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 32

The Converging-Pointers Algorithm If a 0 is encountered, Copy values from right to the left until pointers converge in the middle Time efficiency Count examinations and copies Best case n examinations, no copies Θ(n) 33

Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup 34

The Converging-Pointers Algorithm (continued) Time efficiency (continued) Worst case n examinations, n-1 copies Θ(n) Space efficiency Space efficient 35

Binary Search Algorithm 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 36

Figure 3.18 Binary Search Algorithm (list must be sorted) 37

Binary Search Algorithm (continued) Efficiency Best case 1 comparison Θ(1) Worst case lg n comparisons lg n: The number of times n can be divided by two before reaching 1 Θ(lg n) 38

Binary Search Algorithm (continued) Tradeoff Sequential search Slower, but works on unordered data Binary search Faster (much faster), but data must be sorted first 39

Figure 3.21 A Comparison of n and lg n 40

Pattern-Matching Algorithm 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 41

Pattern-Matching Algorithm (continued) Efficiency Best case Pattern does not match at all n - m + 1 comparisons Θ(n) Worst case Pattern almost matches at each point m (n - m + 1) comparisons Θ(m x n) 42

Figure 3.22 Order-of-Magnitude Time Efficiency Summary 43

When Things Get Out of Hand Θ(lgn), Θ(n), Θ(n 2 ) algorithms Work done is no worse than a constant multiple of n 2 Polynomially bounded algorithms Intractable algorithms Run in worse than polynomial time Examples (suspected) Hamiltonian circuit Bin-packing 44

When Things Get Out of Hand (continued) Exponential algorithm Θ(2 n ) More work than any polynomial in n Approximation algorithms Run in polynomial time Provide a close approximation to a solution 45

Figure 3.25 Comparisons of lg n, n, n 2, and 2 n 46

Figure 3.27 A Comparison of Four Orders of Magnitude 47

Summary of Level 1 Level 1 (Chapters 2 and 3) explored algorithms Chapter 2 Pseudocode Sequential, conditional, and iterative operations Algorithmic solutions to various practical problems Chapter 3 Desirable properties for algorithms Time and space efficiencies of a number of algorithms 48

Summary Desirable attributes in algorithms Correctness Ease of understanding (clarity) Elegance Efficiency Efficiency an algorithm s careful use of resources is extremely important 49

Summary (continued) 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 50