Music and Coding Part 5

Discovering Graphs through Jazz Improvisation and Modulation

Introduction

In previous posts, we explored linked lists, stacks and queues, and trees. Each data structure had a unique musical analogy, from the C Major scale to chords and harmonic hierarchies. Now, we’ll look at graphs, which take us to a new level of flexibility, with connections between any two points.

Musically, a graph can represent jazz improvisation or key modulations. Jazz often involves shifting between chords and scales in unexpected ways, allowing musicians to improvise freely. Similarly, a graph structure allows data points (nodes) to connect in any pattern, capturing complex relationships without a strict hierarchy.


Graphs: Connecting Keys and Chords in Jazz Improvisation

A graph is made up of nodes (vertices) and edges (connections between nodes). In jazz improvisation, think of each node as a chord or key, and each edge as a possible transition to another chord or key. Unlike a tree, where each child has a specific relationship with a parent, a graph’s nodes are free to connect in any direction, allowing for intricate and flexible relationships.

Let’s create a graph in Python to represent jazz-style modulations and chord changes in C Major, where each chord connects to other chords based on common jazz progressions.


Python Code for Graph (Jazz Progression and Modulation)

Below is a Python code snippet to set up a graph of chord connections. Feel free to copy and paste this code in your IDE.

python# Define the Graph class to represent chord connections
class ChordGraph:
    def __init__(self):
        self.graph = {}  # Dictionary to hold chord connections

    def add_chord(self, chord):
        if chord not in self.graph:
            self.graph[chord] = []  # Each chord has a list of connected chords

    def add_connection(self, chord1, chord2):
        self.graph[chord1].append(chord2)  # Create a connection from chord1 to chord2
        self.graph[chord2].append(chord1)  # Create a connection back (undirected graph)

    def show_connections(self):
        for chord, connections in self.graph.items():
            print(f"{chord} is connected to: {', '.join(connections)}")

# Initialize a chord graph
jazz_graph = ChordGraph()

# Add chords (nodes)
for chord in ["C", "G", "D", "Em", "Am", "F", "Bb", "E7", "A7"]:
    jazz_graph.add_chord(chord)

# Add connections (edges) between chords for a jazz-style progression
jazz_graph.add_connection("C", "G")
jazz_graph.add_connection("C", "Am")
jazz_graph.add_connection("G", "D")
jazz_graph.add_connection("G", "Em")
jazz_graph.add_connection("Am", "D")
jazz_graph.add_connection("Am", "F")
jazz_graph.add_connection("F", "Bb")
jazz_graph.add_connection("D", "E7")
jazz_graph.add_connection("E7", "A7")

# Display the graph structure
jazz_graph.show_connections()

Explanation of the Code

  1. ChordGraph Class: This class represents a graph with chords as nodes and connections as edges.
    • add_chord adds a chord to the graph if it’s not already present.
    • add_connection creates a two-way connection (undirected edge) between two chords, mimicking the freedom of jazz improvisation.
    • show_connections displays each chord and its connected chords, illustrating the relationships.
  2. Adding Nodes and Connections: We add common jazz chords like C, G, Am, D, and F, then create connections that follow typical jazz transitions. The result is a network of chord relationships that musicians could explore while improvising.

Example Output

Running jazz_graph.show_connections() produces output like:

vbnetC is connected to: G, Am
G is connected to: C, D, Em
Am is connected to: C, D, F
F is connected to: Am, Bb
D is connected to: G, E7, Am
E7 is connected to: D, A7
A7 is connected to: E7
Bb is connected to: F

Here, C connects to G and Am, G connects to D and Em, and so forth, forming a web of possible paths.


Graphs and Musical Freedom

Graphs allow us to explore musical freedom:

  • Chord Transitions: You can modulate between any chords connected by an edge.
  • Improvisation Paths: In jazz, this flexibility allows musicians to navigate progressions based on feel rather than rules.
  • Modulations: Edges between nodes can represent a modulation to a new key, offering an unexpected path and tonal change.

Unlike a tree, where paths are hierarchical, graphs let us explore circular paths and non-linear progressions, ideal for the dynamic nature of jazz.


Final Thoughts

Graphs give us a powerful way to represent connections and choices in music. They capture the essence of jazz improvisation and allow musicians to flow through progressions freely, following connections that feel right at the moment. Next time, we’ll delve into Hash Tables and relate them to musical patterns and memory, such as recognizing recurring themes.

Feel free to try expanding this graph by adding new chords or even connecting different keys, simulating how modulations might work in an improvisational jazz piece.


Questions for Reflection and Practice

  1. What might happen if you made this a directed graph, where each chord only leads to another in one direction?
    • How would that change the way you navigate or improvise within the graph?
  2. Can you think of other musical scenarios where graphs could be useful?
    • Consider complex song structures or ways to represent interactions between instruments.
  3. If you wanted to represent different modes or keys in this graph, how would you modify it?
    • Try adding chords in a new key and exploring connections between keys.
  4. How might this graph change if each connection had a “weight,” like in weighted graphs?
    • For example, connections to closer harmonic relationships could have lower “weights” and distant modulations higher ones.
Lydia Bandy Avatar

Published by Lydia Bandy

Harpist, Pianist, Award Winning Music Teacher, and Software Engineer🎼👨‍💻 Bridging music and technology with 25 years experience as a harpist, pianist, and award-winning instructor! Having 3-4 years as a software engineer and web developer, my goal is to teach coding and music theory to the world🌎🎵 Facebook/Instagram/YouTube: @LydiasPianoStudio

Leave a comment