Problem Solving
Algorithms:
Efficiency, Analysis and Order
Chapter 1 Foundations of Algorithms
Problem Solving Techniques
• So far, in this course we have been talking about data
structures
• We now turn our attention from the data to the problems to be
solved
• There are often several fundamentally different
techniques that can be used to solve the same problem
• Applying a technique to a problem results in a
step-by-step procedure to solve the problem
• A good programmer is capable of analyzing the steps
the mind takes in solving a problem
The step-by-step solution
•
This step-by-step
procedure is called an algorithm
• The purpose of studying these problem-solving
techniques is so that you have a repertoire of techniques to consider when trying
to solve a problem
• A given problem can often be solved using several
techniques, but one may be much faster than the others
• So we will be concerned with analyzing how
efficient a technique is in solving a problem in terms of time and space
Communicating algorithms
• One way to write an algorithm is in English
– This is fairly hard to translate into a programming
language.
• Another way is in a programming language like Java
– We often get tangled in the syntax, and lose sight of
the logic (steps) of
the algorithm.
•
Another way is to
use pseudocode
– The text uses kind of a Java shorthand, but avoids
Java syntax when it is cumbersome
Examples of the text pseudocode
•
Keytype is much like Comparable; it means the items are from any ordered set
• Array indexing can be from ranges other than 0..n; in this pseudocode, the range is specified
• If mathematical expressions or English is more
succinct or clear than Java, it is used:
Examples: If (low< x < high)
exchange x and y
• Look at overhead of binary search in psedocode
Importance of Developing Efficient
Algorithms
•
Problem:
Searching a sorted array
–
We have two
algorithms Binary and Sequential Search
• Suppose the item we are looking for is not in the
array and the list has 16 items
–
How many
comparisons will a sequential search make?
–
How many
comparisons will a binary search make?
• If we double the number of items in the list, how many
comparisons will each make then?
• Every time we double the size of the list, we add just
one more comparison to a binary search
Algorithms to for Fibonacci sequence
• There are at least two algorithms to find the nth
term of the Fibonacci sequence
• Here is the Fibonacci sequence; how do we find the
next term?
1 1
2 3 5
8 13 ……
• Two algorithmic techniques
– Recursive
– Iterative
Recursive algorithm to find nth term
• public static int fib(int n)
{ if (n<=1) return n;
else return fib(n-1) +
fib(n-2);
}
• How efficient is this algorithm? Look at the number of terms computed
Efficiency of recursive
solution.
• Notice that the number of terms more than doubles
every time n increases by 2.
• Call T(n) the number of terms.
•
Then T(n) > 2*T(n-2)
• We know that T(0) =1
•
How did you
book derive T(n) > 2n/2
•
To see this, take
a concrete case n=6 (work on board)
Efficiency of the iterative solution
• The recursive solution required that the same value be
computed over and over
• Suppose we save each value as it is computed
• Look at iterative code on overhead
– Once the values are computed, they are saved in an
array.
The moral of this story
• The technique used to solve problems makes a lot of
difference!!