Beispiel #1
0
void PrintGStats(const char s[], PGraph Graph) {

  printf("graph %s, nodes %d, edges %d, empty %s\n",
         s, Graph->GetNodes(), Graph->GetEdges(),
         Graph->Empty() ? "yes" : "no");

}
Beispiel #2
0
 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;
 }
Beispiel #3
0
 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); }
   }
 }
Beispiel #4
0
int main(int argc, char* argv[]) {
        PGraph Graph = TSnap::LoadEdgeList<PNGraph>("facebook_combined.txt",0,1);

        graphMl(Graph);
        gexf(Graph);
        gdf(Graph);
        graphson(Graph);
        IAssert(Graph->IsOk());
        return 0;
}
Beispiel #5
0
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();
}
Beispiel #6
0
 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();
 }
Beispiel #7
0
int main()
{
	//Number of levels 
	
	int emptyStart = 0;
	int final_level = 0;
	int final_number = 0;
	int depth = 1;
	//string config =		  "1110111111111111111111111111111111111111111111111111111";
	//string final_config = "0000000000100000000000000000000000000000000000000000000";
	int n;// = 8;
	string config;// =		  "01111111111111111111111111111111111111111111111111111111111111111";
	string final_config;// = "100000000000000000000000000000000000000000000000000000000000000000";

	cout<<"Please enter the number of level's you would like in this game: ";
	cin>>n;
	cout<<endl<<"Please enter the initial state which is a series of "<<(float)(n*(n+1)/2)<<" 0's and 1's where 1 reprents a peg and 0 represents an empty space."<<endl;
	cin>>config;
	cout<<endl<<"Please enter the final state which is a series of "<<(float)(n*(n+1)/2)<<" 0's and 1's where 1 reprents a peg and 0 represents an empty space."<<endl;
	cin>>final_config;
	cout<<endl<<endl;
	PegTable *found;

	PegTable *table = new PegTable(n,config);
	PGraph graph;
	stack<PegTable*> stac;
	const clock_t begin_time = clock();
	found = graph.DFS(table,final_config);
	cout<<endl<<float((clock() - begin_time))/1000<<" seconds"<<endl<<endl;
	if(found == 0)
	{
		cout<<"The solution state for this configuration does not exist"<<endl;
		return 0;
	}

//	sf::Clock Clock;
//

	while(found != 0)
	{
		stac.push(found);
		found = found->parent;
	}


	
	while(!stac.empty())
	{
		found = stac.top();
		stac.pop();
		found->Print();
	}

	return 0;
}
Beispiel #8
0
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;
}
Beispiel #9
0
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;
}
Beispiel #10
0
 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);
     }
   }
 }
Beispiel #11
0
PGraph WriteGraph(TStr Filename) {
  int NNodes = 100;
  PGraph Graph = PGraph::TObj::New();
  for (int i = 0; i < NNodes; i++) {
    Graph->AddNode(i);
  }
  for (int i = 1; i < NNodes - 1; i++) {
    Graph->AddEdge(i, i+1);
    Graph->AddEdge(i, i-1);
  }
  TFOut OutStream(Filename);
  Graph->Save(OutStream);
  return Graph;
}
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 << " ";
}
Beispiel #13
0
int main(int argc, char* argv[]) {
  typedef PNGraph PGraph;  //   directed graph

  printf("Creating graph for Livejournal\n");

  TFIn FIn("data/livejournal_scc.graph");
  PGraph graph = TNGraph::Load(FIn);
  IAssert(graph->IsOk());
  printf("Nodes: %d\n", graph->GetNodes());

  test<PGraph>(graph, true, false);
  test<PGraph>(graph, false, true);
  test<PGraph>(graph, true, true);


  return 0;
}
Beispiel #14
0
void CheckGraphCorrect(PGraph Saved, PGraph Loaded) {
  EXPECT_EQ(Saved->GetNodes(), Loaded->GetNodes());
  EXPECT_EQ(Saved->GetEdges(), Loaded->GetEdges());
  for (typename PGraph::TObj::TEdgeI EI = Saved->BegEI(); EI < Saved->EndEI(); EI++) {
    TInt Src = EI.GetSrcNId();
    TInt Dst = EI.GetDstNId();
    EXPECT_TRUE(Loaded->IsEdge(Src, Dst));
  }
}
Beispiel #15
0
void PrintGraphStatTable(const PGraph& G, TStr OutFNm, TStr Desc="") {
  TFltPrV DegCCfV;
  int64 ClosedTriads, OpenTriads;
  int FullDiam;
  double EffDiam;
  TSnap::PrintInfo(G, OutFNm);
  TExeTm ExeTm; printf("C");
  const double CCF = TSnap::GetClustCf(G, DegCCfV, ClosedTriads, OpenTriads);
  printf("[%s]D", ExeTm.GetStr());
  TSnap::GetBfsEffDiam(G, 1000, false, EffDiam, FullDiam);
  printf("[%s]CC", ExeTm.GetStr());
  PGraph WCC = TSnap::GetMxWcc(G);
  PGraph SCC = TSnap::GetMxScc(G);
  printf("[%s]\n", ExeTm.GetStr());
  FILE* F = stdout;
  if (! OutFNm.Empty()) {
    F = fopen(TStr::Fmt("%s.html", OutFNm.CStr()).CStr(), "wt"); }
  fprintf(F, "\n");
  fprintf(F, "<table id=\"datatab\" summary=\"Dataset statistics\">\n");
  fprintf(F, "  <tr> <th colspan=\"2\">Dataset statistics</th> </tr>\n");
  fprintf(F, "  <tr><td>Nodes</td> <td>%d</td></tr>\n", G->GetNodes());
  fprintf(F, "  <tr><td>Edges</td> <td>%d</td></tr>\n", G->GetEdges());
  fprintf(F, "  <tr><td>Nodes in largest WCC</td> <td>%d (%.3f)</td></tr>\n", WCC->GetNodes(), WCC->GetNodes()/double(G->GetNodes()));
  fprintf(F, "  <tr><td>Edges in largest WCC</td> <td>%d (%.3f)</td></tr>\n", WCC->GetEdges(), WCC->GetEdges()/double(G->GetEdges()));
  fprintf(F, "  <tr><td>Nodes in largest SCC</td> <td>%d (%.3f)</td></tr>\n", SCC->GetNodes(), SCC->GetNodes()/double(G->GetNodes()));
  fprintf(F, "  <tr><td>Edges in largest SCC</td> <td>%d (%.3f)</td></tr>\n", SCC->GetEdges(), SCC->GetEdges()/double(G->GetEdges()));
  fprintf(F, "  <tr><td>Average clustering coefficient</td> <td>%.4f</td></tr>\n", CCF);
  fprintf(F, "  <tr><td>Number of triangles</td> <td>%s</td></tr>\n", TUInt64(ClosedTriads).GetStr().CStr());
  fprintf(F, "  <tr><td>Fraction of closed triangles</td> <td>%.4g</td></tr>\n", ClosedTriads/double(ClosedTriads+OpenTriads));
  fprintf(F, "  <tr><td>Diameter (longest shortest path)</td> <td>%d</td></tr>\n", FullDiam);
  fprintf(F, "  <tr><td>90-percentile effective diameter</td> <td>%.2g</td></tr>\n", EffDiam);
  fprintf(F, "</table>\n");
  fprintf(F, "<br>\n");
  if (! OutFNm.Empty()) {
    fprintf(F, "\n<table id=\"datatab\" summary=\"Table of datasets\">\n");
    fprintf(F, "<tr>\n");
	  fprintf(F, "  <th>File</th>\n");
	  fprintf(F, "  <th>Description</th>\n");
    fprintf(F, "</tr>\n");
    fprintf(F, "<tr>\n");
	  fprintf(F, "  <td><a href=\"%s.txt.gz\">%s.txt.gz</a></td>\n", OutFNm.CStr(), OutFNm.CStr());
	  fprintf(F, "  <td>%s</td>\n", Desc.CStr());
    fprintf(F, "</tr>\n");
    fprintf(F, "</table>\n");
    fclose(F);
    TSnap::SaveEdgeList(G, OutFNm+".txt", Desc);
  }
}
Beispiel #16
0
int main(int argc, char* argv[]) {
  typedef PNGraph PGraph; // directed graph

  printf("Creating graph for wikiTalk\n");

  PGraph graph = TSnap::LoadEdgeList<PGraph>("data/wiki-Talk.txt", 0, 1);
  IAssert(graph->IsOk());
  printf("Graph (%d, %d)\n", graph->GetNodes(), graph->GetEdges());

  printf("Getting max scc\n");
  PGraph maxScc = TSnap::GetMxScc(graph);
  printf("Scc (%d, %d)\n", maxScc->GetNodes(), maxScc->GetEdges());
  TFOut FOut("data/wikiTalk_scc.graph");
  maxScc->Save(FOut);

  return 0;
}
Beispiel #17
0
bool test(PGraph &graph, bool followOut, bool followIn) {
  printf("\n================================\nFollowOut: %d, FollowIn: %d\n", followOut, followIn);
  int iters = 10;
  for (int k = 0; k < iters; k++) {
    TRnd rnd = TRnd((int)time(0));
    int start = graph->GetRndNId(rnd);
    rnd.PutSeed(0);
//    int target = graph->GetRndNId(rnd);
//    printf("Start node: %d, target node: %d\n", start, target);
    int target = -1;
    printf("Start node: %d\n", start);

    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);

    /* Hybrid */
    TBreathFS<PGraph> bfs_hybrid(graph, true);
    int maxDist_hybrid = bfs_hybrid.DoBfsHybrid(start, followOut, followIn, target);

    gettimeofday(&tv2, NULL);
    double time_hybrid = timeInSeconds(tv1, tv2);

    /* Original */
    gettimeofday(&tv1, NULL);

    TBreathFS<PGraph> bfs(graph, true);
    int maxDist = bfs.DoBfs(start, followOut, followIn, target);

    gettimeofday(&tv2, NULL);
    double time = timeInSeconds(tv1, tv2);

    /* Check results */
    if (maxDist_hybrid != maxDist) {
      printf("MaxDist incorrect.\n");
      return false;
    }
    if (target == -1) {
      if (!checkResults<PGraph>(bfs_hybrid, bfs)) {
        printf("NIdDistH values incorrect!\n");
        return false;
      }
    }

    printf("Execution times: Original: %.2f, Hybrid: %.2f\n", time, time_hybrid);
  }
  return true;
}
Beispiel #18
0
// Test graph generators
TYPED_TEST(GraphPercolationTest, PercolationThreshold) {

  typedef TypeParam TGraph;
  typedef TPt<TGraph> PGraph;

  int Nodes = 100;
  
  PGraph Graph;
  
  int rep = 100;
  double p, tol = 1e-3, lowerBound = 0.0, upperBound = 1.0;
  
  TRnd Rnd(0);
  
  // Line graph   
  
  Graph = TSnap::GenLine<PGraph>(Nodes);
  
  printf("Line\n====\n");
  printf("Nodes = %d\n", Graph->GetNodes());
  printf("Edges = %d\n", Graph->GetEdges());
  p = TSnap::FindPercolationThreshold(Graph, tol, lowerBound, upperBound, rep);
  printf("p = %f\n", p);
  
  // Line graph   
  
  Graph = TSnap::GenGrid<PGraph>((int) round(sqrt(Nodes)), (int) round(sqrt(Nodes)));
  
  printf("Grid\n====\n");
  printf("Nodes = %d\n", Graph->GetNodes());
  printf("Edges = %d\n", Graph->GetEdges());
  p = TSnap::FindPercolationThreshold(Graph, tol, lowerBound, upperBound, rep);
  printf("p = %f\n", p);
  
  // Full graph 
  
  Graph = TSnap::GenFull<PGraph>(Nodes);
  
  printf("Full\n====\n");
  printf("Nodes = %d\n", Graph->GetNodes());
  printf("Edges = %d\n", Graph->GetEdges());
  p = TSnap::FindPercolationThreshold(Graph, tol, lowerBound, upperBound, rep);
  printf("p = %f\n", p);
  
}
Beispiel #19
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;
}
Beispiel #20
0
void DemoFullBfsDfs() {
  
  const int NNodes = 500;
  
  PGraph G = GenFull<PGraph>(NNodes);
  PNGraph GOut;
  int TreeSz, TreeDepth;
  
  // Get BFS tree from first node without following links (demos different options)
  GOut = GetBfsTree(G, 1, false, false);
  GetSubTreeSz(G, 1, false, false, TreeSz, TreeDepth);
  printf("FollowOut=false, FollowIn=false, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", 
        GOut->GetNodes(), G->GetEdges());
  printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth);
  
  GOut = GetBfsTree(G, NNodes-1, true, true);
  GetSubTreeSz(G, 1, true, true, TreeSz, TreeDepth);
  printf("FollowOut=true, FollowIn=true, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", 
        GOut->GetNodes(), G->GetEdges());
  printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth);
  
  GOut = GetBfsTree(G, NNodes/2, true, false);
  GetSubTreeSz(G, 1, true, false, TreeSz, TreeDepth);
  printf("FollowOut=true, FollowIn=false, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", 
        GOut->GetNodes(), G->GetEdges());
  printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth);
  
  GOut = GetBfsTree(G, 1, false, true);
  GetSubTreeSz(G, 1, false, true, TreeSz, TreeDepth);
  printf("FollowOut=false, FollowIn=true, GOut->GetNodes() == %d, GOut->GetEdges() = %d\n", 
        GOut->GetNodes(), G->GetEdges());
  printf("TreeSz == %d, TreeDepth = %d\n", TreeSz, TreeDepth);
  
  TIntV NIdV;
  int StartNId, Hop, Nodes;
  
  StartNId = 1;
  Hop = 1;
  Nodes = GetNodesAtHop(G, StartNId, Hop, NIdV, HasGraphFlag(typename PGraph::TObj, gfDirected));
  printf("StartNId = %d, Nodes = %d, GetNodesAtHop NIdV.Len() = %d\n", StartNId, Nodes, NIdV.Len());
  
  TIntPrV HopCntV;
  Nodes = GetNodesAtHops(G, StartNId, HopCntV, HasGraphFlag(typename PGraph::TObj, gfDirected));
  printf("StartNId = %d, Nodes = %d, GetNodesAtHops HopCntV.Len() = %d\n", StartNId, Nodes, HopCntV.Len());
  
  int Length, SrcNId, DstNId;
  SrcNId = 1;
  DstNId = NNodes-1;
  
  Length = GetShortPath(G, SrcNId, DstNId, HasGraphFlag(typename PGraph::TObj, gfDirected));
  printf("SPL Length = %d\n", Length);
  
  TIntH NIdToDistH;
  int MaxDist = 9;
  Length = GetShortPath(G, SrcNId, NIdToDistH, HasGraphFlag(typename PGraph::TObj, gfDirected), MaxDist);
//  for (int i = 0; i < min(5,NIdToDistH.Len()); i++) {
//    printf("NIdToDistH[%d] = %d\n", i, NIdToDistH[i].Val);
//  }
  
  int FullDiam;
  double EffDiam, AvgDiam;
  int NTestNodes = 10;
  
  for (int IsDir = 0; IsDir < 2; IsDir++) {
    printf("IsDir = %d:\n", IsDir);
    
    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, AvgDiam);
    printf("EffDiam = %.3f, FullDiam = %d, AvgDiam = %.3f\n", EffDiam, FullDiam, AvgDiam);
    
    TIntV SubGraphNIdV;
    for (int i = 0; i < NTestNodes; i++) {
      SubGraphNIdV.Add(G->GetRndNId());
    }
//    for (int i = 0; i < SubGraphNIdV.Len(); i++) {
//      printf("SubGraphNIdV[%d] = %d\n", i, SubGraphNIdV[i].Val);
//    }
    
    EffDiam = GetBfsEffDiam(G, NTestNodes, SubGraphNIdV, IsDir, EffDiam, FullDiam);
    printf("For subgraph: EffDiam = %.3f, FullDiam = %d\n", EffDiam, FullDiam);
    
  }
  
}
Beispiel #21
0
double PercentMxScc(const PGraph& Graph) {
  
  PGraph MxSccSz = GetMxScc(Graph);
  
  return (double) MxSccSz->GetNodes() / (double) Graph->GetNodes();
}
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;
}
Beispiel #23
0
void PrintGraph(const char s[], const PGraph& Graph) {
  printf("---- %s -----\n",s);
  Graph->Dump();
}