Efficiency of Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
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
HST 952 Computing for Biomedical Scientists Lecture 10.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
CS0007: Introduction to Computer Programming Array Algorithms.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
What is an Algorithm? (And how do we analyze one?)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Efficiency of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
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 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.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
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
Designing Algorithms Csci 107 Lecture 4.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Analysis of Algorithm.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
{ 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)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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 陆嘉恒 中国人民大学 信息学院
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Analysis of Algorithms
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
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.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
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 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
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.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Computer Science 112 Fundamentals of Programming II
Lecture – 2 on Data structures
The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency Chapter 10.
Chapter 3: The Efficiency of Algorithms
Invitation to Computer Science 5th Edition
Presentation transcript:

Efficiency of Algorithms February 9th

Today Data cleanup algorithms Algorithm efficiency Copy-over Shuffle-left Converging pointers Algorithm efficiency Efficiency of data cleanup algorithms Reading: Chapter 3

Comparing Algorithms Algorithm There are many ways to solve a problem Design Correctness Efficiency Also, clarity, elegance, ease of understanding There are many ways to solve a problem Conceptually Also different ways to write pseudocode for the same conceptual idea How to compare algorithms?

Efficiency of Algorithms Efficiency: Amount of resources used by an algorithm Space (number of variables) Time (number of instructions) When design algorithm must be aware of its use of resources If there is a choice, pick the more efficient algorithm!

Efficiency of Algorithms Does efficiency matter? Computers are so fast these days… Yes, efficiency matters a lot! There are problems (actually a lot of them) for which all known algorithms are so inneficient that they are impractical Remember the shortest-path-through-all-cities problem from Lab1…

Data Cleanup Algorithms What are they? A systematic strategy for removing errors from data. Why are they important? Errors occur in all real computing situations. How are they related to the search algorithm? To remove errors from a series of values, each value must be examined to determine if it is an error. E.g., suppose we have a list d of data values, from which we want to remove all the zeroes (they mark errors), and pack the good values to the left. Legit is the number of good values remaining when we are done. 5 3 4 0 6 2 4 0 d1 d2 d3 d4 d5 d6 d7 d8 Legit

Data Cleanup: Copy-Over algorithm Idea: Scan the list from left to right and copy non-zero values to a new list Copy-Over Algorithm (Fig 3.2) Get values for n and the list of n values A1, A2, …, An Set left to 1 Set newposition to 1 While left <= n do If Aleft is non-zero Copy A left into B newposition (Copy it into position newposition in new list Increase left by 1 Increase newposition by 1 Else increase left by 1 Stop

Data Cleanup: The Shuffle-Left Algorithm Idea: go over the list from left to right. Every time we see a zero, shift all subsequent elements one position to the left. Keep track of nb of legitimate (non-zero) entries How does this work? How many loops do we need?

Shuffle-Left Algorithm (Fig 3.1) Get values for n and the list of n values A1, A2, …, An Set legit to n Set left to 1 Set right to 2 Repeat steps 6-14 until left > legit 6 if Aleftt ≠ 0 7 Increase left by 1 8 Increase right by 1 9 else 10 Reduce legit by 1 Repeat 12-13 until right > n Copy Aight into Aright-1 Increase right by 1 14 Set right to left + 1 15 Stop

Exercising the Shuffle-Left Algorithm 5 3 4 0 6 2 4 0 d1 d2 d3 d4 d5 d6 d7 d8 legit

Data Cleanup: The Converging-Pointers Algorithm Idea: One finger moving left to right, one moving right to left Move left finger over non-zero values; If encounter a zero value then Copy element at right finger into this position Shift right finger to the left

Converging Pointers Algorithm (Fig 3.3) Get values for n and the list of n values A1, A2,…,An Set legit to n Set left to 1 Set right to n Repeat steps 6-10 until left ≥ right If the value of Aleft≠0 then increase left by 1 Else Reduce legit by 1 Copy the value of Aright to Aleft 10 Reduce right by 1 if Aleft=0 then reduce legit by 1. Stop

Exercising the Converging Pointers Algorithm 5 3 4 0 6 2 4 0 d1 d2 d3 d4 d5 d6 d7 d8 legit

Efficiency of Algorithms How to measure time efficiency? Running time: let it run and see how long it takes On what machine? On what inputs? We want a measure of time efficiency which is independent of machine, speed etc Look at an algorithm pseudocode and estimate its running time Look at 2 algorithm pseudocodes and compare them

Time Efficiency Is this accurate? Time efficiency depends on input (Time) efficiency of an algorithm: the number of pseudocode instructions (steps) executed Is this accurate? Not all instructions take the same amount of time… But..Good approximation of running time in most cases Time efficiency depends on input Example: the sequential search algorithm In the best case, how fast can the algorithm halt? In the worst case, how fast can the algorithm halt?

Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for any collection of data values. Best case efficiency is the minimum number of steps that an algorithm can take any collection of data values. Average case efficiency - the efficiency averaged on all possible inputs - must assume a distribution of the input - we normally assume uniform distribution (all keys are equally probable) If the input has size n, efficiency will be a function of n

Worst Case Efficiency for Sequential Search Get the value of target, n, and the list of n values 1 Set index to 1 1 Set found to false 1 Repeat steps 5-8 until found = true or index > n n 5 if the value of listindex = target then n Output the index 0 Set found to true 0 8 else Increment the index by 1 n 9 if not found then 1 10 Print a message that target was not found 0 Stop 1 Total 3n+5

Analysis of Sequential Search Time efficiency Best-case : 1 comparison target is found immediately Worst-case: 3n + 5 comparisons Target is not found Average-case: 3n/2+4 comparisons Target is found in the middle Space efficiency How much space is used in addition to the input?

Order of Magnitude Worst-case of sequential search: Simplification: 3n+5 comparisons Are these constants accurate? Can we ignore them? Simplification: ignore the constants, look only at the order of magnitude n, 0.5n, 2n, 4n, 3n+5, 2n+100, 0.1n+3 ….are all linear we say that their order of magnitude is n 3n+5 is order of magnitude n: 3n+5 = (n) 2n +100 is order of magnitude n: 2n+100=(n) 0.1n+3 is order of magnitude n: 0.1n+3=(n) ….

Efficiency of Copy-Over Best case: all values are zero: no copying, no extra space Worst-case: No zero value: n elements copied, n extra space Time: (n) Extra space: n

Efficiency of Shuffle-Left Space: no extra space (except few variables) Time Best-case No zero value: no copying ==> order of n = (n) Worst case All zero values: every element thus requires copying n-1 values one to the left n x (n-1) = n2 - n = order of n2 = (n2) (why?) Average case Half of the values are zero n/2 x (n-1) = (n2 - n)/2 = order of n2 = (n2)

Efficiency of Converging Pointers Algorithm Space No extra space used (except few variables) Time Best-case No zero value No copying => order of n = (n) Worst-case All values zero: One copy at each step => n-1 copies order of n = (n) Average-case Half of the values are zero: n/2 copies

Data Cleanup Algorithms Copy-Over worst-case: time (n), extra space n best case: time (n), no extra space Shuffle-left worst-case: time (n2), no extra space Best-case: time (n), no extra space Converging pointers worst-case: time (n), no extra space