Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

Similar documents
Algorithm Analysis and Design

Algorithmic Analysis. Go go Big O(h)!

arxiv: v3 [cs.ds] 18 Apr 2011

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

The p-sized partitioning algorithm for fast computation of factorials of numbers

Data Structures and Algorithms

Lecture 1. Introduction

Advanced Algorithms and Data Structures

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Advanced Algorithms and Data Structures

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

Topic Analysis PART-A

SRI VENKATESWARA COLLEGE OF ENGINEERING. COURSE DELIVERY PLAN - THEORY Page 1 of 6

Analysis of Algorithms Part I: Analyzing a pseudo-code

A Simplified Correctness Proof for a Well-Known Algorithm Computing Strongly Connected Components

DESIGN AND ANALYSIS OF ALGORITHMS

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Outline and Reading. Analysis of Algorithms 1

Lecture 15: Algorithms. AP Computer Science Principles

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Lecture Notes for Chapter 2: Getting Started

Design and Analysis of Computer Algorithm Lecture 1. Assoc. Prof.Pradondet Nilagupta Department of Computer Engineering

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

アルゴリズムの設計と解析 (W4022) 教授 : 黄潤和 広野史明 (A4/A10)

Introduction to Analysis of Algorithms

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Assignment 1 (concept): Solutions

CS2 Algorithms and Data Structures Note 1

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Pseudo code of algorithms are to be read by.

International Journal of Scientific & Engineering Research, Volume 4, Issue 7, July ISSN

COMP6700/2140 Recursive Operations

Introduction to Algorithms Third Edition

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

PAijpam.eu A NOTE ON KNUTH S IMPLEMENTATION OF EXTENDED EUCLIDEAN GREATEST COMMON DIVISOR ALGORITHM Anton Iliev 1, Nikolay Kyurkchiev 2, Angel Golev 3

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Algorithm Analysis. CENG 707 Data Structures and Algorithms

The Running Time of Programs

CS/COE 1501

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

Position Sort. Anuj Kumar Developer PINGA Solution Pvt. Ltd. Noida, India ABSTRACT. Keywords 1. INTRODUCTION 2. METHODS AND MATERIALS

Organisation. Assessment

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 2014

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Algorithms and Data Structures, or

Choice of C++ as Language

Ph.D. Comprehensive Examination Design and Analysis of Algorithms

Review for Midterm Exam

Computer Algorithms. Introduction to Algorithm

CS 157: Assignment 5

Department of Information Technology

arxiv: v1 [cs.dm] 1 Jul 2016

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Module 2: Classical Algorithm Design Techniques

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Loops / Repetition Statements

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

COURSEBOOK for COMPSCI.220FT Algorithms and Data Structures

MA/CSSE 473 Day 17. Divide-and-conquer Convex Hull. Strassen's Algorithm: Matrix Multiplication. (if time, Shell's Sort)

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS JUN 2015

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Data Structures and Algorithms. Part 2

END-TERM EXAMINATION

WYSE Academic Challenge State Finals Computer Science 2007 Solution Set

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Data Structures Lecture 3 Order Notation and Recursion

Data Structures and Algorithms Chapter 2

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Outline. Computer Science 331. Desirable Properties of Hash Functions. What is a Hash Function? Hash Functions. Mike Jacobson.

Algorithm Analysis. Performance Factors

UNIT 1 ANALYSIS OF ALGORITHMS

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

CS 3410 Ch 5 (DS*), 23 (IJP^)

Geometric Data Structures

Subject: Computer Science

Lecture 3. Brute Force

Lecture 2: Getting Started

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Elementary maths for GMT. Algorithm analysis Part II

VISUALIZING NP-COMPLETENESS THROUGH CIRCUIT-BASED WIDGETS

JPROFILE102: A System for Experimental Analysis of Algorithms

Algorithms and Programming

CS 261 Data Structures. Big-Oh Analysis: A Review

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

arxiv: v1 [cs.ds] 3 Oct 2017

Midterm CSE 21 Spring 2012

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Sorting Pearson Education, Inc. All rights reserved.

Transcription:

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course Irena Pevac Department of Computer Science Central Connecticut State University, New Britain, CT, USA Abstract: We propose introducing algorithm time efficiency analysis by utilizing a set of template examples that illustrate the following seven basic time performance complexity categories: log n, n, n log n, n 2, n 3, a n, and n!. The complexity category of some function f(n) is the set of all functions that have the same asymptotic growth as f(n), which is denoted Ө(f(n)). The template examples are simple, their time efficiency analysis is not mathematically intensive, and they correspond to various algorithms. Both iterative and recursive algorithm templates are discussed. In addition, we provide template modifications that do not affect asymptotic growth. Modified templates remain in the same complexity category. Finally, we list several problems for which algorithm time performance belongs to a given complexity category. We argue that the approach of using templates and their transformations not only complements the introduction to time efficiency analysis, but also can improve understanding of this traditionally difficult topic in an Algorithms course. Its advantage is that it sheds some light as to which part or steps of the algorithm affect its run time the most. The template approach also provides flexibility for reaching the appropriate level of in depth discussion suitable for particular learner's background. Keywords: Algorithms, Time Performance Analysis, Teaching 1 Introduction Algorithm time efficiency is an important topic in an algorithms course. This paper discusses the mathematical approach to analyze time efficiency which is independent of the implementation and machine performance details and is used in algorithm course textbooks [1] to [8], [10], and [11]. Most textbooks introduce formal analysis with O, o, Ө, ω, and Ω, and illustrate growth rates functions: log n, n, n log n, n 2, n 3, a n, and n! by displaying their values for different values of n, and by displaying pictures of plotted functions together to provide comparison. The introduction is then typically followed by detailed time performance analysis of several algorithms. The students in an algorithms course often experience difficulty understanding formal analysis of mathematically involved advanced algorithms from various domains and design types. We believe that: 1) learning templates for each of the seven basic complexity categories; 2) learning which modifications of templates produce results that still remain in the same complexity category; and 3) practicing time efficiency analysis of simple algorithms for each complexity category should be done before starting with mathematically intensive analysis of algorithms that are difficult to grasp. This also smooths the transition into studying how to group algorithms by their design type and their domain. We strongly believe that this approach will improve learning the traditionally difficult topic of time efficiency analysis. 2 Templates for the seven basic complexity categories In the next subsections we discuss several templates used in many simple problems to analyze algorithms that belong to the following seven basic complexity categories: log n, n, n log n, n 2, n 3, a n, and n!. Both iterative and recursive algorithm templates are discussed.

Java based pseudo-code is used to write templates. Next, for each template, we list possible modifications that will not change its asymptotic time performance. We omit the illustration of each type of modification with concrete values and also omit proof that it does not change asymptotic growth. In the classroom, an instructor can provide the proof for one or two complexity categories and students can be divided into groups to prove the rest. At the end, for each of the seven complexity categories, we list several examples of problems that can be implemented by using one of the templates to produce an algorithm that belongs to that particular complexity category. More algorithm examples for various complexity categories can be found in [9]. In the classroom students could be asked to provide additional examples for each category. Again, work could be distributed among several student groups. 2.1 Templates for logarithmic run time Template 1 for (i=1; i<=n; i=i*2) Variations for template 1 include one or more of the following: 1) control variable i can be initialized to start at any nonnegative constant instead of starting at 1; 2) performing one basic operation in the loop can be replaced with performing any constant number of basic operations; and 3) control variable i can be multiplied by any other positive constant c in place of 2. Template 2 for (i=n; i>=1; i=i/2) Variations for template 2 include one or more of the following: 1) condition to stop looping can have control variable i greater than or equal to a nonnegative constant; 2) performing one basic operation in the loop can be replaced with performing any constant number of basic operations; and 3) control variable i can be divided by any positive constant c instead of 2. Template 3 LogRec(int n) if (n==1) { LogRec(n/2) } Variations for template 3 include one or more of variable n has one or more of given initial constant values; 2) performing one basic operation can be replaced with performing any constant number of basic operations either in the base case or in the recursive step part; and finally, 3) a recursive call can be made with variable n reduced by any positive constant factor c instead of 2. Obviously, it must be provided that base case is reached. The following simple algorithms with logarithmic time performance can be used to illustrate the above template examples: 1) determine the number of digits in binary representation of a given decimal number n; 2) determine the number of digits of a given integer number n; 3) convert base ten (decimal) number n into base 2 (or base 3, or base 8, or any other constant); 4) calculate a raised to the power of n based on formula a n = 1 if n=1, a n = a n/2 *a n/2 if n>1, and n is even, and a n = a * a n/2 *a n/2, otherwise; 5) print recursively letter "A" log b n times; 6) for given integer number n return the string of digits representing number n in base 3; 7) binary search for worst case (unsuccessful search); and 8) display the digits of a given positive integer n in reverse order.

2.2 Templates for linear run time Template 4 values; 2) performing one basic operation can be replaced with performing a constant number of basic operations in the base case or in the recursive step part; and finally, 3) recursive call can be made with variable n decreased by any positive constant c instead 1, provided that base case is reached.. Variations for template 4 include one or more of the following: 1) control variable i can be initialized to start at any nonnegative constant; 2) performing one basic operation in the loop can be replaced with performing any constant number of basic operations; and 3) control variable i can be increased by any positive constant c instead of 1. Template 5 for (i=n; i>=1; i=i-1) Variations for template 5 include one or more of the following: 1) the initial value for variable i can be n + constant (positive or negative) instead of n; 2) condition to stop looping can have control variable i greater than or eaqual to any positive constant; 3) performing one basic operation in the loop can be replaced with performing any constant number of basic operations; and 4) control variable i can be decreased by any positive constant c instead of 1. Template 6 LinearRec(int n) if n=1 { LinearRec(n-1) } Variations for template 6 include one or more of variable n has one or more of given constant Template 7 LinearRec(int n) if n>1 { LinearRec(n/2) LinearRec(n/2) } Variations for template 7 include one or more of variable n has one or more given constant values; 2) performing one basic operation can be replaced with performing any constant number of basic operations in the base case or in the recursive step part; 3) the order of performing one or more basic operations and making two recursive calls to sub-problems can be any permutation of those three steps; 4) several basic operations can be done before, in between, or after the two recursive calls; and finally, 5) recursive calls can be made with variable n reduced by another positive constant factor c instead of 2. Template 8 LinearRec(int n) if n>1 { LinearRec(n/3) LinearRec(n/3) LinearRec(n/3) } Variations for template 7 include one or more of variable n has one or more of given constant values; 2) performing one basic operation can be replaced with performing any constant number of basic operations in the base case or in the recursive step part before, after, or in

between any two of the three recursive calls to sub-problems; 3) a constant number k of recursive calls can be made instead of three calls, where each recursive sub-problem has variable n reduced by factor k. The following simple algorithms with linear time performance can be used to illustrate the above discussed template examples 4-7: 1) determine the sum of first n numbers; 2) determine the sum of first n squares; 3) determine the sum of first n cubes; 4) determine the sum of array components; 5) determine the harmonic sum; 6) determine the largest array component; 7) determine the index of the largest array component; 8) perform a linear search for a given key in the array with n components; 9) calculate a raised to the power of n based on the formula a n = 1 if n=1, and a n = a n/2 *a n/2 if n>1 and nis even, and a n = a * a n/2 *a n/2 otherwise, where two identical recursive calls to subproblems of size n/2 are made in the code; and 10) print letter "A" n times. 2.3 Templates for n - log n run time Template 9 for (j=1; j<=n; j=j*2) Variations for template 9 include one or more of the following: 1) control variable i of the outer loop can be initialized to start at any nonnegative constant instead of starting at 1; 2) performing one basic operation in the inner loop can be replaced with performing any constant number of basic operations; 3) control variable i can be increased by any positive constant instead of being increased by 1; 4) control variable j can be initialized to any positive constant; and 5) control variable j can be multiplied by another positive constant c instead of being multiplied by 2. Finally, since the two loops are not correlated they can be interchanged without affecting the time performance. Template 10 N_Log_N_Rec(int n) { N_Log_N_Rec( n/2 ) N_Log_N_Rec( n/2 ) Perform n basic operations } Variations for template 10 that do not affect asymptotic time performance include one or more of the following: base case might be one of more values for n that are constant. For such base case values any constant number of basic operations can be performed. During the recursive step the basic operation can be performed an+b times where a and b are constants. Those basic operations can be performed before, after or in between the two recursive calls of size n/2. Template 11 N_Log_N_Rec(int n) { N_Log_N_Rec( n/4 ) N_Log_N_Rec( n/4 ) N_Log_N_Rec( n/4 ) N_Log_N_Rec( n/4 ) Perform n basic operations } Variations for template 11 that do not affect asymptotic time performance include one or more of the following: 1) base case might be one of more values for n that are constant. For such base case values any constant number of basic operations can be performed; 2) during the recursive step the basic operation can be performed an+b times where a and b are constants. Those basic operations can be performed before, after or in between any two of the four recursive calls of size n/4. The following algorithms with n - log n time performance can be used to illustrate the above

discussed template examples 9-11: 1) draw letter "A" n log n times; 2) merge sort; 3) heap sort; and 4) draw the following squares using recursive algorithm. Base case is when side length is less than or equal to a given constant. No basic operation is performed in that case. At the highest level of recursion, one rectangle centered at x,y with side length equal to n is drawn. In addition, four recursive calls are made with center coordinates shifted 2n in each of the following four directions: 1) up, left, 2) up, right, 3) down, left, and 4) down, right. Each of the four recursive calls has size n reduced by factor 4. 2.4 Templates for quadratic run time Template 12 for (j=1; j<=n; j=j+1) Template 13 for (j=i; j<=n; j=j+1) Template 14 for (j=1; j<=i; j=j+1) All variations described for a single loop for linear time are applicable for each of the two nested loops in template examples 12-14. Template 15 QuadraticRec(int n) if (n>0) { do n basic operations QuadraticRec( n-1) } Variations to template 15 that preserve quadratic run time include: 1) base case and/or recursive step where several constant number of basic operations is performed; 2) a recursive call to a sub-problem with size decreased by constant value instead of n-1; 3) the order of recursive call to a sub-problem, and performing one, or performing some constant number of basic operations, can be interchanged. Template 16 QuadraticRec(int n) { QuadraticRec( n/2 ) QuadraticRec( n/2 ) QuadraticRec( n/2 ) QuadraticRec( n/2 ) Do constant number of basic operations } The following simple algorithms with quadratic time performance can be used to illustrate the above discussed template examples 12-16: 1) add all components in a given n x n matrix; 2) determine average of all components in a two dimensional array of size n x n; 3) determine the largest of all components in a two dimensional array of size n x n; 4) selection sort; 5) insertion sort; 6) bubble sort; 7) draw given letter "A" n squared times; 8) draw the squares using the following recursive algorithm: base case is when side length is less than or equal to a given constant. No basic operation is performed for the base case. At highest level of recursion, one rectangle centered at x,y with side length equal to n is drawn. In addition, four recursive calls are made with the center coordinates shifted 2n in each of the following four directions: 1) up, left, 2) up, right, 3) down, left, and 4) down, right. Each of the four recursive calls has size n reduced by factor 2; 9) draw the squares using the following recursive algorithm. Base case is when side length is less than or equal to a given constant. One basic operation is performed in that case. At the highest level of recursion, one rectangle is drawn centered at x,y with side length equal to a given constant. In addition, four recursive calls are made with center coordinates shifted 2n in each of the following

four directions: 1) up, left, 2) up, right, 3) down, left, and 4) down, right. Each of the four recursive calls has size n reduced by factor 2. } 2.5 Templates for cubic run time Template 17 for (j=1; j<=n; j=j+1) for (k=1; k<=n; k=k+1) Template 18 for (j=1; j<=i; j=j+1) for (k=1; k<=n; k=k+1) Variations are analogous to the variations specified for iterative quadratic templates. Template 19 CubicRec(int n) { do n squared basic operations CubicRec(n-1)} Variations to template 20 include base case for n equal to a given constant or n less than given constant and performing up to n*n basic operations. In recursive step, performing one basic operation can be replaced by performing a linear or quadratic number of basic operations. The following simple algorithms with cubic time performance can be used to illustrate the above discussed template examples 17-19: 1) add all components in a given n x n x n matrix; 2) determine the average of all components in a three dimmensional array of size n x n x n; 3) determine the largest of all components in a three dimmensional array of size n x n x n; 4) determine the lengths of the shortest paths between all pairs of vertices in a graph represented with adacency matrix (use Floyd algorithm); 5) print letter the "A" n^3 times; 6) determine the product of two n x n matrices using a brute force algorithm; 7) determine if a given three sets of cardinality n are disjoint; 8) apply Gaussian elimination to transform n x n matrix into upper triangular form; and 9) use Floyd algorithm for shortest paths between all pairs of vertices in a graph represented with adjacency matrix. 2.6 Templates for exponential run time Variations are analogous to the variations for the LinerRec example. Template 20 CubRec(int n) { Template 21 IterativeExponential(int n) limit=1 for (i=1; i<=n; i++) { for(j=1; j<= limit; j++) limit = limit * 2} Variations include: 1) starting loop variables from given constants instead of 1; or 2) ending the outer loop at n const; 3) we can replace the statement limit= limit*2 with the statement

limit= limit*k where k is a constant that is greater than 2. The run time will be a faster growing class but it will still be exponential k^n. for recursive algorithms only. Such learning tool could be used as a supplemental course material for Algorithms courses using several textbooks. Template 22 ExponentialRec(int n) if (n=0) { ExponentialRec( n-1 ) ExponentialRec( n-1 )} Variations include base case at n equal to or less than a given constant and performing a constant number of basic operations at base case or anywhere during the recursive step. 2.7 Templates for n factorial run time Template 23 FactorialRec(int n) if (n=1) for (int i=1; i<=n; i++) FactorialRec(n-1) The following algorithms with n factorial time performance can be used to illustrate the template example 23: 1) print all permutations for given n; 2) print all paths for traveling salesman (exsaustive search brute force algorithm); and 3) print letter "A" n factorial times. 3 Future work The above described approach could be used to design a learning tool similar to ESRATEA (Pevac, 2010) intended for learning and practicing time performance analysis of all types of algorithms. Currently, ESRATEA is designed References [1] Aho, A. V., Ullman, J. D. (1995). Foundations of Computer Science (2nd ed.). Computer Science Press. [2] Baase, S., Van Gelder, A. (2000). Computer Algorithms (3rd ed.). Addison Wesley Longman. [3] Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Clifford, S. (2007). Introduction to Algorithms (2nd ed.). MIT Press; McGraw- Hill Book Company. [4] Goodrich M., Tamassia R. (2002). Algorithm Design: Foundations, Analysis, and Internet Examples John Wiley & Sons. [5] Johnsonbaugh, R., Schaefer, M. Algorithms (2004). Pearson. [6] Levitin, A. (2007). The Design & Analysis of Algorithms (2nd ed.). Pearson- Addison Wesley. [7] McConnell, J. J. (2008). Analysis of Algorithms (2nd ed.). Jones and Bartlett Publishers. [8] Neapolitan, R., Naimipour K. (2004). Foundations of Algorithms Using Java Pseudocode Jones and Bartlett. [9] Pevac, I. ESRATEA Educational Software for Recursive Algorithm Time Efficiency Analysis WORLDCOMP'10 Proc. of the 2010 Int. Conf. on Frontiers in Education: Computer Science & Computer Engineering, (Ed. Arabnia, Clincy), Las Vegas, NV, SCREA Press, 2010, pages 367-373. [10] Sedgewick, R., Wayne, K. (2011) Algorithms (4th ed.). Addison Wesley. [11] Shaffer C. (1998) A Practical Introduction to Data Structures and Algorithm Analysis Prentice Hall.