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); } } }
int FindCascadeRoot(const PGraph& G, const TIntH& NIdInfTmH) { // earliest infected node int Min=TInt::Mx, MinNId=-1; for (typename PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { const int t = NIdInfTmH.GetDat(NI.GetId()); if (t < Min && NI.GetInDeg()==0) { Min=t; MinNId=NI.GetId(); } } IAssert(MinNId!=-1); return MinNId; }
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(); }
double PercentDegree(const PGraph& Graph, const int Threshold=0) { int Cnt = 0; for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetDeg() >= Threshold) Cnt++; } return (double)Cnt / (double) Graph->GetNodes(); }
int MxDegree(const PGraph& Graph) { int MaxDeg = 0; for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetDeg() > MaxDeg) { MaxDeg = NI.GetDeg(); } } return MaxDeg; }
int NodesGTEDegree(const PGraph& Graph, const int Threshold=0) { int Cnt = 0; for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetDeg() >= Threshold) Cnt++; } return Cnt; }
void SampleCascade(const PGraph& InfCasc, const PGraph& NetCasc, const TIntH& NIdInfTmH, const double& PStep=0.05, const int& NRuns=1, const bool& DivByM=true) { for (int Run=0; Run < NRuns; Run++) { for (double P = PStep; P <= 1.01; P += PStep) { TIntV NIdV; for (typename PGraph::TObj::TNodeI NI = InfCasc->BegNI(); NI < InfCasc->EndNI(); NI++) { if (TInt::Rnd.GetUniDev() < P) { NIdV.Add(NI.GetId()); } } PGraph InfG = TSnap::GetSubGraph(InfCasc, NIdV); PGraph NetG = TSnap::GetSubGraph(NetCasc, NIdV); if (InfG->GetNodes()==0) { continue; } TakeStat(InfG, NetG, NIdInfTmH, P, DivByM); } } }
void BenchmarkGraphNodeI(PGraph Graph, std::ofstream& file, bool isDefrag) { int NCount = 0; int i = 0; clock_t start = clock(); for (i = 0; i < 50; i++) { NCount = 0; for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } } double msec = (clock() - start) * 1000.0 / CLOCKS_PER_SEC; printf("Nodes: %d Edges: %d Time: %f ms\n", NCount, Graph->GetEdges(), msec/50); file << msec/50 << " "; }
int main(int argc, char* argv[]) { //// what type of graph do you want to use? //typedef PUNGraph PGraph; // undirected graph typedef PNGraph PGraph; // directed graph //typedef PNEGraph PGraph; // directed multigraph //typedef TPt<TNodeNet<TInt> > PGraph; //typedef TPt<TNodeEdgeNet<TInt, TInt> > PGraph; // this code is independent of what particular graph implementation/type we use printf("Creating graph:\n"); PGraph G = PGraph::TObj::New(); for (int n = 0; n < 15; n++) { G->AddNode(); // if no parameter is given, node ids are 0,1,...,9 } G->AddEdge(1, 4); printf(" Edge 1 -- 4 added\n"); G->AddEdge(1, 3); printf(" Edge 1 -- 3 added\n"); G->AddEdge(2, 5); printf(" Edge 2 -- 5 added\n"); G->AddEdge(3, 2); printf(" Edge 3 -- 2 added\n"); G->AddEdge(3, 5); printf(" Edge 3 -- 5 added\n"); G->AddEdge(3, 10); printf(" Edge 3 -- 10 added\n"); G->AddEdge(4, 5); printf(" Edge 4 -- 5 added\n"); G->AddEdge(4, 7); printf(" Edge 4 -- 7 added\n"); G->AddEdge(4, 8); printf(" Edge 4 -- 8 added\n"); G->AddEdge(5, 6); printf(" Edge 5 -- 6 added\n"); G->AddEdge(6, 13); printf(" Edge 6 -- 13 added\n"); G->AddEdge(7, 4); printf(" Edge 7 -- 4 added\n"); G->AddEdge(8, 7); printf(" Edge 8 -- 7 added\n"); G->AddEdge(8, 9); printf(" Edge 8 -- 9 added\n"); G->AddEdge(9, 10); printf(" Edge 9 -- 10 added\n"); G->AddEdge(9, 12); printf(" Edge 9 -- 12 added\n"); G->AddEdge(10, 3); printf(" Edge 10 -- 3 added\n"); G->AddEdge(10, 6); printf(" Edge 10 -- 6 added\n"); G->AddEdge(11, 12); printf(" Edge 11 -- 12 added\n"); G->AddEdge(12, 9); printf(" Edge 12 -- 9 added\n"); G->AddEdge(12, 11); printf(" Edge 12 -- 11 added\n"); G->AddEdge(12, 14); printf(" Edge 12 -- 14 added\n"); G->AddEdge(13, 14); printf(" Edge 13 -- 14 added\n"); G->AddEdge(14, 13); printf(" Edge 14 -- 13 added\n"); /*for (int e = 0; e < 10; e++) { const int NId1 = G->GetRndNId(); const int NId2 = G->GetRndNId(); if (G->AddEdge(NId1, NId2) != -2) { printf(" Edge %d -- %d added\n", NId1, NId2); } else { printf(" Edge %d -- %d already exists\n", NId1, NId2); } }*/ IAssert(G->IsOk()); //G->Dump(); // delete //PGraph::TObj::TNodeI NI = G->GetNI(0); //printf("Delete edge %d -- %d\n", NI.GetId(), NI.GetOutNId(0)); //G->DelEdge(NI.GetId(), NI.GetOutNId(0)); const int RndNId = G->GetRndNId(); printf("Delete node %d\n", RndNId); G->DelNode(RndNId); IAssert(G->IsOk()); // dump the graph printf("Graph (%d, %d)\n", G->GetNodes(), G->GetEdges()); for (PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { printf(" %d: ", NI.GetId()); for (int e = 0; e < NI.GetDeg(); e++) { printf(" %d", NI.GetNbrNId(e)); } printf("\n"); } // dump subgraph TIntV NIdV; for (PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { if (NIdV.Len() < G->GetNodes()/2) { NIdV.Add(NI.GetId()); } } PGraph SubG = TSnap::GetSubGraph(G, NIdV); //SubG->Dump(); // get NGraph { PNGraph NG = TSnap::ConvertGraph<PNGraph>(SubG); NG->Dump(); IAssert(NG->IsOk()); TSnap::ConvertSubGraph<PNGraph>(G, NIdV)->Dump(); } // get NEGraph { PNEGraph NEG = TSnap::ConvertGraph<PNEGraph>(SubG); NEG->Dump(); IAssert(NEG->IsOk()); TSnap::ConvertSubGraph<PNGraph>(G, NIdV)->Dump(); } TSnap::TestAnf<PUNGraph>(); return 0; }
int main(int argc, char* argv[]) { //// what type of graph do you want to use? typedef PUNGraph PGraph; // undirected graph //typedef PNGraph PGraph; // directed graph //typedef PNEGraph PGraph; // directed multigraph //typedef TPt<TNodeNet<TInt> > PGraph; //typedef TPt<TNodeEdgeNet<TInt, TInt> > PGraph; // this code is independent of what particular graph implementation/type we use printf("Creating graph:\n"); PGraph G = PGraph::TObj::New(); for (int n = 0; n < 10; n++) { G->AddNode(); // if no parameter is given, node ids are 0,1,...,9 } G->AddEdge(0, 1); for (int e = 0; e < 10; e++) { const int NId1 = G->GetRndNId(); const int NId2 = G->GetRndNId(); if (G->AddEdge(NId1, NId2) != -2) { printf(" Edge %d -- %d added\n", NId1, NId2); } else { printf(" Edge %d -- %d already exists\n", NId1, NId2); } } IAssert(G->IsOk()); //G->Dump(); // delete PGraph::TObj::TNodeI NI = G->GetNI(0); printf("Delete edge %d -- %d\n", NI.GetId(), NI.GetOutNId(0)); G->DelEdge(NI.GetId(), NI.GetOutNId(0)); const int RndNId = G->GetRndNId(); printf("Delete node %d\n", RndNId); G->DelNode(RndNId); IAssert(G->IsOk()); // dump the graph printf("Graph (%d, %d)\n", G->GetNodes(), G->GetEdges()); for (PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { printf(" %d: ", NI.GetId()); for (int e = 0; e < NI.GetDeg(); e++) { printf(" %d", NI.GetNbrNId(e)); } printf("\n"); } // dump subgraph TIntV NIdV; for (PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) { if (NIdV.Len() < G->GetNodes()/2) { NIdV.Add(NI.GetId()); } } PGraph SubG = TSnap::GetSubGraph(G, NIdV); //SubG->Dump(); // get UNGraph { PUNGraph UNG = TSnap::ConvertGraph<PUNGraph>(SubG); UNG->Dump(); IAssert(UNG->IsOk()); TSnap::ConvertSubGraph<PNGraph>(G, NIdV)->Dump(); } // get NGraph { PNGraph NG = TSnap::ConvertGraph<PNGraph>(SubG); NG->Dump(); IAssert(NG->IsOk()); TSnap::ConvertSubGraph<PNGraph>(G, NIdV)->Dump(); } // get NEGraph { PNEGraph NEG = TSnap::ConvertGraph<PNEGraph>(SubG); NEG->Dump(); IAssert(NEG->IsOk()); TSnap::ConvertSubGraph<PNGraph>(G, NIdV)->Dump(); } TSnap::TestAnf<PUNGraph>(); return 0; }