Beispiel #1
0
// Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in a single line.
void IOConnList() {
  
  const int NNodes = 500;
  const int NEdges = 2000;
  
  const char *FName = "demo.graph.dat";
  
  PNEGraph GOut, GIn;
  GOut = GenRndGnm<PNEGraph>(NNodes, NEdges);
  
  // Create graph file
  FILE *F = fopen(FName, "w");
  for (TNEGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) {
    fprintf(F, "%d", NI.GetId());
    for (int i = 0; i < NI.GetOutDeg(); i++) {
      fprintf(F, " %d", NI.GetOutNId(i));
    }
    fprintf(F, "\n");
  }
  fclose(F);
  
  // Load edge list
  GIn = LoadConnList<PNEGraph>(FName);
  
  PrintGStats("ConnList - Out", GOut);
  PrintGStats("ConnList - In", GIn);
}
Beispiel #2
0
// Save and load directed, undirected and multi-graphs, where node names are strings
void IOEdgeListStr() {
  
  const int NNodes = 1000;
  const int NEdges = 5000;
  
  const char *FName = "demo.graph.dat";
  
  PNEGraph GOut, GIn;      // Can also be PUNGraph or PNGraph
  GOut = GenRndGnm<PNEGraph>(NNodes, NEdges);
  
  // Output nodes as random strings
  TIntStrH OutNIdStrH;
  TStrHash<TInt> OutStrNIdH;
  
  // Generate unique random strings for graph
  TStr RandStr;
  for (TNEGraph::TNodeI NI = GOut->BegNI(); NI < GOut->EndNI(); NI++) {
    do {
      RandStr.Clr();
      TInt RandLen = TInt::Rnd.GetUniDevInt(5, 30);
      for (int i = 0; i < RandLen; i++) {
        TStr RandChar(TInt::Rnd.GetUniDevInt(33, 126));
        RandStr += RandChar;
      }
    }
    while (OutStrNIdH.IsKey(RandStr) || RandStr[0] == '#'); // Not unique or starts with comment
    OutNIdStrH.AddDat(NI.GetId(), RandStr);
    OutStrNIdH.AddDat(RandStr, NI.GetId());
  }
  
  // Create graph file
  FILE *F = fopen(FName, "w");
  for (TNEGraph::TEdgeI EI = GOut->BegEI(); EI < GOut->EndEI(); EI++) {
    TInt Src = EI.GetSrcNId();
    TInt Dst = EI.GetDstNId();
    fprintf(F, "%s %s\n", OutNIdStrH[Src].CStr(), OutNIdStrH[Dst].CStr());
  }
  fclose(F);
  
  // Load edge list of strings
  TStrHash<TInt> InStrToNIdH;
  GIn = LoadEdgeListStr<PNEGraph>(FName, 0, 1, InStrToNIdH);
  
  PrintGStats<PNEGraph>("EdgeListStr - Out", GOut);
  PrintGStats<PNEGraph>("EdgeListStr - In", GIn);

}
Beispiel #3
0
// Test node, edge creation
TEST(TNEGraph, ManipulateNodesEdges) {
  int NNodes = 1000;
  int NEdges = 100000;
  const char *FName = "test.graph";

  PNEGraph Graph;
  PNEGraph Graph1;
  PNEGraph Graph2;
  int i;
  int n;
  int NCount;
  int x,y;
  int Deg, InDeg, OutDeg;

  Graph = TNEGraph::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);
    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 (TNEGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
    NCount++;
  }
  EXPECT_EQ(NNodes,NCount);

  // edges per node iterator
  NCount = 0;
  for (TNEGraph::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 (TNEGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
    NCount++;
  }
  EXPECT_EQ(NEdges,NCount);

  // node degree
  for (TNEGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
    Deg = NI.GetDeg();
    InDeg = NI.GetInDeg();
    OutDeg = NI.GetOutDeg();

    EXPECT_EQ(Deg,InDeg+OutDeg);
  }

  // assignment
  Graph1 = TNEGraph::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 = TNEGraph::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());
}