Menu

1 / 2

How Search Algorithms Work — From DFS and BFS to A*

DEV Community·shangkyu shin·6 months ago
#tCux07bB
Reading 0:00
15s threshold

Search algorithms look simple until the state space gets huge.

Then the real question becomes:

Do you explore everything, or do you choose smarter paths first?

That is the core idea behind search in AI.

Core Idea

Search is about moving from a start state to a goal state.

You have:

a current state

possible actions

next states

a goal condition

The algorithm decides which state to explore next.

That one decision changes everything.

The Basic Structure

Most search algorithms follow this pattern:

start from the initial state

while there are states to explore: choose the next state

if it is the goal: return solution

expand possible next states

return failure

Enter fullscreen mode

Exit fullscreen mode

The difference between DFS, BFS, Greedy Search, and A* is mostly this:

How do we choose the next state?

DFS vs BFS

The first important comparison is DFS vs BFS.

DFS goes deep first.

BFS expands level by level.

DFS:

follows one path as far as possible

uses less memory in many cases

can…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.