-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphIO.hpp
64 lines (55 loc) · 1.22 KB
/
graphIO.hpp
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
#include <stdio.h>
#include <cstdlib>
using namespace linalgcpp;
SparseMatrix<double> getLaplacian(std::string fileName, int index, bool edgeRepeat){
std::fstream file(fileName);
int a,b,c;
file>>a>>b>>c;
CooMatrix<double> ADJ(a,b);
for(int i=0;i<a;i++){
ADJ.Add(i,i,0);//otherwise AddDiag() would not work
}
std::vector<double> degree(a);
for(int i=0;i<c;i++){
file >>a>>b;
a-=index;
b-=index;
if(edgeRepeat){
ADJ.Add(a,b,-1);
degree[a]++;
}else{
ADJ.AddSym(a,b,-1);
degree[a]++;
degree[b]++;
}
}
//std::cout<<degree<<std::endl;
//L=D-A
SparseMatrix<double> LAP = ADJ.ToSparse();
LAP.AddDiag(degree);
return LAP;
}
SparseMatrix<double> getWeightedAdjacency(std::string fileName, int index){
std::fstream file(fileName);
int a,b;
file >>a>>b;
CooMatrix<double> ADJ(a,a);
int u,v;
double w;
for(int i=0;i<b;i++){
file >>u>>v>>w;
u-=index;
v-=index;
ADJ.AddSym(u,v,w);
}
return ADJ.ToSparse();
}
SparseMatrix<double> getReducedLaplacian(SparseMatrix<double> lap){
int n = lap.Cols();
std::vector<int> row(n-1);
for(int i=0;i<n-1;i++){
row[i]=i;
}
std::vector<int> col=row;
return lap.GetSubMatrix(row,col);
}