CS108L Computer Science for All Week 11: Video Summaries Search Algorithms Searching is what we when we want to find specific item among a group of items Linear (Sequential) Search Simplest search method: Check every one of its elements, one at a time and in sequence, until the desired one is found Psuedocode Specify the item you are looking for For each item in the list: Check list item to see if it is desired item YES stop the search and return the item's location. NO go to the next item on the list Pros Simple easy to understand and implement Works well for Small lists Single searches of unsorted lists Cons More time consuming than other methods The worst case search number items in the list Binary Search Method: Start with a sorted list Repeat Divide list in half Check to see which half your item is in Select that half Pros Powerful Fast Relatively easy to understand Works well for large sorted data sets Cons Must sort your list first More difficult to implement Document1 Recursion • • • • • • • • What is Recursion? ▫ It is a concept/method used in computer science and mathematics ▫ Recursive problem: The problem can be described as a reduced or smaller form of the same problem ▫ Sometimes the problem gets so small that the solution of the small problem is trivial can solved recursively or has a recursive implementation ▫ Recursion occurs in computer science when you use a recursive implementation Example 5! = 5 * 4 * 3 * 2 * 1 Or 5! = 5 * 4! Can be solved Recursively • Or in general n! = n * n-1 * n-2 * …* 3 * 2 * 1 Or n! = n * (n-1)! Implemented by calling a function or procedure in the body of that same function or procedure. (must be prevented from consuming excessive computing resources) Factorial Psuedocode factorial N if N <=1 (then factorial = 1) (else factorial = N * factorial[N-1]) Pros ▫ Recursive description --- simple, elegant, and easy to explain and understand. ▫ Recursive implementation --- straightforward to build and verify. Cons ▫ Can be difficult to understand at first. ▫ In some programming languages not as efficient as iteratively ▫ Can be difficult to program Circularity infinite loop MUST PUT IN A STOP Why use recursion? ▫ The problem/program may be easier to understand, solve and program using recursion ▫ When you have a recursive description recursive solution is the most direct solution path ▫ Usually creating an easy to verify code is more important than creating the most efficient code For every recursive algorithm, there is an equivalent iterative (looping) algorithm!! Document1 Algorithm Analysis • • • • • What do we mean? ▫ Develop an understanding of the characteristics of the algorithm for different input parameters. Why? ▫ Predict performance/compare/choose algorithms ▫ Frequently more than one solution - Sorting ▫ Understand how it works leads to improvement shorter, simpler, and more elegant algorithms Use Computation Complexity ▫ A way to analyze an algorithm ▫ Takes into account how difficult and how efficient or fast the algorithm is. Usually evaluate ▫ Amount of Time = Time Complexity ▫ Amount of Memory = Space Complexity Time Complexity = Number of Operations • Independent of programming language and computer used • Algorithms are a sequence of steps or operations • The more steps/operations, the longer the algorithm takes to run • Measure of time = number of operations ▫ What is an operation? Anytime you ask the computer to do something • Arithmetic operations • Comparisons • Swaps • Copies or saves • Accessing a list • Number of times through a loop ▫ How do we do it? • Calculation • Look at the basic “loop’ that is used for each input ▫ Do not look at operations outside the “loop” ▫ Put a time value on each operation • Look at how many times is the loop executed for each type/amount of input! • Look at how much input you have. • Run the program • Different types/amounts of inputs ▫ Look at Best/Worst/Average case • Best-Case complexity: This is the complexity of solving the problem for the best input of size n. • Worst-case complexity: This is the complexity of solving the problem for the worst input of size n. This is what we are really concerned with. Document1 Average-case complexity: This is the complexity of solving the problem on an average. Space Complexity (Memory used0 ▫ One algorithm may require more memory. ▫ If memory is a scarce resource, then Space Complexity (memory used) is important ▫ Not as much of a problem as it used to be but data sets are getting huge. There is often a time-space-tradeoff ▫ Want Low Computing time and Low Memory consumption. ▫ One then has to make a compromise Usually look at Computational Complexity (efficiency) ▫ Time Complexity ▫ Space Complexity - Memory ▫ Other things may be important Difficulty • Can we understand and implement it? • Can we modify it? Validation • Can we validate correctness of the code? Suitable • Does the code suit the problem? • • • • Document1

Download
# Video Summaries - New Mexico Computer Science for All