Back to all lessons
FoundationsBeginner

🗺️Algorithms & Graph Search

How computers find good routes

Take your time with this one. The interactive parts are here to help you test the idea, not rush through it.

20 min- Explore at your own pace

Before We Begin

What we are learning today

From GPS to game AI, computers spend a lot of time searching. Dijkstra’s algorithm calmly answers: what is the absolute cheapest way to get from here to there?

How this lesson fits

Welcome to the bedrock of AI. Think of this module as the class warm-up where we learn how computers follow rules, deal with uncertainty, and search for answers—exactly the skills we’ll lean on all year.

The big question

How can something as ordinary as metal and silicon learn to follow rules, handle uncertainty, and still find its way through a messy world?

Trace a computation step by step and explain the “why” out loudReason about chance with simple, friendly distributionsDescribe how search algorithms pick a smart path forward

Why You Should Care

Efficient search is a core move in AI. Before a system can plan or strategize, it must quickly sift through many options.

Where this is used today

  • Google Maps finding the fastest route
  • Internet routing (sending data packets)
  • NPC pathfinding in video games

Think of it like this

Imagine walking through a dim maze with a notebook. You can’t see the exit, but you keep track of costs and pick the next spot that looks promising. Step by step, the path reveals itself.

Easy mistake to make

“Shortest” means lowest total cost based on the weights you pick—time, traffic, or fuel—not just physical distance.

By the end, you should be able to say:

  • Describe a graph using nodes, edges, and weights
  • Explain how Dijkstra’s algorithm updates distances
  • Connect shortest-path search to robotics, maps, and planning

Think about this first

When you use a map app, why might the shortest route avoid a simple straight line? What other “costs” is it weighing?

Words we will keep using

graphnodeedgeweightshortest path

What is Dijkstra's Algorithm?

Dijkstra's algorithm answers a simple question: "What is the fastest way to get there?" It works like a cautious traveler who always explores the closest town first, gradually expanding their map until they hit the destination.

GraphThe map. A collection of places (nodes) and roads (edges).
WeightsThe cost. Distance, time, money—whatever you are trying to save.
Why AI caresBefore a robot moves, it plans. Search is the first step of action.

Interactive Dijkstra Visualizer

UnvisitedVisitedCurrentShortest path

Algorithm Pseudocode

function dijkstra(graph, start):
  dist[start] = 0; dist[all others] = ∞
  pq = priority queue with (0, start)

  while pq not empty:
    (d, u) = pq.pop_min()
    for each neighbor v of u with edge weight w:
      if dist[u] + w < dist[v]:
        dist[v] = dist[u] + w
        pq.push((dist[v], v))
  
  return dist  // shortest distances from start

Do not worry too much about the formula yet. The big idea is that a smart data structure lets the algorithm stay efficient even on larger graphs.

Connections to AI

This idea appears everywhere in AI: robot navigation, game search, network routing, and even recommendation or knowledge systems. Once students understand shortest-path search, they have one of the core building blocks of intelligent planning.