Instruction count for statements Methods Examples

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Analysis of Algorithms
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
CSE115/ENGR160 Discrete Mathematics 03/10/11
Complexity (Running Time)
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Introduction to Computer Programming in c
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Analysis of Performance
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
MS 101: Algorithms Instructor Neelima Gupta
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
By the end of this session you should be able to...
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
12-CRS-0106 REVISED 8 FEB 2013 CSG523/ Desain dan Analisis Algoritma Mathematical Analysis of Nonrecursive Algorithm Intelligence, Computing, Multimedia.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Algorithm Design.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
CISC 235: Topic 1 Complexity of Iterative Algorithms.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Analysis of Algorithms
CSE15 Discrete Mathematics 03/13/17
COMP108 Algorithmic Foundations Algorithm efficiency
Computer Science 112 Fundamentals of Programming II
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Lecture 3 of Computer Science II
Time Complexity Problem size (Input size)
Algorithm Discovery and Design
Programming and Data Structure
Algorithm Discovery and Design
Analysis of Algorithms
Revision of C++.
COMP108 Algorithmic Foundations Searching
At the end of this session, learner will be able to:
COMP108 Algorithmic Foundations Searching
Analysis of Algorithms
Presentation transcript:

Instruction count for statements Methods Examples Topics Instruction count for statements Methods Examples

Computing Instruction Counts Given a (non-recursive) algorithm expressed in pseudo code we explain how to: Assign counts to high level statements Describe methods for deriving an instruction count Compute counts for several examples

Counts for High Level Statements Assignment loop condition for loop for loop body for loop control while loop while loop control while loop body if Note: The counts we use are not accurate; The goal is to derive a correct growth function

Assignment Statement 1. A= B*C-D/F Count1 = 1 In reality? At least 4 Note: When numbers B, C, D, F are very large, algorithms that deal with large numbers will be used and the count will depend on the number of digits needed to store the large numbers.

Loop condition (i < n)&&(!found) Count1 = 1 Note: if loop condition invokes functions, count of function must be used

for loop body for (i=0; i < n; i++) A[i] = i i = 0 Count2 = 1 false i < n stop true A[i] = i i = i + 1

for loop control Count = number times loop condition is executed for (i=0; i < n; i++) <body> Count = number times loop condition is executed (assuming loop condition has a count of 1) i < n false true Body of the loop i = i + 1

for loop control i = 0 for (i=0; i < n; i++) <body> Count1 = number times loop condition i < n is executed = n + 1 Note: last time condition is checked when i =n and (n < n) evaluates to false i < n false true Body of the loop i = i + 1

while loop control Count = number of times loop condition Line 1 i = 0 while (i < n){ A[i] = i i = i + 1} Count = number of times loop condition is executed (assuming loop condition has a count of 1) Line 2 i < n false true A[i] = i Line 3 i = i + 1 Line 4

while loop control Count2 = number times loop condition Line 1 i = 0 while (i < n){ A[i] = i i = i + 1} Count2 = number times loop condition (i < n) is executed = n + 1 Line 2 i < n false true A[i] = i Line 3 i = i + 1 Line 4

Countif = 1 +max{count2, count3} If statement Line 1: if (i == 0) Line 2: statement else Line 3: statement For worst case analysis, how many counts are there for Countif ? Countif = 1 +max{count2, count3}

Method 1: Sum Line Counts Derive a count for each line of code taking into account of all nested loops Compute total by adding line counts

Method 2: Barometer Operation A “barometer instruction” is selected Count = number of times that barometer instruction is executed. Search algorithms: barometer instruction (x == L[j]?). Sort algorithms: barometer instruction (L[i] <= L[j]?).

Example 1 Method 1 count1= n+1 count1(2) = n*1 = n _________________ 1. for (i=0; i<n; i++ ) 2. A[i] = i Method 1 count1= n+1 count1(2) = n*1 = n _________________ Total = (n+1)+n = 2n+1

Example 1 continued Method 2 Barometer operation = + in body of loop 1. for (i=0; i<n; i++ ) 2. A[i] = i + 1 Barometer operation = + in body of loop count1(+) = n

Example 2: What is count1(2)? Line 1 1.for (i=1;i<=n;i=3*i) 2. A[i] = i Line 1 i <= n no yes A[i] = i Line 2 i = 3* i Line 1

Example 2: What is count1(2)? Line 1 1. for(i=1;i<=n; i=3*i) 2. A[i] = i For simplicity, n = 3k for some positive integer k. Body of the loop executed for i = 1(=30), 31, 32,…,3k. So count1(2)= Since k = log3n, it is executed log3n + 1 times. Line 1 i <= n no yes A[i] = i Line 2 i = 3* i Line 1

Example 3: Sequential Search 1. location=0 while (location<=n-1 && L[location]! = x) 4. location++ 5. return location Barometer operation = (L[location]! = x?) Best case analysis Worst case analysis x == L[0] and the count is 1 x = L[n-1] or x not in the list. Count is n.

Example 4: Barometer is + in body of loop. count2(3(+))) = ? 1. x = 0 2. for (i=0; i<n; i++) 3. for (j=0, j<m; j++) 4. x = x + 1 Barometer is + in body of loop. count2(3(+))) = ?

Example 5: x=0 for (i=0; i<n; i++) for (j=0, j<n2; j++) x = x + 1 Count2(3(+))= ? Answer: n*n2*1

Example 6: Line 1: for (i=0; i<n; i++) Line 2: for (j=0, j<i; j++) Line 3. x = x + 1 Barometer = + Count1(2(+))= ?

Example 7: 1. for (i=0; i<n; i++) 2. for (j=0, j<i; j++) for (k=0; k<=j; k++) x++; count1(2(3))= Note:

Example of Count: Pigeonhole sort Input: Array T containing n keys in ranges 1.. S Idea: (Similar to Histogram) 1) Maintain the count of number of keys in an auxiliary array U 2) Use counts to overwrite array in place 1 7 Input T 2 1 2 4 3 1 2 1 2 3 4 Aux U 2 3 1 1 Output T 1 1 2 2 2 3 4

Algorithm: Pigeonhole-Sort( T, s) for i = 1 to s //initialize U U[i ] = 0 for j = 1 to length[T] U[T[ j ] ] = U[T[ j ] ] + 1 //Count keys q = 1 for j = 1 to s //rewrite T while U[j]>0 T[q] = j U[ j ] = U[ j ]-1 q = q+1

Pseudo code in text Body of a loop indicated by indentation for i =1 to n is equivalent to for (i =1; i <= n; i++) Arrays are assumed to start at index 1

Count: q  1 for j  1 to s //rewrite T while U[j] > 0 T[q] = j U[ j ]  U[ j ]-1 q  q+1 Barometer operation – in line 9 n (not n + s)

Count: Use loop condition of while as barometer operation Count6(7) = n + s since Is this algorithm efficient? Is there a better method than counting for complexity analysis?