concept state in category algorithms

This is an excerpt from Manning's book Grokking Artificial Intelligence Algorithms.
Figure 10.11 An example Q-table and states that it represents
![]()

This is an excerpt from Manning's book Classic Computer Science Problems in Swift: Essential techniques for practicing programmers.
class Node<T>: Comparable, Hashable { let state: T let parent: Node? let cost: Float let heuristic: Float init(state: T, parent: Node?, cost: Float = 0.0, heuristic: Float = 0.0) { self.state = state self.parent = parent self.cost = cost self.heuristic = heuristic } var hashValue: Int { return Int(cost + heuristic) } } func < <T>(lhs: Node<T>, rhs: Node<T>) -> Bool { return (lhs.cost + lhs.heuristic) < (rhs.cost + rhs.heuristic) } func == <T>(lhs: Node<T>, rhs: Node<T>) -> Bool { return lhs === rhs }
We will need one more little tidbit before we can get to implementing DFS. We need a Node class that will be used to keep track of how we got from one state to another state (or from one place to another place) as we search. You can think of a Node as a wrapper around a state. In the case of our maze-solving problem, those states are of type MazeLocation. We’ll call the Node that a state came from its parent. We will also define our Node class as having cost and heuristic properties and as being Comparable and Hashable, so we can reuse it later in the A* algorithm.
class Node<T>: Comparable, Hashable { let state: T let parent: Node? let cost: Float let heuristic: Float init(state: T, parent: Node?, cost: Float = 0.0, heuristic: Float = 0.0) { self.state = state self.parent = parent self.cost = cost self.heuristic = heuristic } var hashValue: Int { return Int(cost + heuristic) } } func < <T>(lhs: Node<T>, rhs: Node<T>) -> Bool { return (lhs.cost + lhs.heuristic) < (rhs.cost + rhs.heuristic) } func == <T>(lhs: Node<T>, rhs: Node<T>) -> Bool { return lhs === rhs }An in-progress depth-first search needs to keep track of two data structures: the stack of states (or “places”) that we are considering searching, which we will call the frontier; and the set of states that we have already searched, which we will call visited. As long as there are more states to visit in the frontier, DFS will keep checking whether they are the goal (if a state is the goal, it will stop and return it) and adding their successors to the frontier. It will also mark each state that has already been searched as visited, so that it does not get caught in a circle, reaching states that have prior visited states as successors. If the frontier is empty, it means there is nowhere left to search.
To find all of the permutations of the items in an array (eventually, our cities), we will also use backtracking. After we make a swap between elements and go down a path of further permutations, we will backtrack to the state before the swap was made so we can make a different swap and go down a different path.
Let’s develop some structures to keep track of the state of the game as it progresses.