Docsity

Data Structures and Algorithms - Homework 1 Sample Questions | EECS 281, Assignments of Algorithms and Programming

Partial preview of the text

Download Data Structures and Algorithms - Homework 1 Sample Questions | EECS 281 and more Assignments Algorithms and Programming in PDF only on Docsity!

Outline

Announcements:

  • HW1 will be online today; Due: Tue, 9/25, 1:00 pm at 281 lockbox in 2240 EECS

Last time:

  • Operation count
  • FindMax, ComputeRank

Today:

  • ComputeRank in N log N time
  • BubbleSort
  • Review of asymptotic analysis
  • Empirical performance evaluation

Algorithm Design

Typical approach:

  • define problem to be solved
  • understand its complexity
  • decompose it into smaller subtasks

Creative refinements:

  • think outside the box: exploit any particular characteristics of the workload or problem
  • don’t lose the forest for the tree: find the real performance bottleneck of the whole program, note coding cost
  • optimize for the common case

Example of Exploiting Characteristics: Bubble Sort v.

Sort array a[20,30,15,10,35,25] by “bubbling” the largest element to the right

Stop program as soon as array is sorted (How do you know that the array is sorted?)

How many CMPs does it take in the worst case?

Taking Advantage of Workload Characteristics

What if array is already sorted initially?

  • bubble1 still takes N(N-1)/2 CMPs
  • bubble2 takes N-1 CMPs

Implication: performance of a given alg. depends on the characteristics of the offered workload (initial values, input)

Conversely, depending on the expected workload, some algorithm/data structure may be more suitable than others

Don’t Lose the Forest...

Operations useful in maintaining an address book:

  • search for a name
  • insert a new name
  • delete a name

How do you do insertion and deletion on an array?

How much does each operation cost, on average, for sorted and unsorted array?

Which algorithm to use (shall we keep the array sorted)?

The Language of Algorithm Analysis

How do you convince your boss that your solution is better than an existing solution?

Big-Oh: Example

Algorithm A has time complexity f (n) = 8n + 128 Algorithm B has time complexity g(n) = n^2

Can we say that f (n) = O(g(n))? Is 8 n + 128 ≤ c · n^2?

Let c = 1, clearly for n = 8, f (n) > g(n). At what value of no is g(n) > f (n), ∀n ≥ no? How about if c = 2 or c = 4? See Fig. 3.1.

Big-Oh: Summary

As long as there is a c > 0 , and an no ≥ 0 , such that c · g(n) ≥ f (n), for all n ≥ no, you can say f (n) = O(g(n)). In this case, 8 n + 128 = O(n^2 ).

In other words, we only care about LARGE n, it doesn’t matter what c is.

Also, asymptotically, n^2 + k = O(n^2 ), why?

(Obviously, c cannot be 10100 (one googol , the conjectured upper bound on the number of atoms in the observable universe)!)

The Common Case: Empirical Performance

What if no > average n or common case n?

To determine the common case performance, given known workload, run empirical peformance measurement.

Note that common case performance is not necessary average case performance. (Why not?)

Pay attention to:

  • system speed
  • system load
  • compiler optimization

Experiment Setup

Factors involved in running an empirical performance study:

  • workload generator: does it generate realistic common cases?
  • reducing system variability: o use the same compiler o use the same machine o run experiment around the same time of day (or use unloaded machine)