// get the node ids in 1-connected components void Get1CnCom(const PUNGraph& Graph, TCnComV& Cn1ComV) { //TCnCom::GetWccCnt(Graph, SzCntV); IAssertR(SzCntV.Len() == 1, "Graph is not connected."); TIntPrV EdgeV; GetEdgeBridges(Graph, EdgeV); if (EdgeV.Empty()) { Cn1ComV.Clr(false); return; } PUNGraph TmpG = TUNGraph::New(); *TmpG = *Graph; for (int e = 0; e < EdgeV.Len(); e++) { TmpG->DelEdge(EdgeV[e].Val1, EdgeV[e].Val2); } TCnComV CnComV; GetWccs(TmpG, CnComV); IAssert(CnComV.Len() >= 2); const TIntV& MxWcc = CnComV[0].NIdV; TIntSet MxCcSet(MxWcc.Len()); for (int i = 0; i < MxWcc.Len(); i++) { MxCcSet.AddKey(MxWcc[i]); } // create new graph: bridges not touching MxCc of G with no bridges for (int e = 0; e < EdgeV.Len(); e++) { if (! MxCcSet.IsKey(EdgeV[e].Val1) && ! MxCcSet.IsKey(EdgeV[e].Val2)) { TmpG->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); } } GetWccs(TmpG, Cn1ComV); // remove the largest component of G for (int c = 0; c < Cn1ComV.Len(); c++) { if (MxCcSet.IsKey(Cn1ComV[c].NIdV[0])) { Cn1ComV.Del(c); break; } } }
void TSubGraphsEnum::RecurBfs1(const int& NId, const int& Depth) { if (Depth == 0) { TIntPrV EdgeV; EdgeH.GetKeyV(EdgeV); EdgeV.Sort(); SgV.Add(EdgeV); return; } const TNGraph::TNodeI NI = NGraph ->GetNI(NId); for (int e = 0; e < NI.GetOutDeg(); e++) { const TIntPr Edge(NId, NI.GetOutNId(e)); if (! EdgeH.IsKey(Edge)) { EdgeH.AddKey(Edge); RecurBfs1(NI.GetOutNId(e), Depth-1); EdgeH.DelKey(Edge); } } for (int e = 0; e < NI.GetInDeg(); e++) { const TIntPr Edge(NI.GetInNId(e), NId); if (! EdgeH.IsKey(Edge)) { EdgeH.AddKey(Edge); RecurBfs1(NI.GetInNId(e), Depth-1); EdgeH.DelKey(Edge); } } }
TFfGGen::TStopReason TUndirFFire::AddNodes(const int& GraphNodes, const bool& FloodStop) { printf("\n***Undirected GEO ForestFire: graph(%d,%d) add %d nodes, burn prob %.3f\n", Graph->GetNodes(), Graph->GetEdges(), GraphNodes, BurnProb); TExeTm ExeTm; int Burned1 = 0, Burned2 = 0, Burned3 = 0; // last 3 fire sizes TIntPrV NodesEdgesV; // create initial set of nodes if (Graph.Empty()) { Graph = PUNGraph::New(); } if (Graph->GetNodes() == 0) { Graph->AddNode(); } int NEdges = Graph->GetEdges(); // forest fire for (int NNodes = Graph->GetNodes() + 1; NNodes <= GraphNodes; NNodes++) { const int NewNId = Graph->AddNode(-1); IAssert(NewNId == Graph->GetNodes() - 1); // node ids have to be 0...N const int StartNId = Rnd.GetUniDevInt(NewNId); const int NBurned = BurnGeoFire(StartNId); // add edges to burned nodes for (int e = 0; e < NBurned; e++) { Graph->AddEdge(NewNId, GetBurnedNId(e)); } NEdges += NBurned; Burned1 = Burned2; Burned2 = Burned3; Burned3 = NBurned; if (NNodes % Kilo(1) == 0) { printf("(%d, %d) burned: [%d,%d,%d] [%s]\n", NNodes, NEdges, Burned1, Burned2, Burned3, ExeTm.GetStr()); NodesEdgesV.Add(TIntPr(NNodes, NEdges)); } if (FloodStop && NEdges>1000 && NEdges / double(NNodes)>100.0) { // average node degree is more than 50 printf("!!! FLOOD. G(%6d, %6d)\n", NNodes, NEdges); return TFfGGen::srFlood; } } printf("\n"); IAssert(Graph->GetEdges() == NEdges); return TFfGGen::srOk; }
void TSkyGridBs::GetLinkWgtDstEntIdPrVDiff( const TIntPrV& OldLinkWgtDstEntIdPrV, const TIntPrV& NewLinkWgtDstEntIdPrV, TIntPrV& NegDiffLinkWgtDstEntIdPrV, TIntPrV& PosDiffLinkWgtDstEntIdPrV){ TIntIntH DstEntIdToLinkWgtH; // set previous-vector for (int WordN=0; WordN<NewLinkWgtDstEntIdPrV.Len(); WordN++){ int LinkWgt=NewLinkWgtDstEntIdPrV[WordN].Val1; int DstEntId=NewLinkWgtDstEntIdPrV[WordN].Val2; DstEntIdToLinkWgtH.AddDat(DstEntId, LinkWgt); } // diff current-vector for (int WordN=0; WordN<OldLinkWgtDstEntIdPrV.Len(); WordN++){ int LinkWgt=OldLinkWgtDstEntIdPrV[WordN].Val1; int DstEntId=OldLinkWgtDstEntIdPrV[WordN].Val2; int CurLinkWgt=DstEntIdToLinkWgtH.AddDat(DstEntId); DstEntIdToLinkWgtH.AddDat(DstEntId, CurLinkWgt-LinkWgt); } // extract vector TIntPrV _DiffLinkWgtDstEntIdPrV; DstEntIdToLinkWgtH.GetDatKeyPrV(_DiffLinkWgtDstEntIdPrV); // clean zeros TIntPrV DiffLinkWgtDstEntIdPrV(_DiffLinkWgtDstEntIdPrV.Len(), 0); for (int EntN=0; EntN<_DiffLinkWgtDstEntIdPrV.Len(); EntN++){ int LinkWgt=_DiffLinkWgtDstEntIdPrV[EntN].Val1; if (LinkWgt!=0){ DiffLinkWgtDstEntIdPrV.Add(_DiffLinkWgtDstEntIdPrV[EntN]);} } // positive-vector DiffLinkWgtDstEntIdPrV.Sort(true); NegDiffLinkWgtDstEntIdPrV=DiffLinkWgtDstEntIdPrV; // negative-vector DiffLinkWgtDstEntIdPrV.Sort(false); PosDiffLinkWgtDstEntIdPrV=DiffLinkWgtDstEntIdPrV; }
void TNmObjBs::GetFqNmObjIdPrV( const TStr& TargetNmObjStr, TIntPrV& FqNmObjIdPrV) const { //printf("Searching %s ...", TargetNmObjStr.CStr()); // get target named-object-id int TargetNmObjId=GetNmObjId(TargetNmObjStr); // collect target named-object frequencies TIntIntH NmObjIdToFqH; // traverse target named-object documents int NmObjDocs=GetNmObjDocs(TargetNmObjId); for (int NmObjDocIdN=0; NmObjDocIdN<NmObjDocs; NmObjDocIdN++){ // get document-id int DocId=GetNmObjDocId(TargetNmObjId, NmObjDocIdN); // traverse named-object in document int DocNmObjs=GetDocNmObjs(DocId); for (int DocNmObjN=0; DocNmObjN<DocNmObjs; DocNmObjN++){ // get named-object & frequency int NmObjId; int TermFq; GetDocNmObjId(DocId, DocNmObjN, NmObjId, TermFq); // increment named-object document frequency NmObjIdToFqH.AddDat(NmObjId)++; } } // get & sort frequency table FqNmObjIdPrV.Clr(); NmObjIdToFqH.GetDatKeyPrV(FqNmObjIdPrV); FqNmObjIdPrV.Sort(false); }
int TGnuPlot::AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { TFltKdV XYFltValV(XYValV.Len(), 0); for (int i = 0; i < XYValV.Len(); i++) { XYFltValV.Add(TFltKd(TFlt(XYValV[i].Val1), TFlt(XYValV[i].Val2))); } return AddPlot(XYFltValV, SeriesTy, Label, Style); }
bool TRf24Radio::Set(const uint16& NodeId, const TIntPrV& ValIdValPrV) { Notify->OnNotifyFmt(TNotifyType::ntInfo, "Calling multiple SET for node %d ...", NodeId); bool Success = true; const int NMsgs = ceil(double(ValIdValPrV.Len()) / double(VALS_PER_PAYLOAD)); int RemainN = ValIdValPrV.Len(); for (int MsgN = 0; MsgN < NMsgs; MsgN++) { const int PayloadSize = TMath::Mn(RemainN, VALS_PER_PAYLOAD); TVec<TRadioValue> ValV(PayloadSize); for (int ValN = 0; ValN < PayloadSize; ValN++) { const TIntPr& ValIdValPr = ValIdValPrV[MsgN*VALS_PER_PAYLOAD + ValN]; TRadioValue& RadioVal = ValV[ValN]; RadioVal.SetValId((char) ValIdValPr.Val1); RadioVal.SetVal(ValIdValPr.Val2); } TMem Payload; TRadioProtocol::GenSetPayload(ValV, Payload); Success &= Send(NodeId, REQUEST_SET, Payload); RemainN -= VALS_PER_PAYLOAD; } return Success; }
void TakeStat(const PGraph& InfG, const PGraph& NetG, const TIntH& NIdInfTmH, const double& P, const bool& DivByM=true) { const double M = DivByM ? InfG->GetNodes() : 1; IAssert(M>=1); PGraph CcInf, CcNet; // largest connected component // connected components and sizes { TCnComV CnComV; TSnap::GetWccs(InfG, CnComV); NCascInf.AddDat(P).Add(CnComV.Len()/M); MxSzInf.AddDat(P).Add(CnComV[0].Len()/M); { int a=0; for (int i=0; i<CnComV.Len(); i++) { a+=CnComV[i].Len(); } AvgSzInf.AddDat(P).Add(a/double(CnComV.Len()*M)); } CcInf = TSnap::GetSubGraph(InfG, CnComV[0].NIdV); TSnap::GetWccs(NetG, CnComV); NCascNet.AddDat(P).Add(CnComV.Len()/M); MxSzNet.AddDat(P).Add(CnComV[0].Len()/M); { int a=0; for (int i=0; i<CnComV.Len(); i++) { a+=CnComV[i].Len(); } AvgSzNet.AddDat(P).Add(a/double(CnComV.Len()*M)); } CcNet = TSnap::GetSubGraph(NetG, CnComV[0].NIdV); } // count isolated nodes and leaves; average in- and out-degree (skip leaves) { int i1=0, i2=0,l1=0,l2=0,r1=0,r2=0,ENet=0,EInf=0; double ci1=0,ci2=0,co1=0,co2=0; for (typename PGraph::TObj::TNodeI NI = InfG->BegNI(); NI < InfG->EndNI(); NI++) { if (NI.GetOutDeg()==0 && NI.GetInDeg()>0) { l1++; } if (NI.GetOutDeg()>0 && NI.GetInDeg()==0) { r1++; } if (NI.GetDeg()==0) { i1++; } if (NI.GetInDeg()>0) { ci1+=1; } if (NI.GetOutDeg()>0) { co1+=1; } EInf+=NI.GetOutDeg(); } for (typename PGraph::TObj::TNodeI NI = NetG->BegNI(); NI < NetG->EndNI(); NI++) { if (NI.GetOutDeg()==0 && NI.GetInDeg()>0) { l2++; } if (NI.GetOutDeg()>0 && NI.GetInDeg()==0) { r2++; } if (NI.GetDeg()==0) { i2++; } if (NI.GetInDeg()>0) { ci2+=1; } if (NI.GetOutDeg()>0) { co2+=1; } ENet+=NI.GetOutDeg(); } if(ci1>0)InDegInf.AddDat(P).Add(EInf/ci1); if(ci2>0)InDegNet.AddDat(P).Add(ENet/ci2); if(co1>0)OutDegInf.AddDat(P).Add(EInf/co1); if(co2>0)OutDegNet.AddDat(P).Add(ENet/co2); NLfInf.AddDat(P).Add(l1/M); NLfNet.AddDat(P).Add(l2/M); NRtInf.AddDat(P).Add(r1/M); NRtNet.AddDat(P).Add(r2/M); NIsoInf.AddDat(P).Add(i1/M); NIsoNet.AddDat(P).Add(i2/M); } // cascade depth { const double M1 = DivByM ? CcNet->GetNodes() : 1; IAssert(M1>=1); int Root=FindCascadeRoot(CcInf, NIdInfTmH); TIntPrV HopCntV; TSnap::GetNodesAtHops(CcInf, Root, HopCntV, true); int MxN=0, Lev=0, IncL=0; for (int i = 0; i < HopCntV.Len(); i++) { if (MxN<HopCntV[i].Val2) { MxN=HopCntV[i].Val2; Lev=HopCntV[i].Val1; } if (i > 0 && HopCntV[i-1].Val2<=HopCntV[i].Val2) { IncL++; } } double D=0; int c=0; TIntH DistH; D = HopCntV.Last().Val1; c=1; // maximum depth if (c!=0 && D!=0) { D = D/c; DepthInf.AddDat(P).Add(D/M1); MxWidInf.AddDat(P).Add(MxN/M1); MxLevInf.AddDat(P).Add(Lev/D); IncLevInf.AddDat(P).Add(IncL/D); } Root=FindCascadeRoot(CcNet, NIdInfTmH); TSnap::GetNodesAtHops(CcNet, Root, HopCntV, true); MxN=0; Lev=0; IncL=0; D=0; c=0; for (int i = 0; i < HopCntV.Len(); i++) { if (MxN<HopCntV[i].Val2) { MxN=HopCntV[i].Val2; Lev=HopCntV[i].Val1; } if (i > 0 && HopCntV[i-1].Val2<=HopCntV[i].Val2) { IncL++; } } D = HopCntV.Last().Val1; c=1; // maximum depth if (c!=0 && D!=0) { D = D/c; DepthNet.AddDat(P).Add(D/M1); MxWidNet.AddDat(P).Add(MxN/M1); MxLevNet.AddDat(P).Add(Lev/D); IncLevNet.AddDat(P).Add(IncL/D); } } }
///////////////////////////////////////////////// // SkyGrid-Base void TSkyGridBs::GetSorted_DocsEntIdPrV(TIntPrV& DocsEntIdPrV){ TIntIntH EntIdToDocsH; for (int EntId=0; EntId<GetEnts(); EntId++){ int Docs=GetEnt(EntId).GetDocIds(); EntIdToDocsH.AddDat(EntId, Docs); } DocsEntIdPrV.Clr(); EntIdToDocsH.GetDatKeyPrV(DocsEntIdPrV); DocsEntIdPrV.Sort(false); }
double GetAvgDepthFromRoot(const PGraph& G) { TMom Mom; TIntPrV HopCntV; for (typename PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { if (NI.GetOutDeg()>0 && NI.GetInDeg()==0) { TSnap::GetNodesAtHops(G, NI.GetId(), HopCntV, true); Mom.Add(HopCntV.Last().Val1()); } } Mom.Def(); return Mom.GetMean(); }
// 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 TCycBs::_SaveTaxonomyTxt(FILE* fOut, const int& Lev, TIntPrV& RelIdVIdPrV, TIntIntH& VIdToLevH){ for (int VidN=0; VidN<RelIdVIdPrV.Len(); VidN++){ int FromRelId=RelIdVIdPrV[VidN].Val1; int SrcVId=RelIdVIdPrV[VidN].Val2; TStr SrcVNm=GetVNm(SrcVId); TCycVrtx& SrcVrtx=GetVrtx(SrcVId); if (!SrcVrtx.IsFlag(cvfHumanOk)){continue;} TStr FlagStr=SrcVrtx.GetFlagStr(); if (FromRelId==-1){ if (Lev>0){fprintf(fOut, "===upper");} else {fprintf(fOut, "===lower");} fprintf(fOut, "=======================================================\n"); fprintf(fOut, "%s - %s\n", SrcVNm.CStr(), FlagStr.CStr()); } else { TStr FromRelNm=GetVNm(FromRelId); fprintf(fOut, "%*c[%s] --> %s\n", (Lev-1)*5, ' ', FromRelNm.CStr(), SrcVNm.CStr()); } TIntPrV UpRelIdVIdPrV; for (int EdgeN=0; EdgeN<SrcVrtx.GetEdges(); EdgeN++){ TCycEdge& Edge=SrcVrtx.GetEdge(EdgeN); int RelId=Edge.GetRelId(); int DstVId=Edge.GetDstVId(); TStr RelNm=GetVNm(RelId); TStr DstVNm=GetVNm(DstVId); if (Lev>0){ // upper taxonomy if ((RelNm=="#$isa")||(RelNm=="#$genls")){ if (!VIdToLevH.IsKey(DstVId)){ VIdToLevH.AddDat(DstVId, Lev+1); UpRelIdVIdPrV.Add(TIntPr(RelId, DstVId)); } } } else { // lower taxonomy if ((RelNm=="~#$isa")||(RelNm=="~#$genls")){ if (!VIdToLevH.IsKey(DstVId)){ VIdToLevH.AddDat(DstVId, Lev-1); UpRelIdVIdPrV.Add(TIntPr(RelId, DstVId)); } } } } // recursive call if (Lev>0){ _SaveTaxonomyTxt(fOut, Lev+1, UpRelIdVIdPrV, VIdToLevH); } else { _SaveTaxonomyTxt(fOut, Lev-1, UpRelIdVIdPrV, VIdToLevH); } } }
TIntPrV TTrawling::PlotMinFqVsMaxSet(const TStr& OutFNm) { InitItemSets(); TIntPrV SzCntH; SzCntH.Add(TIntPr(1, CurItemH.Len())); for (int ItemSetSz = 2; ItemSetSz < 100; ItemSetSz++) { printf("\nItemset size %d: ", ItemSetSz); GetNextFqItemSets(); if (CurItemH.Empty()) { break; } SzCntH.Add(TIntPr(ItemSetSz, CurItemH.Len())); TGnuPlot::PlotValV(SzCntH, "itemSet-"+OutFNm, TStr::Fmt("Minimum Suport = %d", MinSup), "Itemset size", "Number of itemsets > Minimum Support"); } printf("\n\n"); return SzCntH; }
// 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)); } } }
// network cascade: add spurious edges // for more details see "Correcting for Missing Data in Information Cascades" by E. Sadikov, M. Medina, J. Leskovec, H. Garcia-Molina. WSDM, 2011 PNGraph AddSpuriousEdges(const PUNGraph& Graph, const PNGraph& Casc, TIntH NIdTmH) { TIntPrV EdgeV; for (TNGraph::TNodeI NI = Casc->BegNI(); NI < Casc->EndNI(); NI++) { TUNGraph::TNodeI GNI = Graph->GetNI(NI.GetId()); const int Tm = NIdTmH.GetDat(NI.GetId()); for (int i=0,j=0; i < GNI.GetOutDeg(); i++) { const int Dst = GNI.GetOutNId(i); if (NIdTmH.IsKey(Dst) && Tm<NIdTmH.GetDat(Dst) && ! NI.IsNbhNId(Dst)) { EdgeV.Add(TIntPr(GNI.GetId(), Dst)); } } } PNGraph NetCasc = TNGraph::New(); *NetCasc = *Casc; for (int e = 0; e < EdgeV.Len(); e++) { NetCasc->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2); } return NetCasc; }
void TCycBs::SaveTaxonomyTxt(const TStr& FNm){ TFOut FOut(FNm); FILE* fOut=FOut.GetFileId(); for (int VId=0; VId<GetVIds(); VId++){ printf("%d/%d (%.1f%%)\r", 1+VId, GetVIds(), 100.0*(1+VId)/GetVIds()); //if (VId>10){break;} // upper taxonomy {int Lev=0; TIntIntH VIdToLevH; VIdToLevH.AddDat(VId, Lev); TIntPrV UpRelIdVIdPrV; UpRelIdVIdPrV.Add(TIntPr(-1, VId)); _SaveTaxonomyTxt(fOut, Lev+1, UpRelIdVIdPrV, VIdToLevH);} // lower taxonomy {int Lev=0; TIntIntH VIdToLevH; VIdToLevH.AddDat(VId, Lev); TIntPrV UpRelIdVIdPrV; UpRelIdVIdPrV.Add(TIntPr(-1, VId)); _SaveTaxonomyTxt(fOut, Lev-1, UpRelIdVIdPrV, VIdToLevH);} } printf("\n"); }
void TGraphKey::TakeGraph(const PNGraph& Graph, TIntPrV& NodeMap) { TIntSet NodeIdH; int n = 0; NodeMap.Gen(Graph->GetNodes(), 0); for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, n++) { NodeIdH.AddKey(NI.GetId()); NodeMap.Add(TIntPr(NI.GetId(), n)); } Nodes = Graph->GetNodes(); EdgeV.Gen(Nodes, 0); for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { const int NewNId = NodeIdH.GetKeyId(NI.GetId()); for (int i = 0; i < NI.GetOutDeg(); i++) { EdgeV.Add(TIntPr(NewNId, NodeIdH.GetKeyId(NI.GetOutNId(i)))); } } EdgeV.Sort(true); EdgeV.Pack(); }
void GetBiConSzCnt(const PUNGraph& Graph, TIntPrV& SzCntV) { TCnComV BiCnComV; GetBiCon(Graph, BiCnComV); TIntH SzCntH; for (int c =0; c < BiCnComV.Len(); c++) { SzCntH.AddDat(BiCnComV[c].Len()) += 1; } SzCntH.GetKeyDatPrV(SzCntV); SzCntV.Sort(); }
void TNGramBs::GetNGramStrV( const TStr& HtmlStr, TStrV& NGramStrV, TIntPrV& NGramBEChXPrV) const { TIntV NGramIdV; NGramStrV.Clr(); NGramBEChXPrV.Clr(); TNGramBs::GetNGramIdV(HtmlStr, NGramIdV, NGramBEChXPrV); NGramStrV.Gen(NGramIdV.Len(), 0); for (int NGramIdN=0; NGramIdN<NGramIdV.Len(); NGramIdN++){ TStr NGramStr=GetNGramStr(NGramIdV[NGramIdN]); NGramStrV.Add(NGramStr); } }
// Test GetTriadParticip // Number of nodes with x number of triangles it participates in TEST(triad, TestGetTriadParticip) { // Test TUNGraph PUNGraph GraphTUN = TriadGetTestTUNGraph(); TIntPrV TriadCntV; TSnap::GetTriadParticip(GraphTUN, TriadCntV); VerifyGetTriadParticip(TriadCntV); // Test TNGraph which is same as undirected. PNGraph GraphTN = TriadGetTestTNGraph(); TriadCntV.Clr(); TSnap::GetTriadParticip(GraphTN, TriadCntV); VerifyGetTriadParticip(TriadCntV); // Test TNEGraph which is same as undirected. PNEGraph GraphTNE = TriadGetTestTNEGraph(); TriadCntV.Clr(); TSnap::GetTriadParticip(GraphTN, TriadCntV); VerifyGetTriadParticip(TriadCntV); }
void TSkyGridBs::GetLinkWgtDstEntIdPrVChA( const TIntPrV& LinkWgtDstEntIdPrV, TChA& LinkWgtDstEntIdPrVChA){ LinkWgtDstEntIdPrVChA.Clr(); for (int DstEntN=0; DstEntN<LinkWgtDstEntIdPrV.Len(); DstEntN++){ int DstEntId=LinkWgtDstEntIdPrV[DstEntN].Val2; TStr DstEntNm=GetEntNm(DstEntId); int LinkWgt=LinkWgtDstEntIdPrV[DstEntN].Val1; if (DstEntN>0){LinkWgtDstEntIdPrVChA+=' ';} LinkWgtDstEntIdPrVChA+=TStr::Fmt("['%s':%d]", DstEntNm.CStr(), LinkWgt); } }
int GetMaxMinDeg(const PNGraph& G, const TStr& IsDir, const TStr& IsIn, const TStr& IsMax){ TIntPrV DegCnt; if (IsDir == "false"){ PUNGraph U = TSnap::ConvertGraph<PUNGraph>(G); if (IsIn == "true") TSnap::GetInDegCnt(U, DegCnt); else TSnap::GetOutDegCnt(U, DegCnt); } else{ if (IsIn == "true") TSnap::GetInDegCnt(G, DegCnt); else TSnap::GetOutDegCnt(G, DegCnt); } // sort in descending order DegCnt.Sort(false); if (IsMax == "true") return DegCnt[0].Val1; else return DegCnt[DegCnt.Len()-1].Val1; }
// Helper: Verify the distribution of how many triangles a node participates in void VerifyGetTriadParticip(TIntPrV& TriadCntV) { for (TIntPr *Vec = TriadCntV.BegI(); Vec < TriadCntV.EndI(); Vec++) { switch (Vec->Val1) { case 0: EXPECT_EQ(1, Vec->Val2); break; case 1: EXPECT_EQ(2, Vec->Val2); break; case 2: EXPECT_EQ(2, Vec->Val2); break; case 3: EXPECT_EQ(1, Vec->Val2); break; default: EXPECT_FALSE(true); // Not suppose to have more than 3 break; } } }
void TSkyGridEnt::GetSorted_LinkWgtDstEntIdPrV( const uint64& MnTm, const double& TopWgtSumPrc, TIntPrV& LinkWgtDstEntIdPrV) const { double AllLinkWgtSum=0; TIntIntH DstEntIdLinkWgtH; int LinkEnts=GetLinkEnts(); for (int LinkEntN=0; LinkEntN<LinkEnts; LinkEntN++){ int DstEntId=GetLinkEntId(LinkEntN); int EntLinks=GetEntLinks(LinkEntN); int EntLinkWgtSum=0; for (int EntLinkN=0; EntLinkN<EntLinks; EntLinkN++){ const TSkyGridEntLinkCtx& EntLinkCtx=GetEntLinkCtx(LinkEntN, EntLinkN); if (EntLinkCtx.Tm>=MnTm){ EntLinkWgtSum+=EntLinkCtx.LinkWgt;} } DstEntIdLinkWgtH.AddDat(DstEntId, EntLinkWgtSum); AllLinkWgtSum+=EntLinkWgtSum; } LinkWgtDstEntIdPrV.Clr(); DstEntIdLinkWgtH.GetDatKeyPrV(LinkWgtDstEntIdPrV); LinkWgtDstEntIdPrV.Sort(false); // cut long-tail if ((TopWgtSumPrc>0.0)&&(LinkWgtDstEntIdPrV.Len()>0)){ int TopLinkWgt=LinkWgtDstEntIdPrV[0].Val1; if (TopLinkWgt>(3*AllLinkWgtSum)/LinkWgtDstEntIdPrV.Len()){ double CutWgtSum=AllLinkWgtSum*(1-TopWgtSumPrc); int LastValN=LinkWgtDstEntIdPrV.Len()-1; while ((LastValN>0)&&(CutWgtSum>0)){ CutWgtSum-=LinkWgtDstEntIdPrV[LastValN].Val1; LastValN--; } LinkWgtDstEntIdPrV.Trunc(LastValN+1); } } }
void __fastcall TContexterF::CtxNmObjLbClick(TObject *Sender){ TListBox* NmObjLb=CtxNmObjLb; // get named-object string if ((NmObjLb->ItemIndex<0)||(NmObjLb->Items->Count<=NmObjLb->ItemIndex)){ return;} TStr LbItemStr=NmObjLb->Items->Strings[NmObjLb->ItemIndex].c_str(); TStr FqStr; LbItemStr.SplitOnCh(State->EnRootNmObjStr, ' ', FqStr); // get concept-vector PBowSpV ConceptSpV=State->NmObjBs->GetNmObjConcept( State->BowDocBs, State->BowDocWgtBs, State->EnRootNmObjStr); TStrFltPrV WordStrWgtPrV; ConceptSpV->GetWordStrWgtPrV(State->BowDocBs, 100, 0.66, WordStrWgtPrV); // fill concept-list-box EnConceptWordLb->Clear(); for (int WordN=0; WordN<WordStrWgtPrV.Len(); WordN++){ TStr LbItemStr=WordStrWgtPrV[WordN].Val1+ TFlt::GetStr(WordStrWgtPrV[WordN].Val2, " (%.3f)"); EnConceptWordLb->Items->Add(LbItemStr.CStr()); } // get coref-named-objects TIntPrV FqNmObjIdPrV; State->NmObjBs->GetFqNmObjIdPrV(State->EnRootNmObjStr, FqNmObjIdPrV); FqNmObjIdPrV.Sort(false); FqNmObjIdPrV.Trunc(100); // fill coref-named-objects EnCoNmObjLb->Clear(); for (int NmObjN=0; NmObjN<FqNmObjIdPrV.Len(); NmObjN++){ TStr CoNmObjStr=State->NmObjBs->GetNmObjStr(FqNmObjIdPrV[NmObjN].Val2); if (State->EnRootNmObjStr!=CoNmObjStr){ TStr LbItemStr=CoNmObjStr+TInt::GetStr(FqNmObjIdPrV[NmObjN].Val1, " (%d)"); EnCoNmObjLb->Items->Add(LbItemStr.CStr()); } } // context-tree State->EnCtxTree=GetCtxTreeGraph(State->NmObjBs, State->EnRootNmObjStr, State->EnDrawLevels-1); EnPbPaint(Sender); }
// 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); }
// get degrees from current and add it to degrees void AddDegreeStat(const TFltPrV& current, TFltPrV& degrees, TIntPrV& samples){ for (int j = 0; j < current.Len(); j++){ const TFltPr& elem = current[j]; const double& deg = elem.Val1.Val, &nodesCount = elem.Val2.Val; bool wasFound = false; // silly search for (int k = 0; k < degrees.Len(); k++){ if (degrees[k].Val1.Val == deg){ degrees[k].Val2.Val += nodesCount; samples[k].Val2.Val++; wasFound = true; break; } } if (!wasFound){ TFlt d(deg), n(nodesCount); TFltPr val(d,n); degrees.Add(val); TInt di(static_cast<int>(deg)); TIntPr valI(di, 1); samples.Add(valI); } } }
void TGUtil::GetPdf(const TIntPrV& CdfV, TIntPrV& PdfV) { PdfV = CdfV; for (int i = PdfV.Len()-1; i > 0; i--) { PdfV[i].Val2 = PdfV[i].Val2 - PdfV[i-1].Val2; } }
void TGUtil::GetCCdf(const TIntPrV& PdfV, TIntPrV& CCdfV) { CCdfV = PdfV; for (int i = CCdfV.Len()-2; i >= 0; i--) { CCdfV[i].Val2 = CCdfV[i+1].Val2 + CCdfV[i].Val2; } }
///////////////////////////////////////////////// // Graph Utilities void TGUtil::GetCdf(const TIntPrV& PdfV, TIntPrV& CdfV) { CdfV = PdfV; for (int i = 1; i < CdfV.Len(); i++) { CdfV[i].Val2 = CdfV[i-1].Val2 + CdfV[i].Val2; } }