Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 19, 2026

Announcements

  • Project 3 (Campus Walks) due Monday December 7.

    • Take a breather. Get started on this project next week.
  • This week: Lab 9 (dynamic programming) and POTW 9 (potpourri) due Sunday Nov 22.

  • Examlet 4 window is Thursday Nov 19 to Monday Nov 23.

  • Feeling overwhelmed? You are not alone!

Single Source Shortest Path Continued

Reminder: Dijkstra’s Algorithm (1959)

  1. Initialize structure:
    • Distance to each vertex.
    • Predecessor of each vertex.
  2. Repeat these steps:
    • Choose vertex \(v\) whose shortest distance has been found.
    • Update \(v\)’s neighbors with improved distance (and predecessor).

Implementing Dijkstra: Initialization

  • For all v,
    • d[v] =
    • p[v] =
  • For the source s:
    • d[s] =
    • p[s] =

Implementing Dijkstra: Loop

  • Repeat until all nodes visited:
    • Find unvisited v with smallest d[v]
    • Mark v as visited
    • For all unvisited neighbors w of v:
      • If (_________________ < d[w]):
        • d[w] =
        • p[w] =

Dijkstra’s Algorithm in Python

Launch Class Activity: Dijkstra’s Algorithm in PrairieLearn.

You will:

  • Write some Python to implement Dijkstra’s algorithm.
  • Test the implementation on the graph at right.

Comparing Graph Exploration Algorithms

  • Initialize structure (see previous slide).
  • Repeat until all nodes visited:
    • Find unvisited v with smallest d[v]
    • Mark v as visited
    • For all unvisited neighbors w of v:
      • If (d[v] + weight[v,w] < d[w]):
        • d[w] = d[v] + weight[v,w]
        • p[w] = v
  • How is the algorithm similar to BFS/DFS?




  • How is the algorithm different from BFS/DFS?

Several Ways of Exploring a Graph

Property DFS BFS Dijkstra
Data Structure stack queue priority queue
Edge costs? ignored all equal any non-negative
Shortest path? no yes yes
Frontier dive deep explore in layers explore by shortest known distance

Resources