Docsity

Operation Count and Compute Rank - Lecture Notes | EECS 281, Study notes of Algorithms and Programming

Partial preview of the text

Download Operation Count and Compute Rank - Lecture Notes | EECS 281 and more Study notes Algorithms and Programming in PDF only on Docsity!

Outline

Last time:

  • Intro: what this course is about
  • Binary search and simple (back-of-the-envelope) performance analysis

Today:

  • Operation Count
  • FindMax, ComputeRank

Mirror, mirror on the wall,....

Given two algorithms, how do you determine which is more efficient?

Runtime computation: name search

Population (size) Linear Binary EECS 281 (100) 10 ms 0.7 ms (lookup 7 names) UM (40000) 4 secs 1.5 ms Washtenaw 35 secs 1.8 ms MI (9 mil) 15 mins 2.3 ms US (290 mil) 8 hours 2.8 ms World (6 billion) 7 days 3.3 ms

Assume can search 10 names/ms! An 8-year old may take longer

Roofing Problem

A builder can carry two shingles on his shoulder as he climbs up the ladder. He then climbs down and carries two more shingles up the ladder. Each round trip (up and down the ladder) costs $2.

Questions:

  1. What is the cost to build a tree house (8 shingles)?
  2. What is the cost to build a shed (128 shingles)?
  3. What is the cost to build a house (2048 shingles)?

Answers:

  • 8 shingles: 2 ∗ d 8 / 2 e = 40
  • 128 shingles: 2 ∗ d 128 / 2 e = 128
  • 2048 shingles: 2 ∗ d 2048 / 2 e = 2048

What is the ONE operation we counted in this case?

Operation Count Example: Find Max

Given an array of N elements, find the index of the largest element

Questions:

  • What’s the fixed cost of the algorithm?
  • Which operation should we count to compute the variable cost?
  • How many times is this operation executed in the worst case?

Four Operation Count Accounting Rules

Rule 1: Consecutive Statements : S 1 ; S 2 ; S 3 ;... ; SN ; The runtime R of a sequence of statements is the runtime of the statement with the max runtime, max(R(S 1 ), R(S 2 ),... , R(SN )).

Rule 2: Conditional Execution : if (S 1 ) S 2 ; else S 3 ; The runtime of a conditional execution is max(R(S 1 ), R(S 2 ), R(S 3 )).

Four Accounting Rules (cont)

Rule 3: Iteration/Loop : for (S 1 ; S 2 ; S 3 )S 4 ; The runtime of a loop with n iterations is max(R(S 1 ), n ∗ R(S 2 ), n ∗ R(S 3 ), n ∗ R(S 4 )).

Rule 4: Nested Loop :

for (S 1 ; S 2 ; S 3 ) {

for (S 4 ; S 5 ; S 6 ) {

S 7 ;

Count inside out. The runtime is the max of the runtime of each statement multiplied by the total number of times the statement is executed.

Operation Count Example: Compute Rank

Given an unsorted array a of N elements, compute an array r, where r[i] is the rank of a[i] in a.

The rank of an element is defined as the number of all elements in the array that is smaller than it, plus the number of elements to its left that is equal to it.

Example: a[4, 3, 9, 3, 7] gives r[2, 0, 4, 1, 3]

Time Complexity of rank(): how many CMPs in total?

Aside: Pseudocode

  • high-level description of an algorithm
  • more structured than English prose
  • less detailed than a program
  • hides program design issues
  • mathematical formatting allowed
  • special notations : ‘=’ for assignment, ‘==’ for equality testing
  • To ease grading, you MUST use this syntax whenever pseudocode is asked for in this course