SNAP Library 3.0, User Reference  2016-07-20 17:56:49
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 AddNodeUnchecked (int NId=-1)
 Adds a node of ID NId to the graph without performing checks. 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 AddEdgeUnchecked (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the graph without performing checks. More...
 
int AddEdge2 (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the graph. If nodes do not exist, create them. 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 SortNodeAdjV ()
 Sorts the adjacency lists of each node. 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 307 of file graph.h.

Member Typedef Documentation

Definition at line 310 of file graph.h.

Definition at line 309 of file graph.h.

Constructor & Destructor Documentation

TNGraph::TNGraph ( )
inline

Definition at line 416 of file graph.h.

416 : CRef(), MxNId(0), NodeH() { }
THash< TInt, TNode > NodeH
Definition: graph.h:411
TCRef CRef
Definition: graph.h:409
TInt MxNId
Definition: graph.h:410
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 418 of file graph.h.

418 : 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:534
TInt MxNId
Definition: graph.h:410
TNGraph::TNGraph ( const TNGraph Graph)
inline

Definition at line 419 of file graph.h.

419 : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
THash< TInt, TNode > NodeH
Definition: graph.h:411
TInt MxNId
Definition: graph.h:410
TNGraph::TNGraph ( TSIn SIn)
inline

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

Definition at line 421 of file graph.h.

421 : MxNId(SIn), NodeH(SIn) { }
THash< TInt, TNode > NodeH
Definition: graph.h:411
TInt MxNId
Definition: graph.h:410

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 321 of file graph.cpp.

321  {
322  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
323  //IAssert(! IsEdge(SrcNId, DstNId));
324  if (IsEdge(SrcNId, DstNId)) { return -2; }
325  GetNode(SrcNId).OutNIdV.AddSorted(DstNId);
326  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
327  return -1; // edge id
328 }
#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:1063
TNode & GetNode(const int &NId)
Definition: graph.h:413
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:363
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TIntV InNIdV
Definition: graph.h:315
TIntV OutNIdV
Definition: graph.h:315
int TNGraph::AddEdge ( const TEdgeI EdgeI)
inline

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

Definition at line 504 of file graph.h.

504 { 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:321
int TNGraph::AddEdge2 ( const int &  SrcNId,
const int &  DstNId 
)

Adds an edge from node IDs SrcNId to node DstNId to the graph. If nodes do not exist, create them.

Definition at line 336 of file graph.cpp.

336  {
337  if (! IsNode(SrcNId)) { AddNode(SrcNId); }
338  if (! IsNode(DstNId)) { AddNode(DstNId); }
339  if (GetNode(SrcNId).IsOutNId(DstNId)) { return -2; } // edge already exists
340  GetNode(SrcNId).OutNIdV.AddSorted(DstNId);
341  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
342  return -1; // edge id
343 }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1063
TNode & GetNode(const int &NId)
Definition: graph.h:413
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
TIntV InNIdV
Definition: graph.h:315
TIntV OutNIdV
Definition: graph.h:315
int TNGraph::AddEdgeUnchecked ( const int &  SrcNId,
const int &  DstNId 
)

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

Definition at line 330 of file graph.cpp.

330  {
331  GetNode(SrcNId).OutNIdV.Add(DstNId);
332  GetNode(DstNId).InNIdV.Add(SrcNId);
333  return -1; // edge id
334 }
TNode & GetNode(const int &NId)
Definition: graph.h:413
TIntV InNIdV
Definition: graph.h:315
TIntV OutNIdV
Definition: graph.h:315
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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 236 of file graph.cpp.

236  {
237  if (NId == -1) {
238  NId = MxNId; MxNId++;
239  } else {
240  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
241  MxNId = TMath::Mx(NId+1, MxNId());
242  }
243  NodeH.AddDat(NId, TNode(NId));
244  return NId;
245 }
#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:411
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:410
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 448 of file graph.h.

448 { return AddNode(NodeId.GetId()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
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 256 of file graph.cpp.

256  {
257  int NewNId;
258  if (NId == -1) {
259  NewNId = MxNId; MxNId++;
260  } else {
261  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
262  NewNId = NId;
263  MxNId = TMath::Mx(NewNId+1, MxNId());
264  }
265  TNode& Node = NodeH.AddDat(NewNId);
266  Node.Id = NewNId;
267  Node.InNIdV = InNIdV;
268  Node.OutNIdV = OutNIdV;
269  Node.InNIdV.Sort();
270  Node.OutNIdV.Sort();
271  return NewNId;
272 }
#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:411
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:410
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 276 of file graph.cpp.

276  {
277  int NewNId;
278  if (NId == -1) {
279  NewNId = MxNId; MxNId++;
280  } else {
281  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
282  NewNId = NId;
283  MxNId = TMath::Mx(NewNId+1, MxNId());
284  }
285  TNode& Node = NodeH.AddDat(NewNId);
286  Node.Id = NewNId;
287  Node.InNIdV.GenExt(Pool.GetValVPt(SrcVId), Pool.GetVLen(SrcVId));
288  Node.OutNIdV.GenExt(Pool.GetValVPt(DstVId), Pool.GetVLen(DstVId));
289  Node.InNIdV.Sort();
290  Node.OutNIdV.Sort();
291  return NewNId;
292 }
#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:411
TVal * GetValVPt(const int &VId) const
Returns pointer to the first element of the vector with id VId.
Definition: ds.h:1665
int GetVLen(const int &VId) const
Returns the number of elements in the vector with id VId.
Definition: ds.h:1663
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:410
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int TNGraph::AddNodeUnchecked ( int  NId = -1)

Adds a node of ID NId to the graph without performing checks.

Definition at line 247 of file graph.cpp.

247  {
248  if (IsNode(NId)) { return NId;}
249  MxNId = TMath::Mx(NId+1, MxNId());
250  NodeH.AddDat(NId, TNode(NId));
251  return NId;
252 }
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: graph.h:411
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
TInt MxNId
Definition: graph.h:410
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 514 of file graph.h.

514 { 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:479
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
TNodeI TNGraph::BegNI ( ) const
inline

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

Definition at line 479 of file graph.h.

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

Deletes all nodes and edges from the graph.

Definition at line 532 of file graph.h.

532 { MxNId=0; NodeH.Clr(); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
TInt MxNId
Definition: graph.h:410
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 382 of file graph.cpp.

382  {
383  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n); ) {
384  TNode& Node = NodeH[n];
385  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
386  }
387  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
388 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
THash< TInt, TNode > NodeH
Definition: graph.h:411
void Defrag()
Definition: hash.h:513
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
void Pack()
Definition: hash.h:247
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 345 of file graph.cpp.

345  {
346  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
347  { TNode& N = GetNode(SrcNId);
348  const int n = N.OutNIdV.SearchBin(DstNId);
349  if (n!= -1) { N.OutNIdV.Del(n); } }
350  { TNode& N = GetNode(DstNId);
351  const int n = N.InNIdV.SearchBin(SrcNId);
352  if (n!= -1) { N.InNIdV.Del(n); } }
353  if (! IsDir) {
354  { TNode& N = GetNode(SrcNId);
355  const int n = N.InNIdV.SearchBin(DstNId);
356  if (n!= -1) { N.InNIdV.Del(n); } }
357  { TNode& N = GetNode(DstNId);
358  const int n = N.OutNIdV.SearchBin(SrcNId);
359  if (n!= -1) { N.OutNIdV.Del(n); } }
360  }
361 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TNode & GetNode(const int &NId)
Definition: graph.h:413
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
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 294 of file graph.cpp.

294  {
295  { TNode& Node = GetNode(NId);
296  for (int e = 0; e < Node.GetOutDeg(); e++) {
297  const int nbr = Node.GetOutNId(e);
298  if (nbr == NId) { continue; }
299  TNode& N = GetNode(nbr);
300  const int n = N.InNIdV.SearchBin(NId);
301  if (n!= -1) { N.InNIdV.Del(n); }
302  }
303  for (int e = 0; e < Node.GetInDeg(); e++) {
304  const int nbr = Node.GetInNId(e);
305  if (nbr == NId) { continue; }
306  TNode& N = GetNode(nbr);
307  const int n = N.OutNIdV.SearchBin(NId);
308  if (n!= -1) { N.OutNIdV.Del(n); }
309  } }
310  NodeH.DelKey(NId);
311 }
THash< TInt, TNode > NodeH
Definition: graph.h:411
TNode & GetNode(const int &NId)
Definition: graph.h:413
void DelKey(const TKey &Key)
Definition: hash.h:362
void TNGraph::DelNode ( const TNode NodeI)
inline

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

Definition at line 475 of file graph.h.

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

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

Definition at line 437 of file graph.cpp.

437  {
438  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
439  fprintf(OutF, "-------------------------------------------------\nDirected Node Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
440  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
441  const TNode& Node = NodeH[N];
442  fprintf(OutF, " %*d]\n", NodePlaces, Node.GetId());
443  fprintf(OutF, " in [%d]", Node.GetInDeg());
444  for (int edge = 0; edge < Node.GetInDeg(); edge++) {
445  fprintf(OutF, " %*d", NodePlaces, Node.GetInNId(edge)); }
446  fprintf(OutF, "\n out[%d]", Node.GetOutDeg());
447  for (int edge = 0; edge < Node.GetOutDeg(); edge++) {
448  fprintf(OutF, " %*d", NodePlaces, Node.GetOutNId(edge)); }
449  fprintf(OutF, "\n");
450  }
451  fprintf(OutF, "\n");
452 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
THash< TInt, TNode > NodeH
Definition: graph.h:411
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
bool TNGraph::Empty ( ) const
inline

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

Definition at line 530 of file graph.h.

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

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

Definition at line 516 of file graph.h.

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

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

Definition at line 481 of file graph.h.

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

Returns the number of edges in the graph.

Definition at line 313 of file graph.cpp.

313  {
314  int edges=0;
315  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
316  edges+=NodeH[N].GetOutDeg();
317  }
318  return edges;
319 }
THash< TInt, TNode > NodeH
Definition: graph.h:411
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
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 369 of file graph.cpp.

369  {
370  const TNodeI SrcNI = GetNI(SrcNId);
371  const int NodeN = SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
372  IAssert(NodeN != -1);
373  return TEdgeI(SrcNI, EndNI(), NodeN);
374 }
#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:483
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int TNGraph::GetMxNId ( ) const
inline

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

Definition at line 487 of file graph.h.

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

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

Definition at line 483 of file graph.h.

483 { return TNodeI(NodeH.GetI(NId)); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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 376 of file graph.cpp.

376  {
377  NIdV.Gen(GetNodes(), 0);
378  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
379  NIdV.Add(NodeH.GetKey(N)); }
380 }
THash< TInt, TNode > NodeH
Definition: graph.h:411
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
TNode& TNGraph::GetNode ( const int &  NId)
inlineprivate

Definition at line 413 of file graph.h.

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

Definition at line 414 of file graph.h.

414 { return NodeH.GetDat(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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 438 of file graph.h.

438 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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 525 of file graph.h.

525 { 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:483
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:523
int TNGraph::GetRndNId ( TRnd Rnd = TInt::Rnd)
inline

Returns an ID of a random node in the graph.

Definition at line 523 of file graph.h.

523 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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:402
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 454 of file graph.cpp.

454  {
455  PNGraph G = TNGraph::New();
456  for (int i = 0; i < 5; i++) { G->AddNode(i); }
457  G->AddEdge(0,1); G->AddEdge(1,2); G->AddEdge(0,2);
458  G->AddEdge(1,3); G->AddEdge(3,4); G->AddEdge(2,3);
459  return G;
460 }
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
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 232 of file graph.cpp.

232  {
233  return HasGraphFlag(TNGraph::TNet, Flag);
234 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
Directed graph.
Definition: graph.h:307
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 363 of file graph.cpp.

363  {
364  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
365  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
366  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
367 }
TNode & GetNode(const int &NId)
Definition: graph.h:413
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
bool IsOutNId(const int &NId) const
Definition: graph.h:330
bool TNGraph::IsNode ( const int &  NId) const
inline

Tests whether ID NId is a node.

Definition at line 477 of file graph.h.

477 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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 391 of file graph.cpp.

391  {
392  bool RetVal = true;
393  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
394  const TNode& Node = NodeH[N];
395  if (! Node.OutNIdV.IsSorted()) {
396  const TStr Msg = TStr::Fmt("Out-neighbor list of node %d is not sorted.", Node.GetId());
397  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
398  }
399  if (! Node.InNIdV.IsSorted()) {
400  const TStr Msg = TStr::Fmt("In-neighbor list of node %d is not sorted.", Node.GetId());
401  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
402  }
403  // check out-edges
404  int prevNId = -1;
405  for (int e = 0; e < Node.GetOutDeg(); e++) {
406  if (! IsNode(Node.GetOutNId(e))) {
407  const TStr Msg = TStr::Fmt("Out-edge %d --> %d: node %d does not exist.",
408  Node.GetId(), Node.GetOutNId(e), Node.GetOutNId(e));
409  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
410  }
411  if (e > 0 && prevNId == Node.GetOutNId(e)) {
412  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge %d --> %d.",
413  Node.GetId(), Node.GetId(), Node.GetOutNId(e));
414  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
415  }
416  prevNId = Node.GetOutNId(e);
417  }
418  // check in-edges
419  prevNId = -1;
420  for (int e = 0; e < Node.GetInDeg(); e++) {
421  if (! IsNode(Node.GetInNId(e))) {
422  const TStr Msg = TStr::Fmt("In-edge %d <-- %d: node %d does not exist.",
423  Node.GetId(), Node.GetInNId(e), Node.GetInNId(e));
424  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
425  }
426  if (e > 0 && prevNId == Node.GetInNId(e)) {
427  const TStr Msg = TStr::Fmt("Node %d has duplidate in-edge %d <-- %d.",
428  Node.GetId(), Node.GetId(), Node.GetInNId(e));
429  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
430  }
431  prevNId = Node.GetInNId(e);
432  }
433  }
434  return RetVal;
435 }
THash< TInt, TNode > NodeH
Definition: graph.h:411
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:477
int FFirstKeyId() const
Definition: hash.h:236
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 431 of file graph.h.

431 { return PNGraph(new TNGraph(SIn)); }
TNGraph()
Definition: graph.h:416
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 425 of file graph.h.

425 { return new TNGraph(); }
TNGraph()
Definition: graph.h:416
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 429 of file graph.h.

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

Definition at line 434 of file graph.h.

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

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

Definition at line 534 of file graph.h.

534 { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
THash< TInt, TNode > NodeH
Definition: graph.h:411
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 536 of file graph.h.

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

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

Definition at line 538 of file graph.h.

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

Saves the graph to a (binary) stream SOut.

Definition at line 423 of file graph.h.

423 { MxNId.Save(SOut); NodeH.Save(SOut); }
void Save(TSOut &SOut) const
Definition: dt.h:1060
THash< TInt, TNode > NodeH
Definition: graph.h:411
void Save(TSOut &SOut) const
Definition: hash.h:141
TInt MxNId
Definition: graph.h:410
void TNGraph::SortNodeAdjV ( )
inline

Sorts the adjacency lists of each node.

Definition at line 540 of file graph.h.

540 { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { NI.SortNIdV();} }
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:479
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481

Friends And Related Function Documentation

friend class TNGraphMtx
friend

Definition at line 562 of file graph.h.

friend class TPt< TNGraph >
friend

Definition at line 561 of file graph.h.

Member Data Documentation

TCRef TNGraph::CRef
private

Definition at line 409 of file graph.h.

TInt TNGraph::MxNId
private

Definition at line 410 of file graph.h.

THash<TInt, TNode> TNGraph::NodeH
private

Definition at line 411 of file graph.h.


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