Author
anne-stevenson
View
250
Download
0
Embed Size (px)
Data Structure and Algorithms
Algorithms: efficiency and complexityRecursionReading Algorithms
AlgorithmsAlgorithms are stepwise solutions to problemsThere may be more than one algorithm for a particular problem
Efficiency of AlgorithmsTime efficiencySpace efficiency
There may be several algorithms to solve the same problemSometimes a trade-off between time and space is necessary
Factors affecting/ masking speedNumber of input valuesProcessor speedNumber of simultaneous programs etcThe location of items in a data structureDifferent implementations (Software differences)
Time efficiencyCount characteristic operationseg arithmetical operations eg multiplicationsExamine critical section/s of algorithmSingle statement/group of statements/single operationCentral to functioning of algorithm contained in most deeply nested loops of algorithm
Critical SectionVisiting a Distant FriendClose own doorTravel 100 miles by busKnock on friends doorCode ExampleStatement 1;for (int miles = 0; miles < 100; miles ++) Statement 2;Statement 3;Travel 100 miles by busStatement 2;
Different Algorithms for Same ProblemFindingnumber power eg b3 b 20set answer to 1Loop 1 to power times2.1answer becomes answer * number3report answer1 Set answer to 1, num to number, value to power2 Loop while value is positive2.1 if value is odd then multiply answer by num2.2 divide value by 2 ignore remainder, multiply num by itself3Report answerSecond b2 x bb10 x b1010 + 1 timesFirstb x b x bb x b 20 times
Algorithm ComparisonThe first example is said to have complexity of order nwritten as O(n)
The second example is said to have complexity of order log nwritten as O(log n)
O(log n) is better than O(n)
Complexities in Increasing OrderO(1)O(log n)O (n)O (n log n)O (n2)O (n3)O (2n)For large problems always choose an asymptotically superior algorithm (ie an algorithm of the lowest order possible)Feasible algorithms are algorithms which are fast enough to be used in practice. (1-4 are always feasible)
Complexities and Search algorithmsLinear search: unsorted array: O(n)Linear search: sorted array: O(n)Binary search: sorted array: O(log n)
SummaryWhen problems are large use lower order algorithms
RecursionA recursive method is a method that calls itself.
It must have 3 basic propertiesA recursive call (a call to itself)An end case to stop recursionA test to determine whether to stop or continue recursion
Recursive method for factorial of NThe factorial of N is the product of the first N positive integers.
The simple case is that N = 1 which will represent the end case.If N is not equal to 1 then there is a need to call the method again (a recursive call to continue until the case N = 1 is reached)The method will successively make a call of the form:factorial(N-1) each time N is not equal to 1.
Recursive method for Factorialpublic int factorial(int N){ if (N = = 1) { return 1; } else { return N * factorial(N-1); }}
Recursionpublic int factorial(int N){ if (N = = 1) { return 1; } else { return N * factorial(N-1); }}Find the factorial of 5. (5!)Sequence of CallsSequence of Returns= 2= 6= 24= 120
Use RecursionEquivalent iterative solution is too complexRecursive solution is easy to understand and natural
Towers of HanoiMove a number of disks from one peg to another peg by moving one disk at a time and never placing a larger disk on top of a smaller disk.
To help this a third peg is available.
Towers of Hanoi
Towers of Hanoipublic void towersOfHanoi( int N, int source, int destination, int spare){ if ( N = = 1) { // move a single disk from source to destination moveOne ( source, destination ); } else { //spare is remaining pole; move tower of (N-1) disks from source to spare towersOfHanoi( N-1, source, spare, destination); //move single disk from source to destination moveOne( source, destination); //move tower of (N-1) disks from spare to destination towersOfHanoi( N-1, spare, destination, source ); }}
private void moveOne( int source, int destination ){ System.out.println( source + - - - + destination );}
Approaching Recursive Algorithms Formulate a Concise Specification of the Problem What data/information is needed initially? What precisely is accomplished? Represent the problem using an appropriate identifier with parameters Example: Sum of 1st N Positive IntegersProblem Specification A value for N Assuming N>= 0 sum the first N integers (if N=0 the sum is 0) nSum(N)
Approaching Recursive AlgorithmsDesign a high-level strategy for a solution Break the solution down into stages Identify at least one stage that involves solving a simpler version of the same problem Represent the simpler version(s) of the problem using the chosen identifier with appropriate parameters Strategy for Solution Solve nSum(N-1) Add N to the result so obtained
Approaching Recursive AlgorithmsComplete the details of the design Which stages need to be broken down further? Is it relatively straightforward to implement the solution? What are the stopping conditions or base cases? Are there any constraints on the parameters?
Example: Largest in an Array of IntegersProblem Specification Pr(A,N): Find the largest value in first N positions of array A Strategy for Solution Solve Problem Pr(A,N-1) Compare A[N-1] with the result so obtained Required result is the larger of the two values Detailed Solution If N = 1 then the result is A[0] Otherwise Solve Problem Pr(A,N-1) Compare A[N-1] with the result so obtained Required result is the larger of the two values
Implementation static int largest(int[] a, int n) { // Returns largest value of first n values in a int x; if (n > 0) { if (n == 1) return a[0];else { x = largest(a,n-1); if (x > a[n-1]) return x;else return a[n-1];}}}