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
TNEGraph Class Reference

Directed multigraph. More...

#include <graph.h>

Classes

class  TEdge
 
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 TNEGraph TNet
 
typedef TPt< TNEGraphPNet
 

Public Member Functions

 TNEGraph ()
 
 TNEGraph (const int &Nodes, const int &Edges)
 Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. More...
 
 TNEGraph (const TNEGraph &Graph)
 
 TNEGraph (TSIn &SIn)
 Constructor for loading 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...
 
TNEGraphoperator= (const TNEGraph &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...
 
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, int EId=-1)
 Adds an edge with ID EId between node IDs SrcNId and DstNId to the graph. More...
 
int AddEdge (const TEdgeI &EdgeI)
 Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph. More...
 
void DelEdge (const int &EId)
 Deletes an edge with edge ID EId from the graph. More...
 
void DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true)
 Deletes all edges between node IDs SrcNId and DstNId from the graph. More...
 
bool IsEdge (const int &EId) const
 Tests whether an edge with edge ID EId exists in the graph. More...
 
bool IsEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
 Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. More...
 
bool IsEdge (const int &SrcNId, const int &DstNId, int &EId, const bool &IsDir=true) const
 Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. if an edge exists, return its edge ID in EId. More...
 
int GetEId (const int &SrcNId, const int &DstNId) const
 Returns an edge ID between node IDs SrcNId and DstNId, if such an edge exists. Otherwise, return -1. 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
 Returns an iterator referring to edge with edge ID EId. 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...
 
int GetRndEId (TRnd &Rnd=TInt::Rnd)
 Returns an ID of a random edge in the graph. More...
 
TEdgeI GetRndEI (TRnd &Rnd=TInt::Rnd)
 Returns an interator referring to a random edge in the graph. More...
 
void GetNIdV (TIntV &NIdV) const
 Gets a vector IDs of all nodes in the graph. More...
 
void GetEIdV (TIntV &EIdV) const
 Gets a vector IDs of all edges 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 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 PNEGraph New ()
 Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New(). More...
 
static PNEGraph 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 PNEGraph Load (TSIn &SIn)
 Static constructor that loads the graph from a stream SIn and returns a pointer to it. More...
 
static PNEGraph GetSmallGraph ()
 Returns a small multigraph on 5 nodes and 6 edges. More...
 

Private Member Functions

TNodeGetNode (const int &NId)
 
const TNodeGetNode (const int &NId) const
 
TEdgeGetEdge (const int &EId)
 
const TEdgeGetEdge (const int &EId) const
 

Private Attributes

TCRef CRef
 
TInt MxNId
 
TInt MxEId
 
THash< TInt, TNodeNodeH
 
THash< TInt, TEdgeEdgeH
 

Friends

class TPt< TNEGraph >
 

Detailed Description

Directed multigraph.

Node IDs can be arbitrary non-negative integers. Edges have IDs. Nodes and edges have no attributes/data associated with them. There can be more than one directed edge from one source node to a destination node. Self loops (one per node) are allowed as well as multiple (parallel) edges. The directed multigraph 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 563 of file graph.h.

Member Typedef Documentation

Definition at line 566 of file graph.h.

Definition at line 565 of file graph.h.

Constructor & Destructor Documentation

TNEGraph::TNEGraph ( )
inline

Definition at line 698 of file graph.h.

698 : CRef(), MxNId(0), MxEId(0) { }
TCRef CRef
Definition: graph.h:693
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694
TNEGraph::TNEGraph ( const int &  Nodes,
const int &  Edges 
)
inlineexplicit

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

Definition at line 700 of file graph.h.

700 : CRef(), MxNId(0), MxEId(0) { Reserve(Nodes, Edges); }
TCRef CRef
Definition: graph.h:693
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:800
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694
TNEGraph::TNEGraph ( const TNEGraph Graph)
inline

Definition at line 701 of file graph.h.

701 : MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { }
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694
TNEGraph::TNEGraph ( TSIn SIn)
inline

Constructor for loading the graph from a (binary) stream SIn.

Definition at line 703 of file graph.h.

703 : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { }
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694

Member Function Documentation

int TNEGraph::AddEdge ( const int &  SrcNId,
const int &  DstNId,
int  EId = -1 
)

Adds an edge with ID EId between node IDs SrcNId and DstNId to the graph.

Returns the ID of the edge being added. If EId is -1, edge ID is automatically assigned. Aborts, if an edge with ID EId already exists. Aborts, if SrcNId or DstNId are not nodes in the graph.

Definition at line 466 of file graph.cpp.

466  {
467  if (EId == -1) { EId = MxEId; MxEId++; }
468  else { MxEId = TMath::Mx(EId+1, MxEId()); }
469  IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId));
470  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
471  EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId));
472  GetNode(SrcNId).OutEIdV.AddSorted(EId);
473  GetNode(DstNId).InEIdV.AddSorted(EId);
474  return EId;
475 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1027
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TIntV OutEIdV
Definition: graph.h:571
TNode & GetNode(const int &NId)
Definition: graph.h:688
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxEId
Definition: graph.h:694
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:736
TIntV InEIdV
Definition: graph.h:571
int TNEGraph::AddEdge ( const TEdgeI EdgeI)
inline

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

Definition at line 756 of file graph.h.

756 { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); }
int AddEdge(const int &SrcNId, const int &DstNId, int EId=-1)
Adds an edge with ID EId between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:466
int TNEGraph::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 436 of file graph.cpp.

436  {
437  if (NId == -1) {
438  NId = MxNId; MxNId++;
439  } else {
440  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
441  MxNId = TMath::Mx(NId+1, MxNId());
442  }
443  NodeH.AddDat(NId, TNode(NId));
444  return NId;
445 }
#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:695
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TInt MxNId
Definition: graph.h:694
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:736
int TNEGraph::AddNode ( const TNodeI NodeId)
inline

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

Definition at line 728 of file graph.h.

728 { return AddNode(NodeId.GetId()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:436
TEdgeI TNEGraph::BegEI ( ) const
inline

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

Definition at line 774 of file graph.h.

774 { return TEdgeI(EdgeH.BegI(), this); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TNodeI TNEGraph::BegNI ( ) const
inline

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

Definition at line 738 of file graph.h.

738 { return TNodeI(NodeH.BegI(), this); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TNode > NodeH
Definition: graph.h:695
void TNEGraph::Clr ( )
inline

Deletes all nodes and edges from the graph.

Definition at line 798 of file graph.h.

798 { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); }
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694
void TNEGraph::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 527 of file graph.cpp.

527  {
528  for (int kid = NodeH.FFirstKeyId(); NodeH.FNextKeyId(kid); ) {
529  TNode& Node = NodeH[kid];
530  Node.InEIdV.Pack(); Node.OutEIdV.Pack();
531  }
532  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
533  if (! OnlyNodeLinks && ! EdgeH.IsKeyIdEqKeyN()) { EdgeH.Defrag(); }
534 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
THash< TInt, TNode > NodeH
Definition: graph.h:695
void Defrag()
Definition: hash.h:509
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Pack()
Definition: hash.h:243
void TNEGraph::DelEdge ( const int &  EId)

Deletes an edge with edge ID EId from the graph.

Definition at line 477 of file graph.cpp.

477  {
478  IAssert(IsEdge(EId));
479  const int SrcNId = GetEdge(EId).GetSrcNId();
480  const int DstNId = GetEdge(EId).GetDstNId();
481  GetNode(SrcNId).OutEIdV.DelIfIn(EId);
482  GetNode(DstNId).InEIdV.DelIfIn(EId);
483  EdgeH.DelKey(EId);
484 }
#define IAssert(Cond)
Definition: bd.h:262
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1115
int GetSrcNId() const
Definition: graph.h:599
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
int GetDstNId() const
Definition: graph.h:600
void DelKey(const TKey &Key)
Definition: hash.h:358
TEdge & GetEdge(const int &EId)
Definition: graph.h:690
TIntV OutEIdV
Definition: graph.h:571
TNode & GetNode(const int &NId)
Definition: graph.h:688
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
TIntV InEIdV
Definition: graph.h:571
void TNEGraph::DelEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
)

Deletes all edges between node IDs SrcNId and 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 487 of file graph.cpp.

487  {
488  int EId;
489  IAssert(IsEdge(SrcNId, DstNId, EId, IsDir)); // there is at least one edge
490  while (IsEdge(SrcNId, DstNId, EId, IsDir)) {
491  GetNode(SrcNId).OutEIdV.DelIfIn(EId);
492  GetNode(DstNId).InEIdV.DelIfIn(EId);
493  }
494  EdgeH.DelKey(EId);
495 }
#define IAssert(Cond)
Definition: bd.h:262
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1115
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
void DelKey(const TKey &Key)
Definition: hash.h:358
TIntV OutEIdV
Definition: graph.h:571
TNode & GetNode(const int &NId)
Definition: graph.h:688
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
TIntV InEIdV
Definition: graph.h:571
void TNEGraph::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 447 of file graph.cpp.

447  {
448  const TNode& Node = GetNode(NId);
449  for (int out = 0; out < Node.GetOutDeg(); out++) {
450  const int EId = Node.GetOutEId(out);
451  const TEdge& Edge = GetEdge(EId);
452  IAssert(Edge.GetSrcNId() == NId);
453  GetNode(Edge.GetDstNId()).InEIdV.DelIfIn(EId);
454  EdgeH.DelKey(EId);
455  }
456  for (int in = 0; in < Node.GetInDeg(); in++) {
457  const int EId = Node.GetInEId(in);
458  const TEdge& Edge = GetEdge(EId);
459  IAssert(Edge.GetDstNId() == NId);
460  GetNode(Edge.GetSrcNId()).OutEIdV.DelIfIn(EId);
461  EdgeH.DelKey(EId);
462  }
463  NodeH.DelKey(NId);
464 }
#define IAssert(Cond)
Definition: bd.h:262
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
void DelKey(const TKey &Key)
Definition: hash.h:358
TEdge & GetEdge(const int &EId)
Definition: graph.h:690
TNode & GetNode(const int &NId)
Definition: graph.h:688
void TNEGraph::DelNode ( const TNode NodeI)
inline

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

Definition at line 734 of file graph.h.

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

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

Definition at line 589 of file graph.cpp.

589  {
590  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
591  const int EdgePlaces = (int) ceil(log10((double) GetEdges()));
592  fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
593  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
594  fprintf(OutF, " %*d]\n", NodePlaces, NodeI.GetId());
595  fprintf(OutF, " in[%d]", NodeI.GetInDeg());
596  for (int edge = 0; edge < NodeI.GetInDeg(); edge++) {
597  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); }
598  fprintf(OutF, "\n out[%d]", NodeI.GetOutDeg());
599  for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) {
600  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); }
601  fprintf(OutF, "\n");
602  }
603  for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) {
604  fprintf(OutF, " %*d] %*d -> %*d\n", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId());
605  }
606  fprintf(OutF, "\n");
607 }
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:774
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:776
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:738
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:740
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:720
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.h:747
bool TNEGraph::Empty ( ) const
inline

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

Definition at line 796 of file graph.h.

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

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

Definition at line 776 of file graph.h.

776 { return TEdgeI(EdgeH.EndI(), this); }
TIter EndI() const
Definition: hash.h:176
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TNodeI TNEGraph::EndNI ( ) const
inline

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

Definition at line 740 of file graph.h.

740 { return TNodeI(NodeH.EndI(), this); }
TIter EndI() const
Definition: hash.h:176
THash< TInt, TNode > NodeH
Definition: graph.h:695
TEdge& TNEGraph::GetEdge ( const int &  EId)
inlineprivate

Definition at line 690 of file graph.h.

690 { return EdgeH.GetDat(EId); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
const TEdge& TNEGraph::GetEdge ( const int &  EId) const
inlineprivate

Definition at line 691 of file graph.h.

691 { return EdgeH.GetDat(EId); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
int TNEGraph::GetEdges ( ) const
inline

Returns the number of edges in the graph.

Definition at line 747 of file graph.h.

747 { return EdgeH.Len(); }
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
int Len() const
Definition: hash.h:186
TEdgeI TNEGraph::GetEI ( const int &  EId) const
inline

Returns an iterator referring to edge with edge ID EId.

Definition at line 778 of file graph.h.

778 { return TEdgeI(EdgeH.GetI(EId), this); }
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TIter GetI(const TKey &Key) const
Definition: hash.h:178
TEdgeI TNEGraph::GetEI ( const int &  SrcNId,
const int &  DstNId 
) const
inline

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

Definition at line 780 of file graph.h.

780 { return GetEI(GetEId(SrcNId, DstNId)); }
TEdgeI GetEI(const int &EId) const
Returns an iterator referring to edge with edge ID EId.
Definition: graph.h:778
int GetEId(const int &SrcNId, const int &DstNId) const
Returns an edge ID between node IDs SrcNId and DstNId, if such an edge exists. Otherwise, return -1.
Definition: graph.h:772
int TNEGraph::GetEId ( const int &  SrcNId,
const int &  DstNId 
) const
inline

Returns an edge ID between node IDs SrcNId and DstNId, if such an edge exists. Otherwise, return -1.

Definition at line 772 of file graph.h.

772 { int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; }
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
void TNEGraph::GetEIdV ( TIntV EIdV) const

Gets a vector IDs of all edges in the graph.

Definition at line 520 of file graph.cpp.

520  {
521  EIdV.Gen(GetEdges(), 0);
522  for (int E=EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
523  EIdV.Add(EdgeH.GetKey(E));
524  }
525 }
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.h:747
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
int TNEGraph::GetMxNId ( ) const
inline

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

Definition at line 744 of file graph.h.

744 { return MxNId; }
TInt MxNId
Definition: graph.h:694
TNodeI TNEGraph::GetNI ( const int &  NId) const
inline

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

Definition at line 742 of file graph.h.

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

Gets a vector IDs of all nodes in the graph.

Definition at line 514 of file graph.cpp.

514  {
515  NIdV.Gen(GetNodes(), 0);
516  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
517  NIdV.Add(NodeH.GetKey(N)); }
518 }
THash< TInt, TNode > NodeH
Definition: graph.h:695
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:720
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& TNEGraph::GetNode ( const int &  NId)
inlineprivate

Definition at line 688 of file graph.h.

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

Definition at line 689 of file graph.h.

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

Returns the number of nodes in the graph.

Definition at line 720 of file graph.h.

720 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: graph.h:695
int Len() const
Definition: hash.h:186
TEdgeI TNEGraph::GetRndEI ( TRnd Rnd = TInt::Rnd)
inline

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

Definition at line 789 of file graph.h.

789 { return GetEI(GetRndEId(Rnd)); }
TEdgeI GetEI(const int &EId) const
Returns an iterator referring to edge with edge ID EId.
Definition: graph.h:778
int GetRndEId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random edge in the graph.
Definition: graph.h:787
int TNEGraph::GetRndEId ( TRnd Rnd = TInt::Rnd)
inline

Returns an ID of a random edge in the graph.

Definition at line 787 of file graph.h.

787 { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
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
TNodeI TNEGraph::GetRndNI ( TRnd Rnd = TInt::Rnd)
inline

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

Definition at line 785 of file graph.h.

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

Returns an ID of a random node in the graph.

Definition at line 783 of file graph.h.

783 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: graph.h:695
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
PNEGraph TNEGraph::GetSmallGraph ( )
static

Returns a small multigraph on 5 nodes and 6 edges.

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

Definition at line 610 of file graph.cpp.

610  {
611  PNEGraph Graph = TNEGraph::New();
612  for (int i = 0; i < 5; i++) { Graph->AddNode(i); }
613  Graph->AddEdge(0,1); Graph->AddEdge(0,2);
614  Graph->AddEdge(0,3); Graph->AddEdge(0,4);
615  Graph->AddEdge(1,2); Graph->AddEdge(1,2);
616  return Graph;
617 }
static PNEGraph New()
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New().
Definition: graph.h:707
Definition: bd.h:196
bool TNEGraph::HasFlag ( const TGraphFlag Flag) const

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

Definition at line 414 of file graph.cpp.

414  {
415  return HasGraphFlag(TNEGraph::TNet, Flag);
416 }
Directed multigraph.
Definition: graph.h:563
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:38
bool TNEGraph::IsEdge ( const int &  EId) const
inline

Tests whether an edge with edge ID EId exists in the graph.

Definition at line 766 of file graph.h.

766 { return EdgeH.IsKey(EId); }
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool TNEGraph::IsEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
) const
inline

Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.

Definition at line 768 of file graph.h.

768 { int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); }
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
bool TNEGraph::IsEdge ( const int &  SrcNId,
const int &  DstNId,
int &  EId,
const bool &  IsDir = true 
) const

Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. if an edge exists, return its edge ID in EId.

Definition at line 497 of file graph.cpp.

497  {
498  const TNode& SrcNode = GetNode(SrcNId);
499  for (int edge = 0; edge < SrcNode.GetOutDeg(); edge++) {
500  const TEdge& Edge = GetEdge(SrcNode.GetOutEId(edge));
501  if (DstNId == Edge.GetDstNId()) {
502  EId = Edge.GetId(); return true; }
503  }
504  if (! IsDir) {
505  for (int edge = 0; edge < SrcNode.GetInDeg(); edge++) {
506  const TEdge& Edge = GetEdge(SrcNode.GetInEId(edge));
507  if (DstNId == Edge.GetSrcNId()) {
508  EId = Edge.GetId(); return true; }
509  }
510  }
511  return false;
512 }
TEdge & GetEdge(const int &EId)
Definition: graph.h:690
TNode & GetNode(const int &NId)
Definition: graph.h:688
int GetId() const
Definition: graph.h:598
bool TNEGraph::IsNode ( const int &  NId) const
inline

Tests whether ID NId is a node.

Definition at line 736 of file graph.h.

736 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: graph.h:695
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool TNEGraph::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 536 of file graph.cpp.

536  {
537 bool RetVal = true;
538  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
539  const TNode& Node = NodeH[N];
540  if (! Node.OutEIdV.IsSorted()) {
541  const TStr Msg = TStr::Fmt("Out-edge list of node %d is not sorted.", Node.GetId());
542  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
543  }
544  if (! Node.InEIdV.IsSorted()) {
545  const TStr Msg = TStr::Fmt("In-edge list of node %d is not sorted.", Node.GetId());
546  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
547  }
548  // check out-edge ids
549  int prevEId = -1;
550  for (int e = 0; e < Node.GetOutDeg(); e++) {
551  if (! IsEdge(Node.GetOutEId(e))) {
552  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetOutEId(e), Node.GetId());
553  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
554  }
555  if (e > 0 && prevEId == Node.GetOutEId(e)) {
556  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetOutEId(e));
557  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
558  }
559  prevEId = Node.GetOutEId(e);
560  }
561  // check in-edge ids
562  prevEId = -1;
563  for (int e = 0; e < Node.GetInDeg(); e++) {
564  if (! IsEdge(Node.GetInEId(e))) {
565  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetInEId(e), Node.GetId());
566  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
567  }
568  if (e > 0 && prevEId == Node.GetInEId(e)) {
569  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetInEId(e));
570  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
571  }
572  prevEId = Node.GetInEId(e);
573  }
574  }
575  for (int E = EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
576  const TEdge& Edge = EdgeH[E];
577  if (! IsNode(Edge.GetSrcNId())) {
578  const TStr Msg = TStr::Fmt("Edge %d source node %d does not exist.", Edge.GetId(), Edge.GetSrcNId());
579  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
580  }
581  if (! IsNode(Edge.GetDstNId())) {
582  const TStr Msg = TStr::Fmt("Edge %d destination node %d does not exist.", Edge.GetId(), Edge.GetDstNId());
583  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
584  }
585  }
586  return RetVal;
587 }
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:766
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
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:736
static PNEGraph TNEGraph::Load ( TSIn SIn)
inlinestatic

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

Definition at line 713 of file graph.h.

713 { return PNEGraph(new TNEGraph(SIn)); }
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TNEGraph()
Definition: graph.h:698
static PNEGraph TNEGraph::New ( )
inlinestatic

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

Definition at line 707 of file graph.h.

707 { return PNEGraph(new TNEGraph()); }
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TNEGraph()
Definition: graph.h:698
static PNEGraph TNEGraph::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: PNEGraph Graph = TNEGraph::New(Nodes, Edges).

Definition at line 711 of file graph.h.

711 { return PNEGraph(new TNEGraph(Nodes, Edges)); }
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TNEGraph()
Definition: graph.h:698
TNEGraph& TNEGraph::operator= ( const TNEGraph Graph)
inline

Definition at line 716 of file graph.h.

716  { if (this!=&Graph) {
717  MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; } return *this; }
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694
void TNEGraph::Reserve ( const int &  Nodes,
const int &  Edges 
)
inline

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

Definition at line 800 of file graph.h.

800  {
801  if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } }
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
void Gen(const int &ExpectVals)
Definition: hash.h:180
void TNEGraph::Save ( TSOut SOut) const
inline

Saves the graph to a (binary) stream SOut.

Definition at line 705 of file graph.h.

705 { MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut); }
void Save(TSOut &SOut) const
Definition: dt.h:1058
void Save(TSOut &SOut) const
Definition: hash.h:141
THash< TInt, TNode > NodeH
Definition: graph.h:695
THash< TInt, TEdge > EdgeH
Definition: graph.h:696
TInt MxNId
Definition: graph.h:694
TInt MxEId
Definition: graph.h:694

Friends And Related Function Documentation

friend class TPt< TNEGraph >
friend

Definition at line 822 of file graph.h.

Member Data Documentation

TCRef TNEGraph::CRef
private

Definition at line 693 of file graph.h.

THash<TInt, TEdge> TNEGraph::EdgeH
private

Definition at line 696 of file graph.h.

TInt TNEGraph::MxEId
private

Definition at line 694 of file graph.h.

TInt TNEGraph::MxNId
private

Definition at line 694 of file graph.h.

THash<TInt, TNode> TNEGraph::NodeH
private

Definition at line 695 of file graph.h.


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