Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 3, 2026

Announcements

  • Practice Examlet 4 is available.
  • Lab 8 (memoization) and POTW 8 (mostly Sudoku validity) due Sunday.
  • Project 2 (Game of Life) released, due Monday November 16.
    • Time to get started!
  • Feeling overwhelmed? You are not alone!

Today’s Destination: Markov Chains (MCs)

  • Last class we discussed graphs as a new data type.
    • Entities were represented by nodes.
    • The relationship between the entities was represented by edges between nodes.
  • Markov chains (or Markov processes) are a model for systems which evolve probabilistically.
  • MCs can be represented as a graph
    • The nodes are the current state of the system.
    • The edges capture the probabilistic evolution to the next state.

Computer Generated Music

Following Ada

Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations,

the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.

(Ada Lovelace – 1842)

Prelude

Note Probabilities

Note Count Prob
C 3 0.12
D 10 0.38
E 11 0.42
G 2 0.08
total 26 1.00

Note Transition Probabilities

C D E G
C 0 2 0 0
D 3 3 4 0
E 0 5 5 1
G 0 0 1 1
C D E G
C 0.00 1.00 0.00 0.00
D 0.30 0.30 0.40 0.00
E 0.00 0.45 0.45 0.10
G 0.00 0.00 0.50 0.50

Markov Chains

Visualizing the Markov Chain

  • Transition table / matrix is a representation of an MC model.
  • Alternative representation using directed graph:
    • Each node is a state of the model.
    • Each directed edge is labelled with the probability of the corresponding transition.
C D E G
C 0.00 1.00 0.00 0.00
D 0.30 0.30 0.40 0.00
E 0.00 0.45 0.45 0.10
G 0.00 0.00 0.50 0.50

Markov Chains

  • Broadly used model of evolving systems.
    • Google’s original search algorithm PageRank.
    • Natural language processing (NLP).
    • DNA matching.
    • Chemical reaction simulation.
    • Many, many more…
  • Closely related to finite state machines and Markov decision processes.
    • All these models exhibit the Markov property: Future evolution depends only on current state (and not on previous history).

Aside: Adjacency Matrix Representation of a Graph

\(G = (V,E)\)

\(V = \{ A, B, C, D, E, F \}\)

\(E = \{ (E,C), (C,A), (C,D), (A,B), (D,B), (B,F) \}\)


A B C D E F
A 0 1 1 0 0 0
B 1 0 0 1 0 1
C 1 0 0 1 1 0
D 0 1 1 0 0 0
E 0 0 1 0 0 0
F 0 1 0 0 0 0

Building a Song

  • Randomly choose a start note and put it in a list.
  • For the remaining 25 notes:
    • Generate a new note from the MC based on last note.
    • Put the new note in the list.
  • Play the list of notes (using existing rhythm).
C D E G
C 0.00 1.00 0.00 0.00
D 0.30 0.30 0.40 0.00
E 0.00 0.45 0.45 0.10
G 0.00 0.00 0.50 0.50

Demo

Open today’s activity. You will:

  • Build the MC for “Mary had a Little Lamb”
  • Visualize the MC as a directed graph.
  • Generate a new song from the MC.
  • Create an audio version of the new song.

Launch Class Activity: Mary Had a Little Lamb in PrairieLearn.

The only thing that we could have a little more of the same kind words and the answers are very very important