7 printf(
"%d: [", HI.GetKey()());
9 for (
int i = 0; i < Feature.
Len(); ++i) {
13 printf(
"%f", Feature[i]());
43 NthFeature.
Add(HI.GetDat()[N]);
51 printf(
"finish neighborhood features\n");
53 printf(
"finish recursive features\n");
59 printf(
"finish local features\n");
61 printf(
"finish egonet features\n");
65 int SimilarityThreshold = 0;
66 TIntFtrH RetainedFeatures = Features;
75 ++SimilarityThreshold;
76 printf(
"recursion %d: ", SimilarityThreshold);
83 Features.
GetDat(
TInt(NI.GetId())).Add(NI.GetInDeg());
93 Features.
GetDat(NId).Add(ArndEdges);
100 if (0 == NumCurrFeatures) {
104 for (
int i = 0; i < NumCurrFeatures; ++i) {
107 for (
int j = 0; j < NI.GetInDeg(); ++j) {
108 int NbrNId = NI.GetInNId(j);
109 Sum += CurrFeatures.
GetDat(NbrNId)[i]();
111 NewFeatures.
GetDat(NI.GetId()).Add(Sum);
112 NewFeatures.
GetDat(NI.GetId()).Add(0 == NI.GetInDeg()?
113 0 : (float(Sum) / NI.GetInDeg()));
120 const TIntFtrH& NewFeatures,
const int SimilarityThreshold) {
124 const float BinFraction = 0.5;
127 SimilarityThreshold);
134 HI < SrcFeatures.
EndI();
138 DstFeatures.
GetDat(HI.GetKey()).Add(Feature[ColIdx]);
140 for (
int i = 0; i < Feature.
Len(); ++i) {
141 DstFeatures.
GetDat(HI.GetKey()).Add(Feature[i]);
148 const float BinFraction) {
151 for (
int i = 0; i < NumFeatures; ++i) {
155 return LogBinFeatures;
159 const int SimilarityThreshold) {
162 for (
int i = 0; i < NumFeatures; ++i) {
165 for (
int i = 0; i < NumFeatures; ++i) {
167 for (
int j = i + 1; j < NumFeatures; ++j) {
170 !FeatureGraph->
IsEdge(i, j)) {
183 for (
int i = 0; i < Wcc.
Len(); ++i) {
184 RetainedIdx.
Add(Wcc[i][0]);
190 for (
int i = 0; i < RetainedIdx.
Len(); ++i) {
191 const int IdxNewFeatures = RetainedIdx[i] - StartIdxNewFeatures;
192 if (IdxNewFeatures >= 0) {
196 return RetainedFeatures;
202 F.
AddDat(HI.GetKey(), HI.GetDat()[Idx]);
207 SortedNId.
Add(HI.GetKey());
214 int NumNodes = LogBinFeatures.
Len();
217 while (NumAssigned < NumNodes) {
218 int NumToAssign = ceil(BinFraction * (NumNodes - NumAssigned));
219 for (
int i = NumAssigned; i < NumAssigned + NumToAssign; ++i) {
220 int NId = SortedNId[i];
221 LogBinFeatures.
GetDat(NId).Add(BinValue);
223 NumAssigned += NumToAssign;
229 const int SimilarityThreshold) {
231 for (
int i = 0; i < F1.
Len(); ++i) {
232 if (
TFlt::Abs(F1[i] - F2[i]) > SimilarityThreshold) {
241 const int NumNodes = Features.
Len();
243 TFltVV FeaturesMtx(NumNodes, NumFeatures);
245 int i =
GetMtxIdx(HI.GetKey(), NodeIdMtxIdxH);
246 for (
int j = 0; j < NumFeatures; ++j) {
247 FeaturesMtx(i, j) = HI.GetDat()[j];
257 for (
int i = 0; i < XDim; ++i) {
259 for (
int j = 0; j < YDim; ++j) {
263 printf(
"%f", Matrix(i, j)());
272 TFltVV Matrix(XDim, YDim);
273 for (
int i = 0; i < XDim; ++i) {
274 for (
int j = 0; j < YDim; ++j) {
275 Matrix(i, j) = (double)Seed / 10007;
276 Seed = (Seed * 1871) % 10007;
288 double Cost = 100, NewCost = 0;
293 TFltVV NewW(NumNodes, NumRoles);
294 TFltVV NewH(NumRoles, NumFeatures);
295 TFltVV Product(NumNodes, NumFeatures);
297 TFltVV *PW = &W, *PH = &H, *PNewW = &NewW, *PNewH = &NewH, *Tmp;
299 while (
TFlt::Abs((NewCost - Cost)/Cost) > Threshold) {
304 for (
int i = 0; i < NumNodes; i++) {
305 for (
int j = 0; j < NumFeatures; j++) {
306 NewCost += V(i, j) *
TMath::Log(Product(i, j)) - Product(i, j);
310 for (
int i = 0; i < NumNodes; i++) {
311 for (
int a = 0; a < NumRoles; a++) {
313 for (
int u = 0; u < NumFeatures; ++u) {
315 SumU += V(i, u) / Product(i, u) * PH->
At(a, u);
318 PNewW->At(i, a) = PW->
At(i, a) * SumU;
321 for (
int i = 0; i < NumRoles; i++) {
324 for (
int i = 0; i < NumNodes; i++) {
325 for (
int j = 0; j < NumRoles; j++) {
326 Sum[j] += PNewW->At(i, j);
329 for (
int i = 0; i < NumNodes; i++) {
330 for (
int j = 0; j < NumRoles; j++) {
331 PNewW->At(i, j) /= Sum[j];
335 for (
int a = 0; a < NumRoles; a++) {
336 for (
int u = 0; u < NumFeatures; u++) {
338 for (
int i = 0; i < NumNodes; ++i) {
340 SumI += PW->
At(i, a) * V(i, u) / Product(i, u);
343 PNewH->At(a, u) = PH->At(a, u) * SumI;
347 Tmp = PW; PW = PNewW; PNewW = Tmp;
348 Tmp = PH; PH = PNewH; PNewH = Tmp;
359 for (
int i = 0; i < V.
GetXDim(); ++i) {
360 for (
int j = 0; j < V.
GetYDim(); ++j) {
361 TFlt ValueV = V(i, j);
362 TFlt ValueGF = GF(i, j);
366 E += ValueV *
TMath::Log(ValueV / ValueGF) - ValueV + ValueGF;
377 H.
AddDat(HI.GetKey(), Idx);
384 return NodeIdMtxIdxH.
GetDat(NodeId)();
389 HI < NodeIdMtxIdxH.
EndI();
391 if (HI.GetDat() == MtxId) {
392 return HI.GetKey()();
400 for (
int i = 0; i < G.
GetXDim(); i++) {
403 for (
int j = 0; j < G.
GetYDim(); j ++) {
409 int NodeId =
GetNodeId(i, NodeIdMtxIdxH);
410 Roles.
AddDat(NodeId, Role);
416 TStr RoleToColor[10] = {
"white",
"black",
"red",
"green",
"blue",
417 "yellow",
"gold",
"cyan",
"magenta",
"brown" };
420 Color.
AddDat(HI.GetKey(), RoleToColor[HI.GetDat()].
CStr());
426 printf(
"--roles (node ID: role ID)--\n");
429 printf(
"(%d: %d)\n", HI.GetKey()(), HI.GetDat()());
436 Fp = fopen(Path.
CStr(),
"w");
439 for (
int i = 0; i < XDim; ++i) {
440 for (
int j = 0; j < YDim; ++j) {
444 fprintf(Fp,
"%f", Matrix(i, j)());
453 Fp = fopen(Path.
CStr(),
"w");
454 fprintf(Fp,
"--roles (node ID role ID)--\n\n");
456 fprintf(Fp,
"%d\t%d\n", HI.GetKey()(), HI.GetDat()());
void DrawGViz(const PGraph &Graph, const TGVizLayout &Layout, const TStr &PltFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Draws a given Graph using a selected GraphViz Layout engine with nodes colored.
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
TIntFtrH SummarizeConnectedComponents(const PUNGraph FeatureGraph, const TIntFtrH &Features, const TIntFtrH &NewFeatures)
Summarizes s-friend graph and return retained features.
void AddLocalFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds local features to the node-feature mapping.
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
int GetEdges() const
Returns the number of edges in the graph.
TSizeTy Len() const
Returns the number of elements in the vector.
Node iterator. Only forward iteration (operator++) is supported.
TIntIntH FindRoles(const TFltVV &G, const TIntIntH &NodeIdMtxIdxH)
Gets matrix index of the node ID.
PUNGraph GetEgonet(const PUNGraph &Graph, const int CtrNId, int &ArndEdges)
Returns the egonet of node CtrNId as center in undirected graph Graph. And returns number of edges ar...
void PlotRoles(const PUNGraph Graph, const TIntIntH &Roles)
Plots found roles on a picture (.png).
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
const TDat & GetDat(const TKey &Key) const
void CalcNonNegativeFactorization(const TFltVV &V, const int NumRoles, TFltVV &W, TFltVV &H, const double Threshold)
Performs non-negative factorization V = WH. 2nd dim of W == number of roles.
int GetMtxIdx(const TInt NodeId, const TIntIntH &NodeIdMtxIdxH)
Gets matrix index of the node ID.
TIntFtrH PruneRecursiveFeatures(const PUNGraph Graph, const TIntFtrH &Features, const TIntFtrH &NewFeatures, const int SimilarityThreshold)
Prunes recursive features.
TFltVV ConvertFeatureToMatrix(const TIntFtrH &Features, const TIntIntH &NodeIdMtxIdxH)
Converts node-feature mapping to matrix. (i, j): i-th node, j-th feature.
TIntFtrH ExtractFeatures(const PUNGraph Graph)
Performs feature extraction, the first step of RolX.
const TDat & GetDat() const
void PrintRoles(const TIntIntH &Roles)
Prints found roles on stdout.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
TFtr GetNthFeature(const TIntFtrH &Features, const int N)
Gets the n-th feature of all nodes.
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
void AddRecursiveFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds recursive features to the node-feature mapping.
TIntFtrH GenerateRecursiveFeatures(const PUNGraph Graph, const TIntFtrH &CurrFeatures)
Generates recursive features out of current features.
bool FltIsZero(const TFlt Number)
Whether the float is zero.
void AddNeighborhoodFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds neighborhood features (local + egonet) to the node-feature mapping.
int GetNodeId(const TInt MtxId, const TIntIntH &NodeIdMtxIdxH)
void AssignBinValue(const TVec< TInt > &SortedNId, const float BinFraction, TIntFtrH &LogBinFeatures)
Assigns logarithmic binning value to features.
bool IsSimilarFeature(const TFtr &F1, const TFtr &F2, const int SimilarityThreshold)
Whether the two features are similar, given similarity threshold.
TIntIntH CreateNodeIdMtxIdxHash(const TIntFtrH &Features)
Creates the mapping of .
void PrintMatrix(const TFltVV &Matrix)
Prints feature matrix to stdout.
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
static void Multiply(const TFltVV &A, const TFltV &x, TFltV &y)
static double Abs(const double &Flt)
void PrintFeatures(const TIntFtrH &Features)
Prints all nodes' feature.
void FPrintMatrix(const TFltVV &Matrix, const TStr &Path)
Prints feature matrix to file.
TFltVV CreateRandMatrix(const int XDim, const int YDim)
Creates a random matrix with specified dimension.
TFlt CalcDescriptionLength(const TFltVV &V, const TFltVV &G, const TFltVV &F)
Calculates the description length L = M + E.
void AddEgonetFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds egonet features to the node-feature mapping.
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
static double Log(const double &Val)
TVec< TFlt > TFtr
Feature of a node.
TVec< TInt > GetNIdSorted(const TIntFtrH &Features, const int Idx)
Sorts the Idx-th feature, return the list of corresponding node ID.
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
void AppendFeatures(TIntFtrH &DstFeatures, const TIntFtrH &SrcFeatures, const int ColIdx)
Appends all src features to dst features.
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 FPrintRoles(const TIntIntH &Roles, const TStr &Path)
Prints found roles to file.
TDat & AddDat(const TKey &Key)
const TVal & At(const int &X, const int &Y) const
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
PUNGraph BuildFeatureGraph(const TIntFtrH &LogBinFeatures, const int SimilarityThreshold)
Builds s-friend graph given similarity threshold.
TIntFtrH CalcVerticalLogBinning(const TIntFtrH &Features, const float BinFraction)
Calculates vertical logarithmic binning features from the given features.
void SortByDat(const bool &Asc=true)