Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Data Structures & Algorithm Data Structures & Algorithm.

Similar presentations


Presentation on theme: "Introduction Data Structures & Algorithm Data Structures & Algorithm."— Presentation transcript:

1 Introduction Data Structures & Algorithm Data Structures & Algorithm

2 Algorithm: Outline, the essence of a computational procedure, step-by-step instructions Program: an implementation of an algorithm in some programming language Data structure: Organization of data needed to solve the problem 2012-20132

3 3 History Name: Persian mathematician Mohammed al- Khowarizmi, in Latin became Algorismus First algorithm: Euclidean Algorithm, greatest common divisor, 400-300 B.C. 19 th century – Charles Babbage, Ada Lovelace. 20 th century – Alan Turing, Alonzo Church, John von Neumann

4 2012-20134 Algorithmic problem Infinite number of input instances satisfying the specification. For example: A sorted, non-decreasing sequence of natural numbers. The sequence is of non-zero, finite length: 1, 20, 908, 909, 100000, 1000000000. 3. Specification of input ? Specification of output as a function of input

5 2012-20135 Algorithmic Solution Algorithm describes actions on the input instance Infinitely many correct algorithms for the same algorithmic problem Input instance, adhering to the specification Algorithm Output related to the input as required

6 2012-20136 What is a Good Algorithm? Efficiency: Running time Space used Efficiency as a function of input size: Number of data elements (numbers, points) A number of bits in an input number

7 2012-20137 Measuring the Running Time How should we measure the running time of an algorithm? Approach 1: Experimental Study Write a program that implements the algorithm Run the program with data sets of varying size and composition. Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time.

8 2012-20138 Limitation Experimental Studies Experimental studies have several limitations: It is necessary to implement and test the algorithm in order to determine its running time. Experiments can be done only on a limited set of inputs, and may not be indicative of the running time on other inputs not included in the experiment. In order to compare two algorithms, the same hardware and software environments should be used.

9 2012-20139 Beyond Experimental Studies We will now develop a general methodology for analyzing the running time of algorithms. In contrast to the "experimental approach", this methodology: Uses a high-level description of the algorithm instead of testing one of its implementations. Takes into account all possible inputs. Allows one to evaluate the efficiency of any algorithm in a way that is independent from the hardware and software environment.

10 Pseudo-code Pseudo-code is a description of an algorithm that is more structured than usual prose but less formal than a programming language. Example: finding the maximum element of an array. Algorithm arrayMax(A, n): Input: An array A storing n integers. Output: The maximum element in A. currentMax  A[0] for i  1 to n -1 do if currentMax < A[i] then currentMax  A[i] return currentMax Pseudo-code is our preferred notation for describing algorithms. However, pseudo-code hides program design issues. 2012-201310

11 What is Pseudo-code? A mixture of natural language and high-level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm. Expressions: use standard mathematical symbols to describe numeric and boolean expressions use  for assignment (“=” in Java) use = for the equality relationship (“==” in Java) Method Declarations: Algorithm name( param1, param2 ) 2012-201311

12 What is Pseudo-code? Programming Constructs: decision structures: if... then... [else ] while-loops: while... Do repeat-loops: repeat... until … for-loop: for... Do array indexing: A[i] Functions/Method calls: object method(args) returns: return value 2012-201312

13 Analysis of Algorithms Primitive Operations: Low-level computations independent from the programming language can be identified in pseudocode. Examples: calling a function/method and returning from a function/method arithmetic operations (e.g. addition) comparing two numbers indexing into an array, etc. By inspecting the pseudo-code, we can count the number of primitive operations executed by an algorithm. 2012-201313

14 2012-201314 Sort Example: Sorting INPUT sequence of numbers a 1, a 2, a 3,….,a n b 1,b 2,b 3,….,b n OUTPUT a permutation of the sequence of numbers 2 5 4 10 7 2 4 5 7 10 Correctness For any given input the algorithm halts with the output: b 1 < b 2 < b 3 < …. < b n b 1, b 2, b 3, …., b n is a permutation of a 1, a 2, a 3,….,a n Correctness For any given input the algorithm halts with the output: b 1 < b 2 < b 3 < …. < b n b 1, b 2, b 3, …., b n is a permutation of a 1, a 2, a 3,….,a n Running time Depends on number of elements (n) how (partially) sorted they are algorithm Running time Depends on number of elements (n) how (partially) sorted they are algorithm

15 2012-201315 Insertion Sort A 1nj 368497251 i Strategy Start “empty handed” Insert a card in the right position of the already sorted hand Continue until all cards are inserted/sorted Strategy Start “empty handed” Insert a card in the right position of the already sorted hand Continue until all cards are inserted/sorted for j=2 to length(A) do key=A[j] “insert A[j] into the sorted sequence A[1..j-1]” i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key for j=2 to length(A) do key=A[j] “insert A[j] into the sorted sequence A[1..j-1]” i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key

16 2012-201316 Analysis of Insertion Sort for j=2 to length(A) do key=A[j] “insert A[j] into the sorted sequence A[1..j-1]” i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key cost c 1 c 2 0 c 3 c 4 c 5 c 6 c 7 times n n-1 n-1 n-1 n-1 Time to compute the running time as a function of the input size Total Time= n(c1+c2+c3+c7)+ (c4+c5+c6)- (c2+c3+c5+c6+c7)

17 2012-201317 Best/Worst/Average Case Best case : elements already sorted  t j =1, running time = f(n), i.e., linear time. Worst case : elements are sorted in inverse order  t j =j, running time = f(n 2 ), i.e., quadratic time Average case : t j =j/2, running time = f(n 2 ), i.e., quadratic time Total Time= n(c1+c2+c3+c7)+ (c4+c5+c6)-(c2+c3+c5+c6+c7)

18 2012-201318 Best/Worst/Average Case (2) For a specific size of input n, investigate running times for different input instances: 1n 2n 3n 4n 5n 6n

19 2012-201319 Best/Worst/Average Case (3) For inputs of all sizes: 1n 2n 3n 4n 5n 6n Input instance size Running time 1 2 3 4 5 6 7 8 9 10 11 12 ….. best-case average-case worst-case

20 2012-201320 Best/Worst/Average Case (4) Worst case is usually used: It is an upper-bound and in certain application domains (e.g., air traffic control, surgery) knowing the worst-case time complexity is of crucial importance For some algorithms worst case occurs fairly often The average case is often as bad as the worst case Finding the average case can be very difficult

21 2012-201321 Asymptotic Notation Goal : to simplify analysis by getting rid of unneeded information (like “rounding” 1,000,001≈1,000,000) We want to say in a formal way 3n 2 ≈ n 2 The “Big-Oh” Notation: given functions f(n) and g(n), we say that f(n) is O( g(n) ) if and only if there are positive constants c and n 0 such that f(n)≤ c g(n) for n ≥ n 0

22 2012-201322 Asymptotic Notation The “big-Oh” O-Notation asymptotic upper bound f(n) = O(g(n)), if there exists constants c and n 0, s.t. f(n)  c g(n) for n  n 0 f(n) and g(n) are functions over non- negative integers Used for worst-case analysis Input Size Running Time

23 2012-201323 Example g (n)  n c g(n )  4n n For functions f(n) and g(n) (to the right) there are positive constants c and n 0 such that: f(n)≤ c g(n) for n ≥ n 0 conclusion: 2n+6 is O(n).

24 2012-201324 Another Example On the other hand… n 2 is not O(n) because there is no c and n 0 such that: n 2 ≤ cn for n ≥ n 0 (As the graph to the right illustrates, no matter how large a c is chosen there is an n big enough that n 2 >cn ).

25 2012-201325 Asymptotic Notation (cont.) Note: Even though it is correct to say “7n - 3 is O(n 3 )”, a better statement is “7n - 3 is O(n)”, that is, one should make the approximation as tight as possible (for all values of n). Simple Rule: Drop lower order terms and constant factors 7n-3 is O(n) 8n 2 log n + 5n 2 + n is O(n 2 log n)

26 2012-201326 Asymptotic Analysis of The Running Time Use the Big-Oh notation to express the number of primitive operations executed as a function of the input size. For example, we say that the arrayMax algorithm runs in O (n) time. Comparing the asymptotic running time -an algorithm that runs in O (n) time is better than one that runs in O (n 2 ) time -similarly, O (log n) is better than O (n) -hierarchy of functions: log n << n << n 2 << n 3 << 2 n Caution! Beware of very large constant factors. An algorithm running in time 1,000,000 n is still O (n) but might be less efficient on your data set than one running in time 2n 2, which is O (n 2 )

27 2012-201327 Example of Asymptotic Analysis An algorithm for computing prefix averages Algorithm prefixAverages1(X): Input : An n-element array X of numbers. Output : An n -element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. for i  0 to n - 1 do a  0 for j  0 to i do a  a + X[ j ] A[ i ]  a/( i + 1) return array A Analysis... 1 step i iterations with i=0,1,2...n-1 n iterations

28 2012-201328 Another Example A better algorithm for computing prefix averages: Algorithm prefixAverages2(X): Input: An n-element array X of numbers. Output: An n -element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. s  0 for i  0 to n do s  s + X[ i ] A[ i ]  s/( i + 1) return array A Analysis...

29 2012-201329 Asymptotic Notation (terminology) Special classes of algorithms: logarithmic : O(log n) linear : O(n) quadratic : O(n 2 ) polynomial : O(n k ), k ≥ 1 exponential : O(a n ), n > 1 “Relatives” of the Big-Oh  (f(n)): Big Omega--asymptotic lower bound  (f(n)): Big Theta--asymptotic tight bound

30 2012-201330 Growth Functions

31 2012-201331 Growth Functions (2)

32 2012-201332 The “big-Omega”  Notation asymptotic lower bound f(n) =  (g(n)) if there exists constants c and n 0, s.t. c g(n)  f(n) for n  n 0 Used to describe best-case running times or lower bounds of algorithmic problems E.g., lower-bound of searching in an unsorted array is  (n). Input Size Running Time Asymptotic Notation (2)

33 2012-201333 The “big-Theta”  Notation asymptoticly tight bound f(n) =  (g(n)) if there exists constants c 1, c 2, and n 0, s.t. c 1 g(n)  f(n)  c 2 g(n) for n  n 0 f(n) =  (g(n)) if and only if f(n) =  (g(n)) and f(n) =  (g(n)) O(f(n)) is often misused instead of  (f(n)) Input Size Running Time Asymptotic Notation (4)

34 2012-201334 Asymptotic Notation (5) Two more asymptotic notations "Little-Oh" notation f(n)=o(g(n)) non-tight analogue of Big-Oh For every c, there should exist n 0, s.t. f(n)  c g(n) for n  n 0 Used for comparisons of running times. If f(n)=o(g(n)), it is said that g(n) dominates f(n). "Little-omega" notation f(n)=  (g(n)) non-tight analogue of Big-Omega

35 2012-201335 Asymptotic Notation (6) Analogy with real numbers f(n) = O(g(n))  f  g f(n) =  (g(n))  f  g f(n) =  (g(n))  f  g f(n) = o(g(n))  f  g f(n) =  (g(n))  f  g Abuse of notation: f(n) = O(g(n)) actually means f(n)  O(g(n))

36 2012-201336 Comparison of Running Times Running Time in  s Maximum problem size (n) 1 second1 minute1 hour 400n25001500009000000 20n log n40961666667826087 2n22n2 707547742426 n4n4 3188244 2n2n 192531

37 2012-201337 Math You Need to Review Logarithms and Exponents properties of logarithms: log b (xy) = log b x + log b y log b (x/y) = log b x - log b y log b x a = alog b x Log b a = log x a/log x b properties of exponentials : a (b+c) = a b a c a bc = (a b ) c a b /a c = a (b-c) b = a log a b b c = a c*log a b

38 2012-201338 A Quick Math Review Geometric progression given an integer n 0 and a real number 0< a  1 geometric progressions exhibit exponential growth Arithmetic progression

39 2012-201339 Summations The running time of insertion sort is determined by a nested loop Nested loops correspond to summations for j  2 to length(A) key  A[j] i  j-1 while i>0 and A[i]>key A[i+1]  A[i] i  i-1 A[i+1]  key for j  2 to length(A) key  A[j] i  j-1 while i>0 and A[i]>key A[i+1]  A[i] i  i-1 A[i+1]  key

40 2012-201340 Proof by Induction We want to show that property P is true for all integers n  n 0 Basis : prove that P is true for n 0 Inductive step : prove that if P is true for all k such that n 0  k  n – 1 then P is also true for n Example Basis

41 2012-201341 Proof by Induction (2) Inductive Step

42 Thank You 2012-201342


Download ppt "Introduction Data Structures & Algorithm Data Structures & Algorithm."

Similar presentations


Ads by Google