Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Algorithm Analysis.
Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Analysys & Complexity of Algorithms Big Oh Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 3 Growth of Functions
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
The Efficiency of Algorithms
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
Analysis of Performance
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Program Efficiency and Complexity
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ 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)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Asymptotic Analysis (based on slides used at UMBC)
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Algorithm Analysis (Big O)
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Introduction to Analysis of Algorithms
Analysis of Algorithms
Lecture 06: Linked Lists (2), Big O Notation
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Analysis of Algorithms
Presentation transcript:

Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms

How to Compare Formulas? Answer depends on value of N: N 50N 2 +31N 3 +24N+15 3N 2 +N+21+4*3 N

What Happened? CSE 246 Data Structures and Algorithms Fall2009 Quratulain 3 N ◦ One term dominated the sum

As N Grows, Some Terms Dominate CSE 246 Data Structures and Algorithms Fall2009 Quratulain 4

Order of Magnitude Analysis CSE 246 Data Structures and Algorithms Fall2009 Quratulain 5 Measure speed with respect to the part of the sum that grows quickest Ordering:

Order of Magnitude Analysis (cont) CSE 246 Data Structures and Algorithms Fall2009 Quratulain 6 Furthermore, simply ignore any constants in front of term and simply report general class of the term: grows proportionally to When comparing algorithms, determine formulas to count operation(s) of interest, then compare dominant terms of formulas

Analyzing Running Time CSE 246 Data Structures and Algorithms Fall2009 Quratulain 7 1. n = read input from user 2. sum = 0 3. i = 0 4. while i < n 5. number = read input from user 6. sum = sum + number 7. i = i mean = sum / n T(n), or the running time of a particular algorithm on input of size n, is taken to be the number of times the instructions in the algorithm are executed. Pseudo code algorithm illustrates the calculation of the mean (average) of a set of n numbers: StatementNumber of times executed n+1 5n 6n 7n 81 The computing time for this algorithm in terms on input size n is: T(n) = 4n + 5.

Big-O Notation Computer scientists like to categorize algorithms using Big- O notation. For sufficiently large N, the value of a function is largely determined by its dominant term. Big-O notation is used to capture the most dominant term in function and to represent the growth rate. Big-o notation also allows us to establish a relative order among functions by comparing dominant terms Thus, if running time of an algorithm is O(N 2 ), then ignoring constant, it can be guaranteeing that running time bound to quadratic function. CSE 246 Data Structures and Algorithms Fall2009 Quratulain 8

Big-O Notation Quratulain Rajput 9 If you tell a computer scientist that they can choose between two algorithms: one whose time complexity is O( N ) Another is O( N 2 ), then the O( N ) algorithm will likely be chosen. Let’s find out why…

Big-O Notation Let’s consider a function on N, T(N) N is a measure of the size of the problem we try to solve, e.g., Definition: T(N) is O( g(N) ) if: So, if T(N) is O( g(N) ) then T(N) grows no faster than g(N). Example 1 Suppose, for example, that T is the time taken to run an algorithm to process data in an array of length N. We expect that T will be a function of N and hence write: If we now say that T(N) is O(N) we are saying that T(N) grows no faster than N. This is good news – it tells us that if we double the size of the array, we can expect that the time taken to run the algorithm will double (roughly speaking). CSE 246 Data Structures and Algorithms Fall2009 Quratulain 10

Example CSE 246 Data Structures and Algorithms Fall2009 Quratulain 11 Suppose f(n) = n 2 + 3n - 1. We want to show that f(n) = O(n 2 ). f(n) = n 2 + 3n - 1 < n 2 + 3n (subtraction makes things smaller so drop it) <= n 2 + 3n 2 (since n <= n 2 for all integers n) = 4n 2 Therefore, if C = 4, we have shown that f(n) = O(n 2 ). Notice that all we are doing is finding a simple function that is an upper bound on the original function. Because of this, we could also say that This would be a much weaker description, but it is still valid. f(n) = O(n 3 ) since (n 3 ) is an upper bound on n 2

Question Suppose you have two algorithms (Algorithm A and Algorithm B) and that both algorithms are O(N). Does this mean that they both take the same amount of time to run?

Example We will now look at various Java code segments and analyze the time complexity of each: int count = 0; int sum = 0; while( count < N ) { sum += count; System.out.println( sum ); count++; } Total number of constant time statements executed...?

Example int count = 0; int sum = 0; while( count < N ) { int index = 0; while( index < N ) { sum += index * count; System.out.println( sum ); index++; } count++; } Total number of constant time statements executed ….?

Example int count = 0; while( count < N ) { int index = 0; while( index < count + 1 ) { sum++; index++; } count++; } Total number of constant time statements executed…?

Example int count = N; int sum = 0; while( count > 0 ) { sum += count; count = count / 2; } Total number of constant time statements executed…?