m180: data structures & algorithms in java algorithm analysis arab open university 1

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

Upload: emerald-marcia-mason

Post on 17-Jan-2016

227 views

Category:

Documents


1 download

TRANSCRIPT

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

1

M180: Data Structures & Algorithms in Java

Algorithm Analysis

Arab Open University

Page 2: 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.

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

Timing

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

Timing

• ArrayList: 0 milliseconds• LinkedList: 0 milliseconds

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

Timing

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

Timing

• Results Vary

• Variation is beyond our control.• ArrayList is roughly twice as fast as the method from

LinkedList.

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

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.

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

Asymptotic Notation

• Method A is 10n² - 5 milliseconds to process n elements.

• Method B is 100n + 200 milliseconds.

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

Asymptotic Notation

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

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?

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

Asymptotic Notation

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

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.

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

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.

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

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

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

Asymptotic Notation

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

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

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

Asymptotic Notation

• Problem:f (n) = 5n³ + 3n² – 4n + 11

• Answer:Θ(n³)

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

Asymptotic Notation

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

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.

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

Asymptotic Notation

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

Asymptotic Notation

• Synchronized absquatulation takes time in Θ(n²)

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

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.

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

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.

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

Counting Steps

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

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)

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

Counting Steps

• r: the number of ranks.• s: the number of suits.

– Θ((r + 1)s) = Θ(rs)

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

Counting Steps

• Deck constructor the most expensive step in the algorithm.

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

Counting Steps

• Adds up only the numbers for which j ≤ i.

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

Counting Steps

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

n

i

i1

)(2

)1( 2nnn

)( 2

1

nnnin

i

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

Counting Steps

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

Counting Steps

• A single for loop typically takes time in Θ(n) a doubly nested for loop typically takes time in Θ(n2).– 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.

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

Best, Worst, and Average Case

• Difficult to analyze because of the if statement.

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

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)

Page 34: M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 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:

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

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.

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

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

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

Best, Worst, and Average Case

Best case ≤ average case ≤ worst case