230 likes | 373 Views
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)
E N D
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) • Combine the solutions back up the chain to reach the solution to the original problem
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!
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
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
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
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
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.
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
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
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
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)
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…
Artificial and real intelligence • What would be a better estimate for h(n)?
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?
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?
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?