-
Notifications
You must be signed in to change notification settings - Fork 0
/
computePPH.cpp
700 lines (580 loc) · 18.9 KB
/
computePPH.cpp
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
Code to compute 1-dimensional persistent path homology of a network.
Author: Samir Chowdhury
Date: March 26, 2019
Compile with options:
clang++ -std=c++11 -Ofast -march=native computePPH.cpp -o computePPH
Or
clang++ -std=c++11 computePPH.cpp -o computePPH
./computePPH
*/
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<sstream>
#include<chrono>
using namespace std;
using namespace std::chrono;
// FUNCTION DECLARATIONS HERE
// main functions for pph
tuple<int, vector<vector<double> > > readFile(string fileName);
vector<vector<int>> getp0(int numNodes);
tuple<vector<double>, vector<vector<int>>> getp1(vector<vector<double>> &edgeData);
tuple<vector<double>, vector<vector<int>>> getp2(vector<vector<double>> &edgeData);
vector<vector<int>> simpleBoundary(vector<int> &path);
void addInfAPaths(vector<vector<int>> &p1, vector<double> &p1time, vector<vector<int>> &p2);
tuple<vector<vector<int>>, int> basisChange(vector<int> &kpath,
vector<bool> &pjmarked, vector<vector<int>> &pj, vector<vector<vector<int>>> &tj);
vector<vector<int>> removeUnmarked(vector<vector<int>> &pjsummands, vector<bool> &pjmarked, vector<vector<int>> &pj);
tuple<int, int> getMaxIdx(vector<vector<int>> &pjsummands, vector<vector<int>> &pj);
/*void buildSlots(vector<vector<int>> &p0, vector<vector<int>> &p1, vector<vector<int>> &p2,
vector<vector<vector<int>>> &t0, vector<vector<vector<int>>> &t1, vector<vector<vector<int>>> &t2);
*/
// helper functions
vector<vector <double> > sortByCol(int sortBy, vector<vector<double>> &unsrtMat);
void print2DVector(vector<vector<double> > &myvec);
void print2DIntVector(vector<vector<int> > &myvec);
void print1DIntVector(vector<int> &myvec);
void print1DVector(vector<double> &myvec);
void print1DBoolVector(vector<bool> &myvec);
tuple<bool, int> findPathInList(vector<int> &path, vector<vector<int>> &list);
bool hasRepeats(vector<int> &myvec);
vector<vector<int>> symDiff(vector<vector<int>> &arr1, vector<vector<int>> &arr2);
// Global variable
const int maxTime = 10000;
// Start here
int main()
{
int numNodes;
vector<vector<double> > edgeData;
string fileName;
//fileName = "cyc3.txt";
//fileName = "cycleNet10.txt";
//fileName = "randNet4.txt";
// ask user (from terminal) for fileName
cout << "Type in a file name (e.g. 'cycleNet10.txt')" << endl;
cin >> fileName;
tie(numNodes,edgeData) = readFile(fileName);
vector<vector<int>> p0 = getp0(numNodes);
vector<vector<int>> p1;
vector<double> p1time;
tie(p1time,p1) = getp1(edgeData);
vector<vector<int>> p2;
vector<double> p2time;
tie(p2time,p2) = getp2(edgeData);
addInfAPaths(p1,p1time,p2);
/*
Marked paths
Different from python implementation.
Instead of checking if a summand is in
the pool of marked paths, we actually find
the index of the summand in the corresponding list.
then we set that index to true (to denote marked)
in the vector of bools labeled pXmarked.
Indices of pXmarked agree with pX.
*/
// all p0 are marked.
vector<bool> p0marked(p0.size(),true);
vector<bool> p1marked(p1.size(),false);
vector<bool> p2marked(p2.size(),false);
// Build slots
vector<vector<vector<int>>> t0(p0.size());
vector<vector<vector<int>>> t1(p1.size());
vector<vector<vector<int>>> t2(p2.size());
// timer starts now
auto start = high_resolution_clock::now();
// Loop over 1-paths
//print2DIntVector(p1);
// adding 0 at end bc all are 0-paths
vector<vector<int>> summands0;
int maxIdx0;
for (int j = 0; j<p1.size(); ++j){
tie(summands0, maxIdx0) = basisChange(p1[j],p0marked, p0,t0);
// if we get summand and slot empty, fill slot
// otherwise summand is empty, mark and move on
if (summands0.empty()){
p1marked[j] = true;
}
else {
t0[maxIdx0] = summands0;
}
/*
t0[maxIdx0] = summands0;
cout << "at t0 index " << maxIdx0 << " we have " << endl;
print2DIntVector(t0[maxIdx0]);
cout << "next"<<endl;
j = 1;
tie(summands0, maxIdx0) = basisChange(p1[1],p0marked, p0,t0);
t0[maxIdx0] = summands0;
cout << "at t0 index " << maxIdx0 << " we have " << endl;
print2DIntVector(t0[maxIdx0]);
cout << "next"<<endl;
j = 2;
tie(summands0, maxIdx0) = basisChange(p1[2],p0marked, p0,t0);
if (summands0.empty()){
cout << "empty sum" << endl;
p1marked[j] = true;
t0[maxIdx0] = summands0;
cout << "at t0 index " << maxIdx0 << " we have " << endl;
print2DIntVector(t0[maxIdx0]);
}
cout << "marked 0" << endl;
print1DBoolVector(p0marked);
cout << "marked 1" << endl;
print1DBoolVector(p1marked);
*/
}
//print1DBoolVector(p1marked);
// Loop over 2-paths
//print2DIntVector(p2);
// adding 1 at end bc all are 1-paths
vector<vector<int>> summands1;
int maxIdx1;
//types should match that of pers1
double birth;
double death;
vector<double> tmpPers;
vector<vector<double>> pers1;//should be double
for (int j = 0; j<p2.size(); ++j){
tie(summands1, maxIdx1) = basisChange(p2[j],p1marked, p1,t1);
// if we get summand and slot empty, fill slot
// otherwise summand is empty, mark and move on
if (summands1.empty()){
p2marked[j] = true;
}
else {
t1[maxIdx1] = summands1;
/*
Calculating persistence a little haphazardly
Because 1-paths have at = et,
calculating et(p1[maxIdx1]) is easy. This is
entry time of the boundary summand with highest index.
Death time will be et(p2[j]), which is max of
at(p1[maxIdx1]) and at(p2[j])
*/
birth = p1time[maxIdx1];
death = p2time[j];
if (birth < death){
tmpPers.push_back(birth);
tmpPers.push_back(death);
pers1.push_back(tmpPers);
}
tmpPers.clear();
}
}
/*
print1DBoolVector(p2marked);
cout << "let's check slots" << endl;
print2DIntVector(t1[3]);
cout << "let's check slots" << endl;
print2DIntVector(t1[4]);
cout << "let's check slots" << endl;
print2DIntVector(t1[5]);
*/
// now pick up infinite pers bars
/*
if a slot is marked and empty, get its
entry time (=allow time for 1-paths).
This is the birth time.
Death time will be maxTime.
*/
death = maxTime; //death time is fixed
tmpPers.clear();
for (int i = 0; i<p1marked.size();++i){
if (p1marked[i]==true && t1[i].empty()){
birth = p1time[i];
tmpPers.push_back(birth);
tmpPers.push_back(death);
pers1.push_back(tmpPers);
tmpPers.clear();
}
}
// write output to file
ofstream outputfile;
outputfile.open ("res_"+fileName);
for (int i = 0; i < pers1.size(); ++i){
for (int j = 0; j < pers1[i].size(); ++j){
outputfile << pers1[i][j];
if (j ==0 ){
outputfile << ", ";
}
}
outputfile << '\n';
}
outputfile.close();
// stop timer
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "done! " << "pers1 is " << endl;
print2DVector(pers1);
cout << "time elapsed " << duration.count() << " milliseconds" << endl;
return 0;
}
tuple<vector<vector<int>>, int> basisChange(vector<int> &kpath,
vector<bool> &pjmarked, vector<vector<int>> &pj, vector<vector<vector<int>>> &tj){
/*
Column reduction operation. Here j = k-1.
Take a k-path, compute its boundary summands.
Differs from PPH algorithm 2, only returns u and i.
u = "good" summands
i = max index of a summand
Thus need an extra function to compute et, the entry time.
*/
int maxIdx;
vector<vector<int>> summands;
summands = simpleBoundary(kpath);
/*
Remove unmarked summands. All 0-paths are marked, so don't
need this if k = 1;
*/
if (kpath.size()>1){
summands = removeUnmarked(summands, pjmarked, pj);
}
//print1DIntVector(kpath);
//cout << "boundary" << endl;
//print2DIntVector(summands);
// Loop will start here
while (!summands.empty()){
/*
Find term with highest index
*/
int summandIdx;
tie(maxIdx,summandIdx)=getMaxIdx(summands,pj);
//cout << "maxidx is " << maxIdx << endl;
//cout << "summandidx is " << summandIdx << endl;
//cout << "is spot empty?" << tj[maxIdx].empty() << endl;
if (tj[maxIdx].empty()){
// if empty, break out of loop
break;
}
// column reduction step
//
summands = symDiff(summands, tj[maxIdx]);
//cout << "summands are " << endl;
//print2DIntVector(summands);
}
if (summands.empty()){
//cout << "ok got empty summand" << endl;
}
return make_tuple(summands, maxIdx);
}
vector<vector<int>> symDiff(vector<vector<int>> &arr1, vector<vector<int>> &arr2){
/* builds the symmetric difference of two lists of j-paths
*/
vector<vector<int>> res;
vector<int> tmp;
bool found_tmp;
int index_found_tmp;
// for each row of arr1, if not in arr2, add to res
for (int i = 0; i<arr1.size(); ++i){
tmp = arr1[i];
tie(found_tmp,index_found_tmp)=findPathInList(tmp,arr2);
if (!found_tmp){
res.push_back(tmp);
}
}
// for each row of arr2, if not in arr1, add to res
for (int i = 0; i<arr2.size(); ++i){
tmp = arr2[i];
tie(found_tmp,index_found_tmp)=findPathInList(tmp,arr1);
if (!found_tmp){
res.push_back(tmp);
}
}
return res;
}
tuple<int, int> getMaxIdx(vector<vector<int>> &pjsummands, vector<vector<int>> &pj){
/* given a list of j-paths,
pick out the j-path which has the
greatest index in pj. return
this index as maxIdx. also return the
index of the summand achieving maxIdx
as summandIdx */
int maxIdx, summandIdx;
vector<int> tmp;
bool found_tmp;
int index_found_tmp;
// initialize using 0th summand provided
summandIdx = 0;
tmp = pjsummands[summandIdx];
tie(found_tmp,index_found_tmp)=findPathInList(tmp,pj);
maxIdx = index_found_tmp;
// see if other summands have higher index
for (int i =1; i < pjsummands.size(); ++i){
tmp = pjsummands[i];
tie(found_tmp,index_found_tmp)=findPathInList(tmp,pj);
if (index_found_tmp > maxIdx){
// if bigger index found, update
summandIdx = i;
maxIdx = index_found_tmp;
}
}
return make_tuple(maxIdx,summandIdx);
}
vector<vector<int>> removeUnmarked(vector<vector<int>> &pjsummands, vector<bool> &pjmarked, vector<vector<int>> &pj){
vector<vector<int>> newSummands;
vector<int> tmp;
bool found_tmp;
int index_found_tmp;
for (int i = 0; i < pjsummands.size(); ++i){
tmp = pjsummands[i];
tie(found_tmp,index_found_tmp)=findPathInList(tmp,pj);
if (pjmarked[index_found_tmp]==true){
newSummands.push_back(tmp);
}
}
return newSummands;
}
void addInfAPaths(vector<vector<int>> &p1, vector<double> &p1time, vector<vector<int>> &p2){
/* go through 2-paths, see which "missing"
paths need to be added at infinity*/
vector<int> tmp;
bool hasRep;
bool inp1;
int index_found_tmp;
int lp = p2[0].size();
for (int i = 0; i<p2.size(); ++i){
for (int j = 0; j < lp; ++j){
for (int k = 0; k< lp; ++k){
if (k!=j){
tmp.push_back(p2[i][k]);
}
}
//print1DIntVector(tmp);
hasRep = hasRepeats(tmp);
tie(inp1,index_found_tmp)=findPathInList(tmp,p1);
if (hasRep==0 && inp1==0){
p1.push_back(tmp);
p1time.push_back(maxTime);
}
tmp.clear();
/*
cout << "has rep" << hasRepeats(tmp) << endl;
cout << "inp1 " << inp1 << endl;
cout << "next" << endl;
*/
tmp.clear();
}
}
}
vector<vector<int>> simpleBoundary(vector<int> &path){
/*
Compute the boundary summands of a p-path, p>= 1.
*/
vector<vector<int>> summands;
vector<int> tmp;
/*
outer loop is the hat operator.
it says which index to exclude.
*/
for (int i = 0; i< path.size();++i){
for (int j = 0; j < path.size(); ++j){
if (i!=j){
tmp.push_back(path[j]);
}
}
// summand built, now test for repeats
if (hasRepeats(tmp)==false){
summands.push_back(tmp);
}
tmp.clear();
}
return summands;
}
bool hasRepeats(vector<int> &myvec){
bool rep = false;
for (int i =0; i< myvec.size()-1; ++i){
if (myvec[i] == myvec[i+1]){
rep = true;
}
}
return rep;
}
tuple<bool, int> findPathInList(vector<int> &path, vector<vector<int>> &list){
/* function that searches for p-path in list of p-paths
If found, return (found, index of path in list).
Otherwise return (!found, list.end())
*/
// initialize return objects
bool found;
int index;
// create iterator and perform find
vector<vector<int>>::iterator it;
it = find(list.begin(), list.end(), path);
if (it != list.end()){
found = true;
index = distance(list.begin(),it);
//cout << "Found item at position " << index << endl;
}
else {
found = false;
index = distance(list.begin(),it);
//cout << "item not found. distance" << index << endl;
}
return make_tuple(found,index);
}
tuple<vector<double>, vector<vector<int>>> getp2(vector<vector<double> > &edgeData){
vector<vector<double>> p2temp;
vector<double> temp(4);
//double tmpTime;
vector<double> helpTemp(2);
for (int i = 0; i < edgeData.size(); i++){
helpTemp[0] = edgeData[i][0];
helpTemp[1] = edgeData[i][1];
for (int j = 0; j < edgeData.size();j++){
if (edgeData[j][0] == helpTemp[1]){
temp[0] = helpTemp[0];//first vertex
temp[1] = helpTemp[1];//second vertex
temp[2] = edgeData[j][1];//third vertex
//max of first/second pair
temp[3] = max(edgeData[j][2],edgeData[i][2]);
p2temp.push_back(temp);
//p2time.push_back();
//cout << helpTemp[0] << helpTemp[1] << edgeData[j][1] << endl;
}
}
}
//print2DVector(p2temp);
p2temp = sortByCol(3,p2temp);
//cout << "try again" << endl;
//print2DVector(p2temp);
vector<vector<int>> p2;
vector<double> p2time(p2temp.size());
vector<int> tmp(3); //tmp(3) for 2-paths
for (int i = 0; i < p2temp.size() ; ++i){
p2time[i] = p2temp[i].back();
for (int j = 0; j < 3; ++j){
// j< 3 above for 2-paths
tmp[j] = p2temp[i][j];
}
p2.push_back(tmp);
}
return make_tuple(p2time,p2);
}
tuple<vector<double>, vector<vector<int>>> getp1(vector<vector<double> > &edgeData){
// get 1 paths and entry times
vector<vector<int>> p1;
vector<double> p1time;
vector<int> temp(2);
double tmpTime;
for (int i =0; i < edgeData.size() ; i++){
temp[0] = edgeData[i][0];
temp[1] = edgeData[i][1];
tmpTime = edgeData[i][2];
p1.push_back(temp);//append
p1time.push_back(tmpTime);
}
return make_tuple(p1time,p1);
}
vector<vector <int>> getp0(int numNodes){
// get 0 paths
vector<vector<int>> p0;
vector<int> tmp;
for (int i=1; i<= numNodes; i++){
tmp.push_back(i);
p0.push_back(tmp);
tmp.clear();
}
return p0;
}
tuple<int, vector<vector<double> > > readFile(string fileName){
/*
Function that reads a provided text file.
Top row of file contains number of nodes
Remaining rows contain directed edge pairs and
entry time.
Important: node labels are 1, 2, 3, ... (do not start from 0!)
Multiple values returned, hence using tuple class
edgeData returned sorted by time
*/
// create ifstream object
ifstream file;
string currentLine;
// initialize objects that will be returned
int numNodes;
vector<vector<double>> edgeData;
double a, b,t ;
file.open(fileName);
getline(file,currentLine);//first row has number of nodes
numNodes = stoi(currentLine);//convert to integer
// now append each row to vector of vectors
vector<double> tempVect(3); //to store each (edge, time) row
while (getline(file,currentLine)){
stringstream lineSS(currentLine); //build stringstream object
lineSS >> a >> b >> t;//pass stringstream into variables
tempVect[0] = a;
tempVect[1] = b;
tempVect[2] = t;
edgeData.push_back(tempVect);//append
}
file.close();
edgeData = sortByCol(2,edgeData);//sort by time
return make_tuple(numNodes, edgeData);
}
void print2DVector(vector<vector<double> > &myvec){
/* Little helper function for printing
out all the values of a 2d vector of doubles
*/
for (int i = 0; i < myvec.size(); i++){
for (int j = 0; j < myvec[i].size(); j++){
cout << myvec[i][j] << " ";
}
cout << endl;
}
}
void print2DIntVector(vector<vector<int> > &myvec){
/* Little helper function for printing
out all the values of a 2d vector of ints
*/
for (int i = 0; i < myvec.size(); i++){
for (int j = 0; j < myvec[i].size(); j++){
cout << myvec[i][j] << " ";
}
cout << endl;
}
}
void print1DIntVector(vector<int> &myvec){
/* Print all values of int vector
*/
for (int i = 0; i < myvec.size(); i++){
cout << myvec[i] << endl;
}
}
void print1DBoolVector(vector<bool> &myvec){
/* Print all values of bool vector
*/
for (int i = 0; i < myvec.size(); i++){
cout << myvec[i] << endl;
}
}
void print1DVector(vector<double> &myvec){
/* Print all values of int vector
*/
for (int i = 0; i < myvec.size(); i++){
cout << myvec[i] << endl;
}
}
vector<vector <double> > sortByCol(int sortBy, vector<vector <double> > &unsrtMat){
/*
Function that sorts a matrix of doubles by a particular column. E.g.
121
232
311 becomes
121
311
232
Uses sort() from <algorithm> library.
Third argument in sort() is a comparator; in this case
it is a lambda expression.
*/
// [sortBy] puts variable inside capture-list
sort(unsrtMat.begin(),unsrtMat.end(),
[sortBy] (const std::vector<double> &a, const std::vector<double> &b)
{
return a[sortBy] < b[sortBy];
});
return unsrtMat;
}