CPSC 171 Introduction to Computer Science Efficiency of Algorithms.

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.
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
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
What is an algorithm? Informally: An Algorithm is a step by step method for solving a problem. It’s purpose is to break a larger task down so that each.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Efficiency of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
CS107 Introduction to Computer Science
Chapter 3 The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
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.
Searching and Sorting Arrays
Chapter 3: The Efficiency of Algorithms
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
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 陆嘉恒 中国人民大学 信息学院
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
CSC 211 Data Structures Lecture 13
CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Sorting.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
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.
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.
CS 302 Data Structures Algorithm Efficiency.
Introduction to Search Algorithms
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Sorting by Tammy Bailey
Chapter 3: The Efficiency of Algorithms
MSIS 655 Advanced Business Applications Programming
Algorithm Efficiency and Sorting
Chapter 3: The Efficiency of Algorithms
Searching and Sorting Arrays
Algorithm Efficiency and Sorting
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Invitation to Computer Science 5th Edition
Presentation transcript:

CPSC 171 Introduction to Computer Science Efficiency of Algorithms

Announcements Read Chapter 3 Lab 2, 3 due this Thursday (Hand in electronically)

An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition

Properties of Algorithms Correctness Does the algorithm always work Clarity Is the algorithm easy to understand Elegance (Style) Example: Gauss asked to add numbers from 1 to 100 Efficiency Speed of algorithm Space required to run algorithm

Efficiency of Algorithms How long does it take an algorithm to run? Depends on computer Depends on input data Benchmarking Run same algorithm and same input on difference machines Runs same algorithm on same machine using difference input Need a method of measuring inherent efficiency of algorithm, independent of machine or input Count how many times “work unit” is executed

Game Time Guess My Number 20 Questions Guess Who? For Each of these games understand different strategies for finding the answer. What is the best strategy?

Telephone Book Example Task Find a particular person’s number from a list of telephone subscribers, given the name Sequential Search Algorithm outline Start with the first entry and check its name, then repeat the process for all entries

Figure 3.1 Sequential Search Algorithm

How Efficient is the Algorithm? Best Case: Worst Case: Average Case: 1 Comparison N Comparisons N/2 Comparisons

Order of Magnitude Total number of operations performed is some constant number of operations times N 2N 3N ½*N Linear  (n)

Figure 3.4 Work = cn for Various Values of c

Telephone Book Example 2 Task Find a particular person’s number from a sorted list of telephone subscribers, given the name Binary Search Algorithm outline Always select the middle name. If the persons name is greater than selected name then search top half, otherwise search bottom half of remaining names.

Figure 3.18 Binary Search Algorithm (list must be sorted)

How Efficient is the Algorithm? Best Worst Average 1 Comparison Log N Comparisons  (Log n) About Log N Comparisons  (Log n)

Figure 3.21 A Comparison of n and lg n

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

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

Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup

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 )

The Shuffle-Left Algorithm (continued) Space efficiency n slots for n values, plus a few local variables  (n)

The Copy-Over Algorithm Use a second list Copy over each nonzero element in turn

Figure 3.15 The Copy-Over Algorithm for Data Cleanup

The Copy-Over Algorithm Time efficiency Count examinations and copies Best case  All zeros  n examinations and 0 copies   (n)

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

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

The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle

Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup

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)

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

Figure 3.17 Analysis of Three Data Cleanup Algorithms