// 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; }
///Generate graph using the AGM model. CProbV = vector of Pc PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const TFltV& CProbV, TRnd& Rnd, const double PNoCom) { PUNGraph G = TUNGraph::New(100 * CmtyVV.Len(), -1); printf("AGM begins\n"); for (int i = 0; i < CmtyVV.Len(); i++) { TIntV& CmtyV = CmtyVV[i]; for (int u = 0; u < CmtyV.Len(); u++) { if ( G->IsNode(CmtyV[u])) { continue; } G->AddNode(CmtyV[u]); } double Prob = CProbV[i]; RndConnectInsideCommunity(G, CmtyV, Prob, Rnd); } if (PNoCom > 0.0) { //if we want to connect nodes that do not share any community TIntSet NIDS; for (int c = 0; c < CmtyVV.Len(); c++) { for (int u = 0; u < CmtyVV[c].Len(); u++) { NIDS.AddKey(CmtyVV[c][u]); } } TIntV NIDV; NIDS.GetKeyV(NIDV); RndConnectInsideCommunity(G,NIDV,PNoCom,Rnd); } printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges()); G->Defrag(); return G; }
int GenPy(PUNGraph &res, ofstream& TFile, const TStr& parameters) { Env = TEnv(parameters, TNotify::StdNotify); TStr mN = Env.GetIfArgPrefixStr("-module:", "random_graphs", "Module name"); TStr fN = Env.GetIfArgPrefixStr("-func:", "fast_gnp_random_graph", "Function name"); PyObject **G = new PyObject*[1]; char *moduleName = mN.CStr(); char *funcName = fN.CStr(); AddFuncInfo(); TStrV args, argTypes; if (!ParseArgs(funcName, parameters, args, argTypes)) { printf("Fail to parse arguments for NetworkX generation...\n"); return 0; }; TExeTm execTime; if (!CallPyFunction(moduleName, funcName, args, argTypes, G)) { cout << "CallPyFunction() raised error. Execution terminated.\n"; system("pause"); exit(1); }; TFile << "Time of generation of graph by NetworkX: " << execTime.GetTmStr() << endl; execTime.Tick(); PyObject*** nodes = new PyObject**[1]; GetNodes(G, nodes); int nodesCount = PyList_Size(*(nodes[0])); //printf("nodesCount = %d, ", nodesCount); res = PUNGraph::TObj::New(); res->Reserve(nodesCount, nodesCount*nodesCount); for (size_t i = 0; i < nodesCount; i++) res->AddNode(i); Py_DECREF(nodes); PyObject*** edges = new PyObject**[1]; GetEdges(G, edges); int edgesCount = PyList_Size(*(edges[0])); //printf("edgesCount = %d\n", edgesCount); for (size_t i = 0; i < edgesCount; i++) { PyObject* item = PySequence_Fast_GET_ITEM(*(edges[0]), i); int v1, v2; PyObject* node = PySequence_Fast_GET_ITEM(item,0); v1 = PyLong_AsLong(node); node = PySequence_Fast_GET_ITEM(item,1); v2 = PyLong_AsLong(node); res->AddEdge(v1,v2); } TFile << "Time of copying of graph from NetworkX representation: " << execTime.GetTmStr() << endl; Py_DECREF(G); Py_DECREF(edges); //Py_Finalize(); // очищение памяти, отданной интерпретатору return 0; }
//Graph: 3--0--4 // /| // 1-2 PUNGraph TUNGraph::GetSmallGraph() { PUNGraph Graph = TUNGraph::New(); for (int i = 0; i < 5; i++) { Graph->AddNode(i); } Graph->AddEdge(0,1); Graph->AddEdge(0,2); Graph->AddEdge(0,3); Graph->AddEdge(0,4); Graph->AddEdge(1,2); return Graph; }
PUNGraph get_PUNGraph (const int *m, const int nval, const int nodes) { PUNGraph g = PUNGraph::New (); for (int i = 1; i<= nodes; i++) { g->AddNode(i); } for (int i =0; i < nval; i++) { g->AddEdge(m[i], m[nval + i]); } return g; }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("Clique Percolation Method. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); TExeTm ExeTm; Try const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input undirected graph file (single directed edge per line)"); const int OverlapSz = Env.GetIfArgPrefixInt("-k:", 2, "Min clique overlap"); TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "", "Output file prefix"); if (OutFNm.Empty()) { OutFNm = InFNm.GetFMid(); } PUNGraph G; if (InFNm == "DEMO") { // small demo graph G = TUNGraph::New(); for (int i = 1; i < 8; i++) { G->AddNode(i); } G->AddEdge(1,2); G->AddEdge(2,3); G->AddEdge(2,4); G->AddEdge(3,4); G->AddEdge(4,5); G->AddEdge(4,7); G->AddEdge(5,6); G->AddEdge(5,7); G->AddEdge(6,7); // draw the small graph using GraphViz TSnap::DrawGViz(G, gvlNeato, "small_graph.png", "", true); } // load graph else if (InFNm.GetFExt().GetLc()==".ungraph") { TFIn FIn(InFNm); G=TUNGraph::Load(FIn); } else if (InFNm.GetFExt().GetLc()==".ngraph") { TFIn FIn(InFNm); G=TSnap::ConvertGraph<PUNGraph>(TNGraph::Load(FIn), false); } else { G = TSnap::LoadEdgeList<PUNGraph>(InFNm, 0, 1); } // find communities TVec<TIntV> CmtyV; TCliqueOverlap::GetCPMCommunities(G, OverlapSz+1, CmtyV); // save result FILE *F = fopen(TStr::Fmt("cpm-%s.txt", OutFNm.CStr()).CStr(), "wt"); fprintf(F, "# %d Overlapping Clique Percolation Communities (min clique overlap %d)\n", CmtyV.Len(), OverlapSz); fprintf(F, "# Each line contains nodes belonging to the same community community\n"); for (int i = 0; i < CmtyV.Len(); i++) { fprintf(F, "%d", CmtyV[i][0].Val); for (int j = 1; j < CmtyV[i].Len(); j++) { fprintf(F, "\t%d", CmtyV[i][j].Val); } fprintf(F, "\n"); } Catch printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }
// Generate TUNGraph PUNGraph GetTestTUNGraph() { PUNGraph Graph = TUNGraph::New(); for (int i = 0; i < 20; i++) { Graph->AddNode(i); } for (int i = 0; i < 20; i++) { Graph->AddEdge(i,(i+1) % 20); Graph->AddEdge(i,(i+2) % 20); Graph->AddEdge(i,(i+3) % 20); } return Graph; }
// Generate TUNGraph PUNGraph TriadGetTestTUNGraph() { PUNGraph Graph = TUNGraph::New(); for (int i = 0; i < 6; i++) { Graph->AddNode(i); } for (int i = 1; i < 6; i++) { Graph->AddEdge(0, i); } Graph->AddEdge(2, 3); Graph->AddEdge(1, 5); Graph->AddEdge(2, 5); return Graph; }
/// Generates a random scale-free graph using the Geometric Preferential /// Attachment model by Flexman, Frieze and Vera. /// See: A geometric preferential attachment model of networks by Flexman, /// Frieze and Vera. WAW 2004. /// URL: http://math.cmu.edu/~af1p/Texfiles/GeoWeb.pdf PUNGraph GenGeoPrefAttach(const int& Nodes, const int& OutDeg, const double& Beta, TRnd& Rnd) { PUNGraph G = TUNGraph::New(Nodes, Nodes*OutDeg); TFltTrV PointV(Nodes, 0); TFltV ValV; // points on a sphere of radius 1/(2*pi) const double Rad = 0.5 * TMath::Pi; for (int i = 0; i < Nodes; i++) { TSnapDetail::GetSphereDev(3, Rnd, ValV); PointV.Add(TFltTr(Rad*ValV[0], Rad*ValV[1], Rad*ValV[2])); } const double R2 = TMath::Sqr(log((double) Nodes) / (pow((double) Nodes, 0.5-Beta))); TIntV DegV, NIdV; int SumDeg; for (int t = 0; t < Nodes; t++) { const int pid = t; const TFltTr& P1 = PointV[pid]; // add node if (! G->IsNode(pid)) { G->AddNode(pid); } // find neighborhood DegV.Clr(false); NIdV.Clr(false); SumDeg=0; for (int p = 0; p < t; p++) { const TFltTr& P2 = PointV[p]; if (TMath::Sqr(P1.Val1-P2.Val1)+TMath::Sqr(P1.Val2-P2.Val2)+TMath::Sqr(P1.Val3-P2.Val3) < R2) { NIdV.Add(p); DegV.Add(G->GetNI(p).GetDeg()+1); SumDeg += DegV.Last(); } } // add edges for (int m = 0; m < OutDeg; m++) { const int rnd = Rnd.GetUniDevInt(SumDeg); int sum = 0, dst = -1; for (int s = 0; s < DegV.Len(); s++) { sum += DegV[s]; if (rnd < sum) { dst=s; break; } } if (dst != -1) { G->AddEdge(pid, NIdV[dst]); SumDeg -= DegV[dst]; NIdV.Del(dst); DegV.Del(dst); } } } return G; }
void UndirCopy(PNGraph& dir_graph, PUNGraph& undir_graph) { // Add all of the nodes into the new graph for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI(); node++) { int curr_node = node.GetId(); undir_graph->AddNode(curr_node); } for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI(); node++) { int curr_node = node.GetId(); for (int e = 0; e < node.GetOutDeg(); ++e) { int nbr_node = node.GetOutNId(e); if (!undir_graph->IsEdge(curr_node, nbr_node)) { undir_graph->AddEdge(curr_node, nbr_node); } } } }
PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const double& DensityCoef, const double& ScaleCoef, TRnd& Rnd){ TFltV CProbV; double Prob; for(int i=0;i<CmtyVV.Len();i++) { Prob = ScaleCoef*pow(double(CmtyVV[i].Len()),-DensityCoef); if(Prob>1){Prob = 1;} CProbV.Add(Prob); } PUNGraph G = TUNGraph::New(); printf("AGM begins\n"); for(int i=0;i<CmtyVV.Len();i++) { TIntV& CmtyV = CmtyVV[i]; for(int u=0;u<CmtyV.Len();u++) { G->AddNode(CmtyV[u]); } Prob = CProbV[i]; printf("\r%d(%d)/%d",i,CmtyVV[i].Len(),CmtyVV.Len()); RndConnectInsideCommunity(G,CmtyV,Prob,Rnd); } printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges()); return G; }
PUNGraph TCliqueOverlap::CalculateOverlapMtx(const TVec<TIntV>& MaxCliques, int MinNodeOverlap) { const int n = MaxCliques.Len(); //Convert max cliques to HashSets TVec<THashSet<TInt> > cliques; for (int i=0; i<n; i++) { const int len = MaxCliques[i].Len(); cliques.Add(); if (len < MinNodeOverlap) { continue; } THashSet<TInt>& set = cliques.Last(); set.Gen(len); for (int j=0; j<len; j++) { set.AddKey(MaxCliques[i][j]); } } //Init clique clique overlap matrix PUNGraph OverlapMtx = TUNGraph::New(); for (int i=0; i < n; i++) { OverlapMtx->AddNode(i); } //Calculate clique clique overlap matrix for (int i=0; i<n; i++) { for (int j=i+1; j<n; j++) { if (Intersection(cliques[i], cliques[j]) >= MinNodeOverlap) { OverlapMtx->AddEdge(i,j); } } } return OverlapMtx; }
// Test node, edge creation void ManipulateNodesEdges() { int NNodes = 10000; int NEdges = 100000; const char *FName = "test.graph"; PUNGraph Graph; PUNGraph Graph1; PUNGraph Graph2; int i; int n; int NCount; int ECount1; int ECount2; int x,y; bool t; Graph = TUNGraph::New(); t = Graph->Empty(); // create the nodes for (i = 0; i < NNodes; i++) { Graph->AddNode(i); } t = Graph->Empty(); n = Graph->GetNodes(); // create random edges NCount = NEdges; while (NCount > 0) { x = (long) (drand48() * NNodes); y = (long) (drand48() * NNodes); // Graph->GetEdges() is not correct for the loops (x == y), // skip the loops in this test if (x != y && !Graph->IsEdge(x,y)) { n = Graph->AddEdge(x, y); NCount--; } } PrintGStats("ManipulateNodesEdges:Graph",Graph); // get all the nodes NCount = 0; for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } // get all the edges for all the nodes ECount1 = 0; for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { ECount1++; } } ECount1 /= 2; // get all the edges directly ECount2 = 0; for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { ECount2++; } printf("graph ManipulateNodesEdges:Graph, nodes %d, edges1 %d, edges2 %d\n", NCount, ECount1, ECount2); // assignment Graph1 = TUNGraph::New(); *Graph1 = *Graph; PrintGStats("ManipulateNodesEdges:Graph1",Graph1); // save the graph { TFOut FOut(FName); Graph->Save(FOut); FOut.Flush(); } // load the graph { TFIn FIn(FName); Graph2 = TUNGraph::Load(FIn); } PrintGStats("ManipulateNodesEdges:Graph2",Graph2); // remove all the nodes and edges for (i = 0; i < NNodes; i++) { n = Graph->GetRndNId(); Graph->DelNode(n); } PrintGStats("ManipulateNodesEdges:Graph",Graph); Graph1->Clr(); PrintGStats("ManipulateNodesEdges:Graph1",Graph1); }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("Memetracker Converter. Build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()), 1, true); const TStr netinfInputFile = Env.GetIfArgPrefixStr( "-netinfInputFile=", "data/memetracker/InfoNet5000Q1000NEXP.txt", "File containing the inferred network, used as underlying structure"); const TStr clusteredCascadesInputFile = Env.GetIfArgPrefixStr( "-clusteredCascadesInputFile=", "data/memetracker/clust-qt08080902w3mfq5.txt", "File containing clustered instances of memes and their path on the web"); const TInt cascadeInputLine = Env.GetIfArgPrefixInt( "-cascadeInputLine=", 75926, "Specific cascade to read from clusteredCascadesInputFile"); const TStr snapNetworkOutputFile = Env.GetIfArgPrefixStr( "-snapNetworkOutputFile=", "data/memetracker/memeNetwork.dat", "File where to write the output network as a snap binary"); const TStr groundTruthOutputFile = Env.GetIfArgPrefixStr( "-groundTruthOutputFile=", "data/memetracker/memeGroundTruth.txt", "File where to write the ground truth"); // Build the network from the most popular websites. ifstream inputfile(netinfInputFile.CStr()); string line; getline(inputfile, line); // read header line from file map<string, unsigned int> urlToNodeIdHash; PUNGraph graph = PUNGraph::New(); unsigned int nodeId = 0; while (getline(inputfile, line)) { istringstream iss(line); string idx, src, dst; iss >> idx >> src >> dst; if (urlToNodeIdHash.find(src) == urlToNodeIdHash.end()) { urlToNodeIdHash[src] = nodeId; graph->AddNode(nodeId); nodeId++; } if (urlToNodeIdHash.find(dst) == urlToNodeIdHash.end()) { urlToNodeIdHash[dst] = nodeId; graph->AddNode(nodeId); nodeId++; } graph->AddEdge(urlToNodeIdHash[src], urlToNodeIdHash[dst]); } // Save network. { TFOut FOut(snapNetworkOutputFile); graph->Save(FOut); } // Read one memetracker entry. ifstream memetracker(clusteredCascadesInputFile.CStr()); for (int i = 0; i < cascadeInputLine; ++i) getline(memetracker, line); getline(memetracker, line); int entries, dummyInt; istringstream iss(line); iss >> dummyInt >> entries; cout << "Building cascade for "; while (!iss.eof()) { string phrase; iss >> phrase; cout << phrase << " "; } cout << endl; // Dump cascade to some file. ofstream dumpStream; dumpStream.open(groundTruthOutputFile.CStr()); string dummy, url; map<string, unsigned int> infectionTimeHash; unsigned int infectionTime = 0; for (int i = 0; i < entries; ++i) { // Read through each "infected" URL. getline(memetracker, line); istringstream iss(line); // These fields of the cascade entry ar not important. iss >> dummy >> dummy >> dummy >> dummy; iss >> url; // Parse the URL and identify the host website. uri::uri instance(url); assert(instance.is_valid()); // If node not in network or already infected, skip. if (urlToNodeIdHash.find(instance.host()) == urlToNodeIdHash.end() || infectionTimeHash.find(instance.host()) != infectionTimeHash.end()) continue; infectionTimeHash[instance.host()] = infectionTime++; // Dump as pair of <nodeId, infectionTime>. dumpStream << urlToNodeIdHash[instance.host()] << " " << infectionTimeHash[instance.host()] << endl; } return 0; }
// Test node, edge creation TEST(TUNGraph, ManipulateNodesEdges) { int NNodes = 10000; int NEdges = 100000; const char *FName = "test.graph"; PUNGraph Graph; PUNGraph Graph1; PUNGraph Graph2; int i; int n; int NCount; int x,y; int Deg, InDeg, OutDeg; Graph = TUNGraph::New(); EXPECT_EQ(1,Graph->Empty()); // create the nodes for (i = 0; i < NNodes; i++) { Graph->AddNode(i); } EXPECT_EQ(0,Graph->Empty()); EXPECT_EQ(NNodes,Graph->GetNodes()); // create random edges NCount = NEdges; while (NCount > 0) { x = (long) (drand48() * NNodes); y = (long) (drand48() * NNodes); // Graph->GetEdges() is not correct for the loops (x == y), // skip the loops in this test if (x != y && !Graph->IsEdge(x,y)) { n = Graph->AddEdge(x, y); NCount--; } } EXPECT_EQ(NEdges,Graph->GetEdges()); EXPECT_EQ(0,Graph->Empty()); EXPECT_EQ(1,Graph->IsOk()); for (i = 0; i < NNodes; i++) { EXPECT_EQ(1,Graph->IsNode(i)); } EXPECT_EQ(0,Graph->IsNode(NNodes)); EXPECT_EQ(0,Graph->IsNode(NNodes+1)); EXPECT_EQ(0,Graph->IsNode(2*NNodes)); // nodes iterator NCount = 0; for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } EXPECT_EQ(NNodes,NCount); // edges per node iterator NCount = 0; for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NCount++; } } EXPECT_EQ(NEdges*2,NCount); // edges iterator NCount = 0; for (TUNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { NCount++; } EXPECT_EQ(NEdges,NCount); // node degree for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { Deg = NI.GetDeg(); InDeg = NI.GetInDeg(); OutDeg = NI.GetOutDeg(); EXPECT_EQ(Deg,InDeg); EXPECT_EQ(Deg,OutDeg); } // assignment Graph1 = TUNGraph::New(); *Graph1 = *Graph; EXPECT_EQ(NNodes,Graph1->GetNodes()); EXPECT_EQ(NEdges,Graph1->GetEdges()); EXPECT_EQ(0,Graph1->Empty()); EXPECT_EQ(1,Graph1->IsOk()); // saving and loading { TFOut FOut(FName); Graph->Save(FOut); FOut.Flush(); } { TFIn FIn(FName); Graph2 = TUNGraph::Load(FIn); } EXPECT_EQ(NNodes,Graph2->GetNodes()); EXPECT_EQ(NEdges,Graph2->GetEdges()); EXPECT_EQ(0,Graph2->Empty()); EXPECT_EQ(1,Graph2->IsOk()); // remove all the nodes and edges for (i = 0; i < NNodes; i++) { n = Graph->GetRndNId(); Graph->DelNode(n); } EXPECT_EQ(0,Graph->GetNodes()); EXPECT_EQ(0,Graph->GetEdges()); EXPECT_EQ(1,Graph->IsOk()); EXPECT_EQ(1,Graph->Empty()); Graph1->Clr(); EXPECT_EQ(0,Graph1->GetNodes()); EXPECT_EQ(0,Graph1->GetEdges()); EXPECT_EQ(1,Graph1->IsOk()); EXPECT_EQ(1,Graph1->Empty()); }