Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 17, 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!

Graphs

We have seen graphs and corresponding algorithms for:

Data Task Algorithm
Images / grids Voronoi diagrams / flood fill breadth first search
Music / evolving systems sampling / simulation Markov chains
Games path to solution depth first search

Next few lectures:

  • Look at geospatial data / maps,
  • For the purpose of path planning / route finding,
  • Using a dynamic programming algorithm.

Maps

Geographic Applications

  • Geospatial data is often stored as a graph.

    • Nodes are features of interest.
    • Edges are physical or logical connections between features.
  • In geospatial data sets, nodes often have associated location information.

  • When thinking of the data set as a graph, location information may be:

    • Directly useful for visualization.
    • Only indirectly (or not at all) useful for choosing and implementing an appropriate graph algorithm.

Open Street Maps

An open-source alternative to the data behind Google / Apple maps:

https://www.openstreetmap.org/

  • OSM stores raw geographic data (such as intersections, roads, tags, …).
  • OSM has an application programming interface (API) through which we can request that data.
import osmnx as ox

place_names = ['UBC','Vancouver','Stanley Park']
ox.geocode_to_gdf(place_names)

From Maps to Graphs

  • NetworkX package provides for storing and manipulating graphs.
  • Access OSM data through the NetworkX interface using osmnx module.
  • In particular, we can extract the road network as a graph where:
    • Nodes are intersections (with lat/long coordinates).
    • Edges are roads with attributes (length, max speed, etc.).
  • We will apply graph algorithms to find routes or substructures.
  • We will visualize results using Matplotlib or Folium.

Open Street Maps Demo

You will:

  • Download map data for Vancouver and the Endowment Lands.
  • Explore amenities within that map.
  • Create a shortest path route between two locations in the map.
  • Visualize everything as both static and interactive maps.

Launch Class Activity: Fun with Open Street Maps in PrairieLearn.

Single Source Shortest Path

Single Source Shortest Path

  • Given: a starting vertex (source or root) \(s\).
  • Find: paths of least total cost from \(s\) to every vertex in the graph.

What if we have a specific destination vertex?

  • Modify the algorithm (called “A*“).
    • Asymptotic cost is the same.
    • Actual run-time is better
    • More complicated code.

SSSP Example

  • Input: Weighted graph \(G\) and a start vertex \(s\).
    • Edge weights of \(G\) must be non-negative.
  • Output: A subgraph \(G’\) consisting of the shortest (minimum total cost) paths from \(s\) to every other vertex in the graph.

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