This blogpost is based on a “Path finding in AI” chapter in [[Algo in a nutshell]] . The illustration problem is the 8-puzzle. The chapter’s highlight is the famed A*search, but I will focus on the brute-force DFS/BFS and other basic stuff.
Each tree node is a “board-state”, or “snapshot”. Each directed edge between two nodes is a “move”.
(Not every developer candidate has the “tree” concept for a board problem. Recall the BBG odometer problem?)
The tree is recombinant, since two paths can reach the same snapshot. Therefore, the search tree is a directed graph. In practice, this directed graph is more like a tree, so it’s useful to treat it as a tree.
Q: shall we use edge set to represent this directed graph?
A: I doubt it , because the graph is being constructed as we go. The graph is not given to us as static input.
Equivalence between two snapshots is based on a snapshot.key() method. Conceptually, two equivalent snapshots be mirror images or rotated images of each other.
snapshot.storedData() returns arbitrary stored data for a given tree node.
— BFS on this type of search tree
- would evaluate a tremendous (costly) number of snapshots
- 🙂 no recursion
- 🙂 queue is constant-time
- 🙂 guarantees to find shortest path
— DFS on this type of search trees
- maintains a relative small stack of “open” nodes and a large set of “closed” nodes. Closed means fully explored dead-ends.
- .. I think open means not fully explored, including the white and grey nodes defined on P144.
- .. what if we come back to a node existing on the current path? It would represent a useless move, not one of the validMoves()
- DFS (not BFS) features back-tracking
- 😦 DFS doesn’t know when the current node is steps away to the endgame
— relevance to CIV?
I guess some advanced interviews (Google?) may pose an unusual problem that has elegant solutions based on the essentials of path-finding. The problem might be solvable on-paper in 45-minutes iFF you have those basic concepts, either from reading, or raw intelligence, or “intelligent conversation with the interviewer“. I would say prior reading is a huge advantage.
In reality, most candidates won’t have the knowledge or the raw intelligence, so the conversation is really the only way out. Interviewer knows that you the candidate may have zero prior experience, so she attempts to drop hints to “guide” you, as a test of your communication/problem-solving capacity. I remember Rahul said some candidates (like Deepak) simply couldn’t get the hint. Sadly, I don’t always get the hint. Recall the first Facebook onsite.