8. Comparison of Algorithms

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Program Performance & Asymptotic Notations CSE, POSTECH.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
{ 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)
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
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.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Algorithm Analysis (Big O)
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Complexity & Big-O Notation: From the Basics CompSci Club 29 May 2014.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Mathematical Foundation
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Analysis of Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Searching – Linear and Binary Searches
Algorithmic Efficency
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to Analysis of Algorithms
Introduction to complexity
Analysis of Algorithms
Data structure – is the scheme of organizing related information.
Introduction to Analysis of Algorithms
Lecture 2 Analysis of Algorithms
Program Efficiency Interested in “order of magnitude”
DATA STRUCTURES Introduction: Basic Concepts and Notations
Algorithm Efficiency Algorithm efficiency
Complexity Analysis.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Efficiency (Chapter 2).
Algorithm design and Analysis
Introduction to Algorithms Analysis
CSC 205 Java Programming II
Analysys & Complexity of Algorithms
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
Programming and Data Structure
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Algorithmic Complexity
Algorithms Analysis Algorithm efficiency can be measured in terms of:
Algorithms Analysis Algorithm efficiency can be measured in terms of:
Algorithmic complexity
Complexity Analysis (Part II)
Analysis of Algorithms
Algorithm Efficiency and Sorting
Presentation transcript:

8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this note is from “C++ Plus Data Structure” textbook slides

Map to Joe’s Diner

Which Cost More to Feed?

How efficient is an algorithm? Efficiency concerns: CPU usage (time complexity) memory usage (space complexity) disk usage network usage We discuss time complexity in this course.

Order of Magnitude of a Function The order of magnitude, or Big-O notation, of a function expresses the computing time of a problem as the term in a function that increases most rapidly relative to the size of a problem. Big-O is used to express time complexity of an algorithm

Example How many time do we add to sum? int sum = 0, n = 100; for(int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) sum += i * j; How many time do we add to sum? outer loop repeat n times each iteration add i times to sum 0+1+2+…+n-1 = n(n-1)/2 The time required by an algorithm is proportional to the number of “basic operations” that it performs! Big-O: measure growth O( 0.5n2 – 0.5n ) = O( n2 – n ) = O(n2) Note that the big-O expressions do not have constants or low-order terms. This is because, when N gets large enough, constants and low-order terms don't matter.

Comparison of Two Algorithms sum of consecutive integers 1 to 20 (n) O(n) O(1)

Types of Complexity Best case complexity: Worst case complexity: related to minimum # of steps required by an algorithm, given an ideal set of inputs Worst case complexity: related to maximum # of steps required by an algorithm, given the worst possible set of inputs Average case complexity: related to the average number of steps required by an algorithm, calculated across all possible sets of inputs this is the one we usually use.

Big-O Formal Definition A function T(N) is O(F(N)) if for some constant c and for all values of N greater than some value n0: T(N) <= c * F(N) Example: T(N) = 4*N2+7  it is O(N2) why? when c = 5, n0 = 2, T(N) <= c * F(N)

Finding “John Smith” Best case: O(1) How about algorithm 2? Worst case: O(N) Average case: O(N) average # of steps: (1+2+…+N)/N = (N+1)/2

Binary Search Example Look for 1 in sorted list 1-64. [1-32]  [1-16][1-8][1-4][1-2][1] we need to look 6 times: 26 = 64 this is the worst case. In the worst case, look for an item in a sorted list of size N, we need to look k times 2k = N  k = log2N  O(log2N)!

Names of Orders of Magnitude O(1) bounded (by a constant) time O(log2N) logarithmic time O(N) linear time O(N*log2N) N*log2N time O(N2) quadratic time O( 2N ) exponential time

Comparison of Orders N log2N N*log2N N2 2N 1 0 0 1 2 2 1 2 4 4 1 0 0 1 2 2 1 2 4 4 4 2 8 16 16 8 3 24 64 256 16 4 64 256 65,536 32 5 160 1024 4,294,967,296 64 6 384 4096 128 7 896 16,384

Comparison of Orders http://www.objc.io/issue-7/collections.html

How to Determine Complexities Sequence of statements: total time = time(statement 1) + time(statement 2) + ... + time(statement k) statement 1; statement 2; ... statement k;

How to Determine Complexities Branch: total time = max(time(sequence 1), time(sequence 2)) if (condition) { sequence of statements 1 } else { sequence of statements 2 }

How to Determine Complexities Loops: total time = N*time(sequence) How about nested loops? for (i = 0; i < N; i++) { sequence of statements }

Nested loop example for (i = 0; i < N; i++) for ( j = 0; j < i; j++ ) sequential statements i j total#of inner iteration 0 none 0 1 0 1 2 0,1 2 … N-1 0,1,…N-2 N-1 total time = (0+1+2+…+N-1)*time(sequence) = N(N-1)/2 * time(sequence)

Nested loop exercise i j total#of inner iteration for (i = -1; i < N+3; i++) for ( j = N+4; j >= i; j-- ) sequential statements i j total#of inner iteration

Nested loop exercise i j total#of inner iteration for (i = n+8; i > -2; i--) for ( j = 0; j < i+6; j++ ) sequential statements i j total#of inner iteration

Exercise: what is the Big-O? for ( i = 0 ; i < n ; i++ ) { subtotal = 0; if ( flag == true ) { for( j=i ; j > 0; j--) subtotal += j; tot += subtotal; } else subtotal = -1;

Exercise Array based sorted list: Sorted linked list: find(linear search): insert: delete: Sorted linked list: find: O(n) O(n)+O(n) = O(n) O(n)+O(n) = O(n) O(n) O(n)+O(1) = O(n) O(n)+O(1) = O(n) How about unsorted list?