-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChordGraph.h
More file actions
434 lines (342 loc) · 11.1 KB
/
ChordGraph.h
File metadata and controls
434 lines (342 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#ifndef CHORDGRAPH_H
#define CHORDGRAPH_H
#ifndef NULL
#define NULL 0
#endif
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <queue>
#include <limits.h>
#include <string.h>
using namespace std;
class Vertex;
class Edge;
#ifndef DEFAULT_VERTEX_STATUS
#define UNKNOWN 0
#define IDENTIFIED 1
#define VISITED 2
#define DEFAULT_VERTEX_STATUS 0
#endif
// The progression from one Chord to another
class Edge {
public:
Vertex * source;
Vertex * target;
double weight; // The total number of times a progression from the source to the target occurs
int flux;
// Constructor
Edge(Vertex * pSource, Vertex * pTarget, double pWeight)
: source(pSource), target(pTarget), weight(pWeight){ }
//Destructor
~Edge() {}
};
// The Chord
class Vertex {
public:
int id; // id represents the order this Vertex was added to a graph
string label; // label/chord (unique)
int status = 0; // or color
int repeat; // The number of times a chord repeats in a row
vector<Edge*> edges; // A vector of all the progressions from this Chord
// Constructor
Vertex(int pId, string pLabel) :
id(pId), label(pLabel), status(DEFAULT_VERTEX_STATUS), repeat(0) {}
// Destructor
~Vertex() {
for (auto iter = edges.begin(); iter != edges.end(); iter++) {
Edge * l = *iter;
delete l;
}
}
};
// A representation of the entire song containing all chords and their progressions
class ChordGraph {
public:
map<string, Vertex*> vertices; // The list of Chords
//int[][] matrix;
// Constructor
ChordGraph(){}
// Destructor
~ChordGraph(){
for (auto iter = vertices.begin(); iter != vertices.end(); iter++) {
Vertex * v = iter->second;
delete v;
}
}
//Creates an adjacency matrix from an adjacency list
vector<vector<int>> getMatrix(){
int len = vertices.size();
vector< vector<int>> vec(len, vector<int>(len));
//int row = 0;
for (auto iter = vertices.begin();
iter != vertices.end(); iter++) {
Vertex * v = iter->second;
cout << v->label;
cout << ": ";
cout << v->id;
cout << " ";
//cout << v->label << endl;
int row = v->id;
for(int i = 0; i < v->edges.size(); i++){
Vertex* n = v->edges[i]->target;
int col = n->id;
vec[row][col] = v->edges[i]->weight;
//cout << n->id << endl;
}
}
cout << endl;
//cout << "MEME" <<endl;
return vec;
}
//BEGIN FORD FULKENSON STUFF
/* Returns true if there is a path from source 's' to sink 't' in
residual graph. Also fills parent[] to store the path */
//#define V 11
bool bfs(int** rGraph, int s, int t, int parent[], int size)
{
// Create a visited array and mark all vertices as not visited
bool visited[size];
memset(visited, 0, sizeof(visited));
// Create a queue, enqueue source vertex and mark source vertex
// as visited
queue <int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
// Standard BFS Loop
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v=0; v<size; v++)
{
if (visited[v]==false && rGraph[u][v] > 0)
{
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
// If we reached sink in BFS starting from source, then return
// true, else false
return (visited[t] == true);
}
int fordFulkerson(int** graph, int s, int t, int size)
{
int u, v;
// Create a residual graph and fill the residual graph with
// given capacities in the original graph as residual capacities
// in residual graph
// int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates
// residual capacity of edge from i to j (if there
// is an edge. If rGraph[i][j] is 0, then there is not)
int ** rGraph;
rGraph = new int*[size];
for (int i = 0; i < size; ++i) {
rGraph[i] = new int[size];
}
for (u = 0; u < size; u++)
for (v = 0; v < size; v++)
rGraph[u][v] = graph[u][v];
int parent[size]; // This array is filled by BFS and to store path
int max_flow = 0; // There is no flow initially
// Augment the flow while tere is path from source to sink
while (bfs(rGraph, s, t, parent, size))
{
// Find minimum residual capacity of the edges along the
// path filled by BFS. Or we can say find the maximum flow
// through the path found.
int path_flow = INT_MAX;
for (v=t; v!=s; v=parent[v])
{
u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}
// update residual capacities of the edges and reverse edges
// along the path
for (v=t; v != s; v=parent[v])
{
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}
// Add path flow to overall flow
max_flow += path_flow;
}
//for(int i = 0; i < size; i++){
//cout << parent[i];
//cout << ",";
// }
//cout << endl;
// Return the overall flow
return max_flow;
}
//Finds the chord veretex with highest degree (does not include weighting)
//This is the chord that has the most diversity coming and leaving it
int findMaxDegree(){
//auto iter = vertices.begin();
Vertex* max = vertices.begin()->second;
for (auto iter = vertices.begin();
iter != vertices.end(); iter++) {
Vertex * v = iter->second;
//cout << v->label << endl;
if(v->edges.size() > max->edges.size()){
max = v;
}
}
cout << "MAXIMUM DEGREE IS: " << max->label << " WITH DEGREE OF " << max->edges.size() << endl;
//cout << max->label << endl;
// cout << max->edges.size() << endl;
return max->edges.size();
}
void bruteForceMaximumFlow(int** arr, int size, string* chordArr){
int maximumFlow = -1;
int maxSource = -1;
int maxSink = -1;
//Brute force solution between the total maximum flow between any two source sink permutation
//Total checks: (V)*(V-1), O(V^2) V = number of chords in the song.
//Edmonds Karp is O(V*E^2)
//So thus our complexity is O(V^3*E^2)
for (auto sourceIter = vertices.begin();
sourceIter != vertices.end(); sourceIter++) {
Vertex* sourceVertex = sourceIter->second;
int s = sourceVertex->id;
for (auto sinkIter = vertices.begin();
sinkIter != vertices.end(); sinkIter++) {
Vertex* sinkVertex = sinkIter->second;
if(sourceVertex != sinkVertex){
int t = sinkVertex->id;
//Do the Edmond Karp
int flow = fordFulkerson(arr, s, t, size);
//cout << flow << endl;
if(flow > maximumFlow){
maximumFlow = flow;
maxSource = s;
maxSink = t;
}
//cout << x << endl;
//maximumFlow = edmonds(g, source, sink)
}
}
//cout << endl;
}
cout << "MAX FLOW IS: " << maximumFlow << endl;
cout << "SOURCE: " << chordArr[maxSource] << endl;
cout << "SINK: " << chordArr[maxSink] << endl;
}
//Gets the matrix in vector form and then turns it into array form
void doFordFulkerson(){
vector<vector<int>> vec = getMatrix();
int size = vec.size();
int **arr;
arr = new int*[size];
string* chordArr = new string[size];
//a chord dictionary
for (auto iter = vertices.begin();
iter != vertices.end(); iter++) {
Vertex * v = iter->second;
chordArr[v->id] = v->label;
}
for (int i = 0; i < size; ++i) {
arr[i] = new int[size];
}
for(int i = 0; i < vec.size(); i++){
for(int j = 0; j < vec.size(); j++){
arr[i][j] = vec[i][j];
}
}
cout << "Brute forcing..." << endl;
bruteForceMaximumFlow(arr, size, chordArr);
//cout << x << e
}
// Returns the maximum flow from s to t in the given graph
//END FORD FULKINSUN STUFF
bool bfs(Vertex* source){
// queue<Vertex*> q;
// q.push(source);
// source->status = 1;
// while(!q.empty()){
// Vertex* v = q.front();
// q.pop();
// cout << v->label << endl;
// for(int i = 0; i < v->edges.size(); i++){
// Vertex* n = v->edges[i]->target;
// //not visited
// if(n->status != 1){
// q.push(n);
// n->status = 1;
// }
// }
// }
// //q.push(source);
// //cout << q.front()->label << endl;
return true;
}
// Function for adding a chord progression to the graph
void addProgression(string chord1, string chord2){
if (vertices.find(chord1) == vertices.end()){ // If there is not already a vertex of chord1
// Create new vertex for chord1
vertices[chord1] = new Vertex(vertices.size(), chord1);
cout << "Vertex created for " << chord1 << endl;
}
if (vertices.find(chord2) == vertices.end()){ // If there is not already a vertex of chord2
// Create new vertex for chord2
vertices[chord2] = new Vertex(vertices.size(), chord2);
cout << "Vertex created for " << chord2 << endl;
}
// v1 and v2 refer to chord1 and chord2
Vertex * v1 = vertices[chord1];
Vertex * v2 = vertices[chord2];
// If this is a repetition
if (chord1 == chord2){
(v1->repeat)++;;
cout << "Chord " << v1->label << " repeated " << v1->repeat << " times" << endl;
} else { // if not
bool found = false;
int edgeIndex;
// Search if this progression already exists
for (int i = 0; i < v1->edges.size(); i++){
if (v1->edges[i]->target->label == chord2){
found = true;
edgeIndex = i;
break;
}
}
// If it doesn't exist
if (!found){
// Make the new edge
v1->edges.push_back(new Edge(v1, v2, 1));
cout << "Made new edge from " << v1->label << " to " << v2->label << endl;
cout << "Weight: " << v1->edges.back()->weight << endl;
} else { // If it does exists
// Update the edge by adding 1 to the weight
(v1->edges[edgeIndex]->weight)++;
cout << "Updated edge from " << v1->label << " to " << v2->label << endl;
cout << "New Weight: " << v1->edges[edgeIndex]->weight << endl;
}
}
return;
}
// A print-out representation of the graph
string toString() const {
stringstream s;
for (auto iterV = vertices.begin(); iterV != vertices.end(); iterV++) {
Vertex * v = iterV->second;
s << v->label << "(" << v->repeat << "): ";
for (auto iterE = v->edges.begin(); iterE != v->edges.end(); iterE++) {
Edge * e = *iterE;
s << e->target->label << "(" << e->weight << ") ";
}
s << endl;
}
return s.str();
}
};
// Defined in ChordGraph.cpp
bool parseChords(ChordGraph & cg, char * fileName);
#endif