1 / 23

Chapter 20

Chapter 20. Recursion. Principle of Recursion. Find an algorithm to break the problem into simpler cases of the same problem Repeat the algorithm to break sub-problems into yet simpler cases Continue until the sub-problems reach terminal states or conditions (solutions)

golda
Download Presentation

Chapter 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 20 Recursion

  2. Principle of Recursion • Find an algorithm to break the problem into simpler cases of the same problem • Repeat the algorithm to break sub-problems into yet simpler cases • Continue until the sub-problems reach terminal states or conditions (solutions) • Combine the solutions back up the chain to reach the solution to the original problem

  3. Factorial—the classic recursion

  4. Fibonacci Numbers • Fibonacci studied how animal populations grew and discovered a mathematical model for “multiplying like rabbits” • Fib(0) = 0 • Fib(1) = 1 • Fib(n) = Fib(n-1) + Fib(n-2) for n > 1 • An example illustrating how an elegant algorithm can perform extremely poorly!

  5. A better implementation

  6. Recursive Binary Search • Binary search of an ordered list recursively divides the search space in half • Each iteration, the middle element is tested to see if it is the desired item (and returned if so) • Otherwise, the search is repeated for the upper or lower half depending upon whether the middle element is above or below the desired element in the search order

  7. Search illustrated • Desired element is 4 • 0 1 2 3 4 5 6 • mid is 3, search above • 4 5 6 • mid is 5, search below • 4 • element found

  8. Using RandomAccessFile too!

  9. Lab 7The Travelling Salesperson Problem • Write a recursive algorithm to solve the travelling salesperson problem • Due November 27 • Results can be shown graphically or the path can be output as an ordered list of nodes on the console • City coordinates will be provided on Thursday • Use the A* algorithm to optimize the search

  10. Problem Statement • The classic TSP requires finding the shortest path connecting a set of points. • Solving the problem optimally by brute force testing of all possible paths is an n! problem • Using heuristic algorithms to solve the problem by eliminating suboptimal paths or estimating near-optimal paths are two approaches

  11. The A* Algorithm • f'(n) = g(n) + h(n) • g(n) is the total distance it has taken to get from the starting position to the current location. • h(n) is the estimated distance from the current position to the goal destination/state.

  12. How it works • Two lists are used: the candidate path (CLOSED list), and the remaining nodes not yet added (OPEN list) • Initially the candidate path list is empty and all nodes are in the OPEN list • Initialize the shortest path to some value greater than the shortest possible path • Select the first node and add it to the path • Calculate h(1) to estimate the distance of the remaining path • g(1) is the actual sum of distances along the path

  13. At each step, add g(n) + h(n) to get the total projected path length f(n) • If all nodes are added to the path, resulting in a new shortest path, save the path and set the shortest path found to f(n) • Note that the final step is to connect the last node in the path to the first node in the path! • If f(n) > shortest path found so far, then stop expanding this path since it will not be the shortest

  14. Abandoning a path means returning from the recursive function without expanding any more nodes • At each step, use some heuristic to select the next node to add to the path to find g(n+1) and h(n+1) • Remove that node from the OPEN list and add it to the candidate path list • Repeat until all paths have been explored or eliminated

  15. The recursive function will return when all paths have been explored or eliminated • At that point, the shortest path found is the solution • The key to a fast solution is to create a good heuristic for estimating the remaining distance • A secondary optimization is efficiently choosing the next node to test on the path (which can be selected using h(n) to decide)

  16. Estimation Heuristics • To optimize the solution, h(n) must estimate a remaining distance that is less than or equal to the true remaining distance! • The simplest h(n) is to estimate zero, which is guaranteed to be a lower bound • Unfortunately, this would cause the algorithm to expand every path • We can do much better than that…

  17. Artificial and real intelligence • What would be a better estimate for h(n)?

  18. Artificial and real intelligence • What would be a better estimate for h(n)? • How about summing the distances to the nearest neighbor for each remaining node?

  19. Artificial and real intelligence • What would be a better estimate for h(n)? • How about summing the distances to the nearest neighbor for each remaining node? • How about summing the distances to the nearest neighbor not already on the path?

  20. Artificial and real intelligence • What would be a better estimate for h(n)? • How about summing the distances to the nearest neighbor for each remaining node? • How about summing the distances to the nearest neighbor not already on the path? • What are the trade-offs between these two heuristics? How would you decide which approach is better?

  21. Testing on a real map

More Related