Successfully reported this slideshow.                                Upcoming SlideShare
×

# Classic Computer Science Problems with Swift: enhancing your Swift and iOSdev skill set

2,967 views

Published on

Apple's Swift language is the de-facto standard for iOS and Mac development, and it's rapidly becoming a great choice for any general-purpose programming task. By exploring classic computer science problems in Swift, you'll deepen your skill as a programmer and also discover the breadth of the fantastic Swift language in way that's far more interesting and engaging than the typical language reference.

Save 42% off Classic Computer Science Problems with Swift with code slkopec at: https://www.manning.com/books/classic-computer-science-problems-in-swift

Published in: Software
• Full Name
Comment goes here.

Are you sure you want to Yes No • Be the first to comment

### Classic Computer Science Problems with Swift: enhancing your Swift and iOSdev skill set

1. 1. A* Search in Swift: navigating a maze Save 42% off Classic Computer Science Problems in Swift with code slkopec at manning.com.
2. 2. Building a maze Finding a path through a maze is analogous to many common search problems in computer science. So let’s use a maze to illustrate the A* algorithm. Our maze will be a two-dimensional array of Cell. A Cell is an enum with raw Character values where O will represent an empty space while a X will represent a blocked space.
3. 3. Building a maze // A Cell represents the status of a grid location in the maze enum Cell: Character { case Empty = "O" case Blocked = "X" case Start = "S" case Goal = "G" case Path = "P" } typealias Maze = [[Cell]]
4. 4. Building a maze The maze that is generated should be fairly sparse so that there is almost always a path from a given starting node to a given ending node (this is for testing out our algorithms after all). We’ll let the caller of generateMaze() decide on the exact sparseness. When a random number beats the threshold of the sparseness parameter in question, we’ll simply replace an empty space with a wall.
5. 5. Building a maze srand48(time(nil)) // seed random number generator // sparseness is the approximate percentage of walls represented // as a number between 0 and 1 func generateMaze(rows: Int, columns: Int, sparseness: Double) -> Maze { // initialize maze full of empty spaces var maze: Maze = Maze(repeating: [Cell](repeating: .Empty, count: columns), count: rows) // put walls in for row in 0..<rows { for col in 0..<columns { if drand48() < sparseness { //chance of wall maze[row][col] = .Blocked } } } return maze }
6. 6. Building a maze Now that we have a maze, we’ll also want a way to print it succinctly to the console. We want its characters to be close together so it looks like a real maze. func printMaze(_ maze: Maze) { for i in 0..<maze.count { print(String(maze[i].map{ \$0.rawValue })) } }
7. 7. Building a maze We’ll need a way to refer to an individual location in the maze that is Hashable. So let’s define a custom struct for maze locations. struct MazeLocation: Hashable { let row: Int let col: Int var hashValue: Int { return row.hashValue ^ col.hashValue } } func == (lhs: MazeLocation, rhs: MazeLocation) -> Bool { return lhs.row == rhs.row && lhs.col == rhs.col }
8. 8. Building a maze Let’s arbitrarily define the goal as always being at location 9, 9. let goal = MazeLocation(row: 9, col: 9) func goalTest(ml: MazeLocation) -> Bool { return ml == goal } We also need to build a successorsForMaze() that returns an appropriate successors() function for our maze.
9. 9. successorsForMaze() function func successorsForMaze(_ maze: Maze) -> (MazeLocation) -> [MazeLocation] { func successors(ml: MazeLocation) -> [MazeLocation] { //no diagonals var newMLs: [MazeLocation] = [MazeLocation]() if (ml.row + 1 < maze.count) && (maze[ml.row + 1][ml.col] != .Blocked) { newMLs.append(MazeLocation(row: ml.row + 1, col: ml.col)) } if (ml.row - 1 >= 0) && (maze[ml.row - 1][ml.col] != .Blocked) { newMLs.append(MazeLocation(row: ml.row - 1, col: ml.col)) } if (ml.col + 1 < maze.count) && (maze[ml.row][ml.col + 1] != .Blocked) { newMLs.append(MazeLocation(row: ml.row, col: ml.col + 1)) } if (ml.col - 1 >= 0) && (maze[ml.row][ml.col - 1] != .Blocked) { newMLs.append(MazeLocation(row: ml.row, col: ml.col - 1)) } return newMLs } return successors }
10. 10. Breadth-first and depth-first searches This presentation is about A* search, but it assumes some foreknowledge regarding BFS and DFS. Basically, In depth-first-search, the search proceeds along a continuously deeper path until it hits a barrier and must backtrack to the last decision point. In a breadth-first-search, the closest elements to the starting location are searched first. This is shown on the next slide.
11. 11. BFS DFS Breadth-first and depth-first searches
12. 12. A* Search Breadth-first and depth-first searches can be time consuming, like peeling an onion layer by layer. An A* search also aims to find the shortest path from a start state to a goal state, but does so in a different manner. An A* search uses a combination of a cost function and a heuristic function to focus its search on pathways most likely to get to the goal quickly.
13. 13. The cost function The cost function, g(n), examines the cost to get to a particular state. In the case of a maze, this would be how many previous steps we had to go through to get to the state in question. This essentially analyses what has already been done, i.e. in the case of a maze, how we got to wherever we are in the maze.
14. 14. The heuristic function The heuristic function, h(n), gives an estimate of the cost to get from the state in question to the goal state. This essentially is looking ahead and trying to predict how much more it will take to get to the end state, in this case, the end of a maze.
15. 15. The heuristic function It can be proven that, if h(n) is an admissible heuristic, then the final path found will be optimal. An admissible heuristic is one which never overestimates the cost to reach the goal (on a two- dimensional plane, an example would be a straight-line distance heuristic – since a straight-line is always the shortest path).
16. 16. Cost function + heuristic function The total cost for any state being considered is f(n), which is simply the combination of g(n) and h(n). In fact f(n) = g(n) + h(n). When choosing the next state to explore off of the frontier, A* search picks the one with the lowest f(n) – this is how A* search distinguishes itself from breadth- first search and depth-first search.
17. 17. Priority queues A* search uses a priority queue as the data structure for its frontier, when choosing the state on the frontier with the lowest f(n). This keeps elements in good internal order, such that the first element that emerges is the one with the highest priority – in this case the one with the lowest f(n).
18. 18. Priority queues in Swift Unlike the standard libraries of many modern programming languages, Swift’s standard library does not contain a built-in priority queue. Luckily, we don’t have to build one from scratch – the author of the book has already put one together for you! It can be found at the open source project SwiftPriorityQueue.
19. 19. SwiftPriorityQueue In order to determine the priority of a particular element versus another of its kind, SwiftPriorityQueue requires that the type of its elements implement the Swift standard library protocol comparable. For this reason, the Node class was defined as Comparable and therefore had to implement the < operator. A Node is compared to another by looking at its respective f(n), which is simply the sum of the properties cost and heuristic.
20. 20. Heuristics A heuristic is an intuition about the way to solve a problem that points in the right direction. In the case of maze solving, a heuristic aims to help choose the best maze location to search next, in the quest to reach the goal. Basically, a heuristic is an educated guess that comes as close to the real distance as possible without going over it.
21. 21. Euclidean Distance As you might remember from geometry class, the shortest path between two points is a straight line. Therefore, a straight line heuristic will always be admissible for any maze solving problem. The Euclidean distance, derived from the Pythagorean theorem, states that: distance = √((difference in x)² + (difference in y)²)
22. 22. Euclidean Distance For mazes, the difference in x is equivalent to the difference in columns of two maze locations, and the difference in y is equivalent to the difference in rows. This is shown in the code below. func euclideanDistance(ml: MazeLocation) -> Float { let xdist = ml.col - goal.col let ydist = ml.row - goal.row return sqrt(Float((xdist * xdist) + (ydist * ydist))) }
23. 23. Euclidean Distance Start Goal
24. 24. Manhattan distance Euclidean distance is great but for a maze in which you can only move in one of four directions, there is an even better solution: Manhattan distance. Manhattan distance is derived from navigating the streets of Manhattan, the most famous of New York City’s boroughs, which is laid out in a grid pattern.
25. 25. Manhattan distance To get anywhere in Manhattan, one needs to walk a certain number of horizontal blocks and a certain number of vertical blocks. Therefore, Manhattan distance is derived by simply finding the difference in rows between two maze locations and summing it with the difference in columns – as shown below. func manhattanDistance(ml: MazeLocation) -> Float { let xdist = abs(ml.col - goal.col) let ydist = abs(ml.row - goal.row) return Float(xdist + ydist) }
26. 26. Manhattan distance Start Goal
27. 27. Manhattan distance with A* search Because this heuristic more accurately follows the actuality of navigating our mazes (moving vertically and horizontally instead of in diagonal straight lines), it comes closer to the actual distance from any maze location to the goal than Euclidean distance does. For a grid, A* search coupled with Manhattan distance will search fewer states than an A* search coupled with Euclidean distance, so the former is a more efficient choice. However, Euclidean would be better than Manhattan for a non-grid problem.
28. 28. The A* algorithm We already have a priority queue, but one other thing we need is a dictionary, which will allow us to keep track of the lowest cost (g(n)) of each node we visit – as there is a heuristic function in play, in the case of inconsistency, some nodes might be visited twice. If a node found through a new direction has a lower cost to get to than was previously established, the new (cheaper) route is preferred.
29. 29. The A* algorithm For the sake of simplicity, the function astar() does not take a cost calculation function as a parameter. Instead we just consider every hop in our maze to have a cost of 1. This is displayed in the code on the next slide.
30. 30. The A* algorithm func astar<StateType: Hashable>(initialState: StateType, goalTestFn: (StateType) -> Bool, successorFn: (StateType) -> [StateType], heuristicFn: (StateType) -> Float) - > Node<StateType>? { // frontier is where we've yet to go var frontier: PriorityQueue<Node<StateType>> = PriorityQueue<Node<StateType>>(ascending: true, startingValues: [Node(state: initialState, parent: nil, cost: 0, heuristic: heuristicFn(initialState))]) // explored is where we've been var explored = Dictionary<StateType, Float>() explored[initialState] = 0 // keep going while there is more to explore while let currentNode = frontier.pop() { let currentState = currentNode.state // if we found the goal, we're done if goalTestFn(currentState) { return currentNode } // check where we can go next and haven't explored for child in successorFn(currentState) { let newcost = currentNode.cost + 1 //1 assumes a grid, there should be a cost function for more sophisticated applications if (explored[child] == nil) || (explored[child] !> newcost) { explored[child] = newcost frontier.push(Node(state: child, parent: currentNode, cost: newcost, heuristic: heuristicFn(child))) } } } return nil // never found the goal }
31. 31. A* search Congratulations, you have learned the efficient searching algorithm, A*. Go ahead and try the code for yourself: var maze3 = generateMaze(rows: 10, columns: 10, sparseness: 0.2) if let solution = astar(initialState: start, goalTestFn: goalTest, successorFn: successorsForMaze(maze3), heuristicFn: manhattanDistance) { let path = nodeToPath(solution) markMaze(&maze3, path: path, start: start, goal: goal) printMaze(maze3) } Code for these functions can be found here.
32. 32. See how to solve other classic computer science problems with Swift - save 42% off Classic Computer Science Problems in Swift with code slkopec at manning.com. Also see: