Download presentation

Presentation is loading. Please wait.

Published byKatelyn Hansen Modified over 2 years ago

1
Input sizeTime I1I1 T1T1 I2I2 T2T2 …… Algorithm Analysis We want to quantify the behavior of an algorithm. Useful to compare efficiency of two algorithms on the same problem. Observations: 1.A program (algorithm) consumes resources: time and space 2.Amount of resources directly related to size of input. Say, we have the following table:

2
How can we derive the function T = f(I) ? Problems: Too many parameters to learn this function It depends on Machine in which program is run Compiler used Programming language Programmer who writes the code Solution: Imagine our algorithm runs on an algorithm machine that accepts pseudo-code. Assumptions: READs and WRITEs take constant time Arithmetic operations take constant time Logical operations take constant time

3
Even though we made various assumptions, … it is still complicated. Instead, we quantify our algorithm on the worst-case input. This is called worst-case analysis Also, the average-case analysis exists: Requires probability distribution of set of inputs which is usually unknown. Not studied in this course.

4
Input Size: not always easy to determine, and problem dependent. Some examples: 1.Graph-theoretic problem: Number of vertices, V, and number of edges, E. 2.Matrix multiplication: Number of rows and columns of input matrices. 3.Sorting: The number of elements, n.

5
Not needs to find exactly what T = f(I) is, but we can say: T(I) = O(f(I)) For example, the time complexity of mergesort of n elements: T(n) = O(n log n) What does it mean? Behavior of mergesort is better than a constant times n log n, where n n 0.

6
Growth of Functions: The analysis of the complexity of an algorithm is linked to a problem of growth of functions. Let f(n) be a function of a positive integer n. The dominant term of f(n) determines the behavior of f(n) as n. For example, let: f(n) = 2n 3 + 3n 2 + 4n + 1 The dominant term of f(n) is 2n 3. This means that as n becomes large (n ): 2n 3 dominates the behavior of f(n) The other terms contributions become much less significant.

7
Example 2: The term dominates the behavior of f(n) as n Example 3: The term dominates the behavior of f(n) as n The rate of growth means how function behaves as n. It is determined by its dominant term. The big-Oh notation is a short-hand way of expressing this.

8
The relationship: f(n) = O(n 2 ) is interpreted as: f(n) grows no faster than n 2 as n becomes large (n ). The dominant term of f(n) does not grow faster than n 2. It can grow as fast as n 2 the most accurate description. In this case: Another notation used, not discussed here. Also, O(n 2 ) is true for: We could make up as many such functions as we wish. In general, the description: the most accurate possible (the smaller).

9
Problem: To find the most accurate description for any function… in terms of the big-Oh notation. A formal definition of: f(n) = O(g(n)) is that the inequality: f(n) c * g(n) holds for all n n 0, where n 0 and c are positive constants f(n) and g(n) are functions mapping nonnegative integers to real numbers Informally f(n) is order g(n)

10
Input Size: n f(n)f(n) c * g(n) n0n0 Graphically:

11
Example: f(n) = 7n 2 +.5n + 6 and g(n) = n 2 f(n) is O(n 2 ), provided that c = 10 and n 0 = 2 n 0 = 2 7n 2 +.5n n 2

12
In general, if f(n) = a 0 + a 1 n + … + a d-1 n d-1 + a d n d Then, f(n) is O(n d ) We will see other functions too: For example: O(log n), O(n log n), etc. Having defined the big-Oh notation, now … Seven functions that often appear in algorithm analysis: Constant 1 Logarithmic log n Linear n N-Log-N n log n Quadratic n 2 Cubic n 3 Exponential 2 n

13
1.f(n) = 8 + n/2 + n 4 /10 4 O(n 4 ) 2. f(n) = log 4 n + nO(n) 3. f(n) = log 2 n + nO( n) 4. f(n) = n 2 - n - nO(n 2 ) 5. f(n) = n 2 /log 2 nO(n 2 /log 2 n) 6. f(n) = log 2/3 n + log n 2 O(log n) Others examples What is the smallest big-oh complexity associated to algorithms for which the running time is given by the following functions:

14
Analysis of Examples Given a list of n elements, find the minimum (or maximum). Then, T(n) = O(n) We look at all elements to determine minimum (maximum). Given n points in the plane, find the closest pair of points. In this case, T(n) = O(n 2 ) why? a brute-force algorithm that looks at all n 2 pairs of points.

15
Given n points in a plane, determine if any three points are contained in a straight line. In this case, T(n) = O(n 3 ) why? a brute-force algorithm that searches all n 3 triplets.

16
Maximum Contiguous Subsequence (MCS) Problem Given a sequence S of n integers: S = a 1, a 2, a 3, …, a n-1, a n a contiguous subsequence is: a i, a i+1, …, a j-1, a j, where 1 i j n. The problem: Determine a contiguous subsequence such that: a i + a i+1 + … + a j-1 + a j 0 is maximal. Some examples: S = -1, -2, -3, -4, -5, -6 MCS is empty, it has value 0 by definition.

17
For the sequence: -1, 2, 3, -3, 2, an MCS is 2, 3 whose value is = 5. Note: There may be more than one MCS. For example: -1, 1, -1, 1, -1, 1 has six MCS whose value is 1

18
An O(n 2 ) Algorithm for MCS Search problems have an associated search space. To figure out: How large the search space is. For the MCS problem: How many subsequences need be examined? For example: -1, 2, 3, -3, 2 Then, the subsequences that begin with –1 are: -1, 2 -1, 2, 3 -1, 2, 3, -3 -1, 2, 3, -3, 2

19
The ones beginning with 2 are: 2 2, 3 2, 3, -3 2, 3, -3, 2 Those beginning with 3 are: 3 3, -3 3, -3, 2 The ones beginning with –3: -3 -3, 2 and beginning with 2, just one: 2

20
Then, including the empty sequence, a total of 16 examined. In general, given a 1, a 2, a 3, …, a n-1, a n We have n sequences beginning with a 1 : a 1 a 1, a 2 a 1, a 2, a 3 …. a 1, a 2, a 3, …, a n-1, a n n-1 beginning with a 2 : a 2 a 2, a 3 …. a 2, a 3, …, a n-1, a n

21
and so on. Then, two subsequences beginning with a n-1 : a n-1 a n-1, a n and, finally, one beginning with a n a n Total of possible subsequences: … n-1 + n + 1 = n(n+1)/2 + 1 Analysis: The dominant term is n 2 /2, hence search space is O(n 2 ). A brute-force algorithm follows…

22
Algorithm MCSBruteForce Input: A sequence a 1, a 2, a 3, …, a n-1, a n. Output: value, start and end of MCS. maxSum 0 for i = 1 to n do Set sum 0 for j = i to n do sum sum + aj if (sum > maxSum). maxSum sum start i end j Print start, end, maxSum and STOP.

23
Improved MCS Algorithm Think of avoiding looking at all the subsequences. Introduce the following notion. Given: a i, a i+1, …, a k, a k+1, …, a j (1) the subsequence: a i, a i+1, …, a k is a prefix of (1), where i k j. The prefix sum is: a i + a i+1 + … + a k Observation: In an MCS no prefix sum can be negative.

24
In the previous example, -1, 2, 3, -3, 2, we exclude: -1, 2 -1, 2, 3 -1, 2, 3, -3 -1, 2, 3, -3, 2 and -3 -3, 2 as being possible candidates.

25
In general: If ever sum < 0, skip over index positions from i+1, …, j Also, if sum 0 always for a starting position i, none of positions i+1, …, n is a candidate start position, since a i and all following prefix sums are non-negative. So the only time we need to consider a new starting position is when the sum becomes negative and all the index positions from i+1, …, j can be skipped. The improved MCS algorithm inspects a i just once. The algorithm follows….

26
Algorithm MCSImproved Set i 1; Set start end 1 Set maxSum sum 0 for j = 1 to n do sum sum + aj if (sum > maxSum) maxSum sum start i end j if (sum < 0) i j + 1 sum 0 Print start, end, maxSum and STOP.

27
Analysis of the Algorithms Algorithm MCSBruteForce: The outer loop is executed n times For each i, the inner loop is executed n – i + 1 times Thus, the total number of times the inner loop is executed: Algorithm MCSImproved: It has a single for loop, which visits all n elements. Hence,

28
What is the big-Oh complexity of the following algorithm? What is the value of Sum after the execution of this algorithm with the values: n = 5, m = 10, and p = 6? Sum = 2; For (i = 0, i < n; i++) { For (j = 0; j m; j++) { For (k = 1; k < p; k++) { Sum++;

29
What is the big-Oh complexity of the following algorithm? What is the value of Sum after the execution of this algorithm with the values: n = 8? Sum = -5; For (i = 0, i < n; i++) { For (j = 0; 2*j < i; j++) { Sum++;

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google