A Python-based tool designed to reorder songs within a Spotify playlist to achieve the shortest average distance between songs. Distance between songs are determined by the euclidean distance between two songs vectors with vectors being computed using a songs audio features. This creates a travelling salesman probem (TSP)
- Extracts audio features from songs using the Spotify API to create feature vectors.
- Computes Euclidean distances between songs based on feature vectors.
- Implements three optimization algorithms to solve TSP:
- Naive Nearest Neighbor: A greedy approach for reordering.
- Minimum Spanning Tree (MST): Constructs optomized ordering using a tree structure.
- Heuristics: Custom algorithm for order optimization.
- Saves reordered playlist to user's Spotify account.
- A greedy algorithm that starts at a random song and iteratively adds the nearest unvisited song to the order.
- Advantages: Simple and fast solution
- Disadvantages: May result in suboptimal solutions.
- Creates a tree that connects all songs with a minimal edge weight and derives a Hamiltonian path from it.
- Intensive solution for larger playlists. Takes a longer amount of time to run.
This project compares the performance of the algorithms above based on the average Euclidean distance throughout the current playlist ordering. The following are the results after running playlist optimization for playlists of the following sizes:
10 Songs
| Algorithm | Avg Distance |
|---|---|
| Guessing | 0.4 |
| NNN | 0.57 |
| MST | 0.48 |
| Heur | 0.4 |
100 Songs
| Algorithm | Avg Distance |
|---|---|
| Guessing | 0.58 |
| NNN | 0.32 |
| MST | 0.31 |
| Heur | 0.54 |
1000 Songs
| Algorithm | Avg Distance |
|---|---|
| Guessing | 0.85 |
| NNN | 0.26 |
| MST | 0.28 |
| Heur | 0.83 |
5000 Songs
| Algorithm | Avg Distance |
|---|---|
| Guessing | 0.97 |
| NNN | 0.23 |
| MST | 0.25 |
| Heur | 0.95 |
With larger playlist sizes, Naive Nearest Neighbor provides the smallest average Euclidean distance overall with the MST solution performing only slightly worse. The heuristics solution proves to be little better than randomly guessing a playlist order.