Download presentation

Presentation is loading. Please wait.

Published byEmerald Marcia Mason Modified over 2 years ago

1
M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1

2
Timing To know which of two methods is faster, the most obvious approach is to time them. –System.currentTimeMillis() –The number of milliseconds since midnight, January 1, 1970, Greenwich mean time. –A modern computer is so fast that, on either of these data structures, the get() method takes less than one millisecond to run.

3
Timing

4
ArrayList: 0 milliseconds LinkedList: 0 milliseconds

5
Timing

6
Results Vary Variation is beyond our control. ArrayList is roughly twice as fast as the method from LinkedList.

7
Timing If we get element 50 instead of element 5. ArrayList, get() jumps right to the array element in question. LinkList traverse all the previous list nodes to find the one we want. Timing experiment provides empirical evidence. We can use some formal, mathematical tools to make statements about the efficiency of an algorithm.

8
Asymptotic Notation Method A is 10n² - 5 milliseconds to process n elements. Method B is 100n + 200 milliseconds.

9
Asymptotic Notation

10
The differences for small values of n are relatively insignificant. –What really concerns us is the asymptotic behavior of the running-time functions: What happens as n becomes very large?

11
Asymptotic Notation

12
To keep our running-time expressions general, we allow them to contain unspecified constants. –Algorithm A is an² + b –Algorithm B is cn + d a, b, c, and d are unspecified constants that depend on factors such as the speed of the hardware.

13
Asymptotic Notation Orders: –Functions can be classified into orders Monotonically non-decreasing: –f (n + 1) ≥ f (n) –Algorithm for which the running-time function did not fit into this category would be fairly strange.

14
Asymptotic Notation Θ(f ): –“the order of f ” or “order f ” n² is one of the functions in Θ(n²) –Θ(2ⁿ) is the largest –Θ(1) is the smallest. For sufficiently large n, a function is asymptotically larger than any function in a lower order. Example: –Θ(n log n) is asymptotically larger than any function in Θ(n).

15
Asymptotic Notation

16
Multiplying or dividing a function by a positive constant doesn't change its order. 3n² and 0.2n² are both in Θ(n²) A function's order is not changed by adding or subtracting a function in a lower order. 2ⁿ – n + log n is in Θ(2ⁿ)

17
Asymptotic Notation Problem: f (n) = 5n³ + 3n² – 4n + 11 Answer: Θ(n³)

18
Asymptotic Notation

19
Column frobbing algorithm is in Θ(n²) Row zorching algorithm is in Θ(n). Θ(n) is a lower order, so we should choose row zorching.

20
Asymptotic Notation

21
Synchronized absquatulation takes time in Θ(n²)

22
Counting Steps When we analyze an algorithm, we're aiming for: –The order of the running time. Write the algorithm down in precise English or in any programming language, such as Java. Determine how many steps are accomplished by each line and how many times the line is executed. The time used by the line is the product of these two expressions. The total running time for the algorithm is the sum of the time required for each line. The order of this expression is the same as the order of the most time-consuming line.

23
Counting Steps Size of the list: n –A single step: Accessing or setting a variable or field, including an element of an array. Addition, subtraction, multiplication, division, and other arithmetic operators. Finding the length of an array or String. Comparisons using ==, <, etc. Any fixed number of single steps, such as two additions and a variable assignment. Operators count as single steps. –This is not necessarily true of methods in the Java library.

24
Counting Steps

25
Tally starts at 0 and ends up equal to n, there must be n passes through the loop. –Running time for size() is linear. 1 2 3 4 5 6 8 (lines) c + c + c + c(n + 1) + cn + cn + c = 3cn + 5c Θ(n)

26
Counting Steps r: the number of ranks. s: the number of suits. –Θ((r + 1)s) = Θ(rs)

27
Counting Steps Deck constructor the most expensive step in the algorithm.

28
Counting Steps Adds up only the numbers for which j ≤ i.

29
Counting Steps Total number of passes in the inner loop: –1+2+...+n

30
Counting Steps

31
A single for loop typically takes time in Θ(n) a doubly nested for loop typically takes time in Θ(n 2 ). –Do not over-generalize the result about loops. –Enhanced for loops generally runs at most n times, where n is the number of elements in the data structure being traversed. –A loop may run less than n times if it is stopped early by a return or break statement or if it deals with more than one element on each pass.

32
Best, Worst, and Average Case Difficult to analyze because of the if statement.

33
Best, Worst, and Average Case We have to decide which kind of analysis we're doing. Best-case analysis –Tells us how fast the program runs if we get really lucky about the data. Contains() Θ(1)

34
Best, Worst, and Average Case Worst-case analysis –Contains() –This means assuming that target is not in the ArraList, giving a running time of Θ(n). Average-case analysis –Requires that we make some assumption about what the “average” data set looks like. –Average running time is:

35
Best, Worst, and Average Case We must always be careful to choose our events so that they are exhaustive –at least one of them will occur. Mutually exclusive –no more than one of them will occur. With n possible events, the probability of each event occurring is 1/n. –Assuming that they are equally likely.

36
Best, Worst, and Average Case In the contains() method, if target is at index –0, there is 1 pass through the loop. –1, there are 2 passes –...

37
Best, Worst, and Average Case Best case ≤ average case ≤ worst case

Similar presentations

Presentation is loading. Please wait....

OK

Lecture 2 Computational Complexity

Lecture 2 Computational Complexity

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google