Operation Counting

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Get a hint
Hint

Running Time

Get a hint
Hint

depends on the size and the organization of the input

generally, we seek upper bounds (worst case)

Get a hint
Hint

Limitations of Experimental Studies

Get a hint
Hint
  • The algorithm must be implemented, which may take a long time and could be very difficult

  • Results may not be indicative for the running time on other inputs that are not included in the experiments

  • In order to compare two algorithms, the same hardware and software must be used

  • Solution: 

    • Actual runtime can be estimated

    • We will analyse algorithms, and come up with classes of runtimes based on input size

Card Sorting

1/12

Anonymous user
Anonymous user
encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

13 Terms

1
New cards

Running Time

depends on the size and the organization of the input

generally, we seek upper bounds (worst case)

2
New cards

Limitations of Experimental Studies

  • The algorithm must be implemented, which may take a long time and could be very difficult

  • Results may not be indicative for the running time on other inputs that are not included in the experiments

  • In order to compare two algorithms, the same hardware and software must be used

  • Solution: 

    • Actual runtime can be estimated

    • We will analyse algorithms, and come up with classes of runtimes based on input size

3
New cards

Cost of Operations

  • To determine how long an algorithm takes to run we can count-up all the operations it executes

  • Let’s first consider what counts as one operation?

  • Want to be able to analyse the algorithm itself, independent of the choice of language used to execute

4
New cards

Operations and Instructions

Operations in a high-level language may require many machine-level instructions

However, we ignore minor details 

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Operations in a high-level language may require many machine-level instructions</span></p><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">However, we ignore minor details&nbsp;</span></p>
5
New cards

Operation Counting Example

  • The first thing we will do is count how many operations (ignoring minor details) this code executes

  • Given an array of integers arr of size n:

int maxel = arr[0];
for (int i = 0; i < n; i++) {
	if (arr[i] >= maxel) {
		maxel = arr[i];
	}
}
  • The first line of this code requires 1 operation:

    • As we are ignoring minor differences in implementation

  • This operations is always required by the algorithm, regardless of the value of ‘n’ 

  • The for loop initialization code also has to always run

  • This gives us two more operations: 

    • an assignment (‘i = 0’) and a comparison (‘i < n‘)

    • These will run before the first for loop iteration

  • After each for loop iteration, we need two more operations to run,

    • an increment of ’i’ (i.e. ‘i++’) and

    • a comparison (‘i < n’) to check if we'll stay in the loop 

    • Question: How many times do we go around the loop?

  • So, if we ignore the loop body, the number of operations this algorithm needs is 3 + 2n

    • 3 operations at the beginning of the for loop

    • 2 operations at the end of each iteration of which we have ’n’ 

  • Now, looking at the for body

  • We have an array lookup operation and a comparison that always happen

  • But the if body may run or may not run, depending on what the array values are

  • If it happens to be so that ’arr[i] >= maxel’, then we'll run an additional operation: maxel = arr[i]

  • So, we cannot define T(n) easily, because our number of operations doesn't depend solely on the size of input n but also on the organization of the input

    • For example, for arr = [ 1, 2, 3, 4 ] the algorithm will need more operations than for arr = [ 4, 3, 2, 1 ]

6
New cards

For Loop - General Rule

The number of times the instruction within a for loop is executed is given using a general rule

Care: to use this general rule the for loop must be in the above format

<p><span style="font-family: &quot;Open Sans&quot;, sans-serif">The number of times the instruction within a for loop is executed is given using a general rule</span></p><p><span style="font-family: &quot;Open Sans&quot;, sans-serif">Care: to use this general rule the for loop must be in the above format</span></p>
7
New cards

Worst Case Analysis (Mostly used)

  • In the worst-case analysis, we calculate the upper bound on the running time of an algorithm

  • We must know the case that causes a maximum number of operations to be executed

8
New cards

Best Case Analysis (Rarely used)

  • In the best-case analysis, we calculate the lower bound on the running time of an algorithm

  • We must know the case that causes a minimum number of operations to be executed

9
New cards

Average Case Analysis (Rarely used)

  • In average case analysis, we take all possible inputs and calculate the computing time for all of the inputs

  • Sum all the calculated values and divide the sum by the total number of inputs

10
New cards

Sentinel Search

A type of Linear search where the number of comparisons is reduced as compared to the linear search

11
New cards

Linear vs Sentinel Search

both have linear time complexity (worst case)

sentinel search requires executing less instructions

12
New cards

Growth Rate of Functions

(use to compare operation counting answers to find most efficient algorithm)

13
New cards

Summation Formula

<p></p>