230 likes | 358 Vues
This overview introduces key concepts in computational complexity and Big-O notation, providing an essential foundation for solving algorithmic problems effectively. It discusses how to measure and analyze algorithm efficiency, including the time and space resources required. The document examines various searching algorithms, the selection problem of finding the Kth largest number, and emphasizes the importance of performance metrics in algorithm comparison. With practical examples and theoretical definitions, it highlights how to assess and understand algorithm complexity comprehensively.
E N D
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 • Problem: Searching an element in an arrayPossible searching algorithms?
The Selection Problem • Problem: Find the Kth largest number in a set • Solution Possibilities:
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?
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…
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?
Seconds? Microseconds? Nanoseconds? • Can’t use the above when talking about algorithms unless specific to a particular machine at a particular time. • Why not?
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?
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)) ?
The Definitions • T(N) = O(f(n)) if there are positive constants c and n0such that T(N) <= c*f(n) when N >= n0. • T(N) = Ω (g(n)) if there are positive constants c and n0such that T(N) >= c*g(n) when N >= n0. • T(N) = (h(n)) iffT(N) = O(h(n)) and T(N) = Ω (h(n))
The Goal??? • To place a relative ordering on functions • To examine the relative rates of growth. • Example:
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.
Asymptotic Notation: Big-O Graphic Example
Asymptotic Notation: Big-Oh Example: 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?
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.
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)).
Asymptotic Notation: Big-O Show that n2 = O(n3).
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:
Example • The problem is SORTING (ascending) • Best Case: • Worst Case: • Average Case: Data Set: n = _______ 5 3 9 8
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 (inti = 0; i < size; i++) sum += arr[i]; return sum; }