Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Algorithm Analysis.
Analysys & Complexity of Algorithms Big Oh Notation.
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Complexity Analysis (Part I)
Complexity Analysis (Part II)
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
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)
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.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 205 Java Programming II Algorithm Efficiency.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
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.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
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)
Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms.
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.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
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 Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to complexity
Analysis of Algorithms
Lecture 06: Linked Lists (2), Big O Notation
Program Efficiency Interested in “order of magnitude”
Complexity Analysis.
Efficiency (Chapter 2).
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
8. Comparison of Algorithms
Analysis of Algorithms
Presentation transcript:

Computer Science and Software Engineering University of Wisconsin - Platteville 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? —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.5n 2 – 0.5n ) = O( n 2 – n ) = O(n 2 ) int sum = 0, n = 100; for(int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) sum += i * j;

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

Types of Complexity  Best 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 n 0 : T(N) <= c * F(N)  Example: T(N) = 4*N 2 +7  it is O(N 2 ) why? when c = 5, n 0 = 2, T(N) <= c * F(N)

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

Binary Search Example  Look for 1 in sorted list [1-32]  [1-16]  [1-8]  [1-4]  [1-2]  [1] —we need to look 6 times: 2 6 = 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 —2 k = N  k = log 2 N  O(log 2 N)!

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

N log 2 N N*log 2 N N 2 2 N , ,294,967, ,384 Comparison of Orders

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 ijtotal#of inner iteration 0none ,12 … N-10,1,…N-2N-1 total time= (0+1+2+…+N-1)*time(sequence) = N(N-1)/2 * time(sequence)

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; } total time = n * ( 1 + max( ( ( n-1 ) + 1 ), 1 ) )  O(n 2 )

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