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

Similar documents
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, C++ Version, Third Edition

Chapter 3: 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

Analysis of Algorithms

Data Structures and Algorithms Sorting

Algorithm Analysis. Big Oh

4.4 Algorithm Design Technique: Randomization

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

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

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Searching, Sorting. Arizona State University 1

Problems, Algorithms, Programs

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

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

Data Structures and Algorithms

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

8. Binary Search Tree

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

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Intro to Algorithms. Professor Kevin Gold

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

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Brute Force: Selection Sort

Lecture 5: Running Time Evaluation

Basic Data Structures (Version 7) Name:

Measuring algorithm efficiency

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

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

PRAM Divide and Conquer Algorithms

B+ TREES Examples. B Trees are dynamic. That is, the height of the tree grows and contracts as records are added and deleted.

Module 2: Classical Algorithm Design Techniques

lecture notes September 2, How to sort?

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

Chapter 2: Complexity Analysis

Teach A level Compu/ng: Algorithms and Data Structures

Elementary maths for GMT. Algorithm analysis Part II

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Introduction to Algorithms

Sorting. Order in the court! sorting 1

Introduction to the Analysis of Algorithms. Algorithm

Shell s sort. CS61B Lecture #25. Sorting by Selection: Heapsort. Example of Shell s Sort

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

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

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

Sorting. Order in the court! sorting 1

COS 226 Midterm Exam, Spring 2009

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

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

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

Lecture 6 Sorting and Searching

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Solutions to relevant spring 2000 exam problems

Lecture 15: Algorithms. AP Computer Science Principles

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

Lecture 3: Sorting 1

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 12: Indexing and Hashing. Basic Concepts

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

COSC 311: ALGORITHMS HW1: SORTING

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

looking ahead to see the optimum

Memory Management. Memory Management

Chapter 12: Indexing and Hashing

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Transform & Conquer. Presorting

Lower bound for comparison-based sorting

Count Sort, Bucket Sort, Radix Sort. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

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

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

UNIT 1 ANALYSIS OF ALGORITHMS

Chapter 9 Graph Algorithms

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

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

CS:3330 (22c:31) Algorithms

9. Which situation is true regarding a cascading cut that produces c trees for a Fibonacci heap?

CSE 100 Advanced Data Structures

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Chapter 7 Sorting. Terminology. Selection Sort

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

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

DATA STRUCTURES/UNIT 3

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

QuickSort

Chapter 8 Algorithms 1

CSCI 104 Log Structured Merge Trees. Mark Redekopp

2. Sorting. 2.1 Introduction

CS301 - Data Structures Glossary By

Optimal Sequential Multi-Way Number Partitioning

CS 440 Theory of Algorithms /

6. Asymptotics: The Big-O and Other Notations

( D. Θ n. ( ) f n ( ) D. Ο%

Multi-Way Number Partitioning

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

CISC 1100: Structures of Computer Science

Lecture Summary CSC 263H. August 5, 2016

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Transcription:

Chapter 3 The Efficiency of Algorithms INVITATION TO 1 Computer Science

Objectives After studying this chapter, students will be able to: Describe algorithm attributes and why they are important Explain the purpose of efficiency analysis and apply it to new algorithms to determine the order of magnitude of their time efficiencies Describe, illustrate, and use the algorithms from the chapter, including: sequential and binary search, selection sort, data cleanup algorithms, pattern matching Invitation to Computer Science, 6th Edition 2

Objectives (continued) After studying this chapter, students will be able to: Explain which orders of magnitude grow faster or slower than others Describe what an intractable problem is, giving one or more examples, and the purpose of approximation algorithms that partially solve them Invitation to Computer Science, 6th Edition 3

Introduction Many solutions to any given problem How can we judge and compare algorithms? Metaphor: Purchasing a car ease of handling style fuel efficiency Evaluating an algorithm ease of understanding elegance time/space efficiency Invitation to Computer Science, 6th Edition 4

Attributes of Algorithms Attributes of interest: correctness, ease of understanding, elegance, and efficiency Correctness: Is the problem specified correctly? Does the algorithm produce the correct result? Example: pattern matching Problem spec: Given pattern p and text t, determine the location, if any, of pattern p occurring in text t Algorithm correct: does it always work? Invitation to Computer Science, 6th Edition 5

Attributes of Algorithms (continued) Ease of understanding, useful for: checking correctness program maintenance Elegance: using a clever or non-obvious approach Example: Gauss summing of 1 + 2 + + 100 Attributes may conflict: Elegance often conflicts with ease of understanding Attributes may reinforce each other: Ease of understanding supports correctness Invitation to Computer Science, 6th Edition 6

Attributes of Algorithms (continued) Efficiency: an algorithm s use of time and space resources Timing an algorithm is not always useful Confounding factors: machine speed, size of input Benchmarking: timing an algorithm on standard data sets Testing hardware and operating system, etc. Testing real-world performance limits Invitation to Computer Science, 6th Edition 7

Measuring Efficiency Sequential Search Analysis of algorithms: the study of the efficiency of algorithms Searching: the task of finding a specific value in a list of values, or deciding it is not there Sequential search algorithm (from Ch. 2): Given a target value and a random list of values, find the location of the target in the list, if it occurs, by checking each value in the list in turn Invitation to Computer Science, 6th Edition 8

Invitation to Computer Science, 6th Edition 9

Measuring Efficiency Sequential Search (continued) Central unit of work, operations most important for the task, and occurring frequently In sequential search, comparison of target NAME to each name in the list Given a big input list: Best case is smallest amount of work algorithm does Worst case is greatest amount of work algorithm does Average case depends on likelihood of different scenarios occurring Invitation to Computer Science, 6th Edition 10

Measuring Efficiency Sequential Search (continued) Best case: target found with the first comparison Worst case: target never found or last value Average case: if each value is equally likely to be searched, work done varies from 1 to n, averages to n/2 Invitation to Computer Science, 6th Edition 11

Measuring Efficiency Order of Magnitude Order n Order of magnitude n, Θ(n): the set of functions that grow in a linear fashion Invitation to Computer Science, 6th Edition 12

Measuring Efficiency Order of Magnitude Order n (continued) Change in growth as n increases is constant size Invitation to Computer Science, 6th Edition 13

Measuring Efficiency Selection Sort Sorting: The task of putting a list of values into numeric or alphabetical order Key idea: Pass repeatedly over the unsorted portion of the list Each pass select the largest remaining value Move that value to the end of the unsorted values Invitation to Computer Science, 6th Edition 14

Invitation to Computer Science, 6th Edition 15

Measuring Efficiency Selection Sort (continued) Example: Selection Sort on [5, 1, 3, 9, 4] Pass 1: Select 9 as the largest in the whole list Swap with 4 to place in last slot [5, 1, 3, 4, 9] Pass 2: Select 5 as the largest in the first four values Swap with 4 to place in last remaining slot [4, 1, 3, 5, 9] Invitation to Computer Science, 6th Edition 16

Measuring Efficiency Selection Sort (continued) Example: Selection Sort on [5, 1, 3, 9, 4] Pass 3: Select 4 as the largest in the first three Swap with 3 to place in last slot [3, 1, 4, 5, 9] Pass 4: Select 3 as the largest in the first two values Swap with 1 to place in last remaining slot [1, 3, 4, 5, 9] Invitation to Computer Science, 6th Edition 17

Measuring Efficiency Selection Sort (continued) Central unit of work: hidden in find largest step Work done to find largest changes as unsorted portion shrinks (n-1) + (n-2) + + 2 + 1 = n (n-1) / 2 Invitation to Computer Science, 6th Edition 18

Measuring Efficiency Order of Magnitude Order n 2 Order n 2, Θ(n 2 ): the set of functions whose growth is on the order of n 2 Invitation to Computer Science, 6th Edition 19

Measuring Efficiency Order of Magnitude Order n 2 (continued) Eventually, every function with order n 2 has greater values than any function with order n

Invitation to Computer Science, 6th Edition 21

Analysis of Algorithms Data Cleanup Algorithms Given a collection of age data, where erroneous zeros occur, find and remove all the zeros from the data, reporting the number of legitimate age values that remain Illustrates multiple solutions to a single problem Use of analysis to compare algorithms Invitation to Computer Science, 6th Edition 22

Analysis of Algorithms Data Cleanup Algorithms (continued) Shuffle-left algorithm: Search for zeros from left to right When a zero is found, shift all values to its right one cell to the left IS THIS RIGHT? Example: [55, 0, 32, 19, 0, 27] Finds 0 at position 2: [55, 32, 19, 0, 27, 27] Finds 0 at position 4: [55, 32, 19, 27, 27, 27] Invitation to Computer Science, 6th Edition 23

Invitation to Computer Science, 6th Edition 24

Analysis of Algorithms Data Cleanup Algorithms (continued) Analysis of shuffle-left for time efficiency Count comparisons looking for zero AND movements of values Best case: no zeros occur, check each value and nothing more: Θ(n) Worst case: every value is a zero, move n-1 values, then n-2 values, etc.: Θ(n 2 ) Analysis of shuffle-left for space efficiency Uses no significant space beyond input Invitation to Computer Science, 6th Edition 25

Analysis of Algorithms Data Cleanup Algorithms (continued) Copy-over algorithm: Create a second, initially empty, list Look at each value in the original If it is non-zero, copy it to the second list Example: [55, 0, 32, 19, 0, 27] 1. answer = [55] 4. answer = [55, 32, 19] 2. answer = [55] 5. answer = [55, 32, 19] 3. answer = [55, 32] 6. answer = [55, 32, 19, 27] Invitation to Computer Science, 6th Edition 26

Invitation to Computer Science, 6th Edition 27

Analysis of Algorithms Data Cleanup Algorithms (continued) Time efficiency for copy-over Best case: all zeros, checks each value but doesn t copy it: Θ(n) Worst case: no zeros, checks each value and copies it: Θ(n) Space efficiency for copy-over Best case: all zeros, uses no extra space Worst case: no zeros, uses n extra spaces Invitation to Computer Science, 6th Edition 28

Analysis of Algorithms Data Cleanup Algorithms (continued) Converging-pointers algorithm: Keep track of two pointers at the data Left pointer moves left to right and stops when it sees a zero value Right pointer stays put until a zero is found Then its value is copied on top of the zero, and it moves one cell to the left Stop when the left crosses the right Invitation to Computer Science, 6th Edition 29

Invitation to Computer Science, 6th Edition 30

Analysis of Algorithms Data Cleanup Algorithms (continued) Example: [55, 0, 32, 19, 0, 27] [55, 0, 32, 19, 0, 27] L R [55, 0, 32, 19, 0, 27] L R [55, 27, 32, 19, 0, 27] L R [55, 27, 32, 19, 0, 27] LR Invitation to Computer Science, 6th Edition 31

Analysis of Algorithms Data Cleanup Algorithms (continued) Time efficiency for converging-pointers Best case: no zeros, left pointer just moves across to pass the right pointers, examines each value: Θ(n) Worst case: all zeros, examines each value and copies a value over it, right pointer moves left towards left pointer: Θ(n) Space efficiency for converging-pointers No significant extra space needed Invitation to Computer Science, 6th Edition 32

Invitation to Computer Science, 6th Edition 33

Analysis of Algorithms Binary Search Binary Search Algorithm: Given a target value and an ordered list of values, find the location of the target in the list, if it occurs, by starting in the middle and splitting the range in two with each comparison Invitation to Computer Science, 6th Edition 34

Invitation to Computer Science, 6th Edition 35

Analysis of Algorithms Binary Search (continued) Example: target = 10, list = [1, 4, 5, 7, 10, 12, 14, 22] mid = 7, eliminate lower half: [1, 4, 5, 7, 10, 12, 14, 22] mid = 12, eliminate upper half: [1, 4, 5, 7, 10, 12, 14, 22] mid = 10, value found! Invitation to Computer Science, 6th Edition 36

Analysis of Algorithms Binary Search (continued) Central unit of work: comparisons against target Best case efficiency: Value happens to be the first middle value: 1 comparison Worst case efficiency: Value does not appear, repeats as many times as we can divide the list before running out of values: Θ(lg n) Invitation to Computer Science, 6th Edition 37

Analysis of Algorithms Binary Search (continued) Order of magnitude lg n, Θ(lg n), grows very slowly 38

Analysis of Algorithms Pattern Matching Algorithm from chapter 2 Best case: when first symbol of pattern does not appear in text Worst case: when all but last symbol of pattern make up the text Invitation to Computer Science, 6th Edition 39

40

Analysis of Algorithms Pattern Matching (continued) Best case example: pattern = xyz text = aaaaaaaaaaaaaaa At each step, compare x to a and then move on Θ(n) comparisons Worst case example: pattern = aab text = aaaaaaaaaaaaaaa At each step, compare m symbols from pattern against text before moving on Θ(mn) comparisons Invitation to Computer Science, 6th Edition 41

Invitation to Computer Science, 6th Edition 42

When Things Get Out of Hand Polynomially bounded: an algorithm that does work on the order of Θ(n k ) Most common problems are polynomially bounded Hamiltonian circuit is NOT Given a graph, find a path that passes through each vertex exactly once and returns to its starting point Invitation to Computer Science, 6th Edition 43

Invitation to Computer Science, 6th Edition 44

When Things Get Out of Hand (continued) Possible paths in the graph are paths through a tree of choices Most simple case has exactly two choices per vertex Number of paths to examine = number of leaves in the tree Height of the tree = n+1 (n is the number of vertices in the graph) Number of leaves = 2 n Invitation to Computer Science, 6th Edition 45

When Things Get Out of Hand (continued) Exponential algorithm: an algorithm whose order of growth is Θ(k n ) Intractable: problems with no polynomiallybounded solutions Hamiltonian circuit Traveling Salesperson Bin packing Chess Invitation to Computer Science, 6th Edition 46

47

Invitation to Computer Science, 6th Edition 48

Invitation to Computer Science, 6th Edition 49

When Things Get Out of Hand (continued) Approximation algorithms: algorithms that partially solve, or provide sub-optimal solutions to, intractable problems Example: bin packing For each box to be packed check each current bin if new box fits in the bin, place it there if no bin can hold the new box, add a new bin Invitation to Computer Science, 6th Edition 50

Summary We must evaluate the quality of algorithms, and compare competing algorithms to each other Attributes: correctness, efficiency, elegance, and ease of understanding Compare competing algorithms for time and space efficiency (time/space tradeoffs are common) Orders of magnitude capture work as a function of input size: Θ(lg n), Θ(n), Θ(n 2 ), Θ(2 n ) Problems with only exponential algorithms are intractable Invitation to Computer Science, 6th Edition 51