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

## Presentation on theme: "M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1."— Presentation transcript:

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

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.

Timing

ArrayList: 0 milliseconds LinkedList: 0 milliseconds

Timing

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

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.

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

Asymptotic Notation

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?

Asymptotic Notation

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.

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.

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

Asymptotic Notation

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

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

Asymptotic Notation

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

Asymptotic Notation

Synchronized absquatulation takes time in Θ(n²)

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.

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.

Counting Steps

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)

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

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

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

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

Counting Steps

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.

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

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)

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:

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.

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

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

Similar presentations