Docsity

Aspects of Algorithm Efficiency - Discrete Mathematical Structures - Lecture Slides, Slides of Discrete Mathematics

Partial preview of the text

Download Aspects of Algorithm Efficiency - Discrete Mathematical Structures - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

Chapter 9

Efficiency of Algorithms

Efficiency of Algorithms

Efficiency of Algorithms

  • Because the time efficiency can be influenced by many

physical parameters of a computing device, i.e. processor speed, memory size, multi-core, etc., a method of analysis must be used that is not a factor of the processing platform.

  • Evaluation of algorithms can be based on the number

of elementary operations required by the algorithm.

  • Elementary operations are addition, subtraction,

multiplication, division and comparison.

  • All elementary ops are viewed as taking 1 time unit on any

system.

Example

  • Consider algorithms A & B designed to accomplish the

same task. For input size of n

  • A requires 10n to 20n elementary operations.
  • B requires 2n 2 to 4n^2 elementary operations.
  • Which algorithm is more efficient?
  • for n ≤ 10, 2n^2 < 20n, and hence, B is better
  • for n > 10, 20n < 2n^2 , and in this case A is better
  • The answer is dependent on the size of the input. For a

small n B is better, but for a larger inputs A wins. It is important to understand the constraints on the order.

Time Comparisons of Algorithm Orders

Example

  • Consider the following algorithm segment

p =0, x=

for i = 2 to n

p = (p + i) * x

next I

  • Compute the actual number of elementary ops?
    • 1 multi, 1 add for each iteration.
    • 2 ops per iteration.
    • num of iteration = n – 2 + 1 = n-1.
    • num of elementary ops = 2(n-1)
  • Find an order from among the set of power functions
    • by theorem on polynomial orders, 2n – 2 is Θ(n)
    • and thus, segment is Θ(n).

Example

  • Consider the segment where the floor function is used.

for i = ⎣n/2⎦ to n

a = n – I

next I

  • Compute the actual number of subtractions performed.
    • 1 subtraction for each iteration.
    • loop iterates n - ⎣n/2⎦ + 1 times.
      • n is even: ⎣n/2⎦ = n/
      • n – n/2 + 1 = (2n – n + 2)/2 = (n+2)/
      • n is odd: ⎣n/2⎦ = (n-1)/
      • n – (n-1)/2 + 1 = [2n – (n-1) + 2]/2 = (n+3)/
  • Find an order for this segment
    • (n+2)/2 is Θ(n) and hence, segment is Θ(n)

Sequential Search

• Sequential search occurs when every element

in the list is compared to a particular x until

either a match is found or the end of the list.