Chapter 3 The Efficiency of Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CSE Lecture 3 – Algorithms I
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
The Efficiency of Algorithms Chapter 3 CS OUR NEXT QUESTION IS: "How do we know we have a good algorithm?" In the lab session, you will explore.
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
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 Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
CHAPTER 3 CS OUR NEXT QUESTION IS: "How do we know we have a good algorithm?" In the lab session, you will explore algorithms that are related.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Algorithms and Efficiency of Algorithms February 4th.
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.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Chapter 3: The Efficiency of Algorithms
CMPUT101 Introduction to Computing(c) Yngvi Bjornsson & Jia You1 The Efficiency of Algorithms Chapter 3 Topics: Attributes of Algorithms A Choice of Algorithms.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
BIT Presentation 4.  An algorithm is a method for solving a class of problems.  While computer scientists think a lot about algorithms, the term.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
1 Chapter 3: Efficiency of Algorithms Quality attributes for algorithms Correctness: It should do things right No flaws in design of the algorithm Maintainability.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Analysis of 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.
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
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
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
3.3 Complexity of Algorithms
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Computer Science/Ch. Algorithmic Foundation of CS 4-1 Chapter 4 Chapter 4 Algorithmic Foundation of Computer Science.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
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.
Theory of Computation IS 101Y/CMSC 101Y November 18, 2014 Marie desJardins University of Maryland Baltimore County 1.
Chapter 16: Searching, Sorting, and the vector Type.
CMPT 438 Algorithms.
Introduction to complexity
Teach A level Computing: Algorithms and Data Structures
The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
Chapter 3: The Efficiency of Algorithms
3. Brute Force Selection sort Brute-Force string matching
Algorithm Efficiency and Sorting
3. Brute Force Selection sort Brute-Force string matching
Algorithm Efficiency and Sorting
Invitation to Computer Science 5th Edition
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Chapter 3 The Efficiency of Algorithms Algorithmic problem solving Chapter 3 The Efficiency of Algorithms

Attributes of Algorithms Are some algorithms better than others? We expect correctness from our algorithms Ease of understanding; Elegance Analysis of Algorithms Efficiency Term used to describe an algorithm’s careful use of resources Benchmarks Useful for rating one machine against another and for rating how sensitive a particular algorithm is with respect to variations in input on one particular machine

Invitation to Computer Science, 5th Edition Measuring Efficiency Analysis of algorithms The study of the efficiency of algorithms An important part of computer science Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition 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, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.1 Sequential Search Algorithm Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.2 Number of Comparisons to Find NAME in a List of n Names Using Sequential Search Invitation to Computer Science, 5th Edition

Order of Magnitude - Order n Order of magnitude n Anything that varies as a constant times n (and whose graph follows the basic shape of n) Sequential search An Θ(n) algorithm in both the worst case and the average case Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.3 Work = 2n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.4 Work = cn for Various Values of c Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.5 Growth of Work = cn for Various Values of c Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Selection Sort Selection sort algorithm Sorts in ascending order Subtask within selection sort Task of finding the largest number in a list When selection sort algorithm begins The largest-so-far value must be compared to all the other numbers in the list If there are n numbers in the list, n – 1 comparisons must be done Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.6 Selection Sort Algorithm Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.7 Comparisons Required by Selection Sort Invitation to Computer Science, 5th Edition

Selection Sort (continued) Selection sort algorithm Does n exchanges, one for each position in the list to put the correct value in that position Space efficiency of the selection sort Original list occupies n memory locations Storage is needed for: The marker between the unsorted and sorted sections Keeping track of the largest-so-far value and its location in the list Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.8 An Attempt to Exchange the Values at X and Y Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.9 Exchanging the Values at X and Y Invitation to Computer Science, 5th Edition

Order of Magnitude - Order n2 Order of magnitude n2, or Θ(n2) An algorithm that does cn2 work for any constant c Selection sort An Θ(n2) algorithm (in all cases) Sequential search An Θ(n) algorithm (in the worst case) Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.10 Work 5 cn2 for Various Values of c Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.11 A Comparison of n and n2 Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.12 For Large Enough n, 0.25n2 Has Larger Values Than 10n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.13 A Comparison of Two Extreme Q(n2) and Q(n) Algorithms Invitation to Computer Science, 5th Edition

Analysis of Algorithms Data cleanup algorithms The Shuffle-Left Algorithm The Copy-Over Algorithm The Converging-Pointers Algorithm Invitation to Computer Science, 5th Edition

The Shuffle-Left Algorithm Scans list from left to right When a zero is found, copy each remaining data item in the list one cell to the left Value of legit Originally set to the length of the list Is reduced by 1 every time a 0 is encountered Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup Invitation to Computer Science, 5th Edition

The Shuffle-Left Algorithm (continued) To analyze time efficiency: Identify the fundamental units of work the algorithm performs Copying numbers Best case occurs when the list has no 0 values because no copying is required Worst case occurs when the list has all 0 values Invitation to Computer Science, 5th Edition

The Shuffle-Left Algorithm (continued) Worst case Occurs when the list has all 0 values An Θ(n2) algorithm Space-efficient Only requires four memory locations to store the quantities n, legit, left, and right Invitation to Computer Science, 5th Edition

The Copy-Over Algorithm Scans the list from left to right, copying every legitimate (non-zero) value into a new list that it creates Every list entry is examined to see whether it is 0 Every non-zero list entry is copied once Best case Occurs if all elements are 0 Θ(n) in time efficiency No extra space is used

Invitation to Computer Science, 5th Edition Figure 3.15 The Copy-Over Algorithm for Data Cleanup Invitation to Computer Science, 5th Edition

The Copy-Over Algorithm (continued) Worst case Occurs if there are no 0 values in the list Algorithm copies all n non-zero elements into the new list and doubles the space required Θ(n) in time efficiency Time/space tradeoff You gain something by giving up something else Invitation to Computer Science, 5th Edition

The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle Best case A list containing no 0 elements Worst case A list of all 0 entries Θ(n) in time efficiency Is space-efficient Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.17 Analysis of Three Data Cleanup Algorithms Invitation to Computer Science, 5th Edition

The Converging-Pointers Algorithm (continued) In an Θ(n) algorithm: The work is proportional to n In an Θ(n2) algorithm: The work is proportional to the square of n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Binary Search Procedure First looks for NAME at roughly the halfway point in list If name equals NAME, search is over If NAME comes alphabetically before name at halfway point, search is narrowed to the front half of list If NAME comes alphabetically after name at halfway point, search is narrowed to the back half of the list Algorithm halts when NAME is found Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.18 Binary Search Algorithm (list must be sorted) Invitation to Computer Science, 5th Edition

Binary Search (continued) Logarithm of n to the base 2 Number of times a number n can be cut in half and not go below 1 As n doubles: lg n increases by only 1, so lg n grows much more slowly than n Worst case and average case Θ(lg n) comparisons Works only on a list that has already been sorted

Invitation to Computer Science, 5th Edition Figure 3.19 Binary Search Tree for a 7-Element List Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.20 Values for n and lg n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.21 A Comparison of n and lg n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Pattern-Matching Usually involves a pattern length that is short compared to the text length That is, when m is much less than n Best case Θ(n) Worst case Θ(m * n) Invitation to Computer Science, 5th Edition

When Things Get Out of Hand Polynomially bound algorithms Work done is no worse than a constant multiple of n2 Graph A collection of nodes and connecting edges Hamiltonian circuit A path through a graph that begins and ends at the same node and goes through all other nodes exactly once Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.22 Order-of-Magnitude Time Efficiency Summary Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.23 Four Connected Cities Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.24 Hamiltonian Circuits among All Paths from A in Figure 3.23 with Four Links Invitation to Computer Science, 5th Edition

When Things Get Out of Hand Exponential algorithm An Θ(2n) algorithm Brute force algorithm One that beats the problem into submission by trying all possibilities Intractable problem No polynomially bounded algorithm exists Approximation algorithms Do not solve the problem, but provide a close approximation to a solution

Invitation to Computer Science, 5th Edition Figure 3.25 Comparisons of lg n, n, n2, and 2n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.26 Comparisons of lg n, n, n2, and 2n for Larger Values of n Invitation to Computer Science, 5th Edition

Invitation to Computer Science, 5th Edition Figure 3.27 A Comparison of Four Orders of Magnitude Invitation to Computer Science, 5th Edition