Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
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 1 – Basic Concepts
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
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
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
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Elementary Data Structures and Algorithms
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Chapter 3: The Efficiency of Algorithms
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing 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.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
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
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
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.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
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.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
CS 302 Data Structures Algorithm Efficiency.
Complexity Analysis (Part I)
Algorithms.
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
CSC 427: Data Structures and Algorithm Analysis
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Sorting by Tammy Bailey
Chapter 3: The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Chapter 3: The Efficiency of Algorithms
Recurrences (Method 4) Alexandra Stefan.
CSC 427: Data Structures and Algorithm Analysis
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs, Recursion, and Analysis of Algorithms

Section 2.6Analysis of Algorithms1 We need some other basis of comparison to decide which algorithm to use in a given situation. Several criteria could be used to judge which is the “best:” Easiest to understand Which makes the most efficient use of machine memory Which runs most efficiently (time efficiency) We evaluate the time efficiency of an algorithm by estimating the number of operations it must perform. We count only the operations that are basic to the task at hand. The study of the efficiency of algorithms, that is, the number of operations they perform, is called analysis of algorithms.

Section 2.6Analysis of Algorithms2 Suppose we need to search a sorted list of n words or numbers for a particular target value x. ALGORITHM: SequentialSearch SequentialSearch(list L; integer n; itemtype x) //searches a list L of n items for item x Local variable: integer i //marks position in the list i = 1 while L[i]  x and i < n do i = i + 1 end while if L[i] = x then write(“Found”) else write(“Not found”) end if end function SequentialSearch

Section 2.6Analysis of Algorithms3 The sequential search algorithm does the maximum amount of work (number of comparisons) when x is the last item in the list or does not appear at all. In either case, all elements are compared to x, so n comparisons are done. This is the “worst case.” The minimum amount of work is done when x is the very first item on the list. Only one comparison is made. This is the “best case.” If x falls in the exact middle of the list, the search would require roughly n/2 comparisons. For most algorithms, the average behavior is very difficult to determine. We will content ourselves with the worst-case count of the number of operations required.

Section 2.6Analysis of Algorithms4 Analysis Using Recurrence Relations We can recast the sequential search algorithm from an iterative version to a recursive version. SequentialSearchRecursive(list L; integer i, n; itemtype x) //searches list L from L[i] to L[n] for item x if i > n then write(“Not found”) else if L[i] = x then write(“Found”) else SequentialSearch(L, i + 1, n, x) end if

Section 2.6Analysis of Algorithms5 Analysis Using Recurrence Relations Let C(n) represent the maximum number of comparisons required for an n-element list. C(1) = 1 (one comparison to search a 1-element list). C(n) = 1 + C(n  1) for n  2 (one comparison against the first element, then however many comparisons are required for the rest of the list). This is a first-order, linear recurrence relation with constant coefficients. This agrees with our previous analysis for the worst case.

Section 2.6Analysis of Algorithms6 Analysis Using Recurrence Relations Let C(n) stand for the maximum number of comparisons required to do a binary search on an n- element list. Then, C(n/2) stands for the maximum number of comparisons required to search a list half that size. The recurrence relation for C(n) is: C(1) = 1 C(n) = 1 + C(n/2) for n  2, n = 2 m The recurrence relation is solved to C(n) = 1 + log n.

Section 2.6Analysis of Algorithms7 Upper Bound (Euclidean Algorithm) The Euclidean algorithm uses a while loop to do successive divisions to find gcd(a, b) for positive integers a and b, a  b. A recursive version of the Euclidean algorithm can also be written. The key to the recursive version is to recognize that gcd(a, b) involves finding gcd(b, r), where r is the remainder on dividing a by b. We won’t find a recurrence relation, we will find an upper bound. An upper bound is a ceiling on the amount of work an algorithm does; the algorithm can require no more steps than the upper bound, but it may not require that many.

Section 2.6Analysis of Algorithms8 Analysis Using Recurrence Relations To find this upper bound, we will show that if i > j and i is divided by j with remainder r, then r < i/2. There are two cases: 1. If j  i/2, then r < i/2 since r < j. 2. If j > i/2, then i = 1 * j + (i  j); in other words, the quotient is 1 and r is i  j, which is less than i/2. In the Euclidean algorithm, the remainder r at any step becomes the dividend (the number being divided) two steps later. So the successive dividends are at least halved every two divisions. The remainder r at any step becomes the dividend (the number being divided) two steps later. So the successive dividends are at least halved every two divisions. The Value n can be halved log n times; therefore, at most, 2 log n divisions are done. E(n)  2 log n