void GetSngVec(const PNGraph& Graph, TFltV& LeftSV, TFltV& RightSV) { const int Nodes = Graph->GetNodes(); TFltVV LSingV, RSingV; TFltV SngValV; if (Nodes < 500) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TIntH NodeIdH; // create adjecency matrix for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); } } else { // Lanczos TNGraphMtx GraphMtx(Graph); TSparseSVD::LanczosSVD(GraphMtx, 1, 8, ssotFull, SngValV, LSingV, RSingV); } TFlt MxSngVal = TFlt::Mn; int ValN = 0; for (int i = 0; i < SngValV.Len(); i++) { if (MxSngVal < SngValV[i]) { MxSngVal = SngValV[i]; ValN = i; } } LSingV.GetCol(ValN, LeftSV); RSingV.GetCol(ValN, RightSV); IsAllValVNeg(LeftSV, true); IsAllValVNeg(RightSV, true); }
double DirectedModularity(PNGraph& graph, std::vector<int>& communities) { if (graph->GetNodes() != communities.size()) { throw std::logic_error("Number of nodes does not match community size."); } int num_edges = graph->GetEdges(); double score = 0.0; int num_unique = 10; std::map<int, double> outdeg_sums; std::map<int, double> indeg_sums; for (TNGraph::TNodeI node = graph->BegNI(); node < graph->EndNI(); node++) { int comm = communities[node.GetId()]; outdeg_sums[comm] += node.GetOutDeg(); indeg_sums[comm] += node.GetInDeg(); } for (auto& kv : outdeg_sums) { score -= (kv.second / num_edges) * indeg_sums[kv.first]; } for (TNGraph::TNodeI node = graph->BegNI(); node < graph->EndNI(); node++) { int node_ID = node.GetId(); for (int e = 0; e < node.GetOutDeg(); ++e) { int nbr = node.GetOutNId(e); if (communities[node_ID] == communities[nbr]) { score += 1.0; } } } return score / num_edges; }
// Save and load directed, undirected and multi-graphs as list of edges, where nodes are ids void SaveLoadEdgeList() { const int NNodes = 500; const int NEdges = 2000; const char *FName = "demo.graph.dat"; const char *Desc = "Randomly generated graph for input/output."; PNGraph GOut, GIn; GOut = GenRndGnm<PNGraph>(NNodes, NEdges); // Output node IDs as numbers SaveEdgeList(GOut, FName, Desc); // Load edge list GIn = LoadEdgeList<PNGraph>(FName); // Verify all nodes exist in input and output graphs THashSet<TInt> OutNIdH, InNIdH; for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { // Nodes that do not have edges are left off during input if (NI.GetDeg() > 0) { OutNIdH.AddKey(NI.GetId()); } } for (TNGraph::TNodeI NI = GIn->BegNI(); NI < GIn->EndNI(); NI++) { InNIdH.AddKey(NI.GetId()); } PrintGStats<PNGraph>("EdgeList - Out", GOut); PrintGStats<PNGraph>("EdgeList - In", GIn); }
// Save directed, undirected and multi-graphs in GraphVizp .DOT format void IOGViz() { const int NNodes = 500; const int NEdges = 2000; const char *FName1 = "demo1.dot.dat", *FName2 = "demo2.dot.dat"; const char *Desc = "Randomly generated GgraphVizp for input/output."; PNGraph GOut; // Can be PNEGraph or PUNGraph GOut = GenRndGnm<PNGraph>(NNodes, NEdges); SaveGViz(GOut, FName1); // Output node IDs as numbers TIntStrH NIdLabelH; // Generate labels for random graph for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { NIdLabelH.AddDat(NI.GetId(), TStr::Fmt("Node%d", NI.GetId())); } SaveGViz(GOut, FName2, Desc, NIdLabelH); PrintGStats("IOGViz - In", GOut); }
void TNetInfBs::SaveCascades(const TStr& OutFNm) { TFOut FOut(OutFNm); // write nodes to file for (TNGraph::TNodeI NI = GroundTruth->BegNI(); NI < GroundTruth->EndNI(); NI++) { FOut.PutStr(TStr::Fmt("%d,%d\r\n", NI.GetId(), NI.GetId())); // nodes } FOut.PutStr("\r\n"); // write cascades to file for (int i=0; i<CascV.Len(); i++) { TCascade &C = CascV[i]; int j = 0; for (THash<TInt, THitInfo>::TIter NI = C.NIdHitH.BegI(); NI < C.NIdHitH.EndI(); NI++, j++) { if (j > 0) FOut.PutStr(TStr::Fmt(",%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); else FOut.PutStr(TStr::Fmt("%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); } if (C.Len() >= 1) FOut.PutStr(TStr::Fmt("\r\n")); } }
void OnlyD3CEdgesNoBack(PNGraph& dir_graph, PNGraph& d3c_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(); d3c_graph->AddNode(curr_node); } for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI(); node++) { int curr_node = node.GetId(); auto curr_node_it = dir_graph->GetNI(curr_node); for (int out_edge = 0; out_edge < curr_node_it.GetOutDeg(); ++out_edge) { int out_node = curr_node_it.GetOutNId(out_edge); for (int in_edge = 0; in_edge < curr_node_it.GetInDeg(); ++in_edge) { int in_node = curr_node_it.GetInNId(in_edge); if (dir_graph->IsEdge(out_node, in_node) && out_node != in_node) { if (!d3c_graph->IsEdge(in_node, out_node) && !d3c_graph->IsEdge(curr_node, in_node) && !d3c_graph->IsEdge(out_node, curr_node)) { if (!d3c_graph->IsEdge(out_node, in_node)) { d3c_graph->AddEdge(out_node, in_node); } if (!d3c_graph->IsEdge(in_node, curr_node)) { d3c_graph->AddEdge(in_node, curr_node); } if (!d3c_graph->IsEdge(curr_node, out_node)) { d3c_graph->AddEdge(curr_node, out_node); } } } } } } }
void OnlyD3CEdges(PNGraph& dir_graph, PNGraph& d3c_graph, bool recip_edges) { // 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(); d3c_graph->AddNode(curr_node); } for (TNGraph::TNodeI node = dir_graph->BegNI(); node < dir_graph->EndNI(); node++) { int curr_node = node.GetId(); auto curr_node_it = dir_graph->GetNI(curr_node); for (int out_edge = 0; out_edge < curr_node_it.GetOutDeg(); ++out_edge) { int out_node = curr_node_it.GetOutNId(out_edge); for (int in_edge = 0; in_edge < curr_node_it.GetInDeg(); ++in_edge) { int in_node = curr_node_it.GetInNId(in_edge); if (out_node == in_node && !recip_edges) { continue; } if (dir_graph->IsEdge(out_node, in_node) || recip_edges) { if (!d3c_graph->IsEdge(out_node, in_node)) { d3c_graph->AddEdge(out_node, in_node); } if (!d3c_graph->IsEdge(in_node, curr_node)) { d3c_graph->AddEdge(in_node, curr_node); } if (!d3c_graph->IsEdge(curr_node, out_node)) { d3c_graph->AddEdge(curr_node, out_node); } } } } } #ifdef _VERBOSE_ std::cout << "Original graph edge count: " << dir_graph->GetEdges() << std::endl << "D3C graph edge count: " << d3c_graph->GetEdges() << std::endl; #endif }
void TGraphCascade::Print(const TIntV& SortV) { printf("graph start:\n"); if (SortV.Empty()) { for (TNGraph::TNodeI NI = Graph.BegNI(); NI < Graph.EndNI(); NI++) { printf("%s %d %d\n", NodeIdNmH.GetDat(NI.GetId()).CStr(), NI.GetId(), NodeNmIdH.GetDat(NodeIdNmH.GetDat(NI.GetId())).Val); } } else { for (int NodeN = 0; NodeN < SortV.Len(); NodeN++) { printf("%s %d\n", NodeIdNmH.GetDat(SortV[NodeN]).CStr(), SortV[NodeN].Val); } } printf("graph end\n"); }
void TGraphKey::TakeSig(const PNGraph& Graph, const int& MnSvdGraph, const int& MxSvdGraph) { const int Edges = Graph->GetEdges(); Nodes = Graph->GetNodes(); VariantId = 0; SigV.Gen(2+Nodes, 0); // degree sequence TIntPrV DegV(Nodes, 0); for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { DegV.Add(TIntPr(NodeI.GetInDeg(), NodeI.GetOutDeg())); } DegV.Sort(false); SigV.Add(TFlt(Nodes)); SigV.Add(TFlt(Edges)); for (int i = 0; i < DegV.Len(); i++) { SigV.Add(DegV[i].Val1()); SigV.Add(DegV[i].Val2()); } // singular values signature // it turns out that it is cheaper to do brute force isomorphism // checking than to calculate SVD and then check isomorphism if (Nodes >= MnSvdGraph && Nodes < MxSvdGraph) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TFltV SngValV; TFltVV LSingV, RSingV; TIntH NodeIdH; // create adjecency matrix for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d): SngValV.Len():%d\n", Nodes(), Graph->GetEdges(), SngValV.Len()); } // round singular values SngValV.Sort(false); for (int i = 0; i < SngValV.Len(); i++) { SigV.Add(TMath::Round(SngValV[i], RoundTo)); } } //printf("SIG:\n"); for (int i = 0; i < SigV.Len(); i++) { printf("\t%f\n", SigV[i]); } SigV.Pack(); }
void GetSngVals(const PNGraph& Graph, const int& SngVals, TFltV& SngValV) { const int Nodes = Graph->GetNodes(); IAssert(SngVals > 0); if (Nodes < 100) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TFltVV LSingV, RSingV; TIntH NodeIdH; // create adjecency matrix for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId()) + 1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e)) + 1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); } } else { // Lanczos TNGraphMtx GraphMtx(Graph); int CalcVals = int(2*SngVals); //if (CalcVals > Nodes) { CalcVals = int(2*Nodes); } //if (CalcVals > Nodes) { CalcVals = Nodes; } //while (SngValV.Len() < SngVals && CalcVals < 10*SngVals) { try { if (SngVals > 4) { TSparseSVD::SimpleLanczosSVD(GraphMtx, 2*SngVals, SngValV, false); } else { TFltVV LSingV, RSingV; // this is much more precise, but also much slower TSparseSVD::LanczosSVD(GraphMtx, SngVals, 3*SngVals, ssotFull, SngValV, LSingV, RSingV); } } catch(...) { printf("\n ***EXCEPTION: TRIED %d GOT %d values** \n", 2*SngVals, SngValV.Len()); } if (SngValV.Len() < SngVals) { printf(" ***TRIED %d GOT %d values** \n", CalcVals, SngValV.Len()); } // CalcVals += SngVals; //} } SngValV.Sort(false); //if (SngValV.Len() > SngVals) { // SngValV.Del(SngVals, SngValV.Len()-1); } //else { // while (SngValV.Len() < SngVals) SngValV.Add(1e-6); } //IAssert(SngValV.Len() == SngVals); }
// renumbers nodes void TGraphKey::TakeGraph(const PNGraph& Graph) { TIntH NodeIdH; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NodeIdH.AddKey(NI.GetId()); } 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(); }
// Saves and loads a graph in a MATLAB sparse matrix format void IOMatlabSparseMtx() { const int NNodes = 500; const int NEdges = 2000; const char *FName = "demo.matlab.dat"; PNGraph GOut, GIn; GOut = GenRndGnm<PNGraph>(NNodes, NEdges); SaveMatlabSparseMtx(GOut, FName); GIn = TNGraph::New(); GIn->Reserve(NNodes, NEdges); // Read-in Matlab-file FILE *F = fopen(FName, "r"); while (! feof(F)) { int Src, Dst, Edge; fscanf(F, "%d %d %d\n", &Src, &Dst, &Edge); Src--; Dst--; // SNAP graphs start at Node Ids of 0 if (not GIn->IsNode(Src)) { GIn->AddNode(Src); } if (not GIn->IsNode(Dst)) { GIn->AddNode(Dst); } GIn->AddEdge(Src, Dst); } fclose(F); // Verify all nodes exist in input and output graphs (and no more) THashSet<TInt> OutNIdH, InNIdH; for (TNGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) { // Nodes that do not have edges are left off during input if (NI.GetDeg() > 0) { OutNIdH.AddKey(NI.GetId()); } } for (TNGraph::TNodeI NI = GIn->BegNI(); NI < GIn->EndNI(); NI++) { InNIdH.AddKey(NI.GetId()); } PrintGStats("Matlab - Out", GOut); PrintGStats("Matlab - In", GIn); }
void TIncrementalClustering::KeepAtMostOneChildPerNode(PNGraph& G, TQuoteBase *QB, TDocBase *DB) { TIntSet::TIter EndNode = AffectedNodes.EndI(); for (TIntSet::TIter NodeId = AffectedNodes.BegI(); NodeId < EndNode; NodeId++) { TNGraph::TNodeI Node = G->GetNI(NodeId.GetKey()); TQuote SourceQuote; if (QB->GetQuote(Node.GetId(), SourceQuote)) { TInt NodeDegree = Node.GetOutDeg(); if (NodeDegree > 1) { TFlt MaxScore = 0; TInt MaxNodeId = 0; TIntV NodeV; // first pass: check to see if we are pointing to any old nodes - if so, they get higher // priority over the new ones for edge selection. bool ContainsOldNode = false; for (int i = 0; i < NodeDegree; ++i) { if (!NewQuotes.IsKey(Node.GetOutNId(i))) { ContainsOldNode = true; } } // modified edge selection: filter out new nodes if old ones exist. for (int i = 0; i < NodeDegree; ++i) { TInt CurNode = Node.GetOutNId(i); NodeV.Add(CurNode); TQuote DestQuote; if (QB->GetQuote(CurNode, DestQuote)) { TFlt EdgeScore = 0; if (!ContainsOldNode || !NewQuotes.IsKey(Node.GetOutNId(i))) { EdgeScore = ComputeEdgeScore(SourceQuote, DestQuote, DB); } if (EdgeScore > MaxScore) { MaxScore = EdgeScore; MaxNodeId = CurNode; } } } // remove all other edges, backwards to prevent indexing fail for (int i = 0; i < NodeV.Len(); i++) { if (NodeV[i] != MaxNodeId) { G->DelEdge(Node.GetId(), NodeV[i]); } } //printf("Out degree: %d out of %d\n", Node.GetOutDeg(), NodeDegree.Val); } } } fprintf(stderr, "finished deleting edges\n"); }
TIntNNet TMultimodalGraphImplB::GetSubGraph(const TIntV ModeIds) const { TIntNNet SubGraph = TIntNNet(); for (THash<TInt,TInt>::TIter CurI = NodeToModeMapping.BegI(); CurI < NodeToModeMapping.EndI(); CurI++) { if (ModeIds.IsIn(CurI.GetDat())) { SubGraph.AddNode(CurI.GetKey(), CurI.GetDat()); } } for (int ModeIdx1 = 0; ModeIdx1 < ModeIds.Len(); ModeIdx1++) { int ModeId1 = ModeIds.GetVal(ModeIdx1); for (int ModeIdx2 = 0; ModeIdx2 < ModeIds.Len(); ModeIdx2++) { int ModeId2 = ModeIds.GetVal(ModeIdx2); TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(ModeId1, ModeId2); if (!Graphs.IsKey(ModeIdsKey)) { continue; } const TNGraph& Graph = Graphs.GetDat(ModeIdsKey); for (TNGraph::TNodeI it = Graph.BegNI(); it < Graph.EndNI(); it++) { for (int e = 0; e < it.GetOutDeg(); e++) { SubGraph.AddEdge(it.GetId(), it.GetOutNId(e)); } } } } printf("Number of nodes in SubGraph: %d...\n", SubGraph.GetNodes()); printf("Number of edges in SubGraph: %d...\n", SubGraph.GetEdges()); return SubGraph; }
void TempMotifCounter::GetAllNodes(TIntV& nodes) { nodes = TIntV(); for (TNGraph::TNodeI it = static_graph_->BegNI(); it < static_graph_->EndNI(); it++) { nodes.Add(it.GetId()); } }
///////////////////////////////////////////////// // Trawling the web for emerging communities // graph, left points to right TTrawling::TTrawling(const PNGraph& Graph, const int& MinSupport) : MinSup(MinSupport) { TIntH ItemCntH; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { IAssert(NI.GetOutDeg()==0 || NI.GetInDeg()==0); // edges only point from left to right if (NI.GetOutDeg()==0) { continue; } for (int e = 0; e < NI.GetOutDeg(); e++) { ItemCntH.AddDat(NI.GetOutNId(e)) += 1; } } TIntV RightV; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { IAssert(NI.GetOutDeg()==0 || NI.GetInDeg()==0); // edges only point from left to right if (NI.GetOutDeg()==0) { continue; } RightV.Clr(false); for (int e = 0; e < NI.GetOutDeg(); e++) { const int itm = NI.GetOutNId(e); // only include items that already are above minimum support if (ItemCntH.GetDat(itm) >= MinSup) { RightV.Add(itm); } } if (! RightV.Empty()) { NIdSetH.AddDat(NI.GetId(), RightV); } } // for (int n = 0; n < NIdSetH.Len(); n++) { const TIntV& Set = NIdSetH[n]; for (int s = 0; s < Set.Len(); s++) { SetNIdH.AddDat(Set[s]).Add(n); } } }
void TSubGraphsEnum::Gen2Graphs() { // singe edge sub-graphs SgV.Gen(NGraph->GetEdges(), 0); TSimpleGraph SimpleG; TIntPrV& EdgeV = SimpleG.GetEdgeV(); EdgeV.Gen(1); for (TNGraph::TNodeI NI = NGraph->BegNI(); NI < NGraph->EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { EdgeV[0] = TIntPr(NI.GetId(), NI.GetOutNId(e)); SgV.Add(SimpleG); } } SgV.Sort(); // two edge sub-graphs EdgeV.Gen(2); for (int g1 = 0; g1 < SgV.Len()-1; g1++) { const TIntPr& E1 = SgV[g1].GetEdgeV()[0]; for (int g2 = g1+1; g2 < SgV.Len(); g2++) { const TIntPr& E2 = SgV[g2].GetEdgeV()[0]; if (E1.Val2 == E2.Val1 || E1.Val1 == E2.Val2 || E1.Val1 == E2.Val1 || E1.Val2 == E2.Val2) { EdgeV[0] = TMath::Mn(E1, E2); EdgeV[1] = TMath::Mx(E1, E2); SimpleG.Dump(); NextSgV.Add(SimpleG); } } } SgV.MoveFrom(NextSgV); }
void GetSngVec(const PNGraph& Graph, const int& SngVecs, TFltV& SngValV, TVec<TFltV>& LeftSV, TVec<TFltV>& RightSV) { const int Nodes = Graph->GetNodes(); SngValV.Clr(); LeftSV.Clr(); RightSV.Clr(); TFltVV LSingV, RSingV; if (Nodes < 100) { // perform full SVD TFltVV AdjMtx(Nodes+1, Nodes+1); TIntH NodeIdH; // create adjecency matrix (1-based) for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { NodeIdH.AddKey(NodeI.GetId()); } for (TNGraph::TNodeI NodeI = Graph->BegNI(); NodeI < Graph->EndNI(); NodeI++) { const int NodeId = NodeIdH.GetKeyId(NodeI.GetId())+1; for (int e = 0; e < NodeI.GetOutDeg(); e++) { const int DstNId = NodeIdH.GetKeyId(NodeI.GetOutNId(e))+1; // no self edges if (NodeId != DstNId) AdjMtx.At(NodeId, DstNId) = 1; } } try { // can fail to converge but results seem to be good TSvd::Svd1Based(AdjMtx, LSingV, SngValV, RSingV); } catch(...) { printf("\n***No SVD convergence: G(%d, %d)\n", Nodes, Graph->GetEdges()); } } else { // Lanczos TNGraphMtx GraphMtx(Graph); TSparseSVD::LanczosSVD(GraphMtx, SngVecs, 2*SngVecs, ssotFull, SngValV, LSingV, RSingV); //TGAlg::SaveFullMtx(Graph, "adj_mtx.txt"); //TLAMisc::DumpTFltVVMjrSubMtrx(LSingV, LSingV.GetRows(), LSingV.GetCols(), "LSingV2.txt"); // save MTX } TFltIntPrV SngValIdV; for (int i = 0; i < SngValV.Len(); i++) { SngValIdV.Add(TFltIntPr(SngValV[i], i)); } SngValIdV.Sort(false); SngValV.Sort(false); for (int v = 0; v < SngValIdV.Len(); v++) { LeftSV.Add(); LSingV.GetCol(SngValIdV[v].Val2, LeftSV.Last()); RightSV.Add(); RSingV.GetCol(SngValIdV[v].Val2, RightSV.Last()); } IsAllValVNeg(LeftSV[0], true); IsAllValVNeg(RightSV[0], true); }
// 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 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); } } } }
void TForestFire::InfectRnd(const int& NInfect) { IAssert(NInfect < Graph->GetNodes()); TIntV NIdV(Graph->GetNodes(), 0); for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { NIdV.Add(NI.GetId()); } NIdV.Shuffle(Rnd); InfectNIdV.Gen(NInfect, 0); for (int i = 0; i < NInfect; i++) { InfectNIdV.Add(NIdV[i]); } }
void TFfGGen::PlotFireSize(const TStr& FNmPref, const TStr& DescStr) { TGnuPlot GnuPlot("fs."+FNmPref, TStr::Fmt("%s. Fire size. G(%d, %d)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges())); GnuPlot.SetXYLabel("Vertex id (iterations)", "Fire size (node out-degree)"); TFltPrV IdToOutDegV; for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { IdToOutDegV.Add(TFltPr(NI.GetId(), NI.GetOutDeg())); } IdToOutDegV.Sort(); GnuPlot.AddPlot(IdToOutDegV, gpwImpulses, "Node out-degree"); GnuPlot.SavePng(); }
void TNetInfBs::SaveGroundTruth(const TStr& OutFNm) { TFOut FOut(OutFNm); // write nodes to file for (TNGraph::TNodeI NI = GroundTruth->BegNI(); NI < GroundTruth->EndNI(); NI++) { FOut.PutStr(TStr::Fmt("%d,%d\r\n", NI.GetId(), NI.GetId())); // nodes } FOut.PutStr("\r\n"); // write edges to file (not allowing self loops in the network) for (TNGraph::TEdgeI EI = GroundTruth->BegEI(); EI < GroundTruth->EndEI(); EI++) { // not allowing self loops in the Kronecker network if (EI.GetSrcNId() != EI.GetDstNId()) { if (Alphas.IsKey(TIntPr(EI.GetSrcNId(), EI.GetDstNId()))) FOut.PutStr(TStr::Fmt("%d,%d,%f\r\n", EI.GetSrcNId(), EI.GetDstNId(), Alphas.GetDat(TIntPr(EI.GetSrcNId(), EI.GetDstNId())).Val)); else FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId())); } } }
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(); }
void TSubGraphsEnum::RecurBfs(const int& NId, const int& Depth, TSimpleGraph& PrevG) { if (Depth == 0) { TIntPrV& EdgeV = PrevG(); EdgeV.Sort(); for (int i = 1; i < EdgeV.Len(); i++) { if (EdgeV[i-1] == EdgeV[i]) { return; } } SgV.Add(PrevG); return; } const TNGraph::TNodeI NI = NGraph ->GetNI(NId); for (int e = 0; e < NI.GetOutDeg(); e++) { TSimpleGraph CurG = PrevG; CurG.AddEdge(NI.GetId(), NI.GetOutNId(e)); RecurBfs(NI.GetOutNId(e), Depth-1, CurG); } for (int e = 0; e < NI.GetInDeg(); e++) { TSimpleGraph CurG = PrevG; CurG.AddEdge(NI.GetInNId(e), NI.GetId()); RecurBfs(NI.GetInNId(e), Depth-1, CurG); } }
void TNetInfBs::Init() { THash<TInt, TIntV> CascPN; Graph = TNGraph::New(); // reset vectors EdgeGainV.Clr(); CascPerEdge.Clr(); PrecisionRecall.Clr(); for (int c = 0; c < CascV.Len(); c++) { for (int i = 0; i < CascV[c].Len(); i++) { if (!Graph->IsNode(CascV[c].GetNode(i))) Graph->AddNode(CascV[c].GetNode(i)); if (!CascPN.IsKey(CascV[c].GetNode(i))) CascPN.AddDat(CascV[c].GetNode(i)) = TIntV(); CascPN.GetDat(CascV[c].GetNode(i)).Add(c); } CascV[c].InitProb(); } // only add edges that make sense (i.e., at least once coherent in time) for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { TIntV &Cascs = CascPN.GetDat(NI.GetId()); for (int c = 0; c < Cascs.Len(); c++) { for (int i=0; i < CascV[Cascs[c]].Len(); i++) { if (CascV[Cascs[c]].GetNode(i)==NI.GetId()) continue; if (CascV[Cascs[c]].GetTm(CascV[Cascs[c]].GetNode(i)) < CascV[Cascs[c]].GetTm(NI.GetId()) ) { if (!CascPerEdge.IsKey(TIntPr(CascV[Cascs[c]].GetNode(i), NI.GetId()))) { EdgeGainV.Add(TPair<TFlt, TIntPr>(TFlt::Mx, TIntPr(CascV[Cascs[c]].GetNode(i), NI.GetId()))); CascPerEdge.AddDat(TIntPr(CascV[Cascs[c]].GetNode(i), NI.GetId())) = TIntV(); } // Add cascade to hash of cascades per edge (to implement localized update) CascPerEdge.GetDat(TIntPr(CascV[Cascs[c]].GetNode(i), NI.GetId())).Add(Cascs[c]); } } } } }
PJsonVal TGraphCascade::GetGraph() const { PJsonVal G = TJsonVal::NewObj(); for (TNGraph::TNodeI NI = Graph.BegNI(); NI < Graph.EndNI(); NI++) { TStr NodeNm = NodeIdNmH.GetDat(NI.GetId()); PJsonVal ParentsArr = TJsonVal::NewArr(); int InDeg = NI.GetInDeg(); for (int ParentN = 0; ParentN < InDeg; ParentN++) { TStr ParentNm = NodeIdNmH.GetDat(NI.GetInNId(ParentN)); ParentsArr->AddToArr(ParentNm); } G->AddToObj(NodeNm, ParentsArr); } return G; }
void analyzeSimNetProps(TStr messGraph, TStr netGraph) { // Make graphs PNGraph eGraph = TSnap::LoadEdgeListStr<PNGraph>(netGraph, 0, 1); PNGraph mGraph = TSnap::LoadEdgeListStr<PNGraph>(messGraph, 0, 1); PNGraph randGraph = TSnap::GenRndGnm<PNGraph>(mGraph->GetNodes(), mGraph->GetEdges(), true, TInt::Rnd); // Induce network graph from the entire network based on message graph TIntV NIdV; for (TNGraph::TNodeI NI = mGraph->BegNI(); NI < mGraph->EndNI(); NI++) NIdV.AddUnique(NI.GetId()); PNGraph indGraph = TSnap::GetSubGraph<PNGraph>(eGraph, NIdV); //printf("%s:: ", messGraph.CStr()); printf("nodes %d; ", indGraph->GetNodes()); //printf("induced edges %d, random edges %d\n", indGraph->GetEdges(), randGraph->GetEdges()); printf("%d\t%d\t%d\n", indGraph->GetNodes(), indGraph->GetEdges(), mGraph->GetEdges()); }
void TSubGraphsEnum::RecurBfs(const int& MxDepth) { TExeTm ExeTm; SgV.Clr(true); for (TNGraph::TNodeI NI = NGraph->BegNI(); NI < NGraph->EndNI(); NI++) { TSimpleGraph SimpleG; RecurBfs(NI.GetId(), MxDepth, SimpleG); //NGraph->DelNode(NI.GetId()); printf("."); } printf("\ncandidates: %d\n", SgV.Len()); SgV.Sort(); int Cnt = 1; for (int i = 1; i < SgV.Len(); i++) { if (SgV[i-1] != SgV[i]) Cnt++; } printf("distinct: %d\t[%s]\n", Cnt, ExeTm.GetTmStr()); }
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(); }