13 printf(
" Renumbering nodes.\n");
14 Graph = TSnap::ConvertGraph<PNGraph>(GraphPt,
true);
23 for (
int j = 0; j < RowN; j++) {
24 const TIntV& RowV = NodeH[j].OutNIdV;
26 for (
int i = 0; i < RowV.
Len(); i++) {
27 Result[j] += B(RowV[i], ColId);
37 for (
int j = 0; j < RowN; j++) {
38 const TIntV& RowV = NodeH[j].OutNIdV;
40 for (
int i = 0; i < RowV.
Len(); i++) {
41 Result[j] += Vec[RowV[i]];
51 for (
int i = 0; i < ColN; i++) Result[i] = 0.0;
52 for (
int j = 0; j < ColN; j++) {
53 const TIntV& RowV = NodeH[j].OutNIdV;
54 for (
int i = 0; i < RowV.
Len(); i++) {
55 Result[RowV[i]] += B(j, ColId);
65 for (
int i = 0; i < RowN; i++) Result[i] = 0.0;
66 for (
int j = 0; j < RowN; j++) {
67 const TIntV& RowV = NodeH[j].OutNIdV;
68 for (
int i = 0; i < RowV.
Len(); i++) {
69 Result[RowV[i]] += Vec[j];
86 printf(
" Renumbering %d nodes....", GraphPt->
GetNodes());
88 Graph = TSnap::ConvertGraph<PUNGraph>(GraphPt,
true);
94 printf(
"done [%s]\n", ExeTm.GetStr());
103 for (
int j = 0; j < RowN; j++) {
104 const TIntV& RowV = NodeH[j].NIdV;
106 for (
int i = 0; i < RowV.
Len(); i++) {
107 Result[j] += B(RowV[i], ColId);
117 for (
int j = 0; j < RowN; j++) {
118 const TIntV& RowV = NodeH[j].NIdV;
120 for (
int i = 0; i < RowV.
Len(); i++) {
121 Result[j] += Vec[RowV[i]];
131 for (
int i = 0; i < ColN; i++) Result[i] = 0.0;
132 for (
int j = 0; j < ColN; j++) {
133 const TIntV& RowV = NodeH[j].NIdV;
134 for (
int i = 0; i < RowV.
Len(); i++) {
135 Result[RowV[i]] += B(j, ColId);
145 for (
int i = 0; i < RowN; i++) Result[i] = 0.0;
146 for (
int j = 0; j < RowN; j++) {
147 const TIntV& RowV = NodeH[j].NIdV;
148 for (
int i = 0; i < RowV.
Len(); i++) {
149 Result[RowV[i]] += Vec[j];
159 for (
int i = 0; i < ValV.
Len(); i++) {
165 for (
int i = 0; i < ValV.
Len(); i++) {
166 if (ValV[i]>0.0) { IsAllNeg=
false;
break; }
168 if (IsAllNeg && InvertSign) {
169 for (
int i = 0; i < ValV.
Len(); i++) {
170 ValV[i] = -ValV[i]; }
176 const int Nodes = Graph->
GetNodes();
180 TFltVV AdjMtx(Nodes+1, Nodes+1);
185 NodeIdH.
AddKey(NodeI.GetId()); }
187 const int NodeId = NodeIdH.
GetKeyId(NodeI.GetId()) + 1;
188 for (
int e = 0; e < NodeI.GetOutDeg(); e++) {
189 const int DstNId = NodeIdH.
GetKeyId(NodeI.GetOutNId(e)) + 1;
190 if (NodeId != DstNId) AdjMtx.
At(NodeId, DstNId) = 1;
196 printf(
"\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->
GetEdges()); }
200 int CalcVals = int(2*SngVals);
207 else {
TFltVV LSingV, RSingV;
211 printf(
"\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.
Len()); }
212 if (SngValV.
Len() < SngVals) {
213 printf(
" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.
Len()); }
226 const int Nodes = Graph->
GetNodes();
231 TFltVV AdjMtx(Nodes+1, Nodes+1);
235 NodeIdH.
AddKey(NodeI.GetId()); }
237 const int NodeId = NodeIdH.
GetKeyId(NodeI.GetId()) + 1;
238 for (
int e = 0; e < NodeI.GetOutDeg(); e++) {
239 const int DstNId = NodeIdH.
GetKeyId(NodeI.GetOutNId(e)) + 1;
240 if (NodeId != DstNId) AdjMtx.
At(NodeId, DstNId) = 1;
246 printf(
"\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->
GetEdges()); }
253 for (
int i = 0; i < SngValV.
Len(); i++) {
254 if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } }
255 LSingV.
GetCol(ValN, LeftSV);
256 RSingV.
GetCol(ValN, RightSV);
262 const int Nodes = Graph->
GetNodes();
269 TFltVV AdjMtx(Nodes+1, Nodes+1);
273 NodeIdH.
AddKey(NodeI.GetId()); }
275 const int NodeId = NodeIdH.
GetKeyId(NodeI.GetId())+1;
276 for (
int e = 0; e < NodeI.GetOutDeg(); e++) {
277 const int DstNId = NodeIdH.
GetKeyId(NodeI.GetOutNId(e))+1;
278 if (NodeId != DstNId) AdjMtx.
At(NodeId, DstNId) = 1;
284 printf(
"\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->
GetEdges());
293 for (
int i = 0; i < SngValV.
Len(); i++) {
296 SngValIdV.
Sort(
false);
298 for (
int v = 0; v < SngValIdV.
Len(); v++) {
300 LSingV.
GetCol(SngValIdV[v].Val2, LeftSV.
Last());
302 RSingV.
GetCol(SngValIdV[v].Val2, RightSV.
Last());
322 printf(
"\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*EigVals, EigValV.
Len()); }
323 if (EigValV.
Len() < EigVals) {
324 printf(
" ***TRIED %d GOT %d values** \n", 2*EigVals, EigValV.
Len()); }
341 EigVecVV.
GetCol(0, EigVecV);
348 const int Nodes = Graph->GetNodes();
351 int CalcVals = int(2*EigVecs);
352 if (CalcVals > Nodes) { CalcVals = Nodes; }
358 printf(
"\n ***EXCEPTION: TRIED %d GOT %d values** \n", CalcVals, EigValV.
Len()); }
359 if (EigValV.
Len() < EigVecs) {
360 printf(
" ***TRIED %d GOT %d values** \n", CalcVals, EigValV.
Len()); }
364 for (
int i = 0; i < EigValV.
Len(); i++) {
367 EigValIdV.
Sort(
false);
369 for (
int v = 0; v < EigValIdV.
Len(); v++) {
371 EigVecVV.
GetCol(EigValIdV[v].Val2, EigVecV.
Last());
383 if (MaxEigVecs<=1) { MaxEigVecs=1000; }
384 int EigVecs =
TMath::Mn(Graph->GetNodes(), MaxEigVecs);
385 printf(
"start %d vecs...", EigVecs);
389 printf(
"\n ***EXCEPTION: TRIED %d GOT %d values** \n", EigVecs, EigValV.
Len()); }
390 printf(
" ***TRIED %d GOT %d values in %s\n", EigVecs, EigValV.
Len(), ExeTm.
GetStr());
393 if (EigValV.
Empty()) {
return; }
394 for (
int v = 0; v < EigVecVV.
GetCols(); v++) {
395 EigVecVV.
GetCol(v, EigVec);
401 namespace TSnapDetail {
403 double Sum2=0.0, Sum4=0.0;
404 for (
int i = 0; i < EigVec.
Len(); i++) {
405 Sum2 += EigVec[i]*EigVec[i];
406 Sum4 += pow(EigVec[i].Val, 4.0);
408 return Sum4/(Sum2*Sum2);
void GetEigVals(const PUNGraph &Graph, const int &EigVals, TFltV &EigValV)
Computes top EigVals eigenvalues of the adjacency matrix representing a given undirected Graph...
static const T & Mn(const T &LVal, const T &RVal)
double GetInvParticipRatEig(const TFltV &EigVec)
TPair< TFlt, TInt > TFltIntPr
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
void GetInvParticipRat(const PUNGraph &Graph, int MaxEigVecs, int TimeLimit, TFltPrV &EigValIprV)
void GetEigVec(const PUNGraph &Graph, TFltV &EigVecV)
Computes the leading eigenvector of the adjacency matrix representing a given undirected Graph...
static void Lanczos(const TMatrix &Matrix, int NumEig, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
static void SimpleLanczosSVD(const TMatrix &Matrix, const int &CalcSV, TFltV &SngValV, const bool &DoLocalReortoP=false)
static void Lanczos2(const TMatrix &Matrix, int MaxNumEig, int MaxSecs, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
int GetEdges() const
Returns the number of edges in the graph.
THash< TInt, TNode > NodeH
TSizeTy Len() const
Returns the number of elements in the vector.
bool IsAllValVNeg(TFltV &ValV, const bool &InvertSign)
int GetNodes() const
Returns the number of nodes in the graph.
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
int GetNodes() const
Returns the number of nodes in the graph.
TNGraphMtx(const PNGraph &GraphPt)
bool Empty() const
Tests whether the vector is empty.
void GetSngVec(const PNGraph &Graph, TFltV &LeftSV, TFltV &RightSV)
Computes the leading left and right singular vector of the adjacency matrix representing a directed G...
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
TUNGraphMtx(const PUNGraph &GraphPt)
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
void PMultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
void PMultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
static void Svd1Based(const TFltVV &InMtx1, TFltVV &LSingV, TFltV &SingValV, TFltVV &RSingV)
const TVal & Last() const
Returns a reference to the last element of the vector.
TPair< TFlt, TFlt > TFltPr
void GetSngVals(const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
Computes largest SngVals singular values of the adjacency matrix representing a directed Graph...
int GetKeyId(const TKey &Key) const
static void SimpleLanczos(const TMatrix &Matrix, const int &NumEig, TFltV &EigValV, const bool &DoLocalReortoP=false, const bool &SvdMatrixProductP=false)
int AddKey(const TKey &Key)
void PMultiply(const TFltVV &B, int ColId, TFltV &Result) const
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
THash< TInt, TNode > NodeH
Node iterator. Only forward iteration (operator++) is supported.
void GetCol(const TSizeTy &ColN, TVec< TVal, TSizeTy > &Vec) const
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
void SetAllInvertSign(TFltV &ValV, const double &Val)
const char * GetStr() const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
void PMultiply(const TFltVV &B, int ColId, TFltV &Result) const
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const