Lecture 2COMPSCI 220 - AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) 

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

Complexity Analysis (Part II)
MATH 224 – Discrete Mathematics
Algorithm Analysis.
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
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Bigointro1 Algorithm Analysis & Program Testing An introduction.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Running Time Big O Notation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
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.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Discrete Maths Objective to describe the Big-Oh notation for estimating the running time of programs , Semester 2, Running Time of.
CS1020 Data Structures and Algorithms I Lecture Note #11 Analysis of Algorithms.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
Discrete Mathematics Lecture 7. 2 Analysis of Algorithms Analyzing an algorithm Time complexity Space complexity Time complexity Running time needed by.
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 Algorithm Analysis Lectures 3 & 4 Resources Data Structures & Algorithms Analysis in C++ (MAW): Chap. 2 Introduction to Algorithms (Cormen,
Algorithm Analysis (Big O)
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Complexity & Big-O Notation: From the Basics CompSci Club 29 May 2014.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
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.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Mathematical Foundation
Introduction to Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Introduction to complexity
Analysis of Algorithms
Introduction to Algorithms
Analysis of Algorithms
Big-Oh and Execution Time: A Review
Algorithm Analysis Lectures 3 & 4
Chapter 12: Analysis of Algorithms
Introduction to Data Structures
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Searching, Sorting, and Asymptotic Complexity
8. Comparison of Algorithms
Algorithmic complexity
Analysis of Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Complexity Analysis (Part I)
Analysis of Algorithms
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
Presentation transcript:

Lecture 2COMPSCI AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n)  m(m +1)  n / 2 ( n / )  0.25n n For a large n, T(n)  0.25n 2 –e.g., if n ≥10, the linear term 0.5n ≤ 16.7% of T(n) –if n ≥ 500, the linear term 0.5n ≤ 0.4% of T(n)

Lecture 2COMPSCI AP G Gimel'farb2 Quadratic vs linear term T(n) = 0.25n n nT(n)T(n)0.25n 2 0.5n % % % % %

Lecture 2COMPSCI AP G Gimel'farb3 Quadratic Time to Sum Subarrays: T(n)=0.25n n Factor c = 0.25 is referred to as a “ constant of proportionality ” An actual value of the factor does not affect the behaviour of the algorithm for a large n: –10% increase in n  20% increase in T(n) –Double value of n  4-fold increase in T(n): T(2n) = 4 T(n)

Lecture 2COMPSCI AP G Gimel'farb4 Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size becomes large, the most significant term is that which has the largest power of n This term increases faster than other terms which reduce in significance

Lecture 2COMPSCI AP G Gimel'farb5 Running Time: Estimation Rules Constants of proportionality depend on the compiler, language, computer, etc. –It is useful to ignore the constants when analysing algorithms. Constants of proportionality are reduced by using faster hardware or minimising time spent on the “inner loop” – But this would not affect behaviour of an algorithm for a large problem!

Lecture 2COMPSCI AP G Gimel'farb6 Elementary Operations Basic arithmetic operations (  ; – ;  ;  ; % ) Basic relational operators ( ==, !=, >, =, <= ) Basic Boolean operations (AND,OR,NOT) Branch operations, return, … Input for problem domains (meaning of n ): Sorting: n items Graph / path: n vertices / edges Image processing: n pixels Text processing: string length

Lecture 2COMPSCI AP G Gimel'farb7 Estimating Running Time Simplifying assumptions: all elementary statements / expressions take the same amount of time to execute e.g., simple arithmetic assignments return Loops increase in time linearly as k  T body of a loop where k is number of times the loop is executed

Lecture 2COMPSCI AP G Gimel'farb8 Estimating Running Time Conditional / switch statements like if { condition } then { const time T 1 } else { const time T 2 } are more complicated ( one has to account for branching frequencies: T  f true T 1  (1  f true )T 2 ≤ max{ T 1, T 2 } Function calls : T function =  T statements in function Function composition : T(f(g(n))) = T(g(n))  T(f(n))

Lecture 2COMPSCI AP G Gimel'farb9 Estimating Running Time Function calls in more detail: T   T statemen t i... x.myMethod( 5,... ); public void myMethod( int a,... ){ statements 1, 2, …, N } Function composition in more detail: T(f(g(n))) Computation of x  g(n)  T(g(n)) Computation of y  f(x)  T(f(n))

Lecture 2COMPSCI AP G Gimel'farb10 Example 1.5: Textbook, p.11 Logarithmic time due to an exponential change i  k, k 2, k 3, …, k m of the loop control in the range 1 ≤ i ≤ n : for i = k step i  ik until n do … {const # of elementary operations} end for m iterations such that k m  1 < n ≤ k m  T(n)  c  log k n 

Lecture 2COMPSCI AP G Gimel'farb11 Example 1.6: Textbook, p.11 n log n running time of the conditional nested loops: m  2 ; for j  1 to n do if ( j  m ) then m  2m for i  1 to n do …{const # of operations} end for end if end for The inner loop is executed k times for j  2, 4, …, 2 k ; k < log 2 n ≤ k  1 ; in total: T(n)  kn  n  log 2 n 

Lecture 2COMPSCI AP G Gimel'farb12 Exercise 1.2.1: Textbook, p.12 Conditional nested loops: linear or quadratic running time? m  1 ; for j  1 to n do if ( j  m ) then m  m (n  1) for i  1 to n do …{const # of operations} end for end if end for The inner loop is executed only twice, for j  1 and j  n  1 ; in total: T(n)  n  linear running time