23
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1

CSE 3358 Note Set 2

  • Upload
    erma

  • View
    53

  • Download
    0

Embed Size (px)

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

Page 1: CSE 3358  Note Set 2

1

CSE 3358 NOTE SET 2Data Structures and Algorithms

Page 2: CSE 3358  Note Set 2

2

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

Page 3: CSE 3358  Note Set 2

3

Problem For a problem

Different ways to solve – differing algorithms

Problem: Searching an element in an arrayPossible searching algorithms?

Page 4: CSE 3358  Note Set 2

4

The Selection Problem Problem: Find the Kth largest number in

a set Solution Possibilities:

Page 5: CSE 3358  Note Set 2

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?

Page 6: CSE 3358  Note Set 2

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…

Page 7: CSE 3358  Note Set 2

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?

Page 8: CSE 3358  Note Set 2

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?

Page 9: CSE 3358  Note Set 2

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?

Page 10: CSE 3358  Note Set 2

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)) ?

Page 11: CSE 3358  Note Set 2

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))

Page 12: CSE 3358  Note Set 2

12

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

growth. Example:

Page 13: CSE 3358  Note Set 2

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.

Page 14: CSE 3358  Note Set 2

14

Asymptotic Notation: Big-OGraphic Example

Page 15: CSE 3358  Note Set 2

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?

Page 16: CSE 3358  Note Set 2

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.

Page 17: CSE 3358  Note Set 2

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)).

Page 18: CSE 3358  Note Set 2

18

Asymptotic Notation: Big-O

Show that n2 = O(n3).

Page 19: CSE 3358  Note Set 2

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:

Page 20: CSE 3358  Note Set 2

20

Example The problem is SORTING

(ascending)

Best Case:

Worst Case:

Average Case:

59 3

8

Data Set: n = _______

Page 21: CSE 3358  Note Set 2

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

Page 22: CSE 3358  Note Set 2

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;

Page 23: CSE 3358  Note Set 2

23

?