SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
testSnap.cpp
Go to the documentation of this file.
1 // Small example testing basic functionality of SNAP
2 
3 #include "Snap.h"
4 
5 int main(int argc, char* argv[]) {
6  // create a graph and save it
7  { PNGraph Graph = TNGraph::New();
8  for (int i = 0; i < 10; i++) {
9  Graph->AddNode(i); }
10  for (int i = 0; i < 10; i++) {
11  Graph->AddEdge(i, TInt::Rnd.GetUniDevInt(10)); }
12  TSnap::SaveEdgeList(Graph, "graph.txt", "Edge list format"); }
13  // load a graph
14  PNGraph Graph;
15  Graph = TSnap::LoadEdgeList<PNGraph>("graph.txt", 0, 1);
16  // traverse nodes
17  for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
18  printf("NodeId: %d, InDegree: %d, OutDegree: %d\n", NI.GetId(), NI.GetInDeg(), NI.GetOutDeg());
19  printf("OutNodes: ");
20  for (int e = 0; e < NI.GetOutDeg(); e++) { printf(" %d", NI.GetOutNId(e)); }
21  printf("\nInNodes: ");
22  for (int e = 0; e < NI.GetInDeg(); e++) { printf(" %d", NI.GetInNId(e)); }
23  printf("\n\n");
24  }
25  // graph statistic
26  TSnap::PrintInfo(Graph, "Graph info");
27  PNGraph MxWcc = TSnap::GetMxWcc(Graph);
28  TSnap::PrintInfo(MxWcc, "Largest Weakly connected component");
29  // random graph
30  PNGraph RndGraph = TSnap::GenRndGnm<PNGraph>(100, 1000);
31  TGStat GraphStat(RndGraph, TSecTm(1), TGStat::AllStat(), "Gnm graph");
32  GraphStat.PlotAll("RndGraph", "Random graph on 1000 nodes");
33  // Forest Fire graph
34  { TFfGGen ForestFire(false, 1, 0.35, 0.30, 1.0, 0.0, 0.0);
35  ForestFire.GenGraph(100);
36  PNGraph FfGraph = ForestFire.GetGraph(); }
37  // network
39  Net->AddNode(0, "zero");
40  Net->AddNode(1, "one");
41  Net->AddEdge(0, 1, "zero to one");
42 
43  const PNGraph DirectedGraph = TNGraph::New();
44  for (int i = 0; i < 10; i++) {
45  DirectedGraph->AddNode(i);
46  }
47 
48  DirectedGraph->AddEdge(0, 1);
49  DirectedGraph->AddEdge(1, 2);
50  DirectedGraph->AddEdge(2, 3);
51  DirectedGraph->AddEdge(3, 4);
52  DirectedGraph->AddEdge(4, 5);
53  DirectedGraph->AddEdge(5, 6);
54  DirectedGraph->AddEdge(6, 7);
55  DirectedGraph->AddEdge(7, 2);
56  DirectedGraph->AddEdge(8, 9);
57 
58  TIntFltH nodeBtwH;
59  TIntPrFltH edgeBtwH;
60 
61 
62  printf("Testing Betweenness Centrality Calculation \n");
63  TSnap::GetBetweennessCentr<PNGraph> (DirectedGraph, nodeBtwH, edgeBtwH, true);
64  for (TIntFltH::TIter It = nodeBtwH.BegI(); It < nodeBtwH.EndI(); It++) {
65  int node_id = It.GetKey();
66  double centr = It.GetDat();
67  printf("NodeId: %d, Centr: %f \n", node_id, centr);
68  }
69 
70  // printf("Testing Closeness Centrality Calculation \n");
71  // for (TNGraph::TNodeI NI = DirectedGraph->BegNI(); NI < DirectedGraph->EndNI(); NI++) {
72  // int id = NI.GetId();
73  // double centr = TSnap::GetClosenessCentr<PNGraph>(DirectedGraph, id, true);
74  // printf("NodeId: %d, Centr: %f \n", id, centr);
75  // }
76 
77  return 0;
78 }
TStopReason GenGraph(const int &GraphNodes, const bool &FloodStop=true)
Definition: ff.cpp:327
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
Definition: cncom.h:452
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:481
static TFSet AllStat()
Definition: gstat.cpp:401
static TRnd Rnd
Definition: dt.h:1146
static PNet New()
Static constructor that returns a pointer to the network. Call: TPt > Net = TNodeEDatNet::New().
Definition: network.h:661
Statistics of a Graph Snapshot.
Definition: gstat.h:36
void SaveEdgeList(const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr())
Saves a graph into a text file. Each line contains two columns and encodes a single edge:
Definition: gio.h:245
Definition: tm.h:81
int main(int argc, char *argv[])
Definition: testSnap.cpp:5
PNGraph GetGraph() const
Definition: ff.h:64
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:383
Definition: bd.h:196
THKeyDat * EndI
Definition: hash.h:54
Definition: ff.h:49
void PrintInfo(const PGraph &Graph, const TStr &Desc="", const TStr &OutFNm="", const bool &Fast=true)
Prints basic graph statistics.
Definition: gbase.h:87