// IN-OUT edges are swapped (so that the prog runs faster) // Send message via IN edge proportional to the OUT edge weight void TWgtNet::ReinforceEdges(const int& NIters) { THash<TInt, TFlt> OutWgtSumH; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { double wgt = 0; for (int e = 0; e < NI.GetOutDeg(); e++) { wgt += NI.GetOutEDat(e); } OutWgtSumH.AddDat(NI.GetId(), wgt); } printf("Reinforcing edges for %d iterations\n", NIters); // iterate TExeTm ExeTm; for (int iter = 0; iter < NIters; iter++) { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { const double X = TInt::Rnd.GetUniDev() * OutWgtSumH.GetDat(NI.GetId()); double x = 0; int e = 0; for ( ; x + NI.GetOutEDat(e) < X; e++) { x += NI.GetOutEDat(e); } IAssert(IsEdge(NI.GetOutNId(e), NI.GetId())); GetEDat(NI.GetOutNId(e), NI.GetId()) += 1; // reinforce the edge OutWgtSumH.GetDat(NI.GetOutNId(e)) += 1; } if (iter % (NIters/100) == 0) { printf("\r%d [%s]", iter, ExeTm.GetStr()); } } printf(" done.\n"); }
// set Language and Country for movies that do not have the value set // for every movie find the mojority language/country in 1-hop neighborhood and set it void TImdbNet::SetLangCntryByMajority() { // set language while (true) { TIntPrV NIdToVal; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().GetLang() != 0) { continue; } int Nbhs=0; TIntH LangCntH; for (int e = 0; e < NI.GetOutDeg(); e++) { LangCntH.AddDat(NI.GetOutNDat(e).GetLang()) += 1; Nbhs++; } for (int e = 0; e < NI.GetInDeg(); e++) { LangCntH.AddDat(NI.GetInNDat(e).GetLang()) += 1; Nbhs++; } if (LangCntH.IsKey(0)) { Nbhs-=LangCntH.GetDat(0); LangCntH.GetDat(0)=0; } LangCntH.SortByDat(false); if (LangCntH.GetKey(0) == 0) { continue; } if (LangCntH[0]*2 >= Nbhs) { NIdToVal.Add(TIntPr(NI.GetId(), LangCntH.GetKey(0))); } } if (NIdToVal.Empty()) { break; } // done for (int i = 0; i < NIdToVal.Len(); i++) { GetNDat(NIdToVal[i].Val1).Lang = NIdToVal[i].Val2; } printf(" language set: %d\n", NIdToVal.Len()); } int cnt=0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().GetLang()==0) cnt++; } printf(" NO language: %d\n\n", cnt); // set country while (true) { TIntPrV NIdToVal; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().GetCntry() != 0) { continue; } int Nbhs=0; TIntH CntryCntH; for (int e = 0; e < NI.GetOutDeg(); e++) { CntryCntH.AddDat(NI.GetOutNDat(e).GetCntry()) += 1; Nbhs++; } for (int e = 0; e < NI.GetInDeg(); e++) { CntryCntH.AddDat(NI.GetInNDat(e).GetCntry()) += 1; Nbhs++; } if (CntryCntH.IsKey(0)) { Nbhs-=CntryCntH.GetDat(0); CntryCntH.GetDat(0)=0; } CntryCntH.SortByDat(false); if (CntryCntH.GetKey(0) == 0) { continue; } if (CntryCntH[0]*2 >= Nbhs) { NIdToVal.Add(TIntPr(NI.GetId(), CntryCntH.GetKey(0))); } } if (NIdToVal.Empty()) { break; } // done for (int i = 0; i < NIdToVal.Len(); i++) { GetNDat(NIdToVal[i].Val1).Cntry = NIdToVal[i].Val2; } printf(" country set: %d\n", NIdToVal.Len()); } cnt=0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().GetCntry()==0) cnt++; } printf(" NO country: %d\n\n", cnt); }
void TWgtNet::PermEdgeWgt() { TFltV WgtV; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { WgtV.Add(NI.GetOutEDat(e)); } } WgtV.Shuffle(TInt::Rnd); int w = 0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NI.GetOutEDat(e) = WgtV[w++]; } } }
// 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; }
void TWgtNet::PutRnd01Wgts() { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NI.GetOutEDat(e) = TInt::Rnd.GetUniDev(); } } }
void TWgtNet::MulEdgeWgt(const double& MulBy) { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NI.GetOutEDat(e) *= MulBy; } } }
// Return an iterator referring to edge (SrcNId, DstNId) in the graph. TUNGraph::TEdgeI TUNGraph::GetEI(const int& SrcNId, const int& DstNId) const { const int MnNId = TMath::Mn(SrcNId, DstNId); const int MxNId = TMath::Mx(SrcNId, DstNId); const TNodeI SrcNI = GetNI(MnNId); const int NodeN = SrcNI.NodeHI.GetDat().NIdV.SearchBin(MxNId); IAssert(NodeN != -1); return TEdgeI(SrcNI, EndNI(), NodeN); }
void TNEANetMP::Dump(FILE *OutF) const { const int NodePlaces = (int) ceil(log10((double) GetNodes())); const int EdgePlaces = (int) ceil(log10((double) GetEdges())); fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Network: nodes: %d, edges: %d\n", GetNodes(), GetEdges()); for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) { fprintf(OutF, " %*d]\n", NodePlaces, NodeI.GetId()); // load node attributes TIntV IntAttrN; IntAttrValueNI(NodeI.GetId(), IntAttrN); fprintf(OutF, " nai[%d]", IntAttrN.Len()); for (int i = 0; i < IntAttrN.Len(); i++) { fprintf(OutF, " %*i", NodePlaces, IntAttrN[i]()); } TStrV StrAttrN; StrAttrValueNI(NodeI.GetId(), StrAttrN); fprintf(OutF, " nas[%d]", StrAttrN.Len()); for (int i = 0; i < StrAttrN.Len(); i++) { fprintf(OutF, " %*s", NodePlaces, StrAttrN[i]()); } TFltV FltAttrN; FltAttrValueNI(NodeI.GetId(), FltAttrN); fprintf(OutF, " naf[%d]", FltAttrN.Len()); for (int i = 0; i < FltAttrN.Len(); i++) { fprintf(OutF, " %*f", NodePlaces, FltAttrN[i]()); } fprintf(OutF, " in[%d]", NodeI.GetInDeg()); for (int edge = 0; edge < NodeI.GetInDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); } fprintf(OutF, "\n"); fprintf(OutF, " out[%d]", NodeI.GetOutDeg()); for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); } fprintf(OutF, "\n"); } for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) { fprintf(OutF, " %*d] %*d -> %*d", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId()); // load edge attributes TIntV IntAttrE; IntAttrValueEI(EdgeI.GetId(), IntAttrE); fprintf(OutF, " eai[%d]", IntAttrE.Len()); for (int i = 0; i < IntAttrE.Len(); i++) { fprintf(OutF, " %*i", EdgePlaces, IntAttrE[i]()); } TStrV StrAttrE; StrAttrValueEI(EdgeI.GetId(), StrAttrE); fprintf(OutF, " eas[%d]", StrAttrE.Len()); for (int i = 0; i < StrAttrE.Len(); i++) { fprintf(OutF, " %*s", EdgePlaces, StrAttrE[i]()); } TFltV FltAttrE; FltAttrValueEI(EdgeI.GetId(), FltAttrE); fprintf(OutF, " eaf[%d]", FltAttrE.Len()); for (int i = 0; i < FltAttrE.Len(); i++) { fprintf(OutF, " %*f", EdgePlaces, FltAttrE[i]()); } fprintf(OutF, "\n"); } fprintf(OutF, "\n"); }
// set actor's language and country void TImdbNet::SetActorCntryLangByMajority() { // set language TIntPrV NIdToVal; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (! NI().IsActor()) { continue; } IAssert(NI().GetLang() == 0); // no language set IAssert(NI.GetInDeg() == 0); // actors point to movies int Nbhs=0; TIntH LangCntH; for (int e = 0; e < NI.GetOutDeg(); e++) { LangCntH.AddDat(NI.GetOutNDat(e).GetLang()) += 1; Nbhs++; } if (LangCntH.IsKey(0)) { Nbhs-=LangCntH.GetDat(0); LangCntH.GetDat(0)=0; } LangCntH.SortByDat(false); if (LangCntH.GetKey(0) == 0) { continue; } if (LangCntH[0]*2 >= Nbhs) { NIdToVal.Add(TIntPr(NI.GetId(), LangCntH.GetKey(0))); } } for (int i = 0; i < NIdToVal.Len(); i++) { GetNDat(NIdToVal[i].Val1).Lang = NIdToVal[i].Val2; } printf(" language set: %d\n", NIdToVal.Len()); int cnt=0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().IsActor() && NI().GetLang()==0) cnt++; } printf(" Actors NO language: %d\n\n", cnt); // set country NIdToVal.Clr(true); for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (! NI().IsActor()) { continue; } IAssert(NI().GetCntry() == 0); // no country set IAssert(NI.GetInDeg() == 0); // actors point to movies int Nbhs=0; TIntH CntryCntH; for (int e = 0; e < NI.GetOutDeg(); e++) { CntryCntH.AddDat(NI.GetOutNDat(e).GetCntry()) += 1; Nbhs++; } if (CntryCntH.IsKey(0)) { Nbhs-=CntryCntH.GetDat(0); CntryCntH.GetDat(0)=0; } CntryCntH.SortByDat(false); if (CntryCntH.GetKey(0) == 0) { continue; } if (CntryCntH[0]*2 >= Nbhs) { NIdToVal.Add(TIntPr(NI.GetId(), CntryCntH.GetKey(0))); } } for (int i = 0; i < NIdToVal.Len(); i++) { GetNDat(NIdToVal[i].Val1).Cntry = NIdToVal[i].Val2; } printf(" country set: %d\n", NIdToVal.Len()); cnt=0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { if (NI().IsActor() && NI().GetCntry()==0) cnt++; } printf(" Actors NO country: %d\n\n", cnt); }
///////////////////////////////////////////////// // Weighted network double TWgtNet::GetEdgeWgt() const { double wgt = 0; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { wgt += NI.GetOutEDat(e); } } return wgt; }
PMultimodalGraphImplC TMultimodalGraphImplB::ConvertToImplC() const { PMultimodalGraphImplC G = TMultimodalGraphImplC::New(); for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { G->AddNode(TPair<TInt,TInt>(NI.GetModeId(), NI.GetId())); } for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { int OutDeg = NI.GetOutDeg(); int InDeg = NI.GetInDeg(); G->ReserveInAndOutNIdV(TPair<TInt,TInt>(NI.GetModeId(), NI.GetId()), OutDeg, InDeg); TVec<TInt> AdjacentModes = TVec<TInt>(); NI.GetAdjacentModes(AdjacentModes); for (int i = 0; i < AdjacentModes.Len(); i++) { int AdjacentMode = AdjacentModes.GetVal(i); TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(NI.GetModeId(), AdjacentMode); G->AddOutNIds(TPair<TInt,TInt>(NI.GetModeId(),NI.GetId()), Graphs.GetDat(ModeIdsKey).GetOutNId(NI.GetId()), AdjacentMode); G->AddInNIds(TPair<TInt,TInt>(NI.GetModeId(),NI.GetId()), Graphs.GetDat(ModeIdsKey).GetInNId(NI.GetId()), AdjacentMode); } } return G; }
TBPGraph::TEdgeI TBPGraph::GetEI(const int& LeftNId, const int& RightNId) const { const bool IsLL = IsLNode(LeftNId), IsLR = IsRNode(LeftNId); const bool IsRL = IsLNode(RightNId), IsRR = IsRNode(RightNId); IAssertR((IsLL||IsLR)&&(IsRL||IsRR), TStr::Fmt("%d or %d is not a node.", LeftNId, RightNId).CStr()); IAssertR(LeftNId!=RightNId, "No self-edges are allowed."); IAssertR((IsLL&&!IsLR&&!IsRL&&IsRR)||(!IsLL&&IsLR&&IsRL&&!IsRR), "One node should be on the 'left' and the other on the 'right'."); const int LNId = IsLL ? LeftNId : RightNId; // the real left node const int RNId = IsLL ? RightNId : LeftNId; // the real right node const TNodeI SrcNI = GetNI(LNId); const int NodeN = SrcNI.LeftHI.GetDat().NIdV.SearchBin(RNId); IAssertR(NodeN != -1, "Right edge endpoint does not exists!"); return TEdgeI(SrcNI, EndNI(), NodeN); }
void TWgtNet::PermOutEdgeWgt() { TFltV WgtV; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { WgtV.Clr(false); for (int e = 0; e < NI.GetOutDeg(); e++) { WgtV.Add(NI.GetOutEDat(e)); } WgtV.Shuffle(TInt::Rnd); for (int e = 0; e < NI.GetOutDeg(); e++) { NI.GetOutEDat(e) = WgtV[e]; } } }
void TWgtNet::DelMinWgtNodes(const double MinWgt) { printf("Deleting Min Wgt %g nodes\n", MinWgt); printf(" (%d,%d) -->", GetNodes(), GetEdges()); TIntV DelNIdV; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { double wgt = 0; for (int e = 0; e < NI.GetOutDeg(); e++) { wgt += NI.GetOutEDat(e); } if (wgt < MinWgt) { DelNIdV.Add(NI.GetId()); } } for (int d = 0; d < DelNIdV.Len(); d++) { DelNode(DelNIdV[d]); } printf(" (%d,%d)\n", GetNodes(), GetEdges()); }
// Wgt == -1 : take the weight of the edge in the opposite direction void TWgtNet::AddBiDirEdges(const double& Wgt) { TIntPrV EdgeV; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { if (! IsEdge(NI.GetOutNId(e), NI.GetId())) { EdgeV.Add(TIntPr(NI.GetOutNId(e), NI.GetId())); } } } for (int e = 0; e < EdgeV.Len(); e++) { if (Wgt != -1) { AddEdge(EdgeV[e].Val1, EdgeV[e].Val2, Wgt); } else { // edge weight in the opposite direction AddEdge(EdgeV[e].Val1, EdgeV[e].Val2, GetEDat(EdgeV[e].Val2, EdgeV[e].Val1)); } } }
TMultimodalGraphImplB::TEdgeI TMultimodalGraphImplB::GetEI(const TPair<TInt,TInt>& SrcNId, const TPair<TInt,TInt>& DstNId) const { TNodeI CurNode = TNodeI(NodeToModeMapping.GetI(SrcNId.GetVal2()), &Graphs, &NodeToModeMapping); TIntV AdjacentModes = TIntV(); CurNode.GetAdjacentModes(AdjacentModes); TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(SrcNId.GetVal1(), DstNId.GetVal1()); int CurAdjacentMode = 0; for (int i=0; i<AdjacentModes.Len();i++) { if (AdjacentModes.GetDat(i) == DstNId.GetVal1()) { CurAdjacentMode=i; break; } } return TEdgeI(CurNode, EndNI(), Graphs.GetDat(ModeIdsKey).GetEI(SrcNId.GetVal2(), DstNId.GetVal2()).GetCurEdge(), AdjacentModes, CurAdjacentMode); }
void TNEGraph::Dump(FILE *OutF) const { const int NodePlaces = (int) ceil(log10((double) GetNodes())); const int EdgePlaces = (int) ceil(log10((double) GetEdges())); fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges()); for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) { fprintf(OutF, " %*d]\n", NodePlaces, NodeI.GetId()); fprintf(OutF, " in[%d]", NodeI.GetInDeg()); for (int edge = 0; edge < NodeI.GetInDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); } fprintf(OutF, "\n out[%d]", NodeI.GetOutDeg()); for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) { fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); } fprintf(OutF, "\n"); } for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) { fprintf(OutF, " %*d] %*d -> %*d\n", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId()); } fprintf(OutF, "\n"); }
TNGraph::TEdgeI TNGraph::GetEI(const int& SrcNId, const int& DstNId) const { const TNodeI SrcNI = GetNI(SrcNId); const int NodeN = SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId); IAssert(NodeN != -1); return TEdgeI(SrcNI, EndNI(), NodeN); }