5 # define F77_NAME(name) name ## _
7 # define F77_NAME(name) name
13 void F77_NAME(
dsaupd)(
int *ido,
char *bmat,
int *n,
char *which,
int *nev,
14 double *tol,
double *resid,
int *ncv,
double *V,
15 int *ldv,
int *iparam,
int *ipntr,
double *workd,
16 double *workl,
int *lworkl,
int *info);
18 void F77_NAME(
dseupd)(
int *rvec,
char *HowMny,
int *select,
double *d,
19 double *Z,
int *ldz,
double *sigma,
char *bmat,
int *n,
20 char *which,
int *nev,
double *tol,
double *resid,
21 int *ncv,
double *V,
int *ldv,
int *iparam,
int *ipntr,
22 double *workd,
double *workl,
int *lworkl,
int *info);
30 int minval =
MIN(i, j);
31 int maxval =
MAX(i, j);
33 edge_weights(maxval) += 1;
38 if (motif_lc ==
"m1") {
return M1; }
39 else if (motif_lc ==
"m2") {
return M2; }
40 else if (motif_lc ==
"m3") {
return M3; }
41 else if (motif_lc ==
"m4") {
return M4; }
42 else if (motif_lc ==
"m5") {
return M5; }
43 else if (motif_lc ==
"m6") {
return M6; }
44 else if (motif_lc ==
"m7") {
return M7; }
45 else if (motif_lc ==
"m8") {
return M8; }
46 else if (motif_lc ==
"m9") {
return M9; }
47 else if (motif_lc ==
"m10") {
return M10; }
48 else if (motif_lc ==
"m11") {
return M11; }
49 else if (motif_lc ==
"m12") {
return M12; }
50 else if (motif_lc ==
"m13") {
return M13; }
51 else if (motif_lc ==
"bifan") {
return bifan; }
52 else if (motif_lc ==
"bi-fan") {
return bifan; }
53 else if (motif_lc ==
"triangle") {
return triangle; }
54 else if (motif_lc ==
"clique3") {
return clique3; }
55 else if (motif_lc ==
"clique4") {
return clique4; }
56 else if (motif_lc ==
"clique5") {
return clique5; }
57 else if (motif_lc ==
"clique6") {
return clique6; }
58 else if (motif_lc ==
"clique7") {
return clique7; }
59 else if (motif_lc ==
"clique8") {
return clique8; }
60 else if (motif_lc ==
"clique9") {
return clique9; }
61 else if (motif_lc ==
"semiclique") {
return semiclique; }
62 else if (motif_lc ==
"semi-clique") {
return semiclique; }
63 else if (motif_lc ==
"edge") {
return edge; }
64 else if (motif_lc ==
"undir") {
return edge; }
65 else if (motif_lc ==
"undirected") {
return edge; }
188 int max_nodes = graph->
GetMxNId() + 1;
194 int src = NI.GetId();
195 int num_nbrs = NI.GetOutDeg();
197 for (
int i = 0; i < NI.GetInDeg(); ++i) {
198 int dst = NI.GetInNId(i);
199 if (!NI.IsOutNId(dst)) {
207 order =
TIntV(max_nodes);
208 for (
int i = 0; i < order.
Len(); ++i) {
209 order[degrees[i].Dat] = i;
218 int src = NI.GetId();
219 int src_pos = order[src];
222 TIntV neighbors_higher;
223 for (
int i = 0; i < NI.GetOutDeg(); i++) {
224 int nbr = NI.GetOutNId(i);
225 if (order[nbr] > src_pos) {
226 neighbors_higher.
Add(nbr);
229 for (
int i = 0; i < NI.GetInDeg(); i++) {
230 int nbr = NI.GetInNId(i);
231 if (!NI.IsOutNId(nbr) && order[nbr] > src_pos) {
232 neighbors_higher.
Add(nbr);
236 for (
int ind1 = 0; ind1 < neighbors_higher.
Len(); ind1++) {
237 for (
int ind2 = ind1 + 1; ind2 < neighbors_higher.
Len(); ind2++) {
238 int dst1 = neighbors_higher[ind1];
239 int dst2 = neighbors_higher[ind2];
241 if (graph->
IsEdge(dst1, dst2) || graph->
IsEdge(dst2, dst1)) {
242 bool motif_occurs =
false;
245 motif_occurs =
IsMotifM1(graph, src, dst1, dst2);
248 motif_occurs =
IsMotifM2(graph, src, dst1, dst2);
251 motif_occurs =
IsMotifM3(graph, src, dst1, dst2);
254 motif_occurs =
IsMotifM4(graph, src, dst1, dst2);
257 motif_occurs =
IsMotifM5(graph, src, dst1, dst2);
260 motif_occurs =
IsMotifM6(graph, src, dst1, dst2);
263 motif_occurs =
IsMotifM7(graph, src, dst1, dst2);
285 int center = NI.GetId();
289 for (
int i = 0; i < NI.GetOutDeg(); i++) {
290 int nbr = NI.GetOutNId(i);
293 for (
int i = 0; i < NI.GetInDeg(); i++) {
294 int nbr = NI.GetInNId(i);
295 if (!NI.IsOutNId(nbr)) {
300 for (
int ind1 = 0; ind1 < neighbors.
Len(); ind1++) {
301 for (
int ind2 = ind1 + 1; ind2 < neighbors.
Len(); ind2++) {
302 int dst1 = neighbors[ind1];
303 int dst2 = neighbors[ind2];
304 bool motif_occurs =
false;
307 motif_occurs =
IsMotifM8(graph, center, dst1, dst2);
310 motif_occurs =
IsMotifM9(graph, center, dst1, dst2);
313 motif_occurs =
IsMotifM10(graph, center, dst1, dst2);
316 motif_occurs =
IsMotifM11(graph, center, dst1, dst2);
319 motif_occurs =
IsMotifM12(graph, center, dst1, dst2);
322 motif_occurs =
IsMotifM13(graph, center, dst1, dst2);
347 node_ids.
Add(NI.GetId());
349 for (
int i = 0; i < node_ids.
Len(); i++) {
350 for (
int j = i + 1; j < node_ids.
Len(); j++) {
351 int src1 = node_ids[i];
352 int src2 = node_ids[j];
357 for (
int k = 0; k < NI1.
GetOutDeg(); k++) {
360 nbr_counts(nbr) += 1;
366 for (
int k = 0; k < NI2.
GetOutDeg(); k++) {
369 nbr_counts(nbr) += 1;
376 it < nbr_counts.
EndI(); it++) {
383 for (
int ind1 = 0; ind1 < common.
Len(); ind1++) {
384 for (
int ind2 = (ind1 + 1); ind2 < common.
Len(); ind2++) {
385 int dst1 = common[ind1];
386 int dst2 = common[ind2];
407 int src = NI.GetId();
408 for (
int j = 0; j < NI.GetDeg(); j++) {
409 int dst = NI.GetNbrNId(j);
410 if (dst <= src) {
continue; }
415 for (
int k = 0; k < dst_NI.
GetOutDeg(); k++) {
417 if (nbr != src && nbr != dst && NI.IsNbrNId(nbr)) {
422 for (
int k = 0; k < common.
Len(); k++) {
423 for (
int l = k + 1; l < common.
Len(); l++) {
424 int nbr1 = common[k];
425 int nbr2 = common[l];
426 if (!graph->
IsEdge(nbr1, nbr2)) {
445 int src = it.GetSrcNId();
446 int dst = it.GetDstNId();
451 if (!graph->
IsEdge(dst, src) || src < dst) {
459 int src = it.GetSrcNId();
460 int dst = it.GetDstNId();
506 cnw.
Run(clique_size);
557 for (
int i = 0; i <= max_nodes; i++) {
566 for (
int i = 0; i < k + 2; ++i) {
573 for (
int src = 0; src < N; src++) {
575 int deg = src_it.
GetDeg();
576 graph_k[src] =
TIntV(deg);
579 graph_k[src][
edge] = dst;
587 for (
int i = 0; i < U.
Len(); i++) {
597 for (
int i = 0; i < U.
Len(); i++) {
599 int size =
graph_[k][node].Len();
606 degs.
Trunc(end_size);
610 order =
TIntV(degs.Len());
611 for (
int i = 0; i < degs.Len(); i++) {
612 order[i] = degs[i].Dat;
617 for (
int i = 0; i < clique.
Len(); ++i) {
618 for (
int j = i + 1; j < clique.
Len(); ++j) {
625 for (
int i = 0; i < U.
Len(); i++) {
628 for (
int j = 0; j < nbrs.
Len(); j++) {
635 for (
int k = 0; k <
C_.
Len(); k++) {
636 clique[k + 2] =
C_[k];
645 for (
int i = 0; i < Up.
Len(); i++) {
649 TIntV nbrs_klast_new;
650 for (
int j = 0; j < nbrs_klast.
Len(); j++) {
651 int nbr = nbrs_klast[j];
655 nbrs_klast_new.
Add(nbr);
658 graph_[klast][node] = nbrs_klast_new;
672 for (
int i = 0; i < order.
Len(); i++) {
678 for (
int j = 0; j < U_prime.
Len(); j++) {
689 for (
int j = 0; j < U_prime.
Len(); j++) {
718 double tol,
int maxiter) {
736 for (
int i = 0; i < d.
Len(); i++) {
747 for (
int j = 0; j < N; j++) {
750 for (
int ind = 0; ind < W_col.
Len(); ind++) {
751 int i = W_col[ind].Key;
752 double val = W_col[ind].Dat;
762 fvec = evecs.
ColV[1];
763 for (
int i = 0; i < fvec.
Len(); i++) {
774 for (
int i = 0; i < weights.
Len(); i++) {
775 num_edges += weights[i].
Len();
778 for (
int i = 0; i < weights.
Len(); i++) {
781 for (
int i = 0; i < weights.
Len(); i++) {
800 for (
int i = 0; i < ids.
Len(); i++) {
802 if (id_map.
IsKey(
id)) {
818 for (
int i = 0; i < fvec.
Len(); i++) {
824 for (
int i = 0; i < fvec.
Len(); i++) {
825 order[i] = fvec_inds[i].Dat;
833 double total_vol = 0;
834 for (
int ind = 0; ind < order.
Len(); ind++) {
838 total_vol += it->Dat;
841 double vol_comp = total_vol;
843 for (
int ind = 0; ind < order.
Len() - 1; ind++) {
844 int node = order[ind];
849 if (node == nbr) {
continue; }
850 double val = it->Dat;
852 if (rank[nbr] > ind) {
863 double mvol =
MIN(vol, vol_comp);
867 conds[ind] = cut / mvol;
872 double tol,
int maxiter) {
877 int max_wcc_size = 0;
878 int max_wcc_ind = -1;
879 for (
int i = 0; i < components.
Len(); i++) {
880 int size = components[i].
Len();
881 if (size > max_wcc_size) {
886 TCnCom comp = components[max_wcc_ind];
888 if (comp.
Len() == 1) {
889 printf(
"WARNING: No non-trivial connected components "
890 "(likely due to no instances of the motif)\n");
903 for (
int ind1 = 0; ind1 < comp.
Len(); ++ind1) {
904 int c_ind = comp[ind1];
906 int i_ind = id_map(c_ind);
913 int j_ind = id_map(ind2);
916 matrix_entries[j_ind].Add(
TIntFltKd(i_ind, val));
928 Sweep(W, fvec, conds, order);
932 double min_cond = 2.0;
934 for (
int i = 0; i < conds.
Len(); i++) {
935 double cond = conds[i];
936 if (cond < min_cond) {
941 sweepcut.
cond = min_cond;
944 int end = min_ind + 1;
945 if (end >= conds.
Len() / 2) {
947 end = conds.
Len() + 1;
949 for (
int i = start; i < end; i++) {
950 cluster.
Add(rev_id_map[order[i]]);
968 double *resid =
new double[n];
970 int ncv =
MIN(
MAX(2 * nev, 20), n - 1);
972 double *V =
new double[ncv * n];
977 int *iparam =
new int[11];
985 int *ipntr =
new int[11];
987 double *workd =
new double[3 * n];
989 int lworkl = ncv * (ncv + 8);
990 double *workl =
new double[lworkl];
997 F77_NAME(
dsaupd)(&ido, &bmat, &n, &which[0], &nev, &tol, &resid[0], &ncv,
998 &V[0], &ldv, &iparam[0], &ipntr[0], &workd[0], &workl[0],
1002 double *load = &workd[ipntr[0] - 1];
1003 for (
int i = 0; i < n; i++) {
1004 result[i] = load[i];
1010 double *store = &workd[ipntr[1] - 1];
1011 for (
int i = 0; i < n; i++) {
1014 }
else if (ido == 99) {
1030 int *select =
new int[ncv];
1032 double *d =
new double[nev];
1036 F77_NAME(
dseupd)(&rvec, &howmny, select, &d[0], &V[0], &ldv, &sigmar, &bmat,
1037 &n, &which[0], &nev, &tol, &resid[0], &ncv, &V[0], &ldv,
1038 &iparam[0], &ipntr[0], &workd[0], &workl[0], &lworkl, &info);
1045 for (
int i = 0; i < nev; i++) {
1052 for (
int i = 0; i < nev; i++) {
1053 double eval = d_inds[i].Key;
1054 int ind = d_inds[i].Dat;
1057 for (
int j = ind * n; j < ind * n + n; j++) {
void F77_NAME() dsaupd(int *ido, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *V, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *info)
void AdjustLabels(int kcurr, int klast, const TIntV &U)
Edge iterator. Only forward iteration (operator++) is supported.
static bool IsMotifM12(PNGraph graph, int center, int v, int w)
TIter EndI() const
Returns an iterator referring to the past-the-end element in the vector.
void FlushCliques(const TIntV &U)
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
static MotifType ParseMotifType(const TStr &motif)
static bool IsMotifM10(PNGraph graph, int center, int v, int w)
static bool IsMotifM2(PNGraph graph, int u, int v, int w)
static bool IsMotifM7(PNGraph graph, int u, int v, int w)
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
static bool IsMotifM4(PNGraph graph, int u, int v, int w)
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
TSizeTy Len() const
Returns the number of elements in the vector.
TKeyDat< TInt, TFlt > TIntFltKd
Node iterator. Only forward iteration (operator++) is supported.
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
static double Sqrt(const double &x)
static bool IsMotifM11(PNGraph graph, int center, int v, int w)
int GetNodes() const
Returns the number of nodes in the graph.
static void Normalize(TFltV &x)
static bool IsMotifM1(PNGraph graph, int u, int v, int w)
int GetDeg() const
Returns degree of the current node.
void SubgraphDegreeOrder(int k, const TIntV &U, TIntV &order)
static bool IsBidirEdge(PNGraph graph, int u, int v)
static bool IsMotifM13(PNGraph graph, int center, int v, int w)
static bool IsMotifM3(PNGraph graph, int u, int v, int w)
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Edge iterator. Only forward iteration (operator++) is supported.
static void Throw(const TStr &MsgStr)
void UpdateWeights(const TIntV &clique)
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
static PUNGraph UnweightedGraphRepresentation(const WeightVH &weights)
bool IsNIdIn(const int &NId) const
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
static void SpectralCut(const WeightVH &weights, TSweepCut &sweepcut, double tol=kDefaultTol, int maxiter=kMaxIter)
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
void SymeigsSmallest(const TSparseColMatrix &A, int nev, TFltV &evals, TFullColMatrix &evecs, double tol, int maxiter)
TVec< TIntFltKdV > ColSpVV
static void IncrementWeight(int i, int j, WeightVH &weights)
static void MotifAdjacency(PNGraph graph, MotifType motif, WeightVH &weights)
TVec< THash< TInt, TInt > > WeightVH
static void WedgeMotifAdjacency(PNGraph graph, MotifType motif, WeightVH &weights)
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
void CliqueEnum(int k, const TIntV &U)
static void BifanMotifAdjacency(PNGraph graph, WeightVH &weights)
static bool IsMotifM9(PNGraph graph, int center, int v, int w)
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
TVal * TIter
Random access iterator to TVal.
static bool IsMotifM6(PNGraph graph, int u, int v, int w)
static void TriangleMotifAdjacency(PNGraph graph, MotifType motif, WeightVH &weights)
void Multiply(const TFltVV &B, int ColId, TFltV &Result) const
static bool IsUnidirEdge(PNGraph graph, int u, int v)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
static void DegreeOrdering(PNGraph graph, TIntV &order)
PGraph GetKCore(const PGraph &Graph, const int &K)
int GetOutDeg() const
Returns out-degree of the current node.
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
TVec< TVec< TIntV > > graph_
static double NFiedlerVector(const TSparseColMatrix &W, TFltV &fvec, double tol=kDefaultTol, int maxiter=kMaxIter)
void F77_NAME() dseupd(int *rvec, char *HowMny, int *select, double *d, double *Z, int *ldz, double *sigma, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *V, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *info)
Node iterator. Only forward iteration (operator++) is supported.
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
static void MapIdsToFirstN(const TIntV &ids, THash< TInt, TInt > &id_map, TIntV &rev_id_map)
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
static void Sweep(const TSparseColMatrix &W, const TFltV &fvec, TFltV &conds, TIntV &order)
static bool IsMotifM5(PNGraph graph, int u, int v, int w)
static bool IsMotifM8(PNGraph graph, int center, int v, int w)
static void SemicliqueMotifAdjacency(PUNGraph graph, WeightVH &weights)
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
bool IsKey(const TKey &Key) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
void DelSelfEdges(const PGraph &Graph)
Removes all the self-edges from the graph.
void DelLast()
Removes the last element of the vector.
void Trunc(const TSizeTy &_Vals=-1)
Truncates the vector's length and capacity to _Vals elements.
static void CliqueMotifAdjacency(PUNGraph graph, int clique_size, WeightVH &weights)
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
static bool IsNoEdge(PNGraph graph, int u, int v)
static void GetMotifCluster(PNGraph graph, MotifType motif, TSweepCut &sweepcut, double tol=kDefaultTol, int maxiter=kMaxIter)
static void EdgeMotifAdjacency(PNGraph graph, WeightVH &weights)