6 namespace TSnapDetail {
17 Cmty1.
Clr(
false); Cmty2.
Clr(
false);
21 if (BtwEH.
Empty()) {
return; }
24 Graph->DelEdge(NId1, NId2);
26 if (BFS.
GetHops(NId1, NId2) == -1) {
39 for (
int c = 0; c < CnComV.
Len(); c++) {
40 const TIntV& NIdV = CnComV[c]();
42 for (
int i = 0; i < NIdV.
Len(); i++) {
45 EEIn += OutDegH.
GetDat(NIdV[i]);
47 Mod += (EIn-EEIn*EEIn/(2.0*OrigEdges));
49 if (Mod == 0) {
return 0; }
50 else {
return Mod/(2.0*OrigEdges); }
56 float InModule=0.0, OutModule=0.0, Val;
58 for (
int i=0; i<2; i++) {
59 InModule=0.0, OutModule=0.0;
60 if (Qi1.
IsKey(Mds[i])){
61 int CentralModule = Mds[i];
63 if (Module.
GetDat(EI.GetSrcNId()) == Module.
GetDat(EI.GetDstNId()) && Module.
GetDat(EI.GetDstNId()) == CentralModule) {
65 }
else if ((Module.
GetDat(EI.GetSrcNId()) == CentralModule && Module.
GetDat(EI.GetDstNId()) != CentralModule) || (Module.
GetDat(EI.GetSrcNId()) != CentralModule && Module.
GetDat(EI.GetDstNId()) == CentralModule)) {
70 if (InModule+OutModule > 0) {
71 Val = OutModule/(InModule+OutModule);
85 float SumPAlpha = 1.0, SumQi = 0.0, SumQiLogQi=0.0, SumQiSumPAlphaLogQiSumPAlpha = 0.0;
86 for (
int i=0; i<Qi.
Len(); i++) {
88 SumQiLogQi += Qi[i]*log(Qi[i]);
89 SumQiSumPAlphaLogQiSumPAlpha += (Qi[i]+SumPAlpha)*log(Qi[i]+SumPAlpha);
91 return (SumQi*log(SumQi)-2*SumQiLogQi-SumPAlphaLogPAlpha+SumQiSumPAlphaLogQiSumPAlpha);
100 const int NEdges = Graph->GetEdges();
102 OutDegH.
AddDat(NI.GetId(), NI.GetOutDeg());
114 CmtyV.
Swap(CurCmtyV);
116 if (Cmty1.
Len()==0 || Cmty2.
Len() == 0) {
break; }
128 float SumPAlphaLogPAlpha = 0.0;
130 const int e = Graph->GetEdges();
134 DegH.
AddDat(NI.GetId(), NI.GetDeg());
135 float d = ((float)NI.GetDeg()/(float)(2*e));
136 PAlpha.
AddDat(NI.GetId(), d);
137 SumPAlphaLogPAlpha += d*log(d);
138 Module.
AddDat(NI.GetId(),Br);
139 Qi.
AddDat(Module[Br],1.0);
144 float NewCodeLength, PrevIterationCodeLength = 0.0;
145 int OldModule, NewModule;
148 PrevIterationCodeLength = MinCodeLength;
151 for (
int i=0; i<DegH.
GetDat(NI.GetId()); i++) {
152 OldModule = Module.
GetDat(NI.GetId());
153 NewModule = Module.
GetDat(NI.GetNbrNId(i));
154 if (OldModule!=NewModule){
155 Module.
DelKey(NI.GetId());
156 Module.
AddDat(NI.GetId(),NewModule);
159 if (NewCodeLength<MinCodeLength) {
160 MinCodeLength = NewCodeLength;
161 OldModule = NewModule;
163 Module.
DelKey(NI.GetId());
164 Module.
AddDat(NI.GetId(),OldModule);
169 }
while (MinCodeLength<PrevIterationCodeLength);
173 for (
int i=0; i<Module.
Len(); i++) {
178 if (Module.
GetDat(NI.GetId())==Mod)
185 return MinCodeLength;
188 namespace TSnapDetail {
199 TCmtyDat(
const double& NodeDegFrac,
const int& OutDeg) :
220 const double M = 0.5/Graph->
GetEdges();
224 const int OutDeg = NI.GetOutDeg();
225 if (OutDeg == 0) {
continue; }
227 for (
int e = 0; e < NI.GetOutDeg(); e++) {
228 const int DstNId = NI.GetOutNId(e);
229 const double DstMod = 2 * M * (1.0 - OutDeg * Graph->
GetNI(DstNId).
GetOutDeg() * M);
230 Dat.
AddQ(DstNId, DstMod);
240 if (
MxQHeap.Empty()) {
break; }
250 if (TopQ.
Val1 <= 0.0) {
return false; }
252 const int I = TopQ.
Val3;
253 const int J = TopQ.
Val2;
262 double NewQ = DatJ.
NIdQH[i];
295 for (
int j = 0; j < IdCmtyH.
Len(); j++) {
296 CmtyV[j].NIdV.
Swap(IdCmtyH[j]);
double CommunityGirvanNewman(PUNGraph &Graph, TCnComV &CmtyV)
static const T & Mn(const T &LVal, const T &RVal)
Edge iterator. Only forward iteration (operator++) is supported.
int Add(const int &Key)
Adds an element Key to the structure.
void Add(const int &NodeId)
int GetHops(const int &SrcNId, const int &DstNId) const
void Union(const int &Key1, const int &Key2)
Merges sets with elements Key1 and Key2.
TFltIntIntTr FindMxQEdge()
static const T & Mx(const T &LVal, const T &RVal)
void GetNodeWcc(const PGraph &Graph, const int &NId, TIntV &CnCom)
Returns (via output parameter CnCom) all nodes that are in the same connected component as node NId...
TCNMQMatrix(const PUNGraph &Graph)
int DoBfs(const int &StartNode, const bool &FollowOut, const bool &FollowIn, const int &TargetNId=-1, const int &MxDist=TInt::Mx)
Performs BFS from node id StartNode for at maps MxDist steps by only following in-links (parameter Fo...
int GetEdges() const
Returns the number of edges in the graph.
TSizeTy Len() const
Returns the number of elements in the vector.
void GetBetweennessCentr(const PUNGraph &Graph, const TIntV &BtwNIdV, TIntFltH &NodeBtwH, const bool &DoNodeCent, TIntPrFltH &EdgeBtwH, const bool &DoEdgeCent)
Node iterator. Only forward iteration (operator++) is supported.
const TDat & GetDat(const TKey &Key) const
static double Sqr(const double &x)
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
void DelKey(const TKey &Key)
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.
int Find(const int &Key)
Returns the set that contains element Key.
Simple heap data structure.
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
bool FNextKeyId(int &KeyId) const
THeap< TFltIntIntTr > MxQHeap
void DelLink(const int &K)
int GetKeyId(const TKey &Key) const
float Equation(PUNGraph &Graph, TIntFltH &PAlpha, float &SumPAlphaLogPAlpha, TIntFltH &Qi)
void AddQ(const int &NId, const double &Q)
double CommunityCNM(const PUNGraph &Graph, TCnComV &CmtyV)
void Init(const PUNGraph &Graph)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
bool IsKey(const TKey &Key) const
TCmtyDat(const double &NodeDegFrac, const int &OutDeg)
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.
double Infomap(PUNGraph &Graph, TCnComV &CmtyV)
TDat & AddDat(const TKey &Key)
static double CmtyCMN(const PUNGraph &Graph, TCnComV &CmtyV)
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Union Find class (Disjoint-set data structure).
const TKey & GetKey(const int &KeyId) const
TTriple< TFlt, TInt, TInt > TFltIntIntTr
double _GirvanNewmanGetModularity(const PUNGraph &G, const TIntH &OutDegH, const int &OrigEdges, TCnComV &CnComV)
THash< TInt, TCmtyDat > CmtyQH
void CmtyGirvanNewmanStep(PUNGraph &Graph, TIntV &Cmty1, TIntV &Cmty2)
A single step of Girvan-Newman clustering procedure.
TIntFltH MapEquationNew2Modules(PUNGraph &Graph, TIntH &Module, TIntFltH &Qi, int a, int b)
void SortByDat(const bool &Asc=true)