Data Structure and Algorithms
• Algorithms: efficiency and complexity
• Recursion
• Reading
Algorithms
Algorithms
• Algorithms are stepwise solutions to problems
• There may be more than one algorithm for a particular problem
Efficiency of Algorithms
• Time efficiency
• Space efficiency
• There may be several algorithms to solve the same problem
• Sometimes a trade-off between time and space is necessary
Factors affecting/ masking speed
• Number of input values• Processor speed• Number of simultaneous programs etc• The location of items in a data structure• Different implementations (Software differences)
Time efficiency
• Count characteristic operations• eg arithmetical operations eg multiplications
• Examine critical section/s of algorithm– Single statement/group of statements/single
operation– Central to functioning of algorithm contained in
most deeply nested loops of algorithm
Critical SectionVisiting a Distant Friend
1. Close own door
2. Travel 100 miles by bus
3. Knock on friend’s door
Code Example
Statement 1;
for (int miles = 0; miles < 100; miles ++)
Statement 2;
Statement 3;
Travel 100 miles by bus
Statement 2;
Different Algorithms for Same ProblemFinding number power eg b3 b 20
1 set answer to 1
2 Loop 1 to power times
2.1 answer becomes answer * number
3 report answer
1 Set answer to 1, num to number, value to power
2 Loop while value is positive
2.1 if value is odd then multiply answer by num
2.2 divide value by 2 ignore remainder, multiply num by itself
3 Report answer
Second
b2 x b
b10 x b10
10 + 1 times
First
b x b x b
b x b …
20 times
Algorithm Comparison
• The first example is said to have complexity of order n written as O(n)
• The second example is said to have complexity of order log n written as O(log n)
• O(log n) is better than O(n)
Complexities in Increasing Order
1 O(1)
2 O(log n)
3 O (n)
4 O (n log n)
5 O (n2)
6 O (n3)
7 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 algorithms
• Linear search: unsorted array : O(n)
• Linear search: sorted array : O(n)
• Binary search: sorted array : O(log n)
Summary
• When problems are large use lower order algorithms
Recursion
A recursive method is a method that calls itself.
It must have 3 basic properties
1. A recursive call (a call to itself)
2. An end case to stop recursion
3. A test to determine whether to stop or continue recursion
Recursive method for factorial of N
The 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 Factorial
public int factorial(int N){ if (N = = 1) { return 1; } else { return N * factorial(N-1); }}
3 Test
2 End case stops recursion
1 Recursive call
Recursionpublic int factorial(int N){
if (N = = 1) { return 1; }
else { return N * factorial(N-1); }
}
Find the factorial of 5. (5!)
5!
5 * 4!
4 * 3!
3 * 2!
2 * 1!
1
Sequence of Calls Sequence of Returns
5!
5 * 4!
4 * 3!
3 * 2!
2 * 1!
1
= 2
= 6
= 24
= 120
Use Recursion
• Equivalent iterative solution is too complex
• Recursive solution is easy to understand and natural
Towers of Hanoi
• Move 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
1. 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 Integers
1. Problem Specification
A value for NAssuming N>= 0 sum the first N integers (if N=0 the sum is 0)
nSum(N)
Approaching Recursive Algorithms
2. Design 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
2. Strategy for Solution
• Solve nSum(N-1)
• Add N to the result so obtained
Approaching Recursive Algorithms3. Refine steps 1 and 2 if necessary
Think about the way you yourself would solve the problem in principle
If no strategy can be developed consider modifying the specification
3. Detailed Solution
If N <= 0 then return the result 0
Otherwise
• Solve nSum(N-1)
• Add N to the result so obtained
• Return this sum
Approaching Recursive Algorithms
4. Complete 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?
Approaching Recursive Algorithms
5. Implement in the chosen language
Write a method with parameters as specified in step 1
The simpler versions of the problem identified in step 2 are solved by invoking the same method recursively
Ensure that stopping conditions are correctly coded
static int nSum(int n) { // Returns sum of 1st n integers using recursion
if (n <= 0)
return 0;
else
return n + nSum(n-1);
}
Example: Largest in an Array of Integers
1. Problem Specification Pr(A,N): Find the largest value in first N positions of array A
2. 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
3. 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];}
}}