Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

Chapter 2: Algorithm Analysis
Recursion Chapter 11 Chapter 11.
Chapter 7: Sorting Algorithms
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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 2: Algorithm Analysis Application of Big-Oh to program analysis Running Time Calculations Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Chapter 4: Trees Binary Search Trees
Cpt S 223 – Advanced Data Structures
Elementary Data Structures and Algorithms
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Developing Efficient Algorithms.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 24 Developing Efficient Algorithms.
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Foundations of Algorithms, Fourth Edition
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
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.
Chapter 2 Algorithm Analysis Mathematical Background Figure 2.1 Typical growth rates.
Vishnu Kotrajaras, PhD.1 Data Structures
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structure and Algorithm Analysis 02: Algorithm Analysis Hongfei Yan School of EECS, Peking University 3/12/2014.
Algorithm Analysis 1.
Algorithms.
Analysis of Algorithms
Decrease-and-Conquer Approach
Data structure – is the scheme of organizing related information.
Recursion and Logarithms
CS 3343: Analysis of Algorithms
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
Decrease-and-Conquer
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Application: Algorithms
8. Comparison of Algorithms
At the end of this session, learner will be able to:
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
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

2 Binary search Euclid’s algorithm Exponentials Rules to count operations Logarithms in Running Time

3 Divide-and-conquer algorithms Subsequently reducing the problem by a factor of two require O(logN) operations

4 Why logN? A complete binary tree with N leaves has logN levels. Each level in the divide-and- conquer algorithms corresponds to an operation Hence the number of operations is O(logN)

5 Example: 8 leaves, 3 levels

6 Binary Search Solution 1: Scan all elements from left to right, each time comparing with X. O(N) operations.

7 Binary Search Solution 2: O(logN) Find the middle element A mid in the list and compare it with X If they are equal, stop If X < A mid consider the left part If X > A mid consider the right part Do until the list is reduced to one element

8 Euclid's algorithm Finding the greatest common divisor (GCD) GCD of M and N, M > N, = GCD of N and M % N

9 GCD and recursion Recursion: If M%N = 0 return N Else return GCD(N, M%N) The answer is the last nonzero remainder.

10 M N rem

11 long gcd ( long m, long n) { long rem; while (n != 0) { rem = m % n; m = n; n = rem; } return m; } Euclid’s Algorithm (non-recursive implementation)

12 Why O(logN) M % N <= M / 2 After 1 st iteration N appears as first argument, the remainder is less than N/2 After 2 nd iteration the remainder appears as first argument and will be reduced by a factor of two Hence O(logN)

13 Computing X N X N = X*(X 2 ) N / 2,N is odd X N = (X 2 ) N / 2,N is even

14 long pow (long x, int n) { if ( n == 0) return 1; if (is_Even( n )) return pow(x * x, n/2); else return x * pow ( x * x, n/2); }

15 Why O(LogN) If N is odd : two multiplications The operations are at most 2logN: O(logN)

16 Another recursion for X N Another recursive definition that reduces the power just by 1: X N = X*X N -1 Here the operations are N-1, i.e. O(N) and the algorithm is less efficient than the divide-and-conquer algorithm.

17 How to count operations single statements (not function calls) : constant O(1) = 1. sequential fragments: the maximum of the operations of each fragment

18 single loop running up to N, with single statements in its body: O(N) single loop running up to N, with the number of operations in the body O(f(N)): O( N * f(N) ) How to count operations

19 two nested loops each running up to N, with single statements: O(N 2 ) divide-and-conquer algorithms with input size N: O(logN) Or O(N*logN) if each step requires additional processing of N elements How to count operations

20 Example: What is the probability two numbers to be relatively prime? tot = 0; rel = 0; for ( i = 0; i <= n; i++) for (j = i+1; j <= n; j++) { tot++; if ( gcd( i, j ) ==1) rel++; } return (rel/tot); Running time = ?