Exemplo n.º 1
0
PNGraph TNGraph::GetSmallGraph() {
  PNGraph G = TNGraph::New();
  for (int i = 0; i < 5; i++) { G->AddNode(i); }
  G->AddEdge(0,1); G->AddEdge(1,2); G->AddEdge(0,2);
  G->AddEdge(1,3); G->AddEdge(3,4); G->AddEdge(2,3);
  return G;
}
Exemplo n.º 2
0
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
}
Exemplo n.º 3
0
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); }
		    }
                }
            }
        }
    }
}
Exemplo n.º 4
0
// Generate TNGraph
PNGraph GetTestTNGraph() {
  PNGraph Graph = TNGraph::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;
}
Exemplo n.º 5
0
Arquivo: gio.cpp Projeto: pikma/Snap
// DyNetML format, loads all the networks in the file
TVec<PNGraph> LoadDyNetGraphV(const TStr& FNm) {
  TXmlLx XmlLx(TFIn::New(FNm), xspTruncate);
  TVec<PNGraph> GraphV;
  THashSet<TStr> NIdStr;
  while (XmlLx.GetSym()!=xsyEof) {
    if (XmlLx.Sym==xsySTag && XmlLx.TagNm=="network") {
      PNGraph G = TNGraph::New();
      GraphV.Add(G);
      XmlLx.GetSym();
      while (XmlLx.TagNm=="link") {
        TStr Str1, Val1, Str2, Val2;
        XmlLx.GetArg(0, Str1, Val1);  XmlLx.GetArg(1, Str2, Val2);
        IAssert(Str1=="source" && Str2=="target");
        NIdStr.AddKey(Val1); NIdStr.AddKey(Val2);
        const int src=NIdStr.GetKeyId(Val1);
        const int dst=NIdStr.GetKeyId(Val2);
        if (! G->IsNode(src)) { G->AddNode(src); }
        if (! G->IsNode(dst)) { G->AddNode(dst); }
        G->AddEdge(src, dst);
        XmlLx.GetSym();
      }
    }
  }
  return GraphV;
}
Exemplo n.º 6
0
// Generate TNGraph
PNGraph TriadGetTestTNGraph() {
  PNGraph Graph = TNGraph::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;
}
Exemplo n.º 7
0
PNGraph TGraphKey::GetNGraph() const {
  PNGraph G = TNGraph::New();
  for (int i = 0; i < GetNodes(); i++) G->AddNode(i);
  for (int e = 0; e < GetEdges(); e++) {
    G->AddEdge(EdgeV[e].Val1, EdgeV[e].Val2);
  }
  G->Defrag();
  return G;
}
Exemplo n.º 8
0
int TTop2FriendNet::GetRnd2WccSz(const double ProbPick2nd) const {
  TCnComV CnComV;
  PNGraph G = TNGraph::New();
  for (TWgtNet::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) {
    if (NI.GetOutDeg() == 0) { continue; }
    const int NId1 = NI.GetOutNId(TInt::Rnd.GetUniDevInt(NI.GetOutDeg()));
    G->AddNode(NI.GetId());
    G->AddNode(NId1);
    G->AddEdge(NI.GetId(), NId1);
    if (NI.GetOutDeg() > 1 && TInt::Rnd.GetUniDev() <= ProbPick2nd) {
      int NId2 = NI.GetOutNId(TInt::Rnd.GetUniDevInt(NI.GetOutDeg()));
      while (NId2 == NId1) { NId2 = NI.GetOutNId(TInt::Rnd.GetUniDevInt(NI.GetOutDeg())); }
      G->AddNode(NId2);
      G->AddEdge(NI.GetId(), NId2);
    }
  }
  TCnCom::GetWccs(G, CnComV);
  return CnComV[0].Len();
}
Exemplo n.º 9
0
/// returns a perfect binary tree
PNGraph GetTree() {
  PNGraph G = TNGraph::New();
  for (int i = 0; i < 15; i++) {
    G->AddNode(i);
  }
  for (int i = 1; i < 15; i++) {
    G->AddEdge(i,i/2);
  }
  return G;
}
Exemplo n.º 10
0
// Slashdot network
void MakeSlashdotNet(TStr InFNm, TStr OutFNm, TStr Desc) {
  TSsParser Ss(InFNm, ssfTabSep);
  PNGraph Graph = TNGraph::New();
  TStrHash<TInt> StrSet(Mega(1), true);
  while (Ss.Next()) {
    const int SrcNId = StrSet.AddKey(Ss[0]);
    if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
    for (int dst = 2; dst < Ss.Len(); dst++) {
      const int DstNId = StrSet.AddKey(Ss[dst]);
      if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
      Graph->AddEdge(SrcNId, DstNId);
    }
  }
  PrintGraphStatTable(Graph, OutFNm, Desc);
}
Exemplo n.º 11
0
void TGraphEnumUtils::GetGraph(uint64 graphId, int nodes, PNGraph &G) {
  G->Clr();
	//Add nodes;
	for(int i=0; i<nodes; i++) G->AddNode(i);
	//Add edges
	for(int row=0; row<nodes; row++) {
		for(int col=0; col<nodes; col++) {
			int n = row*nodes+col;
			//
			uint64 bits = graphId >> n;
			uint64 mask = 1;
			if((bits & mask)==1) G->AddEdge(row, col);
		}
	}
}
Exemplo n.º 12
0
void TGraphEnumUtils::GetIndGraph(const PNGraph &G, const TIntV &sg, PNGraph &indG) {
	//Add nodes
	for(int i=0; i<sg.Len(); i++) indG->AddNode(sg[i]);
	//Add edges
	for(int i=0; i<sg.Len(); i++) {
		int nId = sg[i];
		TNGraph::TNodeI nIt = G->GetNI(nId);
		//
		int deg = nIt.GetOutDeg();
		for(int j=0; j<deg; j++) {
			int dstId = nIt.GetNbrNId(j);
			if(nId == dstId) continue;
			//
			if(indG->IsNode(dstId)) indG->AddEdge(nId, dstId);
		}
	}
}
Exemplo n.º 13
0
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);
	}
}
Exemplo n.º 14
0
// 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;
}
Exemplo n.º 15
0
// simulate SI model cascade using infection probability Beta until the cascade stops or reaches size MxCascSz
PNGraph RunSICascade2(PUNGraph G, const double& Beta, const int& MxCascSz, TIntH& NIdInfTmH) {
  PNGraph Casc = TNGraph::New();
  const int StartNId = G->GetRndNId();
  Casc->AddNode(StartNId);
  NIdInfTmH.AddDat(StartNId, NIdInfTmH.Len());
  TIntQ Q; Q.Push(StartNId);
  while (! Q.Empty()) {
    const TUNGraph::TNodeI NI = G->GetNI(Q.Top()); Q.Pop();
    for (int i = 0; i < NI.GetOutDeg(); i++) {
      if (TInt::Rnd.GetUniDev() < Beta && ! NIdInfTmH.IsKey(NI.GetOutNId(i))) {
        Casc->AddNode(NI.GetOutNId(i));
        NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
        Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
        if (Casc->GetNodes() == MxCascSz) { return Casc; }
        Q.Push(NI.GetOutNId(i));
      }
    }
  }
  return Casc;
}
Exemplo n.º 16
0
// simulate SI model cascade using infection probability Beta until the cascade reaches size CascSz
PNGraph RunSICascade(PUNGraph G, const double& Beta, const int& CascSz, TIntH& NIdInfTmH) {
  PNGraph Casc = TNGraph::New();
  const int StartId = G->GetRndNId();
  Casc->AddNode(StartId);
  NIdInfTmH.AddDat(StartId, NIdInfTmH.Len());
  for (int X = 0; X < 10*CascSz; X++) {
    TIntV CascNIdV;  Casc->GetNIdV(CascNIdV);
    for (int n = 0; n < CascNIdV.Len(); n++) {
      const TUNGraph::TNodeI NI = G->GetNI(CascNIdV[n]);
      for (int i = 0; i < NI.GetOutDeg(); i++) {
        if (Casc->IsNode(NI.GetOutNId(i))) { continue; }
        if (TInt::Rnd.GetUniDev() < Beta) {
          Casc->AddNode(NI.GetOutNId(i));
          NIdInfTmH.AddDat(NI.GetOutNId(i), NIdInfTmH.Len());
          Casc->AddEdge(NI.GetId(), NI.GetOutNId(i));
          if (Casc->GetNodes() == CascSz) { return Casc; }
        }
      }
    }
  }
  return Casc;
}
Exemplo n.º 17
0
PNGraph getFourHopGraph(const PNGraph& graph, int srcId, int dstId) {
	PNGraph ret = PNGraph::New();
	int shortPath = TSnap::GetShortPath(graph, srcId, dstId, true);
	// printf("shortPath is %d\n", shortPath);
	if (shortPath > 4) {
		return ret;
	}

	std::set<int> nodeIdSet1;
	getOutNeighborNodeIDs(graph, srcId, nodeIdSet1);
	std::set<int> nodeIdSet2;
	getInNeighborNodeIDs(graph, dstId, nodeIdSet2);
	std::set<int> nodeIdSet;
	nodeIdSet.insert(nodeIdSet1.begin(), nodeIdSet1.end());
	nodeIdSet.insert(nodeIdSet2.begin(), nodeIdSet2.end());

	// Add all nodes into new graph;
	std::vector<int> nodeIdList;
	for (std::set<int>::iterator setItr = nodeIdSet.begin(); setItr != nodeIdSet.end(); setItr++) {
		ret->AddNode(*setItr);
		nodeIdList.push_back(*setItr);
	}

	// Add all edges into new graph;
	int nodeNum = nodeIdList.size();
	for (int i = 0; i < nodeNum; ++i) {
		for (int j = 0; j < nodeNum; ++j) {
			if (i == j) continue;
			if (graph->IsEdge(nodeIdList[i], nodeIdList[j], true)) {
				ret->AddEdge(nodeIdList[i], nodeIdList[j]);
			}
		}
	}
	// printf("%d nodes in sub graph\n", ret->GetNodes());
	// printf("%d edges in sub graph\n", ret->GetEdges());
	// printf("%d hops\n", TSnap::GetShortPath(ret, srcId, dstId, true));

	return ret;
}
Exemplo n.º 18
0
int main(int argc, char* argv[]) {
  // create a graph and save it
  { PNGraph Graph = TNGraph::New();
  for (int i = 0; i < 10; i++) {
    Graph->AddNode(i); }
  for (int i = 0; i < 10; i++) {
    Graph->AddEdge(i, TInt::Rnd.GetUniDevInt(10)); }
  TSnap::SaveEdgeList(Graph, "graph.txt", "Edge list format"); }
  // load a graph
  PNGraph Graph;
  Graph = TSnap::LoadEdgeList<PNGraph>("graph.txt", 0, 1);
  // traverse nodes
  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
    printf("NodeId: %d, InDegree: %d, OutDegree: %d\n", NI.GetId(), NI.GetInDeg(), NI.GetOutDeg());
    printf("OutNodes: ");
    for (int e = 0; e < NI.GetOutDeg(); e++) { printf("  %d", NI.GetOutNId(e)); }
    printf("\nInNodes: ");
    for (int e = 0; e < NI.GetInDeg(); e++) { printf("  %d", NI.GetInNId(e)); }
    printf("\n\n");
  }
  // graph statistic
  TSnap::PrintInfo(Graph, "Graph info");
  PNGraph MxWcc = TSnap::GetMxWcc(Graph);
  TSnap::PrintInfo(MxWcc, "Largest Weakly connected component");
  // random graph
  PNGraph RndGraph = TSnap::GenRndGnm<PNGraph>(100, 1000);
  TGStat GraphStat(RndGraph, TSecTm(1), TGStat::AllStat(), "Gnm graph");
  GraphStat.PlotAll("RndGraph", "Random graph on 1000 nodes");
  // Forest Fire graph
  { TFfGGen ForestFire(false, 1, 0.35, 0.30, 1.0, 0.0, 0.0);
  ForestFire.GenGraph(100);
  PNGraph FfGraph = ForestFire.GetGraph(); }
  // network
  TPt<TNodeEDatNet<TStr, TStr> > Net = TNodeEDatNet<TStr, TStr>::New();
  Net->AddNode(0, "zero");
  Net->AddNode(1, "one");
  Net->AddEdge(0, 1, "zero to one");
  return 0;
}
Exemplo n.º 19
0
void MakeLJNets(TStr InFNm, TStr OutFNm, TStr Desc){
  TStrHash<TInt> StrSet(Mega(1), true);
  for (int i = 1; i < 13; i++){
    TStr tmp = "";  
      
    if (i < 10)
      tmp = InFNm + "ljgraph.0" + TInt::GetStr(i);  
    else
      tmp = InFNm + "ljgraph." + TInt::GetStr(i);  
  
    printf("%s\n",tmp());

    TSsParser Ss(tmp, ssfTabSep);
    PNGraph Graph = TNGraph::New();
      
    while (Ss.Next()) {
      const int SrcNId = StrSet.AddKey(Ss[0]);
      if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
      for (int dst = 2; dst < Ss.Len(); dst++) {
        TStr ls,rs;
          ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs);
        if (ls == ">"){
          const int DstNId = StrSet.AddKey(rs);
          if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
          Graph->AddEdge(SrcNId, DstNId);
        }
      }
    }
    if (i < 10)
      OutFNm = "soc-lj-friends.0"+TInt::GetStr(i);
    else
      OutFNm = "soc-lj-friends."+TInt::GetStr(i);
  
    PrintGraphStatTable(Graph, OutFNm, Desc);
  }
}
Exemplo n.º 20
0
void MakeLJGroupsNets(const TStr InFNm, TStr OutFNm, TStr Desc){
  TStrHash<TInt> StrSetU(Mega(1), true);
  TStrHash<TInt> StrSetG(Mega(1), true);
  
  for (int i = 1; i < 13; i++){
    TStr tmp = "";  
      
    if (i < 10)
      tmp = InFNm + "ljgraph.0" + TInt::GetStr(i);  
    else
      tmp = InFNm + "ljgraph." + TInt::GetStr(i);  
  
    printf("%s\n",tmp());

    TSsParser Ss(tmp, ssfTabSep);
    PNGraph Graph = TNGraph::New();
      
    while (Ss.Next()) {
      const int SrcNId = StrSetU.AddKey(Ss[0]);
      if (! Graph->IsNode(SrcNId)) 
         Graph->AddNode(SrcNId); 
      for (int dst = 2; dst < Ss.Len(); dst++) {
        TStr ls,rs;
          ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs);
        if (ls == ">"){
          const int DstNId = StrSetU.AddKey(rs);
          if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
          Graph->AddEdge(SrcNId, DstNId);
        }
      }
    }
    if (i < 10)
      OutFNm = "soc-lj-friends.0"+TInt::GetStr(i);
    else
      OutFNm = "soc-lj-friends."+TInt::GetStr(i);
  
  //  PrintGraphStatTable(Graph, OutFNm, Desc);
  }
  
  for (int i = 5; i < 14; i++){
    TStr tmp = "";       
    
    tmp = InFNm + "ljcomm." + TInt::GetStr(i);  
    printf("%s\n",tmp());
    
    TSsParser Ss1(tmp, ssfTabSep);
  
    PNGraph Graph1 = TNGraph::New();
    PNGraph Graph2 = TNGraph::New();
  
    TSsParser Ss(tmp, ssfTabSep);    
    while (Ss.Next()) {
      const int SrcNId = StrSetU.AddKey(Ss[0]);
      if (! Graph1->IsNode(SrcNId)) { Graph1->AddNode(SrcNId); }
      if (! Graph2->IsNode(SrcNId)) { Graph2->AddNode(SrcNId); }
      
      for (int dst = 2; dst < Ss.Len(); dst++) {   
        TStr ls,rs;
        ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs);            
        const int DstNId = StrSetG.AddKey(rs) + 10000000;
        if (! Graph1->IsNode(DstNId)) { Graph1->AddNode(DstNId); }
        if (! Graph2->IsNode(DstNId)) { Graph2->AddNode(DstNId); }
        if (ls == "<") // member
          Graph1->AddEdge(SrcNId, DstNId);  
        else if (ls == ">") // watching
          Graph2->AddEdge(SrcNId, DstNId);      
      }
    }
    OutFNm = "soc-lj-comm-";
    TStr s = "";
    if (i < 10)
      s = "0";
    
    PrintGraphStatTable(Graph1, OutFNm+"members-" + s + TInt::GetStr(i), Desc+" communities - members");
    PrintGraphStatTable(Graph2, OutFNm+"watchers-"+ s + TInt::GetStr(i), Desc+" communities - watchers");
  }
}
Exemplo n.º 21
0
// 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);
}
Exemplo n.º 22
0
/// returns a perfect binary tree plus one edge
PNGraph GetCircle() {
  PNGraph G = GetTree();
  G->AddEdge(7,2);
  return G;
}
Exemplo n.º 23
0
// 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);
  
}
	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;
	}
Exemplo n.º 25
0
// 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());
}