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; }
// 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 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); } } }
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); } } }
// 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); }
// 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); }
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); } }
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); } } }
// 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; }
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; }
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); }
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; } }
void TNetInfBs::GenNoisyCascade(TCascade& C, const int& TModel, const double &window, TIntPrIntH& EdgesUsed, const double& std_waiting_time, const double& std_beta, const double& PercRndNodes, const double& PercRndRemoval) { TIntPrIntH EdgesUsedC; // list of used edges for a single cascade GenCascade(C, TModel, window, EdgesUsedC, delta, std_waiting_time, std_beta); // store keys TIntV KeyV; C.NIdHitH.GetKeyV(KeyV); // store first and last time double tbeg = TFlt::Mx, tend = TFlt::Mn; for (int i=0; i < KeyV.Len(); i++) { if (tbeg > C.NIdHitH.GetDat(KeyV[i]).Tm) tbeg = C.NIdHitH.GetDat(KeyV[i]).Tm; if (tend < C.NIdHitH.GetDat(KeyV[i]).Tm) tend = C.NIdHitH.GetDat(KeyV[i]).Tm; } // remove PercRndRemoval% of the nodes of the cascades if (PercRndRemoval > 0) { for (int i=KeyV.Len()-1; i >= 0; i--) { if (TFlt::Rnd.GetUniDev() < PercRndRemoval) { // remove from the EdgesUsedC the ones affected by the removal TIntPrV EdgesToRemove; for (TIntPrIntH::TIter EI = EdgesUsedC.BegI(); EI < EdgesUsedC.EndI(); EI++) { if ( (KeyV[i]==EI.GetKey().Val1 && C.IsNode(EI.GetKey().Val2) && C.GetTm(KeyV[i]) < C.GetTm(EI.GetKey().Val2)) || (KeyV[i]==EI.GetKey().Val2 && C.IsNode(EI.GetKey().Val1) && C.GetTm(KeyV[i]) > C.GetTm(EI.GetKey().Val1)) ) { EI.GetDat() = EI.GetDat()-1; if (EI.GetDat()==0) EdgesToRemove.Add(EI.GetKey()); } } for (int er=0; er<EdgesToRemove.Len(); er++) EdgesUsedC.DelKey(EdgesToRemove[er]); C.Del(KeyV[i]); } } // defrag the hash table, otherwise other functions can crash C.NIdHitH.Defrag(); } // Substitute PercRndNodes% of the nodes for a random node at a random time if (PercRndNodes > 0) { for (int i=KeyV.Len()-1; i >= 0; i--) { if (TFlt::Rnd.GetUniDev() < PercRndNodes) { // remove from the EdgesUsedC the ones affected by the change TIntPrV EdgesToRemove; for (TIntPrIntH::TIter EI = EdgesUsedC.BegI(); EI < EdgesUsedC.EndI(); EI++) { if ( (KeyV[i]==EI.GetKey().Val1 && C.IsNode(EI.GetKey().Val2) && C.GetTm(KeyV[i]) < C.GetTm(EI.GetKey().Val2)) || (KeyV[i]==EI.GetKey().Val2 && C.IsNode(EI.GetKey().Val1) && C.GetTm(KeyV[i]) > C.GetTm(EI.GetKey().Val1)) ) { EI.GetDat() = EI.GetDat()-1; if (EI.GetDat()==0) EdgesToRemove.Add(EI.GetKey()); } } for (int er=0; er<EdgesToRemove.Len(); er++) EdgesUsedC.DelKey(EdgesToRemove[er]); printf("Old node n:%d t:%f --", KeyV[i].Val, C.GetTm(KeyV[i])); C.Del(KeyV[i]); // not repeating a label double tnew = 0; int keynew = -1; do { tnew = tbeg + TFlt::Rnd.GetUniDev()*(tend-tbeg); keynew = Graph->GetRndNId(); } while (KeyV.IsIn(keynew)); printf("New node n:%d t:%f\n", keynew, tnew); C.Add(keynew, tnew); KeyV.Add(keynew); } } } // add to the aggregate list (EdgesUsed) EdgesUsedC.Defrag(); for (int i=0; i<EdgesUsedC.Len(); i++) { if (!EdgesUsed.IsKey(EdgesUsedC.GetKey(i))) EdgesUsed.AddDat(EdgesUsedC.GetKey(i)) = 0; EdgesUsed.GetDat(EdgesUsedC.GetKey(i)) += 1; } }
///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); }
int main(int argc, char* argv[]) { string edge=""; const char *edge1 =""; char * s1; char * s2; int from,to,a[2],j=0,RndFullDiam,SmallWorlFullDiam,PrefAttachFullDiam,i,t, FullDiam; int64 triad; double RndclusteringCoeff,PrefAttachclusteringCoeff,SmallWorldclusteringCoeff,clusteringCoeff; double numStrongComp,RndEffDiam, RndAvgPL, SmallWorldEffDiam, SmallWorldAvgPL, PrefAttachEffDiam, PrefAttachAvgPL,EffDiam,AvgPL; PNGraph Graph; PUNGraph RandomGraph, PrefAttach, SmallWorld; TIntPrV edges; TIntFltH PRank; TIntPrV Bridges; TFltV EigV; ofstream P1_measures,P2_measures,P3_measures,bridges; P1_measures.open("P1_measures.txt"); P2_measures.open("P2_measures.txt"); P3_measures.open("P3_measures.txt"); bridges.open("Bridges.txt"); cout<<"\n loading SNAP Graph"; //Graph = TSnap::LoadEdgeList<PNGraph>("SnapGraph.txt",0,1); Graph = TNGraph::New(); t = Graph->Empty(); for (i = 1; i < 78697; i++) { cout<<"adding Node"<<i<<"\n"; Graph->AddNode(i); } //Reading the Annonymized List and creating the SNAP graph ifstream gwfile; gwfile.open("annonymized_edge_List.csv"); if(!gwfile) { cout << "FAILED: file could not be opened" << endl << "Press enter to close."; cin.get(); return 0; }else cout << "SUCCESSFULLY opened file!\n"; cout << "-------------------------\n\n\n"; while(getline(gwfile, edge,'\n')){ edge1=edge.c_str(); s1 = strtok ((char *)edge1,","); s2 = strtok (NULL,","); from = atoi(s1); to = atoi(s2); Graph->AddEdge(from,to); cout<<"Adding Edge"<<from<<"->"<<to<<endl; } TSnap::SaveEdgeList(Graph, "SnapGraph.txt", "File saved as Tab separated"); cout<<"\n graph loaded"; PUNGraph UG = TSnap::ConvertGraph<PUNGraph>(Graph); // P1 Measure // Bridges GetEdgeBridges(UG, Bridges); bridges << "\nNumber of Bridges : " << Bridges.Len() <<endl; for(int i = 0; i<Bridges.Len(); i++) { bridges << Bridges[i].Val1 << "->" << Bridges[i].Val2 <<endl; } // Triads triad = TSnap::GetTriads(Graph); P1_measures << "\nNumber of triads : " << triad <<endl; // P2 Measure // Global Clustering Coefficient clusteringCoeff= TSnap::GetClustCf(Graph); P2_measures << "\nGlobal Clustering Coefficient : " << clusteringCoeff<<"\n\n"; // Top 3 PageRank Values GetPageRank(Graph,PRank); P2_measures << "\nTop 3 Page Rank : " << "\n1st : "<< PRank[0].Val << "\n2nd : "<< PRank[1].Val << "\n3rd : "<< PRank[2].Val <<endl; // Top 3 Eigenvector centrality Values TSnap::GetEigVec(UG, EigV); P2_measures << "\nTop 3 EigenVector Centralities: "<< "\n1st : "<< EigV[0].Val << "\n2nd : "<< EigV[1].Val << "\n3rd : "<< EigV[2].Val <<endl; // P3 Models and Measures With 5000 Nodes and Average Degree 4 as that of Twitter Graph // Creating Random graph RandomGraph = TSnap::GenRndDegK(5000,4); PrintGStats("Random Graph" , TSnap::ConvertGraph<PNGraph>(RandomGraph)); cout<<"\n Random graph created \n"; TSnap::SaveEdgeList(RandomGraph, "RandomGraph.tsv", "File saved as Tab separated"); TSnap::GetBfsEffDiam(RandomGraph, 5000, true, RndEffDiam, RndFullDiam, RndAvgPL); RndclusteringCoeff= TSnap::GetClustCf(RandomGraph); P3_measures << "\n\n\nRandom Graph with 5000 nodes and Average Degree 4: "; P3_measures << "\nAverage Path Length : "<< RndAvgPL; P3_measures << "\nClustering Coefficient : "<< RndclusteringCoeff; // Creating Small World Model Graph SmallWorld = TSnap::GenSmallWorld(5000,4,0.05); PrintGStats("Small World Graph" , TSnap::ConvertGraph<PNGraph>(SmallWorld)); TSnap::SaveEdgeList(SmallWorld, "SmallWorld.tsv", "File saved as Tab separated"); cout<<"\n SmallWorld graph created \n"; TSnap::GetBfsEffDiam(SmallWorld, 5000, true, SmallWorldEffDiam, SmallWorlFullDiam, SmallWorldAvgPL); SmallWorldclusteringCoeff= TSnap::GetClustCf(SmallWorld); P3_measures << "\n\n\nSmall World Graph with 5000 nodes and Average Degree 4 and Beta .05: "; P3_measures << "\nAverage Path Length : "<< SmallWorldAvgPL; P3_measures << "\nClustering Coefficient : "<< SmallWorldclusteringCoeff; // Creating Prefrential Attachment Model Graph PrefAttach = TSnap::GenPrefAttach(5000,4); PrintGStats("Prefrential Graph" , TSnap::ConvertGraph<PNGraph>(PrefAttach)); TSnap::SaveEdgeList(PrefAttach, "PrefrentialGraph.tsv", "File saved as Tab separated"); cout<<"\n PrefAttach graph created \n"; TSnap::GetBfsEffDiam(PrefAttach, 5000, true, PrefAttachEffDiam, PrefAttachFullDiam, PrefAttachAvgPL); PrefAttachclusteringCoeff= TSnap::GetClustCf(PrefAttach); cout<<"\n PrefAttach graph created"; P3_measures << "\n\n\nPrefrential Graph with 5000 nodes and Average Degree 4 : "; P3_measures << "\nAverage Path Length : "<< PrefAttachAvgPL; P3_measures << "\nClustering Coefficient : "<< PrefAttachclusteringCoeff;*/ // Diameter and Average Path Length TSnap::GetBfsEffDiam(Graph, 78696, true, EffDiam, FullDiam,AvgPL); P1_measures << "\nDiameter : " << FullDiam<<endl; P1_measures << "\nAverage Path Length : " << AvgPL<<endl; P1_measures.close(); P2_measures.close(); P3_measures.close(); bridges.close(); return 0; }
// Demos BFS functions on undirected graph that is not fully connected void DemoBFSUndirectedRandom() { PUNGraph G; TStr FName = TStr::Fmt("%s/sample_bfsdfs_unpower.txt", DIRNAME); const int NNodes = 50; G = GenRndPowerLaw(NNodes, 2.5); // Can save/here // SaveEdgeList(G, FName); // G = LoadEdgeList<PUNGraph>(FName); TIntStrH NodeLabelH; for (int i = 0; i < G->GetNodes(); i++) { NodeLabelH.AddDat(i, TStr::Fmt("%d", i)); } DrawGViz(G, gvlNeato, TStr::Fmt("%s/sample_bfsdfs_unpower.png", DIRNAME), "Sample bfsdfs Graph", NodeLabelH); TIntV NIdV; int StartNId, Hop, Nodes; int IsDir = 0; printf("IsDir = %d:\n", IsDir); StartNId = 1; Hop = 1; Nodes = GetNodesAtHop(G, StartNId, Hop, NIdV, IsDir); printf("StartNId = %d, Nodes = %d, GetNodesAtHop NIdV.Len() = %d, NIdV[0] = %d\n", StartNId, Nodes, NIdV.Len(), NIdV[0].Val); TIntPrV HopCntV; Nodes = GetNodesAtHops(G, StartNId, HopCntV, IsDir); printf("StartNId = %d, Nodes = %d, GetNodesAtHops HopCntV.Len() = %d\n", StartNId , Nodes, HopCntV.Len()); // for (int N = 0; N < HopCntV.Len(); N++) { // printf("HopCntV[%d] = (%d, %d)\n", N, HopCntV[N].Val1.Val, HopCntV[N].Val2.Val); // } int Length, SrcNId, DstNId; SrcNId = 1; DstNId = G->GetNodes() - 1; Length = GetShortPath(G, SrcNId, DstNId, IsDir); printf("%d -> %d: SPL Length = %d\n", SrcNId, DstNId, Length); SrcNId = 1; DstNId = 33; Length = GetShortPath(G, SrcNId, DstNId, IsDir); printf("%d -> %d: SPL Length = %d\n", SrcNId, DstNId, Length); TIntH NIdToDistH; int MaxDist = 9; Length = GetShortPath(G, SrcNId, NIdToDistH, IsDir, MaxDist); // for (int i = 0; i < min(5,NIdToDistH.Len()); i++) { // printf("NIdToDistH[%d] = %d\n", i, NIdToDistH[i].Val); // } TInt::Rnd.PutSeed(0); int FullDiam; double EffDiam, AvgSPL; int NTestNodes = G->GetNodes() / 3 * 2; FullDiam = GetBfsFullDiam(G, NTestNodes, IsDir); printf("FullDiam = %d\n", FullDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir); printf("EffDiam = %.3f\n", EffDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir, EffDiam, FullDiam); printf("EffDiam = %.3f, FullDiam = %d\n", EffDiam, FullDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir, EffDiam, FullDiam, AvgSPL); printf("EffDiam = %.3f, FullDiam = %d, AvgDiam = %.3f\n", EffDiam, FullDiam, AvgSPL); TIntV SubGraphNIdV; SubGraphNIdV.Add(0); SubGraphNIdV.Add(4); SubGraphNIdV.Add(31); SubGraphNIdV.Add(45); SubGraphNIdV.Add(18); SubGraphNIdV.Add(11); SubGraphNIdV.Add(11); SubGraphNIdV.Add(48); SubGraphNIdV.Add(34); SubGraphNIdV.Add(30); EffDiam = GetBfsEffDiam(G, NTestNodes, SubGraphNIdV, IsDir, EffDiam, FullDiam); printf("For subgraph: EffDiam = %.4f, FullDiam = %d\n", EffDiam, FullDiam); }
///////////////////////////////////////////////// // Context-Tree PGraph GetCtxTreeGraph( const PNmObjBs& NmObjBs, const TStr& RootNmObjStr, const int& MxDist){ // create distance graph PGraph Graph=TGraph::New(); // create root note int RootNmObjId=NmObjBs->GetNmObjId(RootNmObjStr); PVrtx RootVrtx=TGVrtx::New(RootNmObjId, RootNmObjStr); Graph->AddVrtx(RootVrtx); // create distance vector TIntV NmObjDistV(NmObjBs->GetNmObjs()); NmObjDistV.PutAll(-1); NmObjDistV[RootNmObjId]=0; // create queue TIntPrQ OpenNmObjIdDistPrQ; OpenNmObjIdDistPrQ.Push(TIntPr(RootNmObjId, 0)); while (!OpenNmObjIdDistPrQ.Empty()){ // get name-object-id from queue int NmObjId=OpenNmObjIdDistPrQ.Top().Val1; int NmObjDist=OpenNmObjIdDistPrQ.Top().Val2; OpenNmObjIdDistPrQ.Pop(); IAssert(NmObjDistV[NmObjId]==NmObjDist); // get named-object string TStr NmObjStr=NmObjBs->GetNmObjStr(NmObjId); TStr UcNmObjStr=NmObjStr.GetUc(); printf("[%s:%d] ", NmObjStr.CStr(), NmObjDist); // check distance if (NmObjDist>MxDist){continue;} // get named-object vertex PVrtx SrcVrtx=Graph->GetVrtx(NmObjId); // get named-object children TIntPrV FqNmObjIdPrV; NmObjBs->GetFqNmObjIdPrV(NmObjStr, FqNmObjIdPrV); int SubNmObjs=FqNmObjIdPrV.Len(); // traverse named-object children int CreatedSubNmObjs=0; for (int SubNmObjN=0; SubNmObjN<SubNmObjs; SubNmObjN++){ // get child data int SubNmObjFq=FqNmObjIdPrV[SubNmObjN].Val1; int SubNmObjId=FqNmObjIdPrV[SubNmObjN].Val2; TStr SubNmObjStr=NmObjBs->GetNmObjStr(SubNmObjId); TStr UcSubNmObjStr=SubNmObjStr.GetUc(); TStr SubNmObjVNm=SubNmObjStr; // calculate and add context string formed from coref-named-objects {TChA CtxChA; TIntPrV FqNmObjIdPrV; ContexterF->State->NmObjBs->GetFqNmObjIdPrV(SubNmObjStr, FqNmObjIdPrV); FqNmObjIdPrV.Sort(false); FqNmObjIdPrV.Trunc(ContexterF->State->EnCtxLen); FqNmObjIdPrV.Clr(); for (int NmObjN=0; NmObjN<FqNmObjIdPrV.Len(); NmObjN++){ TStr CoNmObjStr=ContexterF->State->NmObjBs->GetNmObjStr(FqNmObjIdPrV[NmObjN].Val2); if (SubNmObjStr!=CoNmObjStr){ CtxChA+='['; CtxChA+=CoNmObjStr; CtxChA+=']'; CtxChA+='\\';} } if (!CtxChA.Empty()){ SubNmObjVNm=SubNmObjStr+"\\"+CtxChA;}} // push child named-object-id if necessary if (NmObjDistV[SubNmObjId]==-1){ // check number of subnodes int MxCreatedSubNmObjs=0; switch (NmObjDist){ case 0: MxCreatedSubNmObjs=/*20;*/ContexterF->State->EnSubNodes; break; case 1: MxCreatedSubNmObjs=4; break; case 2: MxCreatedSubNmObjs=2; break; case 3: MxCreatedSubNmObjs=1; break; case 4: MxCreatedSubNmObjs=1; break; default: MxCreatedSubNmObjs=0; break; } // check if stop creating branches CreatedSubNmObjs++; if (CreatedSubNmObjs>MxCreatedSubNmObjs){break;} // push edge OpenNmObjIdDistPrQ.Push(TIntPr(SubNmObjId, NmObjDist+1)); NmObjDistV[SubNmObjId]=NmObjDist+1; // create vertex TStr VNm=SubNmObjVNm; PVrtx DstVrtx=TGVrtx::New(SubNmObjId, VNm); Graph->AddVrtx(DstVrtx); // create edge //TStr ENm=TStr("_")+TInt::GetStr(NmObjId)+"-"+TInt::GetStr(SubNmObjId); TStr ENm=TInt::GetStr(SubNmObjFq); // calculate and add context string formed from coref-named-objects if (ContexterF->EnInterNmObjContextCb->Checked){ TChA CtxChA; TStr SrcNmObjStr=NmObjStr; TChA DstNmObjChA=DstVrtx->GetVNm(); if (DstNmObjChA.IsChIn('\\')){ DstNmObjChA.Trunc(DstNmObjChA.SearchCh('\\'));} TStr DstNmObjStr=DstNmObjChA; PBowSpV ConceptSpV=ContexterF->State->NmObjBs->GetNmObjConcept( ContexterF->State->BowDocBs, ContexterF->State->BowDocWgtBs, SrcNmObjStr, DstNmObjStr); TStrFltPrV WordStrWgtPrV; ConceptSpV->GetWordStrWgtPrV( ContexterF->State->BowDocBs, -1, 1, WordStrWgtPrV); TStrV UcWordStrSfV; for (int WordN=0; WordN<WordStrWgtPrV.Len(); WordN++){ // get word TStr UcWordStr=WordStrWgtPrV[WordN].Val1; // remove duplicates if (UcWordStr.IsStrIn(UcNmObjStr)){continue;} if (UcWordStr.IsStrIn(UcSubNmObjStr)){continue;} if (UcNmObjStr.IsStrIn(UcWordStr)){continue;} if (UcSubNmObjStr.IsStrIn(UcWordStr)){continue;} bool Ok=true; for (int WordSfN=0; WordSfN<UcWordStrSfV.Len(); WordSfN++){ if (UcWordStrSfV[WordSfN].IsStrIn(UcWordStr)){Ok=false; break;} if (UcWordStr.IsStrIn(UcWordStrSfV[WordSfN])){Ok=false; break;} } if (!Ok){continue;} // add word UcWordStrSfV.Add(UcWordStr); CtxChA+='['; CtxChA+=UcWordStr; CtxChA+=']'; CtxChA+='\n'; // finish if limit reached if (UcWordStrSfV.Len()>=ContexterF->State->EnCtxLen){break;} } ENm=ENm+"\n"+CtxChA; } // create and add edge to the graph PEdge Edge=TGEdge::New(SrcVrtx, DstVrtx, ENm); Edge->PutWgt(1+log(SubNmObjFq)); Graph->AddEdge(Edge); } } } Graph->SetEdgeWidth(5); Graph->PlaceTreeAsStar(); Graph->RescaleXY(0.1, RootVrtx); // return graph return Graph; }
// Demos BFS functions on directed graph that is not fully connected void DemoBFSDirectedRandom() { PNGraph G = TNGraph::New(); TStr FName = TStr::Fmt("%s/sample_bfsdfs_ngraph.txt", DIRNAME); // Create benchmark graph, initially visually to confirm values are correct const int NNodes = 30; G = GenRndGnm<PNGraph>(NNodes, NNodes*2); // Add some more random edges for (int i = 0; i < 10; i++) { TInt Src, Dst; do { Src = G->GetRndNId(); Dst = G->GetRndNId(); } while (Src == Dst || G->IsEdge(Src, Dst)); G->AddEdge(Src, Dst); } // Add isolated component G->AddNode(NNodes); G->AddNode(NNodes+1); G->AddNode(NNodes+2); G->AddEdge(NNodes, NNodes+1); G->AddEdge(NNodes+1, NNodes+2); G->AddEdge(NNodes+2, NNodes+1); printf("G->GetNodes() = %d, G->GetEdges() = %d\n", G->GetNodes(), G->GetEdges()); // SaveEdgeList(G, FName); // G = LoadEdgeList<PNGraph>(FName); TIntStrH NodeLabelH; for (int i = 0; i < G->GetNodes(); i++) { NodeLabelH.AddDat(i, TStr::Fmt("%d", i)); } DrawGViz(G, gvlDot, TStr::Fmt("%s/sample_bfsdfs_ngraph.png", DIRNAME), "Sample BFS Graph", NodeLabelH); printf("G->GetNodes() = %d, G->GetEdges() = %d\n", G->GetNodes(), G->GetEdges()); TIntV NIdV; int StartNId, Hop, Nodes; // for (int IsDir = 0; IsDir < 2; IsDir++) { int IsDir = 1; printf("IsDir = %d:\n", IsDir); StartNId = 11; Hop = 1; Nodes = GetNodesAtHop(G, StartNId, Hop, NIdV, IsDir); printf("Nodes = %d, GetNodesAtHop NIdV.Len() = %d\n", Nodes, NIdV.Len()); for (int i = 0; i < NIdV.Len(); i++) { printf("NIdV[%d] = %d\n", i, NIdV[i].Val); } printf("Nodes == 2"); printf("NIdV.Len() == 2"); TIntPrV HopCntV; Nodes = GetNodesAtHops(G, StartNId, HopCntV, IsDir); printf("Nodes = %d, GetNodesAtHops HopCntV.Len() = %d\n", Nodes, HopCntV.Len()); printf("Nodes == 10"); printf("HopCntV.Len() == 10"); // for (int N = 0; N < HopCntV.Len(); N++) { // printf("HopCntV[%d] = (%d, %d)\n", N, HopCntV[N].Val1.Val, HopCntV[N].Val2.Val); // } int Length, SrcNId, DstNId; SrcNId = 11; DstNId = G->GetNodes() - 1; Length = GetShortPath(G, SrcNId, DstNId, IsDir); printf("%d -> %d: SPL Length = %d\n", SrcNId, DstNId, Length); SrcNId = 11; DstNId = 27; Length = GetShortPath(G, SrcNId, DstNId, IsDir); printf("%d -> %d: SPL Length = %d\n", SrcNId, DstNId, Length); TIntH NIdToDistH; int MaxDist = 9; Length = GetShortPath(G, SrcNId, NIdToDistH, IsDir, MaxDist); // for (int i = 0; i < min(5,NIdToDistH.Len()); i++) { // printf("NIdToDistH[%d] = %d\n", i, NIdToDistH[i].Val); // } TInt::Rnd.PutSeed(0); int FullDiam; double EffDiam, AvgSPL; int NTestNodes = G->GetNodes() / 2; FullDiam = GetBfsFullDiam(G, NTestNodes, IsDir); printf("FullDiam = %d\n", FullDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir); printf("EffDiam = %.3f\n", EffDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir, EffDiam, FullDiam); printf("EffDiam = %.3f, FullDiam = %d\n", EffDiam, FullDiam); EffDiam = GetBfsEffDiam(G, NTestNodes, IsDir, EffDiam, FullDiam, AvgSPL); printf("EffDiam = %.3f, FullDiam = %d, AvgDiam = %.3f\n", EffDiam, FullDiam, AvgSPL); TIntV SubGraphNIdV; SubGraphNIdV.Add(8); SubGraphNIdV.Add(29); SubGraphNIdV.Add(16); SubGraphNIdV.Add(0); SubGraphNIdV.Add(19); SubGraphNIdV.Add(17); SubGraphNIdV.Add(26); SubGraphNIdV.Add(14); SubGraphNIdV.Add(10); SubGraphNIdV.Add(24); SubGraphNIdV.Add(27); SubGraphNIdV.Add(2); SubGraphNIdV.Add(18); EffDiam = GetBfsEffDiam(G, NTestNodes, SubGraphNIdV, IsDir, EffDiam, FullDiam); printf("For subgraph: EffDiam = %.4f, FullDiam = %d\n", EffDiam, FullDiam); }
///////////////////////////////////////////////// // Best-Paths void GetBestPaths( const TStr& SrcNmObjStr, const TStr& DstNmObjStr, const PNmObjBs& NmObjBs){ int SrcNmObjId=NmObjBs->GetNmObjId(SrcNmObjStr); int DstNmObjId=NmObjBs->GetNmObjId(DstNmObjStr); int NmObjs=NmObjBs->GetNmObjs(); TIntPrV ParLevPrV(NmObjs); TIntPrV DstParLevPrV; ParLevPrV.PutAll(TIntPr(-1, -1)); int CurLev=0; ParLevPrV[SrcNmObjId]=TIntPr(SrcNmObjId, CurLev); forever{ CurLev++; int NewEdges=0; for (int NmObjId1=0; NmObjId1<NmObjs; NmObjId1++){ if (ParLevPrV[NmObjId1].Val2==CurLev-1){ TIntV DocIdV1; NmObjBs->GetNmObjDocIdV(NmObjId1, DocIdV1); for (int NmObjId2=0; NmObjId2<NmObjs; NmObjId2++){ if ((NmObjId2==DstNmObjId)||(ParLevPrV[NmObjId2].Val2==-1)){ TIntV DocIdV2; NmObjBs->GetNmObjDocIdV(NmObjId2, DocIdV2); TIntV IntrsDocIdV; DocIdV1.Intrs(DocIdV2, IntrsDocIdV); if (!IntrsDocIdV.Empty()){ ParLevPrV[NmObjId2]=TIntPr(NmObjId1, CurLev); NewEdges++; if (NmObjId2==DstNmObjId){ DstParLevPrV.Add(TIntPr(NmObjId1, CurLev)); } } } } } } if ((NewEdges==0)||(ParLevPrV[DstNmObjId].Val2!=-1)){ break; } } // prepare graph THash<TStr, PVrtx> VrtxNmToVrtxH; TStrPrV VrtxNmPrV; VrtxNmToVrtxH.AddKey(SrcNmObjStr); VrtxNmToVrtxH.AddKey(DstNmObjStr); // write path ContexterF->NmObjLinkageREd->Clear(); for (int DstParLevPrN=0; DstParLevPrN<DstParLevPrV.Len(); DstParLevPrN++){ ParLevPrV[DstNmObjId]=DstParLevPrV[DstParLevPrN]; int DstParLev=ParLevPrV[DstNmObjId].Val2; TStr DstNmObjStr=NmObjBs->GetNmObjStr(DstNmObjId); ContexterF->NmObjLinkageREd->Lines->Add(DstNmObjStr.CStr()); int ParNmObjId=DstNmObjId; TStr PrevNmObjStr=DstNmObjStr; forever { if (ParNmObjId==SrcNmObjId){break;} ParNmObjId=ParLevPrV[ParNmObjId].Val1; int ParLev=ParLevPrV[ParNmObjId].Val2; TStr CurNmObjStr=NmObjBs->GetNmObjStr(ParNmObjId); TStr ParNmObjStr=TStr::GetSpaceStr((DstParLev-ParLev)*4)+CurNmObjStr; ContexterF->NmObjLinkageREd->Lines->Add(ParNmObjStr.CStr()); // create vertex & edge VrtxNmToVrtxH.AddKey(CurNmObjStr); if (!PrevNmObjStr.Empty()){ if (PrevNmObjStr<CurNmObjStr){ VrtxNmPrV.AddUnique(TStrPr(PrevNmObjStr, CurNmObjStr)); } else if (PrevNmObjStr>CurNmObjStr){ VrtxNmPrV.AddUnique(TStrPr(CurNmObjStr, PrevNmObjStr)); } } // save curent named-object PrevNmObjStr=CurNmObjStr; } } // generate graph // create graph PGraph Graph=TGGraph::New(); // create vertices for (int VrtxN=0; VrtxN<VrtxNmToVrtxH.Len(); VrtxN++){ TStr VrtxNm=VrtxNmToVrtxH.GetKey(VrtxN); PVrtx Vrtx=TGVrtx::New(VrtxNm); VrtxNmToVrtxH.GetDat(VrtxNm)=Vrtx; Graph->AddVrtx(Vrtx); } // create edges for (int EdgeN=0; EdgeN<VrtxNmPrV.Len(); EdgeN++){ PVrtx Vrtx1=VrtxNmToVrtxH.GetDat(VrtxNmPrV[EdgeN].Val1); PVrtx Vrtx2=VrtxNmToVrtxH.GetDat(VrtxNmPrV[EdgeN].Val2); PEdge Edge=new TGEdge(Vrtx1, Vrtx2, TStr::Fmt("_%d", EdgeN), false); Graph->AddEdge(Edge); } // place graph ContexterF->State->ElGraph=Graph; TRnd Rnd(1); ContexterF->State->ElGraph->PlaceSimAnnXY(Rnd, ContexterF->State->ElGks); // draw graph ContexterF->State->ElGks->Clr(); ContexterF->ElPbPaint(NULL); }
void DemoFullBfsDfs() { const int NNodes = 500; PGraph G = GenFull<PGraph>(NNodes); PNGraph GOut; int TreeSz, TreeDepth; // Get BFS tree from first node without following links (demos different options) GOut = GetBfsTree(G, 1, false, false); GetSubTreeSz(G, 1, false, false, TreeSz, TreeDepth); printf("FollowOut=false, FollowIn=false, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", GOut->GetNodes(), G->GetEdges()); printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth); GOut = GetBfsTree(G, NNodes-1, true, true); GetSubTreeSz(G, 1, true, true, TreeSz, TreeDepth); printf("FollowOut=true, FollowIn=true, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", GOut->GetNodes(), G->GetEdges()); printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth); GOut = GetBfsTree(G, NNodes/2, true, false); GetSubTreeSz(G, 1, true, false, TreeSz, TreeDepth); printf("FollowOut=true, FollowIn=false, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", GOut->GetNodes(), G->GetEdges()); printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth); GOut = GetBfsTree(G, 1, false, true); GetSubTreeSz(G, 1, false, true, TreeSz, TreeDepth); printf("FollowOut=false, FollowIn=true, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", GOut->GetNodes(), G->GetEdges()); printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth); TIntV NIdV; int StartNId, Hop, Nodes; StartNId = 1; Hop = 1; Nodes = GetNodesAtHop(G, StartNId, Hop, NIdV, HasGraphFlag(typename PGraph::TObj, gfDirected)); printf("StartNId = %d, Nodes = %d, GetNodesAtHop NIdV.Len() = %d\n", StartNId, Nodes, NIdV.Len()); TIntPrV HopCntV; Nodes = GetNodesAtHops(G, StartNId, HopCntV, HasGraphFlag(typename PGraph::TObj, gfDirected)); printf("StartNId = %d, Nodes = %d, GetNodesAtHops HopCntV.Len() = %d\n", StartNId, Nodes, HopCntV.Len()); int Length, SrcNId, DstNId; SrcNId = 1; DstNId = NNodes-1; Length = GetShortPath(G, SrcNId, DstNId, HasGraphFlag(typename PGraph::TObj, gfDirected)); printf("SPL Length = %d\n", Length); TIntH NIdToDistH; int MaxDist = 9; Length = GetShortPath(G, SrcNId, NIdToDistH, HasGraphFlag(typename PGraph::TObj, gfDirected), MaxDist); // for (int i = 0; i < min(5,NIdToDistH.Len()); i++) { // printf("NIdToDistH[%d] = %d\n", i, NIdToDistH[i].Val); // } int FullDiam; double EffDiam, AvgDiam; int NTestNodes = 10; for (int IsDir = 0; IsDir < 2; IsDir++) { printf("IsDir = %d:\n", IsDir); FullDiam = GetBfsFullDiam(G, NTestNodes, IsDir); printf("FullDiam = %d\n", FullDiam); EffDiam = GetBfsEffDiam (G, NTestNodes, IsDir); printf("EffDiam = %.3f\n", EffDiam); EffDiam = GetBfsEffDiam (G, NTestNodes, IsDir, EffDiam, FullDiam); printf("EffDiam = %.3f, FullDiam = %d\n", EffDiam, FullDiam); EffDiam = GetBfsEffDiam (G, NTestNodes, IsDir, EffDiam, FullDiam, AvgDiam); printf("EffDiam = %.3f, FullDiam = %d, AvgDiam = %.3f\n", EffDiam, FullDiam, AvgDiam); TIntV SubGraphNIdV; for (int i = 0; i < NTestNodes; i++) { SubGraphNIdV.Add(G->GetRndNId()); } // for (int i = 0; i < SubGraphNIdV.Len(); i++) { // printf("SubGraphNIdV[%d] = %d\n", i, SubGraphNIdV[i].Val); // } EffDiam = GetBfsEffDiam(G, NTestNodes, SubGraphNIdV, IsDir, EffDiam, FullDiam); printf("For subgraph: EffDiam = %.3f, FullDiam = %d\n", EffDiam, FullDiam); } }
void TSkyGridBs::SaveTxt(const TStr& FNm, const uint64& CurTm){ // time-limit TStr CurTmStr=TTm::GetTmFromMSecs(CurTm).GetWebLogDateTimeStr(); uint64 CurDateTm=TTm::GetMSecsFromTm(TTm::GetTmFromWebLogDateTimeStr(TTm::GetTmFromMSecs(CurTm).GetWebLogDateStr())); TStr CurDateTmStr=TTm::GetTmFromMSecs(CurDateTm).GetWebLogDateTimeStr(); TUInt64V MnTmV; MnTmV.Add(CurDateTm-0*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-1*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-2*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-4*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-8*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-16*TTmInfo::GetDayMSecs()); MnTmV.Add(CurDateTm-32*TTmInfo::GetDayMSecs()); // get bow //PBowDocBs BowDocBs=GetBowDocBs(3, 5); PBowDocBs BowDocBs=GetBowDocBs(); PBowDocWgtBs BowDocWgtBs=GetBowDocWgtBs(BowDocBs); // open file TFOut FOut(FNm); FILE* fOut=FOut.GetFileId(); // get docs-entities sorted vector TIntPrV DocsEntIdPrV; GetSorted_DocsEntIdPrV(DocsEntIdPrV); // traverse entities for (int EntN=0; EntN<DocsEntIdPrV.Len(); EntN++){ int EntId=DocsEntIdPrV[EntN].Val2; TStr EntNm=GetEntNm(EntId); int EntDocs=DocsEntIdPrV[EntN].Val1; TSkyGridEnt& Ent=GetEnt(EntId); int LinkEnts=Ent.GetLinkEnts(); fprintf(fOut, "'%s' [%d docs] [%d ents]\n", EntNm.CStr(), EntDocs, LinkEnts); // output docs over dates {TStrIntPrV DateStrDocsPrV; int _EntDocs; Ent.GetDocsPerDateV(this, DateStrDocsPrV, _EntDocs); fprintf(fOut, " Docs per Date (%d docs):", _EntDocs); for (int DateN=0; DateN<DateStrDocsPrV.Len(); DateN++){ TStr DateStr=DateStrDocsPrV[DateN].Val1; int Docs=DateStrDocsPrV[DateN].Val2; fprintf(fOut, " [%s:%d]", DateStr.CStr(), Docs); } fprintf(fOut, "\n");} fprintf(fOut, " [Now: %s]\n", CurTmStr.CStr()); TIntPrV PrevLinkWgtDstEntIdPrV; TStrFltPrV PrevWordStrWgtPrV; for (int MnTmN=0; MnTmN<MnTmV.Len(); MnTmN++){ uint64 MnTm=MnTmV[MnTmN]; double PastDays=(CurDateTm-MnTm)/double(TTmInfo::GetDayMSecs()); TStr MnTmStr=TTm::GetTmFromMSecs(MnTm).GetWebLogDateTimeStr(); // get linked entities TIntPrV LinkWgtDstEntIdPrV; Ent.GetSorted_LinkWgtDstEntIdPrV(MnTm, 0.9, LinkWgtDstEntIdPrV); // output difference between previous and current centroid if (MnTmN>0){ TIntPrV NegDiffLinkWgtDstEntIdPrV; TIntPrV PosDiffLinkWgtDstEntIdPrV; GetLinkWgtDstEntIdPrVDiff(LinkWgtDstEntIdPrV, PrevLinkWgtDstEntIdPrV, NegDiffLinkWgtDstEntIdPrV, PosDiffLinkWgtDstEntIdPrV); // output positive change TChA PosDiffLinkWgtDstEntIdPrVChA; GetLinkWgtDstEntIdPrVChA(PosDiffLinkWgtDstEntIdPrV, PosDiffLinkWgtDstEntIdPrVChA); fprintf(fOut, " Pos-Diff: %s\n", PosDiffLinkWgtDstEntIdPrVChA.CStr()); // output negative change TChA NegDiffLinkWgtDstEntIdPrVChA; GetLinkWgtDstEntIdPrVChA(NegDiffLinkWgtDstEntIdPrV, NegDiffLinkWgtDstEntIdPrVChA); fprintf(fOut, " Neg-Diff: %s\n", NegDiffLinkWgtDstEntIdPrVChA.CStr()); } PrevLinkWgtDstEntIdPrV=LinkWgtDstEntIdPrV; // output linked entities int TopLinkEnts=LinkWgtDstEntIdPrV.Len(); TChA LinkWgtDstEntIdPrVChA; GetLinkWgtDstEntIdPrVChA(LinkWgtDstEntIdPrV, LinkWgtDstEntIdPrVChA); fprintf(fOut, " Entities (%d ents): %s\n", TopLinkEnts, LinkWgtDstEntIdPrVChA.CStr()); // get text centroid int CtrDocs; TStrFltPrV WordStrWgtPrV; Ent.GetDocCentroid(this, BowDocBs, BowDocWgtBs, MnTm, 150, 0.9, CtrDocs, WordStrWgtPrV); // output difference between previous and current centroid if (MnTmN>0){ TStrFltPrV NegDiffWordStrWgtPrV; TStrFltPrV PosDiffWordStrWgtPrV; GetWordStrWgtPrVDiff(WordStrWgtPrV, PrevWordStrWgtPrV, NegDiffWordStrWgtPrV, PosDiffWordStrWgtPrV); // output positive change TChA PosDiffWordStrWgtPrVChA; GetWordStrWgtPrVChA(PosDiffWordStrWgtPrV, PosDiffWordStrWgtPrVChA); fprintf(fOut, " Pos-Diff: %s\n", PosDiffWordStrWgtPrVChA.CStr()); // output negative change TChA NegDiffWordStrWgtPrVChA; GetWordStrWgtPrVChA(NegDiffWordStrWgtPrV, NegDiffWordStrWgtPrVChA); fprintf(fOut, " Neg-Diff: %s\n", NegDiffWordStrWgtPrVChA.CStr()); } PrevWordStrWgtPrV=WordStrWgtPrV; // output centroid TChA WordStrWgtPrVChA; GetWordStrWgtPrVChA(WordStrWgtPrV, WordStrWgtPrVChA); fprintf(fOut, " Centroid (%d docs, %d words): %s\n", CtrDocs, WordStrWgtPrV.Len(), WordStrWgtPrVChA.CStr()); // output time fprintf(fOut, " [-%.1f days: %s]\n", PastDays, MnTmStr.CStr()); } // entity clustering /*TVec<TStrFltPrV> EntNmWgtPrVV; Ent.GetEntClustV(this, MnTmV.Last(), 100, 1000, 10, EntNmWgtPrVV); for (int ClustN=0; ClustN<EntNmWgtPrVV.Len(); ClustN++){ TStrFltPrV& EntNmWgtPrV=EntNmWgtPrVV[ClustN]; fprintf(fOut, " Clust-%d:", ClustN); for (int EntN=0; EntN<EntNmWgtPrV.Len(); EntN++){ TStr EntNm=EntNmWgtPrV[EntN].Val1; double Wgt=EntNmWgtPrV[EntN].Val2; fprintf(fOut, " ['%s':%.3f]", EntNm.CStr(), Wgt); } fprintf(fOut, "\n"); }*/ fprintf(fOut, "\n"); } }