1 Efficiency of Algorithms: Analyzing algorithm segments, Insertion sort algorithm Logarithmic Orders Binary Search Algorithm.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Complexity Analysis (Part I)
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
1 Efficiency of Algorithms: Analyzing algorithm segments, Insertion sort algorithm.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Efficiency of Algorithms: Logarithmic Orders Binary Search Algorithm.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Searching CS 105 See Section 14.6 of Horstmann text.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 205 Java Programming II Algorithm Efficiency.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 211 Data Structures Lecture 13
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structure Introduction.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Searching CS 110: Data Structures and Algorithms First Semester,
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Algorithm Analysis 1.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Growth of Functions & Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Teach A level Computing: Algorithms and Data Structures
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
Applied Discrete Mathematics Week 6: Computation
Discrete Mathematics CS 2610
Presentation transcript:

1 Efficiency of Algorithms: Analyzing algorithm segments, Insertion sort algorithm Logarithmic Orders Binary Search Algorithm

2 Computing an Order of an Algorithm Segment Consider the following algorithm segment: max:=a[1]; min:=b[1] for i:=2 to n if max < a[i] then max:=a[i] ; if min > b[i] then min:=b[i] next i if min ≥ max then med:=(min+max)/2 Analysis: Number of loop iterations: n-1 Comparisons in each iteration: 2 Thus, elementary operations in the loop: 2(n-1) Operations after the loop: 3 Total number of operations in the segment: 2(n-1)+3 = 2n+1 Order of the segment: O(n)

3 The Insertion Sort Algorithm Insertion sort arranges the elements of one-dimensional array a[1], …, a[n] into increasing order. for i:=2 to n (insert a[i] into the sorted sequence a[1],…,a[i-1]) key:=a[i] for j:=1 to i-1 if key<a[j] then goto (*) next j (*) (shift the values of a[j],…,a[i-1] to a[j+1],…,a[i]) for k:=i to j+1 a[k]:=a[k-1] next k a[j]:=key next i

4 The Insertion Sort Algorithm: Example Arrange array into increasing order

5 The Insertion Sort Algorithm: Analysis For each i (outer loop), maximum number of elementary operations is i – 1. Thus, the total number of elementary operations: 1+2+…+(n-1) = n(n-1)/2 =.5n 2 -.5n The insertion sort algorithm is O(n 2 ).

6 Logarithmic function Definition: The logarithmic function with base b (b>0, b  1) is the following function from R + to R: log b (x) = the exponent to which b must raised to obtain x. Symbolically, log b x = y  b y = x. Property: If the base b>1, then the logarithmic function is increasing: if x 1 <x 2, then log b (x 1 ) < log b (x 2 ). Note: Logarithmic function grows very slowly, e.g., log 2 (1,024)=10, log 2 (1,048,576)=20.

A property of logarithmic function Proposition 1: If k is an integer and x is a real number with 2 k  x < 2 k+1, then  log 2 x  = k. Proof: 2 k  x < 2 k+1 log 2 (2 k )  log 2 (x) < log 2 (2 k+1 ) (since log 2 x increasing) k  log 2 (x) < k+1 (by definition of log 2 x)  log 2 x  = k (by definition of floor function) ■

8 An application of logarithms Question: Given a positive integer n, how many binary digits are needed to represent n? Solution: Binary representation of n: 1c k-1 c k-2 …c 2 c 1 c 0 which corresponds to n = 2 k + c k-1 ∙2 k-1 + … + c 2 ∙2 2 + c 1 ∙2 + c 0 Since c i  1, n = 2 k + c k-1 ∙2 k-1 + … + c 2 ∙2 2 + c 1 ∙2 + c 0  2 k + 2 k-1 + … = (2 k+1 -1) / (2-1) (as a sum of geometric sequence) = 2 k+1 -1 < 2 k+1 (1) On the other hand, n = 2 k + c k-1 ∙2 k-1 + … + c 2 ∙2 2 + c 1 ∙2 + c 0 ≥ 2 k (2) Combining (1) and (2): 2 k  n < 2 k+1 (3) Based on (3) and Proposition 1, k =  log 2 n  and the number of binary digits is  log 2 n  + 1.

9 Exponential and Logarithmic Orders  For all real numbers b and r with b>1 and r>0 and for all sufficiently large values of x,  log b x  x r ; ( which implies that log b x is O(x r ) )  x r  b x ( which implies that x r is O(b x ) )  For all real numbers b with b>1 and for all sufficiently large values of x, x  x log b x  x 2 (which implies that x is O(x log b x) and x log b x is O(x 2 ) )

Binary Search Algorithm The algorithm searches for an element x in an ascending array of elements a[1],…,a[n]. Algorithm body: index:=0, bot:=1, top:=n while (top ≥ bot and index=0) mid :=  (bot+top) / 2  if a[mid] = x then index := mid if a[mid] > x then top := mid-1 else bot := mid+1 end while Output: index (If index has the value 0 then x is not in the array; otherwise, index gives the index of the array where x is located.)

11 Binary Search Algorithm: Example Suppose a[1]=Amy, a[2]=Bob, a[3]=Dave, a[4]=Erin, a[5]=Jeff, a[6]=John, a[7]=Larry, a[8]=Mary, a[9]=Mike, a[10]=Sam, a[11]=Steve, a[12]=Tom. (sorted in alphabetical order) Search for x=Erin. The table tracing the binary search algorithm: index04 bot114 top1255 mid634

12 The Efficiency of the Binary Search Algorithm At each iteration, the length of the new subarray to be searched is approximately half of the previous one. If n = 2 k +m, where 0  m < 2 k, then n can be split approximately in half k times. Since 2 k  n < 2 k+1, then k =  log 2 n  (by proposition 1) Thus, the number of iterations of the while loop in a worst-case execution of the algorithm is  log 2 n  +1. The number of operations in each loop is constant (doesn’t increase with n). Thus, the binary search algorithm is O(log 2 n).