//GDF Edge Formatting std::string GDFEdges(const PNGraph & graph){ std::string edges; for (PNGraph::TObj::TEdgeI EI = graph->BegEI(); EI < graph->EndEI(); EI++){ edges.append(Helper::intToString(EI.GetSrcNId())); edges.append(", "); edges.append(Helper::intToString(EI.GetDstNId())); edges.append("\n"); } return edges; }
void node2vec(PNGraph& InNet, double& ParamP, double& ParamQ, int& Dimensions, int& WalkLen, int& NumWalks, int& WinSize, int& Iter, bool& Verbose, TIntFltVH& EmbeddingsHV) { PWNet NewNet = PWNet::New(); for (TNGraph::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) { if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); } if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); } NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), 1.0); } node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter, Verbose, EmbeddingsHV); }
void gdf(PNGraph Graph){ std::ofstream graph; graph.open("graph.gdf"); graph << "nodedef>name VARCHAR\n"; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) graph << NI.GetId() << ",\n"; graph << "edgedef>node1 VARCHAR,node2 VARCHAR\n"; for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) graph << EI.GetSrcNId() << "," << EI.GetDstNId() << "\n"; graph.close(); }
uint64 TGraphEnumUtils::GraphId(const PNGraph &G) { int nodes = G->GetNodes(); uint64 id=0; for(TNGraph::TEdgeI it=G->BegEI(); it<G->EndEI(); it++) { int srcId = it.GetSrcNId(); int dstId = it.GetDstNId(); // id += TMath::Pow2(srcId*nodes + dstId); } // return id; }
//GEXF Edge Formatting std::string GEXFEdges(const PNGraph & graph){ std::string edges; int i = 1; for (PNGraph::TObj::TEdgeI EI = graph->BegEI(); EI < graph->EndEI(); EI++, ++i){ edges.append("<edge id=\""); edges.append(Helper::intToString(i)); edges.append("\" source=\""); edges.append(Helper::intToString(EI.GetSrcNId())); edges.append("\" target=\""); edges.append(Helper::intToString(EI.GetDstNId())); edges.append("\" />\n"); } return edges; }
//GraphSON Edge Formatting std::string GraphSONEdges(const PNGraph & graph){ std::string edges; for (PNGraph::TObj::TEdgeI EI = graph->BegEI(); EI < graph->EndEI(); ) { edges.append("{ \"source\": \"" ); edges.append(Helper::intToString(EI.GetSrcNId())); edges.append("\", \"target\": \""); edges.append(Helper::intToString(EI.GetDstNId())); edges.append("\" }"); if (EI++ == graph->EndEI()) edges.append(" ]\n"); else edges.append(",\n"); } edges.append("} }"); return edges; }
void TGraphEnumUtils::GetNormalizedGraph(const PNGraph &G, PNGraph &nG) { //Get bijective map from original node ids to normalized node ids(0,1,2,...) THash<TInt,TInt> map; GetNormalizedMap(G, map); //Add nodes for(int i=0; i<G->GetNodes(); i++) nG->AddNode(i); //Add edges for(TNGraph::TEdgeI eIt=G->BegEI(); eIt<G->EndEI(); eIt++) { int srcId = eIt.GetSrcNId(); int dstId = eIt.GetDstNId(); // int mSrcId = map.GetDat(srcId); int mDstId = map.GetDat(dstId); // nG->AddEdge(mSrcId, mDstId); } }
void graphson(PNGraph Graph){ std::ofstream graph; graph.open("graph.json"); graph << "{\n"; graph << " \"graph\": {\n"; graph << " " << "\"mode\": \"NORMAL\", \n"; graph << " " << "\"vertices\": [\n"; int i = 0; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { i++; graph << " " << "{\n"; graph << " " << "\"_id\": \"" << NI.GetId() << "\",\n"; graph << " " << "\"_type\": \"vertex\"\n"; if (i == Graph->GetNodes()) { graph << " " << "}\n"; } else { graph << " " << "},\n"; } } graph << " " << "],\n"; graph << " " << "\"edges\": [\n"; i = 0; printf("Edges: %d", Graph->GetEdges()); for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { graph << " " << "{\n" << " " << "\"_id\": \"" << i++ << "\",\n" << " " << "\"_type\": \"edge\"\n" << " " << "\"_outV\": \"" << EI.GetSrcNId() << "\"\n" << " " << "\"_inV\": \""<< EI.GetDstNId() << "\"\n"; if (i == Graph->GetEdges()) graph << " " << "}\n"; else graph << " " << "},\n"; } graph << " " << "]\n"; graph << " " << "}\n"; graph << "}\n"; graph.close(); }
void graphMl(PNGraph Graph){ std::ofstream graphml; graphml.open("graph.graphml"); graphml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; graphml << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n"; graphml << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; graphml << " xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n"; graphml << " http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd\">\n"; graphml << " <graph id=\"G\" edgedefault=\"directed\">\n"; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) graphml << " <node id=\"" << NI.GetId() << "\"/>\n"; int i = 1; for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { graphml << " <edge id=\"e" << i++ << "\" directed=\"true" << "\" source=\"" << EI.GetSrcNId() << "\" target=\"" << EI.GetDstNId() << "\"/>\n"; } graphml << " </graph>\n"; graphml << "</graphml>\n"; graphml.close(); }
void TGraphEnumUtils::GetIsoGraphs(const PNGraph &G, TVec<PNGraph> &isoG) { int nodes = G->GetNodes(); // TIntV v(nodes); for(int i=0; i<nodes; i++) v[i]=i; TVec<TIntV> perms; GetPermutations(v, 0, perms); isoG.Gen(perms.Len()); // for(int i=0; i<perms.Len(); i++) { isoG[i] = TNGraph::New(); //Add nodes for(int j=0; j<nodes; j++) isoG[i]->AddNode(j); //Add edges for(TNGraph::TEdgeI eIt=G->BegEI(); eIt<G->EndEI(); eIt++) { int srcId = eIt.GetSrcNId(); int dstId = eIt.GetDstNId(); // int pSrcId = perms[i][srcId]; int pDstId = perms[i][dstId]; // isoG[i]->AddEdge(pSrcId, pDstId); } } }
void gexf(PNGraph Graph){ std::ofstream graph; graph.open("graph.gexf"); graph << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" << "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">\n" << " <graph mode=\"static\" defaultedgetype=\"directed\">\n" << " <nodes>\n"; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) graph << " <node id=\"" << NI.GetId() << "\"/>\n"; graph << " </nodes>\n"; graph << " <edges>\n"; int i = 1; for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) graph << " <edge id=\"e" << i++ << "\" directed=\"true" << "\" source=\"" << EI.GetSrcNId() << "\" target=\"" << EI.GetDstNId() << "\"/>\n"; graph << " </edges>\n" << " </graph>\n" << "</gexf>\n"; graph.close(); }
// Test node, edge creation void ManipulateNodesEdges() { int NNodes = 10000; int NEdges = 100000; const char *FName = "demo.graph.dat"; PNGraph Graph; PNGraph Graph1; PNGraph Graph2; int i; int n; int NCount; int ECount1; int ECount2; int x,y; bool t; Graph = TNGraph::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 = rand() % NNodes; y = rand() % 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 (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } // get all the edges for all the nodes ECount1 = 0; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { ECount1++; } } // get all the edges directly ECount2 = 0; for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { ECount2++; } printf("ManipulateNodesEdges:Graph, nodes %d, edges1 %d, edges2 %d\n", NCount, ECount1, ECount2); // assignment Graph1 = TNGraph::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 = TNGraph::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); }
// Test node, edge creation TEST(TNGraph, ManipulateNodesEdges) { int NNodes = 10000; int NEdges = 100000; const char *FName = "test.graph.dat"; PNGraph Graph; PNGraph Graph1; PNGraph Graph2; int i; int n; int NCount; int x,y; int Deg, InDeg, OutDeg; Graph = TNGraph::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 (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NCount++; } EXPECT_EQ(NNodes,NCount); // edges per node iterator NCount = 0; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { NCount++; } } EXPECT_EQ(NEdges,NCount); // edges iterator NCount = 0; for (TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { NCount++; } EXPECT_EQ(NEdges,NCount); // node degree for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { Deg = NI.GetDeg(); InDeg = NI.GetInDeg(); OutDeg = NI.GetOutDeg(); EXPECT_EQ(Deg,InDeg+OutDeg); } // assignment Graph1 = TNGraph::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 = TNGraph::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()); }