Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Digital Image Processing & Pattern Analysis (CSCE 563) Intensity Transformations Prof. Amr Goneid Department of Computer Science & Engineering The American.
Introduction to Analysis of Algorithms
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Program Design and Development
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Data Structures Review Session 1
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 2. Types of Complexities.
Analysis of Algorithm.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Analysis of Algorithms
Analysis of Algorithms
Lecture 4. RAM Model, Space and Time Complexity
26 Sep 2014Lecture 3 1. Last lecture: Experimental observation & prediction Cost models: Counting the number of executions of Every single kind of command.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Algorithms and Algorithm Analysis The “fun” stuff.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 1. Introduction.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Lecture 2COMPSCI AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) 
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Algorithm Analysis 1.
Complexity Analysis (Part I)
CSCE 210 Data Structures and Algorithms
Chapter 2 Algorithm Analysis
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
Discrete Mathematics CS 2610
Complexity Analysis (Part I)
Analysis of Algorithms
REPETITION Why Repetition?
Complexity Analysis (Part I)
Presentation transcript:

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations

Prof. Amr Goneid, AUC2 Time Complexity Calculations

Prof. Amr Goneid, AUC3 Time Complexity Calculations Performance Measurement & Modeling Performance Measurement Performance Analysis (modeling) Evaluating Number of Operations T(n) Examples of Algorithm Analysis

Prof. Amr Goneid, AUC4 1. Performance Measurement and Modeling Algorithm Actual Code Actual or Pseudo code Measurement Runs Math. Model T(n) and Bounds

Prof. Amr Goneid, AUC5 2. Performance Measurement We measure the performance of an algorithm by time measurement or by counting number of operations. Usually called Amortized Analysis: Repeat measuring the running time m times then divided by m. Important issues are: Choice of Bound Clocking or counting domains Choice of Data Sizes Choice of Data Set for the Bound

Prof. Amr Goneid, AUC6 Time Measurement Example Set repetition count m; Set problem size n; t acc = 0; Repeat on m { start time t1 = GetTime(); Invoke A(n); end time t2 = GetTime(); t acc = t acc + (t2 - t1); } t av = t acc / m;

Prof. Amr Goneid, AUC7 Counting Example 1 int count = 0; float s = 0.0; float A[n][n]; for (i=1; i<=n; i++) for (j = 1, j<=n; j++) { s = s + A[i][j]; count++;} The above code finds the no. of floating point additions T(n) = count as a function of n.

Prof. Amr Goneid, AUC8 Counting Example 2: Insertion Sort Function void insertion (int a[ ], int n) { int i, v, j ; for (i =1; i < n; i++) { v = a[i]; j = i; while(j > 0 && a[j-1] > v) { a[j] = a[j-1]; j--; count++;}; a[j] = v; }

Prof. Amr Goneid, AUC9 Insertion Sort (Performance) T1 ordered, T2 Random, T3 Inversely ordered n T1(n) T2(n) ,052 T3(n) ,900 n(n-1)/ ,900

Prof. Amr Goneid, AUC10 T(n) vs n for insertsort

Prof. Amr Goneid, AUC11 3. Performance Analysis (Modeling) Use a high-level Description (e.g Pseudocode) instead of implementation Identify problem size (n) Identify elementary processes. Assign a number of operations to each. Form a mathematical model of the sum of operations in these processes. Solve to find T(n) independent of HW

Prof. Amr Goneid, AUC12 Performance Analysis (Modeling) Example: Find the sum of all elements in a 2-D array of size (nxn) sum ← 0.0; for (i = 0 to n-1) for (j = 0 to n-1) sum ← sum + a ij The no. of floating point additions T(n) as a function of n is: ∑i∑i ∑j∑j 1

Prof. Amr Goneid, AUC13 4. Evaluating No. of Operations T(n) Usually we specify a certain type of operations, e.g., additions, array comparisons, etc. Simple Statements: T(n) = no. of specified operations. e.g. for z = 2*x + y T(n) = 2 arithmetic operations Code blocks: T(n) = Sum of sub-block T(n)’s

Prof. Amr Goneid, AUC14 No. of Operations T(n) Selection Statements: c = condition, s = statement block if (c) s; T(n) = T c (n) for c false (best case) = T c (n) + T s (n) for c true (worst case) If (c) s1; else s2; Worst case T(n) = T c (n) + max(T s1 (n),T s2 (n))

Example: T(n) = no. of comparisons Given that fun1(n) does n+2 comparisons, module1 does n 2 and module2 does n 4. Find worst case T(n) for the segment: if ( c == fun1(n)) call module1; else call module2; Prof. Amr Goneid, AUC15 No. of Operations T(n) ProcessT(n) ==1 fun1(n)n+2 module1n2n2 module2n4n4 Worst case total ?

Repetition Statements: For Loop Example: T(n) = No. of multiplications Fun(i,n) does 2i+n multiplications m = n*n for (i =1; i <= m; i++) Fun(i,n); Prof. Amr Goneid, AUC16 No. of Operations T(n) ProcessT(n) Compute m 1 Fun(i,n)2i + n TotalT(n) = ?

Prof. Amr Goneid, AUC17 No. of Operations T(n) Repetition Statements: while Loop Example: In the following code: Fun(i,n) does 3i + log n comparisons, find T(n) = no. of comparisons i = 0; while(i <= n) {Fun(i,n); i++;}

Prof. Amr Goneid, AUC18 Running Time The number of operations can be used to predict the running time of an algorithm for a given problem size (n) Example: The number of operations of a sorting algorithm is directly proportional to (n log n). Direct time measurement gives 1 ms to sort 1000 items. Find how long it will take to sort 1,000,000 items. Solution:

Prof. Amr Goneid, AUC19 5. Examples of Algorithm Analysis (a) Uniqueness Test Check whether all the elements in a given array are distinct Input: An array A[0…n-1] Output: Return “true” if all the elements in A are distinct and “false” otherwise

Prof. Amr Goneid, AUC20 Uniqueness Test

Prof. Amr Goneid, AUC21 Exercise Prove the formula: either by mathematical induction or by following the insight of a 10year-old school boy named Carl Friedrich Gauss (1777–1855) who grew up to become one of the greatest mathematicians of all times.

Prof. Amr Goneid, AUC22 (b) More Nested Loops Find the number of double arithmetic operations done by the following piece of code assuming that n = 2 m : for( int t = n; t > 1; t /= 2 ) { for( int u = 1; u < n; u *= 2 ) { for( int v = 0; v < n; v += 2 ) {... // constant number of double arithmetic //operations }

Prof. Amr Goneid, AUC23 More Nested Loops The loop variables can be transformed to: Outermost loop: i = log t = m … 1, Middle loop: j = log u = 0 … m-1 innermost loop: k = v/2 = 0.. n/2-1

Prof. Amr Goneid, AUC24 (c) Cosine Function Evaluation The cosine function can be evaluated using a truncated infinite series: The corresponding algorithm is: float cosine (float x, int n) { float y = -x * x; float s = 1.0; for (int i = 1; i <= n; i++) s = s + pow (y,i) / fact (2*i); return s; }

Prof. Amr Goneid, AUC25 Cosine Function Evaluation The number of arithmetic operations is evaluated as follows: fact (2*i ) uses one mult. + 2i mult. = 2i+1 pow(y,i) uses (i-1) mult. 1 division and 1 addition inside loop 1 mult. outside loop (-x * x) Show that: i.e., Quadratic algorithm.

Prof. Amr Goneid, AUC26 A Faster Cosine Algorithm We express the series in the form where S(x,i) = (-x 2 ) i / (2i)!, and S(x,0) = 1 Expressing S(x,i+1) in terms of S(x,i), then where y = (-x 2 ) and m = 2iwith S(x,0) = 1

Prof. Amr Goneid, AUC27 A Faster Cosine Algorithm Show that in this case: This is a linear algorithm that is much faster than the previous quadratic algorithm

Prof. Amr Goneid, AUC28 (d) Maximum Subsequence Sum Problem Statement Given a sequence of integers (possibly negative), a 1,a 2,...,a n, find the maximum value of (This is zero if all integers are negative). Example: -2, 11, -4, 13, -5, -2, S max = a 2 +a 3 +a 4 = 20

Prof. Amr Goneid, AUC29 Maximum Subsequence Sum Algorithm 1

Prof. Amr Goneid, AUC30 Maximum Subsequence Sum Algorithm 1 (Analysis)

Prof. Amr Goneid, AUC31 Maximum Subsequence Sum Algorithm 2

Prof. Amr Goneid, AUC32 Maximum Subsequence Sum Algorithm 2 (Analysis)

Prof. Amr Goneid, AUC33 Maximum Subsequence Sum Algorithm 3

Prof. Amr Goneid, AUC34 Maximum Subsequence Sum Algorithm 3 (Analysis)

Prof. Amr Goneid, AUC35 (e) Histogram Processing A bad contrast image (F) can be enhanced to an image G by transforming every pixel intensity f ij to another intensity g ij using a method called Histogram Equalization

Prof. Amr Goneid, AUC36 Histogram Processing In practice, gray levels are discrete (k = 0.. L-1) A histogram is an array whose element n k = no. of pixels with gray level k (k=0 is black, k=L-1 is white). Algorithm to build histogram for an image with N rows and M columns (N tot = NM pixels):

Prof. Amr Goneid, AUC37 Histogram Equalization Algorithm (1) The histogram is used in the following algorithm:

Prof. Amr Goneid, AUC38 Histogram Equalization Algorithm (2) A better algorithm first builds a Cumulative array:

Prof. Amr Goneid, AUC39 Histogram Equalization Summary of Total Complexity Algorithm (1): Algorithm (2):

Prof. Amr Goneid, AUC40 Histogram Equalization Numerical Example L = 256 gray levels Algorithm (1): Algorithm (2):