Greedy Best-First Search Demo

Real GPS data: 6 Saudi cities with actual distances and coordinates

h(n) = straight-line distance to Makkah • Now includes Taif (90km from Makkah)
Greedy Best-First Search

Expansion rule: Expand the node that has the lowest value of the heuristic function h(n).

  • Greedy: Always chooses the node that appears closest to goal
  • Fast: Often finds a path quickly
  • Not optimal: Ignores actual cost, may find suboptimal paths
Unlike A*, Greedy search uses only h(n), ignoring the actual cost g(n) to reach each node.

How Greedy Search Works with Real GPS Data

This demo uses real GPS coordinates and actual road distances between Saudi cities. The heuristic h(n) represents the straight-line distance to Makkah:

  • Riyadh: h(n) = 645km (straight-line to Makkah)
  • Qassim: h(n) = 620km
  • Medina: h(n) = 358km
  • Jeddah: h(n) = 65km
  • Taif: h(n) = 67km (new city, 90km by road from Makkah)

Greedy search will always choose the city with the lowest h(n) value first!

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

Click "Next Step" to begin Greedy Best-First search from Riyadh to Makkah, or click "Auto Play" for automatic visualization.

Current Status: Ready

Pseudocode
Data Structures
Frontier (Priority Queue):
Empty
Explored:
Empty
Performance Metrics
0
Steps
0
Explored
-
Path Cost
-
Path Length
Greedy vs A*
Property Greedy A*
Evaluation h(n) only f(n) = g(n) + h(n)
Optimality Not guaranteed Guaranteed*
Speed Fast Slower
Memory Less More
*With admissible heuristic

Understanding Greedy Best-First Search

How Greedy Works
Expansion Strategy:

Always expand the frontier node with the lowest h(n) value - the one that appears closest to the goal according to the heuristic.

Advantages:
  • Fast: Makes quick decisions
  • Simple: Easy to understand and implement
  • Memory efficient: Often explores fewer nodes
  • Good heuristics: Can find near-optimal solutions quickly
Disadvantages:
  • Not optimal: May find suboptimal paths
  • Incomplete: Can get stuck in infinite loops
  • Heuristic dependent: Bad heuristics lead to poor performance
Saudi Cities Result
Expected Behavior with Real GPS Data:

Greedy search will prioritize cities with the lowest h(n) values. From Riyadh, it has three options with these heuristic values:

Heuristic Analysis:
  • Jeddah: h(n) = 65km (very close to Makkah)
  • Taif: h(n) = 67km (almost as close as Jeddah)
  • Qassim: h(n) = 620km (much farther)

Greedy Decision: Will choose Jeddah first (lowest h(n) = 65km), then directly to Makkah.

Taif Addition: Taif creates an interesting choice! At 67km straight-line distance, it's almost as "attractive" as Jeddah (65km) to the greedy algorithm, but the road distance is different (90km vs 80km).
When to Use Greedy Best-First Search
Good for:
  • Real-time systems: When speed is more important than optimality
  • Good heuristics: When your heuristic is very informative
  • Satisficing solutions: When "good enough" is acceptable
  • Large search spaces: When optimal search is too slow
  • Interactive applications: Games, robotics with time constraints
Avoid when:
  • Optimality required: When you need the best solution
  • Poor heuristics: When heuristic is misleading
  • Safety-critical: When mistakes are costly
  • Complex landscapes: With many local minima
  • Complete search needed: When you must explore all options
Real GPS-Based Heuristics
Actual GPS Coordinates Used:
  • Riyadh: 24.7136°N, 46.6753°E
  • Qassim: 26.3260°N, 43.9750°E
  • Medina: 24.5247°N, 39.5692°E
  • Jeddah: 21.4858°N, 39.1925°E
  • Taif: 21.2703°N, 40.4164°E
  • Makkah: 21.3891°N, 39.8579°E
Haversine Formula Used:

The straight-line distances are calculated using the Haversine formula, which accounts for the spherical nature of Earth:

d = 2R × arcsin(√(sin²(Δlat/2) + cos(lat₁) × cos(lat₂) × sin²(Δlng/2)))

where R = 6,371km (Earth's radius)

Educational Value: This demonstrates how real-world AI systems use actual geographic data for pathfinding and routing algorithms.
Key Educational Takeaways
  • Real data matters: GPS coordinates provide accurate heuristics
  • Trade-off: Speed vs optimality
  • Heuristic quality matters: Good h(n) = good performance
  • Greedy decisions: Local choices, global consequences
  • Stepping stone to A*: Understand h(n) before f(n)
  • Real-world relevance: Used in GPS navigation systems
  • Algorithm family: Part of best-first search algorithms
  • Geographic applications: Foundation for mapping and routing