CSE 3358 Note Set 2

Preview:

DESCRIPTION

CSE 3358 Note Set 2. Data Structures and Algorithms. Overview:. What are we measuring and why? Computational complexity introduction Big-O Notation . Problem. For a problem Different ways to solve – differing algorithms - PowerPoint PPT Presentation

Citation preview

1

CSE 3358 NOTE SET 2Data Structures and Algorithms

2

Overview: What are we measuring and why? Computational complexity introduction Big-O Notation

3

Problem For a problem

Different ways to solve – differing algorithms

Problem: Searching an element in an arrayPossible searching algorithms?

4

The Selection Problem Problem: Find the Kth largest number in

a set Solution Possibilities:

5

The Selection Problem If dataset size = 10,000,000 and k =

5,000,000 Previous algos could take several days

Is one algorithm better than the other?

Are there better algorithms?

6

Efficiency Limited amount of resources to use in

solving a problem. Resource Examples:

We can use any metric to compare various algorithms intended to solve the same problem. Using some metrics in for some problems

might not be enlightening…

7

Computational Complexity Computational complexity – the amount

of effort needed to apply an algorithm or how costly it is.

Two most common metrics (and the ones we’ll use) Time (most common) Space

Time to execute an algorithm on a particular data set is system dependent. Why?

8

Seconds? Microseconds? Nanoseconds?

Can’t use the above when talking about algorithms unless specific to a particular machine at a particular time.

Why not?

9

What will we use? Logical units that express the relationship

between the size n of a data set and the amount of time t required to process the data

For algorithm X using dataset n, T(n) = amount of time needed to execute X using n.

Problem: Ordering of the values in n can affect T(n). What to do?

10

How we measure resource usage Three primary ways of mathematically

discussing the amount of resources used by an algorithm O(f(n)) Ω(f(n)) (f(n))

What is ___(f(n)) ?

11

The Definitions T(N) = O(f(n)) if there are positive

constants c and n0 such that T(N) <= c*f(n) when N >= n0.

T(N) = Ω (g(n)) if there are positive constants c and n0 such that T(N) >= c*g(n) when N >= n0.

T(N) = (h(n)) iff T(N) = O(h(n)) and T(N) = Ω (h(n))

12

The Goal??? To place a relative ordering on functions To examine the relative rates of

growth. Example:

13

Big - Oh T(N) = O(f(n))

What does this really mean?

Means: T is big-O of f if there is a positive number c

such that T is not larger than c*f for sufficiently large ns (for all ns larger than some number N)

In other words: The relationship between T and f can be

expressed by stating either that f(n) is an upper bound on the value of T(n) or that , in the long run, T grows at most as fast as f.

14

Asymptotic Notation: Big-OGraphic Example

15

Asymptotic Notation: Big-OhExample: Show that 2n2 + 3n + 1 is O(n2).

By the definition of Big-O 2n2 + 3n + 1 <= c*n2 for all n >= N.So we must find a c and N such that the inequality holds for all n > N. How?

16

Asymptotic Notation: Big-O Reality Check:

We’re interested in what happens to the number of ops needed to solve a problem as the size of the input increases toward infinity.

Not too interested in what happens with small data sets.

17

Notes on Notation Very common to see

T(n) = O(f(n)) Not completely accurate not symmetric about =

Technically, O(f(n)) is a set of functions. Set definition

O(g(n)) = f(n): there are constants c > 0, N>0 such that 0<=f(n)<=g(n) for all n > N.

When we say f(n) = O(g(n)), we really mean thatf(n)∈ O(g(n)).

18

Asymptotic Notation: Big-O

Show that n2 = O(n3).

19

Three Cases for Analysis Best Case Analysis:

when the number of steps needed to complete an algorithm with a data set is minimized

e.g. Sorting a sorted list Worst Case Analysis:

when the maximum number of steps possible with an algorithm is needed to solve a problem for a particular data set

Average Case Analysis:

20

Example The problem is SORTING

(ascending)

Best Case:

Worst Case:

Average Case:

59 3

8

Data Set: n = _______

21

T(N)? For a particular algorithm, how do we

determine T(N)? Use a basic model of computation.

Instructions are executed sequentially Standard simple instructions

Add, subtract, multiply, divide Comparison, store, retrieve

Assumptions: Takes one time unit, T(1) to do anything simple

Determining Complexity How can we determine the complexity of

a particular algorithm?int sum(int* arr, int size)

int sum = 0;

for (int i = 0; i < size; i++)

sum += arr[i];

return sum;

23

?

Recommended