AI Search Algorithms Interactive Demo

Explore BFS, DFS, Dijkstra & A* algorithms with 5 Saudi cities for clear learning

Simplified network: Riyadh → Qassim → Medina → Jeddah → Makkah

Select Search Algorithm

BFS
Breadth-First Search
DFS
Depth-First Search
Dijkstra
Shortest Path
A*
Best-First + Heuristic
Educational Network Design

This simplified 5-city network provides 2 distinct paths from Riyadh to Makkah:

  • Direct Route: Riyadh → Jeddah → Makkah (929km, 2 hops)
  • Scenic Route: Riyadh → Qassim → Medina → Jeddah → Makkah (1,228km, 4 hops)
This design clearly demonstrates how different algorithms make different choices!

Breadth-First Search (BFS)

BFS explores all nodes at the current depth level before moving to nodes at the next depth level. It guarantees finding the shortest path in unweighted graphs.

Step Controls
Legend
Start/Path
Goal
Exploring
Explored
Frontier
Ready to Start

Select an algorithm above and click "Next Step" to begin the search, or click "Auto Play" for automatic visualization.

Current Status: Ready

Pseudocode
Data Structures
Frontier:
Empty
Explored:
Empty
State Graph
Tree visualization starting from Riyadh
Performance Metrics
0
Steps
0
Explored
-
Path Cost
-
Path Length

Algorithm Insights: Different Problems, Different Solutions

Is This the "Shortest Path Problem"?

Not exactly! Our demo shows different types of pathfinding problems. Only Dijkstra and A* solve the true "shortest path problem" (minimum distance). BFS and DFS solve different optimization problems entirely.

Problem Types Solved
Shortest Path Problem

Goal: Minimize total distance/cost

Algorithms: Dijkstra, A*

Our Result: Riyadh → Jeddah → Makkah (929km)

Fewest Hops Problem

Goal: Minimize number of intermediate stops

Algorithm: BFS

Our Result: Riyadh → Jeddah → Makkah (2 stops)

Any Path Problem

Goal: Find any route to destination

Algorithm: DFS

Our Result: Either route (depends on search order)

Our Network Results
Algorithm Path Found Distance Hops
Dijkstra Direct Route 929km 2
A* Direct Route 929km 2
BFS Direct Route 929km 2
DFS Variable Variable 2 or 4
Note: In our network, BFS happens to find the optimal distance, but this is coincidental. In larger networks, BFS often finds longer paths.
When to Use Each Algorithm
Use Dijkstra When:
  • You need the absolutely shortest distance
  • Graph has weighted edges (different costs)
  • Optimality is more important than speed
  • Example: GPS navigation, network routing
Use A* When:
  • You need shortest distance AND fast search
  • You have a good heuristic function
  • Graph is large (A* explores fewer nodes)
  • Example: Game pathfinding, robotics
Use BFS When:
  • You want fewest intermediate stops
  • Graph is unweighted (all edges equal cost)
  • Exploring by levels makes sense
  • Example: Social networks, minimum transfers
Use DFS When:
  • Any path is acceptable (not optimal)
  • You want to explore deeply first
  • Memory is limited (DFS uses less)
  • Example: Maze solving, decision trees
Key Educational Takeaways
  • Different algorithms solve different problems
  • BFS ≠ Shortest path (only shortest in unweighted graphs)
  • Dijkstra guarantees shortest distance
  • A* is Dijkstra with smarter search
  • DFS is fast but not optimal
  • Choose algorithm based on your goal
  • Optimality often comes with computational cost
  • Real-world applications vary by use case