// actors collaboration graph PUNGraph TImdbNet::GetActorGraph() const { TIntPrSet EdgeSet; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().GetTy() == mtyActor) { const int NId1 = NI.GetId(); for (int e = 0; e < NI.GetOutDeg(); e++) { if (NI.GetOutNDat(e).GetTy() != mtyActor) { TNodeI NI2 = GetNI(NI.GetOutNId(e)); for (int e2 = 0; e2 < NI2.GetInDeg(); e2++) { if (NI2.GetInNDat(e2).GetTy() == mtyActor) { const int NId2 = NI2.GetInNId(e2); EdgeSet.AddKey(TIntPr(TMath::Mn(NId1, NId2), TMath::Mx(NId1, NId2))); } } } } } } PUNGraph G = TUNGraph::New(); for (int i = 0; i < EdgeSet.Len(); i++) { const int NId1 = EdgeSet[i].Val1; const int NId2 = EdgeSet[i].Val2; if (! G->IsNode(NId1)) { G->AddNode(NId1); } if (! G->IsNode(NId2)) { G->AddNode(NId2); } G->AddEdge(NId1, NId2); } return G; }
// bridges are edges in the size 2 biconnected components void GetEdgeBridges(const PUNGraph& Graph, TIntPrV& EdgeV) { TCnComV BiCnComV; GetBiCon(Graph, BiCnComV); TIntPrSet EdgeSet; for (int c = 0; c < BiCnComV.Len(); c++) { const TIntV& NIdV = BiCnComV[c].NIdV; if (NIdV.Len() == 2) { EdgeSet.AddKey(TIntPr(TMath::Mn(NIdV[0], NIdV[1]), TMath::Mx(NIdV[0], NIdV[1]))); } } EdgeSet.GetKeyV(EdgeV); }
///Generate bipartite community affiliation from given power law coefficients for membership distribution and community size distribution. void TAGMUtil::ConnectCmtyVV(TVec<TIntV>& CmtyVV, const TIntPrV& CIDSzPrV, const TIntPrV& NIDMemPrV, TRnd& Rnd) { const int Nodes = NIDMemPrV.Len(), Coms = CIDSzPrV.Len(); TIntV NDegV,CDegV; TIntPrSet CNIDSet; TIntSet HitNodes(Nodes); THash<TInt,TIntV> CmtyVH; for (int i = 0; i < CIDSzPrV.Len(); i++) { for (int j = 0; j < CIDSzPrV[i].Val2; j++) { CDegV.Add(CIDSzPrV[i].Val1); } } for (int i = 0; i < NIDMemPrV.Len(); i++) { for (int j = 0; j < NIDMemPrV[i].Val2; j++) { NDegV.Add(NIDMemPrV[i].Val1); } } while (CDegV.Len() < (int) (1.2 * Nodes)) { CDegV.Add(CIDSzPrV[Rnd.GetUniDevInt(Coms)].Val1); } while (NDegV.Len() < CDegV.Len()) { NDegV.Add(NIDMemPrV[Rnd.GetUniDevInt(Nodes)].Val1); } printf("Total Mem: %d, Total Sz: %d\n",NDegV.Len(), CDegV.Len()); int c=0; while (c++ < 15 && CDegV.Len() > 1) { for (int i = 0; i < CDegV.Len(); i++) { int u = Rnd.GetUniDevInt(CDegV.Len()); int v = Rnd.GetUniDevInt(NDegV.Len()); if (CNIDSet.IsKey(TIntPr(CDegV[u], NDegV[v]))) { continue; } CNIDSet.AddKey(TIntPr(CDegV[u], NDegV[v])); HitNodes.AddKey(NDegV[v]); if (u == CDegV.Len() - 1) { CDegV.DelLast(); } else { CDegV[u] = CDegV.Last(); CDegV.DelLast(); } if (v == NDegV.Len() - 1) { NDegV.DelLast(); } else { NDegV[v] = NDegV.Last(); NDegV.DelLast(); } } } //make sure that every node belongs to at least one community for (int i = 0; i < Nodes; i++) { int NID = NIDMemPrV[i].Val1; if (! HitNodes.IsKey(NID)) { CNIDSet.AddKey(TIntPr(CIDSzPrV[Rnd.GetUniDevInt(Coms)].Val1, NID)); HitNodes.AddKey(NID); } } IAssert(HitNodes.Len() == Nodes); for (int i = 0; i < CNIDSet.Len(); i++) { TIntPr CNIDPr = CNIDSet[i]; CmtyVH.AddDat(CNIDPr.Val1); CmtyVH.GetDat(CNIDPr.Val1).Add(CNIDPr.Val2); } CmtyVH.GetDatV(CmtyVV); }