SNAP Library 2.4, User Reference  2015-05-11 19:40:56
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TNGraph Class Reference

Directed graph. More...

#include <graph.h>

Classes

class  TEdgeI
 Edge iterator. Only forward iteration (operator++) is supported. More...
 
class  TNode
 
class  TNodeI
 Node iterator. Only forward iteration (operator++) is supported. More...
 

Public Types

typedef TNGraph TNet
 
typedef TPt< TNGraphPNet
 

Public Member Functions

 TNGraph ()
 
 TNGraph (const int &Nodes, const int &Edges)
 Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. More...
 
 TNGraph (const TNGraph &Graph)
 
 TNGraph (TSIn &SIn)
 Constructor that loads the graph from a (binary) stream SIn. More...
 
void Save (TSOut &SOut) const
 Saves the graph to a (binary) stream SOut. More...
 
bool HasFlag (const TGraphFlag &Flag) const
 Allows for run-time checking the type of the graph (see the TGraphFlag for flags). More...
 
TNGraphoperator= (const TNGraph &Graph)
 
int GetNodes () const
 Returns the number of nodes in the graph. More...
 
int AddNode (int NId=-1)
 Adds a node of ID NId to the graph. More...
 
int AddNode (const TNodeI &NodeId)
 Adds a node of ID NodeI.GetId() to the graph. More...
 
int AddNode (const int &NId, const TIntV &InNIdV, const TIntV &OutNIdV)
 Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV. More...
 
int AddNode (const int &NId, const TVecPool< TInt > &Pool, const int &SrcVId, const int &DstVId)
 Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV in the vector pool Pool, creates edges from the node to all nodes in vector OutNIdVin the vector pool Pool . More...
 
void DelNode (const int &NId)
 Deletes node of ID NId from the graph. More...
 
void DelNode (const TNode &NodeI)
 Deletes node of ID NodeI.GetId() from the graph. More...
 
bool IsNode (const int &NId) const
 Tests whether ID NId is a node. More...
 
TNodeI BegNI () const
 Returns an iterator referring to the first node in the graph. More...
 
TNodeI EndNI () const
 Returns an iterator referring to the past-the-end node in the graph. More...
 
TNodeI GetNI (const int &NId) const
 Returns an iterator referring to the node of ID NId in the graph. More...
 
int GetMxNId () const
 Returns an ID that is larger than any node ID in the graph. More...
 
int GetEdges () const
 Returns the number of edges in the graph. More...
 
int AddEdge (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the graph. More...
 
int AddEdge (const TEdgeI &EdgeI)
 Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph. More...
 
void DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true)
 Deletes an edge from node IDs SrcNId to DstNId from the graph. More...
 
bool IsEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
 Tests whether an edge from node IDs SrcNId to DstNId exists in the graph. More...
 
TEdgeI BegEI () const
 Returns an iterator referring to the first edge in the graph. More...
 
TEdgeI EndEI () const
 Returns an iterator referring to the past-the-end edge in the graph. More...
 
TEdgeI GetEI (const int &EId) const
 Not supported/implemented! More...
 
TEdgeI GetEI (const int &SrcNId, const int &DstNId) const
 Returns an iterator referring to edge (SrcNId, DstNId) in the graph. More...
 
int GetRndNId (TRnd &Rnd=TInt::Rnd)
 Returns an ID of a random node in the graph. More...
 
TNodeI GetRndNI (TRnd &Rnd=TInt::Rnd)
 Returns an interator referring to a random node in the graph. More...
 
void GetNIdV (TIntV &NIdV) const
 Gets a vector IDs of all nodes in the graph. More...
 
bool Empty () const
 Tests whether the graph is empty (has zero nodes). More...
 
void Clr ()
 Deletes all nodes and edges from the graph. More...
 
void Reserve (const int &Nodes, const int &Edges)
 Reserves memory for a graph of Nodes nodes and Edges edges. More...
 
void ReserveNIdInDeg (const int &NId, const int &InDeg)
 Reserves memory for node ID NId having InDeg in-edges. More...
 
void ReserveNIdOutDeg (const int &NId, const int &OutDeg)
 Reserves memory for node ID NId having OutDeg out-edges. More...
 
void Defrag (const bool &OnlyNodeLinks=false)
 Defragments the graph. More...
 
bool IsOk (const bool &ThrowExcept=true) const
 Checks the graph data structure for internal consistency. More...
 
void Dump (FILE *OutF=stdout) const
 Print the graph in a human readable form to an output stream OutF. More...
 

Static Public Member Functions

static PNGraph New ()
 Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New(). More...
 
static PNGraph New (const int &Nodes, const int &Edges)
 Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and Edges edges. More...
 
static PNGraph Load (TSIn &SIn)
 Static constructor that loads the graph from a stream SIn and returns a pointer to it. More...
 
static PNGraph GetSmallGraph ()
 Returns a small graph on 5 nodes and 6 edges. More...
 

Private Member Functions

TNodeGetNode (const int &NId)
 
const TNodeGetNode (const int &NId) const
 

Private Attributes

TCRef CRef
 
TInt MxNId
 
THash< TInt, TNodeNodeH
 

Friends

class TPt< TNGraph >
 
class TNGraphMtx
 

Detailed Description

Directed graph.

Node IDs can be arbitrary non-negative integers. Nodes and edges have no attributes/data associated with them. There is at most one directed edge from one source node to a destination node. There can be an edge between the same pair of nodes in the opposite direction. Self loops (one per node) are allowed but multiple (parallel) edges are not. The directed graph data structure is implemented using sorted adjacency lists. This means adding a node takes constant time, while adding an edge takes linear time (since adjacency list is kept sorted) in the node degree. Accessing arbitrary node takes constant time and accessing any edge takes logarithmic time in the node degree.

Definition at line 296 of file graph.h.

Member Typedef Documentation

Definition at line 299 of file graph.h.

Definition at line 298 of file graph.h.

Constructor & Destructor Documentation

TNGraph::TNGraph ( )
inline

Definition at line 402 of file graph.h.

402 : CRef(), MxNId(0), NodeH() { }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TCRef CRef
Definition: graph.h:395
TInt MxNId
Definition: graph.h:396
TNGraph::TNGraph ( const int &  Nodes,
const int &  Edges 
)
inlineexplicit

Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.

Definition at line 404 of file graph.h.

404 : MxNId(0) { Reserve(Nodes, Edges); }
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:514
TInt MxNId
Definition: graph.h:396
TNGraph::TNGraph ( const TNGraph Graph)
inline

Definition at line 405 of file graph.h.

405 : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TInt MxNId
Definition: graph.h:396
TNGraph::TNGraph ( TSIn SIn)
inline

Constructor that loads the graph from a (binary) stream SIn.

Definition at line 407 of file graph.h.

407 : MxNId(SIn), NodeH(SIn) { }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TInt MxNId
Definition: graph.h:396

Member Function Documentation

int TNGraph::AddEdge ( const int &  SrcNId,
const int &  DstNId 
)

Adds an edge from node IDs SrcNId to node DstNId to the graph.

If the edge already exists return -2. If the edge was successfully added return -1. Normally the function should return an ID of the edge added but since edges in TNGraph have no IDs we return -1. Function aborts if SrcNId or DstNId are not nodes in the graph.

Definition at line 286 of file graph.cpp.

286  {
287  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
288  //IAssert(! IsEdge(SrcNId, DstNId));
289  if (IsEdge(SrcNId, DstNId)) { return -2; }
290  GetNode(SrcNId).OutNIdV.AddSorted(DstNId);
291  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
292  return -1; // edge id
293 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1027
TNode & GetNode(const int &NId)
Definition: graph.h:399
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:313
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TIntV InNIdV
Definition: graph.h:304
TIntV OutNIdV
Definition: graph.h:304
int TNGraph::AddEdge ( const TEdgeI EdgeI)
inline

Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph.

Definition at line 484 of file graph.h.

484 { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:286
int TNGraph::AddNode ( int  NId = -1)

Adds a node of ID NId to the graph.

Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.

Definition at line 208 of file graph.cpp.

208  {
209  if (NId == -1) {
210  NId = MxNId; MxNId++;
211  } else {
212  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
213  MxNId = TMath::Mx(NId+1, MxNId());
214  }
215  NodeH.AddDat(NId, TNode(NId));
216  return NId;
217 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: graph.h:397
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:396
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int TNGraph::AddNode ( const TNodeI NodeId)
inline

Adds a node of ID NodeI.GetId() to the graph.

Definition at line 432 of file graph.h.

432 { return AddNode(NodeId.GetId()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:208
int TNGraph::AddNode ( const int &  NId,
const TIntV InNIdV,
const TIntV OutNIdV 
)

Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV.

Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.

The operation can create inconsistent graphs when the neighboring nodes in vectors InNIdV and OutNIdV do not exist. Use TNGraph::IsOk to check that the resulting graph is consistent after the operation.

Definition at line 221 of file graph.cpp.

221  {
222  int NewNId;
223  if (NId == -1) {
224  NewNId = MxNId; MxNId++;
225  } else {
226  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
227  NewNId = NId;
228  MxNId = TMath::Mx(NewNId+1, MxNId());
229  }
230  TNode& Node = NodeH.AddDat(NewNId);
231  Node.Id = NewNId;
232  Node.InNIdV = InNIdV;
233  Node.OutNIdV = OutNIdV;
234  Node.InNIdV.Sort();
235  Node.OutNIdV.Sort();
236  return NewNId;
237 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: graph.h:397
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1218
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:396
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int TNGraph::AddNode ( const int &  NId,
const TVecPool< TInt > &  Pool,
const int &  SrcVId,
const int &  DstVId 
)

Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV in the vector pool Pool, creates edges from the node to all nodes in vector OutNIdVin the vector pool Pool .

Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.

The operation can create inconsistent graphs when the neighboring nodes stored in the Pool vector are not explicitly added to the graph. Use TNGraph::IsOk to check that the resulting graph is consistent.

Definition at line 241 of file graph.cpp.

241  {
242  int NewNId;
243  if (NId == -1) {
244  NewNId = MxNId; MxNId++;
245  } else {
246  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
247  NewNId = NId;
248  MxNId = TMath::Mx(NewNId+1, MxNId());
249  }
250  TNode& Node = NodeH.AddDat(NewNId);
251  Node.Id = NewNId;
252  Node.InNIdV.GenExt(Pool.GetValVPt(SrcVId), Pool.GetVLen(SrcVId));
253  Node.OutNIdV.GenExt(Pool.GetValVPt(DstVId), Pool.GetVLen(DstVId));
254  Node.InNIdV.Sort();
255  Node.OutNIdV.Sort();
256  return NewNId;
257 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: graph.h:397
TVal * GetValVPt(const int &VId) const
Returns pointer to the first element of the vector with id VId.
Definition: ds.h:1617
int GetVLen(const int &VId) const
Returns the number of elements in the vector with id VId.
Definition: ds.h:1615
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:396
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TEdgeI TNGraph::BegEI ( ) const
inline

Returns an iterator referring to the first edge in the graph.

Definition at line 494 of file graph.h.

494 { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0){NI++;} return TEdgeI(NI, EndNI()); }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:463
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:465
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:350
TNodeI TNGraph::BegNI ( ) const
inline

Returns an iterator referring to the first node in the graph.

Definition at line 463 of file graph.h.

463 { return TNodeI(NodeH.BegI()); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TNode > NodeH
Definition: graph.h:397
void TNGraph::Clr ( )
inline

Deletes all nodes and edges from the graph.

Definition at line 512 of file graph.h.

512 { MxNId=0; NodeH.Clr(); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
TInt MxNId
Definition: graph.h:396
void TNGraph::Defrag ( const bool &  OnlyNodeLinks = false)

Defragments the graph.

After performing many node and edge insertions and deletions to a graph, the graph data structure will be fragmented in memory. This function compacts down the graph data structure and frees unneeded memory.

Definition at line 332 of file graph.cpp.

332  {
333  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n); ) {
334  TNode& Node = NodeH[n];
335  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
336  }
337  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
338 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
THash< TInt, TNode > NodeH
Definition: graph.h:397
void Defrag()
Definition: hash.h:509
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Pack()
Definition: hash.h:243
void TNGraph::DelEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
)

Deletes an edge from node IDs SrcNId to DstNId from the graph.

If the edge (SrcNId, DstNId) does not exist in the graph function still completes. But the function aborts if SrcNId or DstNId are not nodes in the graph.

Definition at line 295 of file graph.cpp.

295  {
296  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
297  { TNode& N = GetNode(SrcNId);
298  const int n = N.OutNIdV.SearchBin(DstNId);
299  if (n!= -1) { N.OutNIdV.Del(n); } }
300  { TNode& N = GetNode(DstNId);
301  const int n = N.InNIdV.SearchBin(SrcNId);
302  if (n!= -1) { N.InNIdV.Del(n); } }
303  if (! IsDir) {
304  { TNode& N = GetNode(SrcNId);
305  const int n = N.InNIdV.SearchBin(DstNId);
306  if (n!= -1) { N.InNIdV.Del(n); } }
307  { TNode& N = GetNode(DstNId);
308  const int n = N.OutNIdV.SearchBin(SrcNId);
309  if (n!= -1) { N.OutNIdV.Del(n); } }
310  }
311 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TNode & GetNode(const int &NId)
Definition: graph.h:399
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
void TNGraph::DelNode ( const int &  NId)

Deletes node of ID NId from the graph.

If the node of ID NId does not exist the function aborts.

Definition at line 259 of file graph.cpp.

259  {
260  { TNode& Node = GetNode(NId);
261  for (int e = 0; e < Node.GetOutDeg(); e++) {
262  const int nbr = Node.GetOutNId(e);
263  if (nbr == NId) { continue; }
264  TNode& N = GetNode(nbr);
265  const int n = N.InNIdV.SearchBin(NId);
266  if (n!= -1) { N.InNIdV.Del(n); }
267  }
268  for (int e = 0; e < Node.GetInDeg(); e++) {
269  const int nbr = Node.GetInNId(e);
270  if (nbr == NId) { continue; }
271  TNode& N = GetNode(nbr);
272  const int n = N.OutNIdV.SearchBin(NId);
273  if (n!= -1) { N.OutNIdV.Del(n); }
274  } }
275  NodeH.DelKey(NId);
276 }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TNode & GetNode(const int &NId)
Definition: graph.h:399
void DelKey(const TKey &Key)
Definition: hash.h:358
void TNGraph::DelNode ( const TNode NodeI)
inline

Deletes node of ID NodeI.GetId() from the graph.

Definition at line 459 of file graph.h.

459 { DelNode(NodeI.GetId()); }
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:259
void TNGraph::Dump ( FILE *  OutF = stdout) const

Print the graph in a human readable form to an output stream OutF.

Definition at line 387 of file graph.cpp.

387  {
388  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
389  fprintf(OutF, "-------------------------------------------------\nDirected Node Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
390  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
391  const TNode& Node = NodeH[N];
392  fprintf(OutF, " %*d]\n", NodePlaces, Node.GetId());
393  fprintf(OutF, " in [%d]", Node.GetInDeg());
394  for (int edge = 0; edge < Node.GetInDeg(); edge++) {
395  fprintf(OutF, " %*d", NodePlaces, Node.GetInNId(edge)); }
396  fprintf(OutF, "\n out[%d]", Node.GetOutDeg());
397  for (int edge = 0; edge < Node.GetOutDeg(); edge++) {
398  fprintf(OutF, " %*d", NodePlaces, Node.GetOutNId(edge)); }
399  fprintf(OutF, "\n");
400  }
401  fprintf(OutF, "\n");
402 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:278
THash< TInt, TNode > NodeH
Definition: graph.h:397
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:424
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
bool TNGraph::Empty ( ) const
inline

Tests whether the graph is empty (has zero nodes).

Definition at line 510 of file graph.h.

510 { return GetNodes()==0; }
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:424
TEdgeI TNGraph::EndEI ( ) const
inline

Returns an iterator referring to the past-the-end edge in the graph.

Definition at line 496 of file graph.h.

496 { return TEdgeI(EndNI(), EndNI()); }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:465
TNodeI TNGraph::EndNI ( ) const
inline

Returns an iterator referring to the past-the-end node in the graph.

Definition at line 465 of file graph.h.

465 { return TNodeI(NodeH.EndI()); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TIter EndI() const
Definition: hash.h:176
int TNGraph::GetEdges ( ) const

Returns the number of edges in the graph.

Definition at line 278 of file graph.cpp.

278  {
279  int edges=0;
280  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
281  edges+=NodeH[N].GetOutDeg();
282  }
283  return edges;
284 }
THash< TInt, TNode > NodeH
Definition: graph.h:397
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
TEdgeI TNGraph::GetEI ( const int &  EId) const

Not supported/implemented!

TNGraph::TEdgeI TNGraph::GetEI ( const int &  SrcNId,
const int &  DstNId 
) const

Returns an iterator referring to edge (SrcNId, DstNId) in the graph.

Definition at line 319 of file graph.cpp.

319  {
320  const TNodeI SrcNI = GetNI(SrcNId);
321  const int NodeN = SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
322  IAssert(NodeN != -1);
323  return TEdgeI(SrcNI, EndNI(), NodeN);
324 }
#define IAssert(Cond)
Definition: bd.h:262
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:467
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:465
int TNGraph::GetMxNId ( ) const
inline

Returns an ID that is larger than any node ID in the graph.

Definition at line 471 of file graph.h.

471 { return MxNId; }
TInt MxNId
Definition: graph.h:396
TNodeI TNGraph::GetNI ( const int &  NId) const
inline

Returns an iterator referring to the node of ID NId in the graph.

Definition at line 467 of file graph.h.

467 { return TNodeI(NodeH.GetI(NId)); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TIter GetI(const TKey &Key) const
Definition: hash.h:178
void TNGraph::GetNIdV ( TIntV NIdV) const

Gets a vector IDs of all nodes in the graph.

Definition at line 326 of file graph.cpp.

326  {
327  NIdV.Gen(GetNodes(), 0);
328  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
329  NIdV.Add(NodeH.GetKey(N)); }
330 }
THash< TInt, TNode > NodeH
Definition: graph.h:397
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:424
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
TNode& TNGraph::GetNode ( const int &  NId)
inlineprivate

Definition at line 399 of file graph.h.

399 { return NodeH.GetDat(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
const TNode& TNGraph::GetNode ( const int &  NId) const
inlineprivate

Definition at line 400 of file graph.h.

400 { return NodeH.GetDat(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int TNGraph::GetNodes ( ) const
inline

Returns the number of nodes in the graph.

Definition at line 424 of file graph.h.

424 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
int Len() const
Definition: hash.h:186
TNodeI TNGraph::GetRndNI ( TRnd Rnd = TInt::Rnd)
inline

Returns an interator referring to a random node in the graph.

Definition at line 505 of file graph.h.

505 { return GetNI(GetRndNId(Rnd)); }
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:467
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:503
int TNGraph::GetRndNId ( TRnd Rnd = TInt::Rnd)
inline

Returns an ID of a random node in the graph.

Definition at line 503 of file graph.h.

503 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
int GetRndKeyId(TRnd &Rnd) const
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
Definition: hash.h:398
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
PNGraph TNGraph::GetSmallGraph ( )
static

Returns a small graph on 5 nodes and 6 edges.

/// Edges:  0 -> 1, 1 -> 2, 0 -> 2, 1 -> 3, 3 -> 4, 2 -> 3
/// 

Definition at line 404 of file graph.cpp.

404  {
405  PNGraph G = TNGraph::New();
406  for (int i = 0; i < 5; i++) { G->AddNode(i); }
407  G->AddEdge(0,1); G->AddEdge(1,2); G->AddEdge(0,2);
408  G->AddEdge(1,3); G->AddEdge(3,4); G->AddEdge(2,3);
409  return G;
410 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:411
Definition: bd.h:196
bool TNGraph::HasFlag ( const TGraphFlag Flag) const

Allows for run-time checking the type of the graph (see the TGraphFlag for flags).

Definition at line 204 of file graph.cpp.

204  {
205  return HasGraphFlag(TNGraph::TNet, Flag);
206 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:38
Directed graph.
Definition: graph.h:296
bool TNGraph::IsEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
) const

Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.

Definition at line 313 of file graph.cpp.

313  {
314  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
315  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
316  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
317 }
TNode & GetNode(const int &NId)
Definition: graph.h:399
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
bool IsOutNId(const int &NId) const
Definition: graph.h:319
bool TNGraph::IsNode ( const int &  NId) const
inline

Tests whether ID NId is a node.

Definition at line 461 of file graph.h.

461 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:397
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool TNGraph::IsOk ( const bool &  ThrowExcept = true) const

Checks the graph data structure for internal consistency.

For each node in the graph check that its neighbors are also nodes in the graph.

Definition at line 341 of file graph.cpp.

341  {
342  bool RetVal = true;
343  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
344  const TNode& Node = NodeH[N];
345  if (! Node.OutNIdV.IsSorted()) {
346  const TStr Msg = TStr::Fmt("Out-neighbor list of node %d is not sorted.", Node.GetId());
347  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
348  }
349  if (! Node.InNIdV.IsSorted()) {
350  const TStr Msg = TStr::Fmt("In-neighbor list of node %d is not sorted.", Node.GetId());
351  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
352  }
353  // check out-edges
354  int prevNId = -1;
355  for (int e = 0; e < Node.GetOutDeg(); e++) {
356  if (! IsNode(Node.GetOutNId(e))) {
357  const TStr Msg = TStr::Fmt("Out-edge %d --> %d: node %d does not exist.",
358  Node.GetId(), Node.GetOutNId(e), Node.GetOutNId(e));
359  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
360  }
361  if (e > 0 && prevNId == Node.GetOutNId(e)) {
362  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge %d --> %d.",
363  Node.GetId(), Node.GetId(), Node.GetOutNId(e));
364  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
365  }
366  prevNId = Node.GetOutNId(e);
367  }
368  // check in-edges
369  prevNId = -1;
370  for (int e = 0; e < Node.GetInDeg(); e++) {
371  if (! IsNode(Node.GetInNId(e))) {
372  const TStr Msg = TStr::Fmt("In-edge %d <-- %d: node %d does not exist.",
373  Node.GetId(), Node.GetInNId(e), Node.GetInNId(e));
374  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
375  }
376  if (e > 0 && prevNId == Node.GetInNId(e)) {
377  const TStr Msg = TStr::Fmt("Node %d has duplidate in-edge %d <-- %d.",
378  Node.GetId(), Node.GetId(), Node.GetInNId(e));
379  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
380  }
381  prevNId = Node.GetInNId(e);
382  }
383  }
384  return RetVal;
385 }
THash< TInt, TNode > NodeH
Definition: graph.h:397
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:461
int FFirstKeyId() const
Definition: hash.h:232
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
char * CStr()
Definition: dt.h:476
static PNGraph TNGraph::Load ( TSIn SIn)
inlinestatic

Static constructor that loads the graph from a stream SIn and returns a pointer to it.

Definition at line 417 of file graph.h.

417 { return PNGraph(new TNGraph(SIn)); }
TNGraph()
Definition: graph.h:402
TPt< TNGraph > PNGraph
Pointer to a directed graph (TNGraph)
Definition: graph.h:16
static PNGraph TNGraph::New ( )
inlinestatic

Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().

Definition at line 411 of file graph.h.

411 { return new TNGraph(); }
TNGraph()
Definition: graph.h:402
static PNGraph TNGraph::New ( const int &  Nodes,
const int &  Edges 
)
inlinestatic

Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and Edges edges.

Call: PNGraph Graph = TNGraph::New(Nodes, Edges).

Definition at line 415 of file graph.h.

415 { return new TNGraph(Nodes, Edges); }
TNGraph()
Definition: graph.h:402
TNGraph& TNGraph::operator= ( const TNGraph Graph)
inline

Definition at line 420 of file graph.h.

420  {
421  if (this!=&Graph) { MxNId=Graph.MxNId; NodeH=Graph.NodeH; } return *this; }
THash< TInt, TNode > NodeH
Definition: graph.h:397
TInt MxNId
Definition: graph.h:396
void TNGraph::Reserve ( const int &  Nodes,
const int &  Edges 
)
inline

Reserves memory for a graph of Nodes nodes and Edges edges.

Definition at line 514 of file graph.h.

514 { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
THash< TInt, TNode > NodeH
Definition: graph.h:397
void Gen(const int &ExpectVals)
Definition: hash.h:180
void TNGraph::ReserveNIdInDeg ( const int &  NId,
const int &  InDeg 
)
inline

Reserves memory for node ID NId having InDeg in-edges.

Definition at line 516 of file graph.h.

516 { GetNode(NId).InNIdV.Reserve(InDeg); }
TNode & GetNode(const int &NId)
Definition: graph.h:399
TIntV InNIdV
Definition: graph.h:304
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:506
void TNGraph::ReserveNIdOutDeg ( const int &  NId,
const int &  OutDeg 
)
inline

Reserves memory for node ID NId having OutDeg out-edges.

Definition at line 518 of file graph.h.

518 { GetNode(NId).OutNIdV.Reserve(OutDeg); }
TNode & GetNode(const int &NId)
Definition: graph.h:399
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:506
TIntV OutNIdV
Definition: graph.h:304
void TNGraph::Save ( TSOut SOut) const
inline

Saves the graph to a (binary) stream SOut.

Definition at line 409 of file graph.h.

409 { MxNId.Save(SOut); NodeH.Save(SOut); }
void Save(TSOut &SOut) const
Definition: dt.h:1058
THash< TInt, TNode > NodeH
Definition: graph.h:397
void Save(TSOut &SOut) const
Definition: hash.h:141
TInt MxNId
Definition: graph.h:396

Friends And Related Function Documentation

friend class TNGraphMtx
friend

Definition at line 540 of file graph.h.

friend class TPt< TNGraph >
friend

Definition at line 539 of file graph.h.

Member Data Documentation

TCRef TNGraph::CRef
private

Definition at line 395 of file graph.h.

TInt TNGraph::MxNId
private

Definition at line 396 of file graph.h.

THash<TInt, TNode> TNGraph::NodeH
private

Definition at line 397 of file graph.h.


The documentation for this class was generated from the following files: