What is a search problem?
Tags: algorithms, computerscience, ai
Published: 2024-04-10 13:14:29

Bad news! Within the last 2 days, someone stole your bike.

You want to find out when.

If you can find the timestamp, you can identify the thief (and maybe the police can arrest them). However, there are 2 days worth of CCTV film to watch to pinpoint the time of the theft. [^2] [^3] 

That's your search problem -- you need to find 1 thing in an ocean of data.

Instead of watching every minute of CCTV recording, you use a search algorithm to find the time the thief struck. 

You pull up the halfway point in the recording. Is the bike still there? Was it stolen yet?

No, your bike is gone.

Now you know that your bike was stolen within the first day of CCTV footage. There is no point in looking for the thief in the second half of the recording. The theft already happened. The thief isn't coming back.

In one simple check, you halved the amount of information we had to search through. You started with 2 days (48 hours) of film, and now know you only need to look through the first 24 hours.

Let's repeat that search technique.

You know the bike was stolen in the first 24 hours of the recording. You jump to the 12 hour mark in the recording. The bike is where you left it. The theft hadn't occurred yet.

The thief stole your bike between the 12th and 24th hour in the CCTV footage. 

In 2 steps, you quartered the amount of time you had to search. You repeatedly check the halfway point in the possible period of the theft. Quickly, you discover when the thief stole your bike.


![visual of how the search states halves with each step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqhfjmfvhc3cdlmfz545.png)

In this example, you see the binary search algorithm in action to crack the search problem.

Here's what you need to know to address a search problem:
- set of states
- the start state
- the goal state
- how to get to the next state (a function)

When searching for your bike, you know the bike could either be:
1) where you left it
2) actively being stolen
3) missing
You are starting with your bike missing and what to find the start, the time, when the thief stole your bike. You use the binary search algorithm to move through the film recording to find a change in the bike's state.

Search problems are unique because you often have heaps of information, but don't know how to sift through it.

You know all the possible states you could end up in. You know which of those possible states you want. The problem is you don't know how to get there. 

What steps should you take? How do you pick what step is best when you have many options?

In many problems, you might not know what end state you desire? You don't know what you are pursuing. The challenge here instead is knowing what path to take.

**Simply: a search problem is how to find Y in X [^1].**

### Other interesting examples of search problems:
- `six degrees of separation`: everyone human on this planet is 6 or fewer social connections away from any other person on the planet
- `wikipedia game`: how can we get from one wikipedia page to another seemingly unrelated page in as few steps as possible?
	- ex: if you start at Obi-Wan Kenobi's page, how many links do you need to click to get to Search Problem?
	1. [Obi Wan Kenobi](https://en.wikipedia.org/wiki/Obi-Wan_Kenobi)
	2. [Computer-generated imagery](https://en.wikipedia.org/wiki/Computer-generated_imagery)
	3. [Algorithm](https://en.wikipedia.org/wiki/Algorithm)
	4. [Optimization problem](https://en.wikipedia.org/wiki/Optimization_problem)
	5. [Search problem](https://en.wikipedia.org/wiki/Search_problem)

---

## Resources & References

[^1]: [wikipedia](https://en.wikipedia.org/wiki/Search_problem)
[^2]: [tweet about bike theft](https://x.com/AlecStapp/status/1728953538301345889?s=20)
[^3]: [article about bike theft](https://www.iflscience.com/computer-scientist-attempts-to-get-the-police-to-find-his-stolen-bike-using-math-67260)