1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Advertisements

Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts COMP-2001 L4&5 Portions.
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Complexity Analysis (Part I)
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
The Seven Functions. Analysis of Algorithms. Simple Justification Techniques. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer Goodrich,
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
PSU CS 311 – Algorithm Design and Analysis Dr. Mohamed Tounsi 1 CS 311 Design and Algorithms Analysis Dr. Mohamed Tounsi
Analysis of Performance
CS2210 Data Structures and Algorithms Lecture 2:
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis Algorithm Analysis I Reading: Weiss, chap.2.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
ANALYSIS OF ALGORITHMS Data Structures. Algorithms zDefinition A step by step procedure for performing some task in a finite amount of time zAnalysis.
1 Big-Oh Notation CS 105 Introduction to Data Structures and Algorithms.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
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)
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
Analysis of Algorithms
Algorithms and Algorithm Analysis The “fun” stuff.
Grading Exams 60 % Lab 20 % Participation 5% Quizes 15%
1 Dr. J. Michael Moore Data Structures and Algorithms CSCE 221 Adapted from slides provided with the textbook, Nancy Amato, and Scott Schaefer.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
Algorithm Analysis (Big O)
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.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
Data Structures and Algorithms
Algorithms Algorithm Analysis.
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Lecture 3 of Computer Science II
Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms

2 What is a good algorithm? It must be correct It must be efficient Implementations of an algorithm must run as fast as possible How is this measured? Running time

3 Running time of a program Amount of time required for a program to execute for a given input If measured experimentally, Dependent on hardware, operating system and software Answer will be in milliseconds, seconds, minutes, hours, …

4 Running time of an algorithm Answer will not be in seconds or minutes Instead, we count the number of operations carried out Result will be a formula in terms of some input size, n Takes into account all possible inputs Example statement on running time: Running time of algorithm A is T(n) = 2n + 3

5 Describing algorithms First, we need to agree on how to describe or specify an algorithm Note: algorithms are intended for humans (programs are intended for computers) Descriptions should be high-level explanations that combine natural language and familiar programming structures: Pseudo-code

6 Pseudo-code example Algorithm arrayMax(A,n): Input: An array A storing n integers. Output: The maximum element in A. currentMax  A[0] for i  1 to n - 1 do if currentMax < A[i] then currentMax  A[i] return currentMax

7 Pseudo-Code conventions General Algorithm Structure Statements Expressions Control Structures

8 Algorithm Structure Algorithm heading Algorithm name(param1, param2,...): Input : input elements Output : output elements statements…

9 Statements Assignment: use  instead of = Method or function call: object.method(arguments) method(arguments) Return statement: return expression Control structures

10 Control Structures decision structures while loops repeat loops for loop n if... then... [else...] n while... do n repeat... until... n for... do

11 Expressions Standard math symbols + - * / ( ) Relational operators = > = != Boolean operators and or not Assignment operator  Array indexing A[i]

12 General rules on pseudo-code Should communicate high-level ideas and not implementation details Syntax is not as tight (e.g., indentation and line-breaks take the place of ; and {} in Java) Clear and informative

13 Back to running time Once an algorithm has been described in pseudo-code, we can now count the primitive operations carried out by the algorithm Primitive operations: assignment, calls, arithmetic operations, comparisons, array accesses, return statements, …

14 Back to arrayMax example Suppose the input array is A = {42, 6, 88, 53}, (n = 4) How many primitive operations are carried out? Some notes The for statement implies assignments, comparisons, subtractions, and increments the statement currentMax  A[i] will sometimes not be carried out

15 What to count currentMax  A[0]: assignment, array access for i  1 to n - 1 do: assignment,comparison,subtraction,increment (2 ops) if currentMax < A[i] then: comparison, access currentMax  A[i]: assignment, access return currentMax: return

16 Counting operations currentMax  A[0]2 for i  1 to n - 1 do15 if currentMax < A[i] then6 currentMax  A[i]2 return currentMax1 OPERATIONS CARRIED OUT26

17 Handling all possible inputs In the example just carried out, running time = 26 for the given input A more useful answer is to provide a formula that applies in general Formula is in terms of n, the input or array size Since there may be statements that do not always execute, depending on input values, there are two approaches Assume worst-case Provide a range

18 Measuring running time currentMax  A[0]2 for i  1 to n - 1 do1+2n+2(n-1) if currentMax < A[i] then2(n-1) currentMax  A[i]0.. 2(n-1) return currentMax1 RUNNING TIME (worst-case)8n - 2 increment comparison & subtraction

19 Nested loops // prints all pairs from the set {1,2,…n} for i  1 to n do for j  1 to n do print i, j How many times does the print statement execute?

20 Nested loops // prints all pairs from the set {1,2,…n} for i  1 to n do for j  1 to n do print i, jn 2 times How about the operations implied in the for statement headers?

21 for statement revisited for i  1 to n do … 1 assignmenti  1 n+1 comparisonsi <= n for each value of i in {1, 2, …, n+1} n increments = n assignments + n adds => 3n+2 operations

22 Nested loops running time for i  1 to n do3n+2 for j  1 to n don (3n+2) print i, jn 2 TOTAL:4n 2 + 5n + 2

23 Nested loops example 2 // prints all pairs from the set {1,2,…n} // excluding redundancies (i.e., 1,1 is not a // real pair, 1,2 and 2,1 are the same pair) for i  1 to n-1 do for j  i+1 to n do print i, j How many times does the print statement execute?

24 Nested loops example 2 for i  1 to n-1 do for j  i+1 to n do print i, j when i = 1,n-1 prints when i = 2,n-2 prints … when i = n-2,2 prints when i = n-1,1 print

25 Nested loops example 2 Number of executions of the print statement: … + n-1 = n(n-1)/2 = ½n 2 – ½n

26 Nested loops example 2 for i  1 to n-1 do for j  i+1 to n do print i, j Take-home exercise: count the operations implied by the for statement headers

27 Running time function considerations In determining the running time of an algorithm, an exact formula is possible only when the operations to be counted are explicitly specified Different answers will result if we choose not to count particular operations Problem: variations in the answers appear arbitrary

28 Classifying running time functions We want to classify running times under particular function categories Goal: some assessment of the running time of the algorithm that eliminates the arbitrariness of counting operations Example: arrayMax runs in time proportional to n

29 Some function categories The constant function:f(n) = c The linear function:f(n) = n The quadratic function:f(n) = n 2 The cubic function:f(n) = n 3 The exponential function:f(n) = b n The logarithm function:f(n) = log n The n log n function:f(n) = n log n

30 Big-Oh notation Consider the following (running time) functions f 1 (n) = 2n + 3 f 2 (n) = 4n 2 + 5n + 2 f 3 (n) = 5n We place f 1 and f 3 under the same category (both are proportional to n) and f 2 under a separate category (n 2 ) We say: 2n+3 is O(n), and 5n is O(n), while 4n 2 + 5n + 2 is O(n 2 ) 2n+3 and 5n are linear functions, while 4n 2 + 5n + 2 is a quadratic function

31 Significance of Big-Oh Example for i  0 to n-1 do A[i]  0 Running time is 2n, if we count assignments & array accesses 4n+2, if we include implicit loop assignments/adds 5n+3, if we include implicit loop comparisons Regardless, running time is O(n)

32 Prefix Averages Problem Given an array X storing n numbers, compute prefix averages or running averages of the sequence of numbers A[i] = average of X[0],X[1],…,X[i] Example Input:X = {1,3,8,2} Output:A = {1,2,4,3.5} Algorithm?

33 Algorithm 1 Algorithm prefixAverages1(A,n): Input: An array X of n numbers. Output: An array A containing prefix averages for i  0 to n - 1 do sum  0 for j  0 to i do sum  sum + X[j] A[i]  sum/(i+1) return array A

34 Running time of Algorithm 1 Exercise: count primitive operations carried out by Algorithm 1 Regardless of what you decide to count, the answer will be of the form: a n 2 + b n + c Observe that the answer is O(n 2 )

35 Algorithm 2 Algorithm prefixAverages2(A,n): Input: An array X of n numbers. Output: An array A containing prefix averages sum  0 for i  0 to n - 1 do sum  sum + X[j] A[i]  sum/(i+1) return array A

36 Running time of Algorithm 2 Running time of Algorithm 2 will be of the form: a n + b Observe that the answer is O(n) Assessment: Algorithm 2 is more efficient than Algorithm 1, particularly for large input (observe the growth rate of n and n 2 )

37 Summary Algorithms are expressed in pseudo-code The running time of an algorithm is the number of operations it carries out in terms of input size Answer depends on which operation(s) we decide to count Big-Oh notation allows us to categorize running times according to function categories Next: formal definition of Big-Oh notation