Analysis of Algorithms. Algorithm Efficiency The efficiency of an algorithm is usually expressed in terms of its use of CPU time The analysis of algorithms.

Slides:



Advertisements
Similar presentations
 O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop.
Advertisements

COS 312 DAY 16 Tony Gauvin. Ch 1 -2 Agenda Questions? Next progress report is March 26 Assignment 4 Corrected – 3 A’s, 1 C, 1 D, 1 F and 1 MIA Assignment.
Chapter 11 Analysis of Algorithms. Chapter Scope Efficiency goals The concept of algorithm analysis Big-Oh notation The concept of asymptotic complexity.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Program Performance & Asymptotic Notations CSE, POSTECH.
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
Complexity Analysis Chapter 1.
Analysis of Algorithms Chapter 11 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
COMP 232 Intro Lecture. Introduction to Course Me – Dr. John Sigle Purpose/goals of the course Purpose/goals Prerequisites - COMP 132 (Java skill & Eclipse)
COS 312 DAY 16 Tony Gauvin. Ch 1 -2 Agenda Questions? Next Capstone progress report is March 26 Assignment 4 Corrected – 3 A’s, 1 C, 1 D, 1 F and 1 MIA.
Analysis of Algorithms
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Efficiency and Sorting Data Structure & Algorithm.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Data Structure Introduction.
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.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Introduction to Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Analysis of Algorithms
Introduction to complexity
Introduction to Algorithms
Introduction to Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Chapter 11 Analysis of Algorithms
Efficiency (Chapter 2).
Algorithm Analysis (not included in any exams!)
Building Java Programs
CHAPTER 2: Analysis of Algorithms
Chapter 12: Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Chapter 2 Analysis of Algorithms
Chapter 2 Analysis of Algorithms
Big O Notation.
DS.A.1 Algorithm Analysis Chapter 2 Overview
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Building Java Programs
Analysis of Algorithms
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
CHAPTER 2: Analysis of Algorithms
Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis of Algorithms
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
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Analysis of Algorithms

Algorithm Efficiency The efficiency of an algorithm is usually expressed in terms of its use of CPU time The analysis of algorithms involves categorizing an algorithm in terms of efficiency An everyday example: washing dishes Suppose washing a dish takes 30 seconds and drying a dish takes an additional 30 seconds Therefore, n dishes require n minutes to wash and dry Java Software Structures, 4th Edition, Lewis/Chase2 - 2

Algorithm Efficiency Now consider a less efficient approach that requires us to redry all previously washed dishes after washing another one Java Software Structures, 4th Edition, Lewis/Chase2 - 3

Problem Size For every algorithm we want to analyze, we need to define the size of the problem The dishwashing problem has a size n – number of dishes to be washed/dried For a search algorithm, the size of the problem is the size of the search pool For a sorting algorithm, the size of the program is the number of elements to be sorted Java Software Structures, 4th Edition, Lewis/Chase2 - 4

Growth Functions We must also decide what we are trying to efficiently optimize – time complexity – CPU time – space complexity – memory space CPU time is generally the focus A growth function shows the relationship between the size of the problem (n) and the value optimized (time) Java Software Structures, 4th Edition, Lewis/Chase2 - 5

Asymptotic Complexity The growth function of the second dishwashing algorithm is t(n) = 15n n It is not typically necessary to know the exact growth function for an algorithm We are mainly interested in the asymptotic complexity of an algorithm – the general nature of the algorithm as n increases Java Software Structures, 4th Edition, Lewis/Chase2 - 6

Asymptotic Complexity Asymptotic complexity is based on the dominant term of the growth function – the term that increases most quickly as n increases The dominant term for the second dishwashing algorithm is n 2 : Java Software Structures, 4th Edition, Lewis/Chase2 - 7 Number of dishes (n)15n 2 45n15n n , , ,0004,500154, ,000,00045,00015,045, ,500,000,000450,0001,500,450, ,000,000,0004,500,000150,004,500, ,000,000,000,00045,000,00015,000,045,000, ,500,000,000,000,000450,000,0001,500,000,450,000,000

Big-Oh Notation The coefficients and the lower order terms become increasingly less relevant as n increases So we say that the algorithm is order n 2, which is written O(n 2 ) This is called Big-Oh notation There are various Big-Oh categories Two algorithms in the same category are generally considered to have the same efficiency, but that doesn't mean they have equal growth functions or behave exactly the same for all values of n Java Software Structures, 4th Edition, Lewis/Chase2 - 8

Big-Oh Categories Some sample growth functions and their Big-Oh categories: Java Software Structures, 4th Edition, Lewis/Chase2 - 9 Growth FunctionOrderLabel t(n) = 17O(1)constant t(n) = 3 log nO(log n)logarithmic t(n) = 20n - 4O(n)linear t(n) = 12n log n + 100nO(n log n)n log n t(n) = 3n 2 + 5n - 2O(n 2 )quadratic t(n) = 8n 3 + 3n 2 O(n 3 )cubic t(n) = 2 n + 18n 2 + 3nO(2 n )exponential

Comparing Growth Functions You might think that faster processors would make efficient algorithms less important A faster CPU helps, but not relative to the dominant term Java Software Structures, 4th Edition, Lewis/Chase AlgorithmTime Complexity Max Problem Size Before Speedup Max Problem Size After Speedup Ans1s1 10s 1 Bn2n2 s2s2 3.16s 2 Cn3n3 s3s3 2.15s 3 D2n2n s4s4 S

Comparing Growth Functions As n increases, the various growth functions diverge dramatically: Java Software Structures, 4th Edition, Lewis/Chase2 - 11

Comparing Growth Functions For large values of n, the difference is even more pronounced: Java Software Structures, 4th Edition, Lewis/Chase2 - 12

Analyzing Loop Execution First determine the order of the body of the loop, then multiply that by the number of times the loop will execute for (int count = 0; count < n; count++) // some sequence of O(1) steps N loop executions times O(1) operations results in a O(n) efficiency Java Software Structures, 4th Edition, Lewis/Chase2 - 13

Analyzing Loop Execution Consider the following loop: count = 1; while (count < n) { count *= 2; // some sequence of O(1) steps } The loop is executed log 2 n times, so the loop is O(log n) Java Software Structures, 4th Edition, Lewis/Chase2 - 14

Analyzing Nested Loops When loops are nested, we multiply the complexity of the outer loop by the complexity of the inner loop for (int count = 0; count < n; count++) for (int count2 = 0; count2 < n; count2++) { // some sequence of O(1) steps } Both the inner and outer loops have complexity of O(n) The overall efficiency is O(n 2 ) Java Software Structures, 4th Edition, Lewis/Chase2 - 15

Analyzing Method Calls The body of a loop may contain a call to a method To determine the order of the loop body, the order of the method must be taken into account The overhead of the method call itself is generally ignored Java Software Structures, 4th Edition, Lewis/Chase2 - 16