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
TNodeNet< TNodeData > Class Template Reference

Node Network (directed graph, TNGraph with data on nodes only). More...

#include <network.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 TNodeData TNodeDat
 
typedef TNodeNet< TNodeData > TNet
 
typedef TPt< TNetPNet
 

Public Member Functions

 TNodeNet ()
 
 TNodeNet (const int &Nodes, const int &Edges)
 Constructor that reserves enough memory for a network of Nodes nodes and Edges edges. More...
 
 TNodeNet (const TNodeNet &NodeNet)
 
 TNodeNet (TSIn &SIn)
 Constructor that loads the network from a (binary) stream SIn. More...
 
virtual ~TNodeNet ()
 
virtual void Save (TSOut &SOut) const
 Saves the network to a (binary) stream SOut. More...
 
bool HasFlag (const TGraphFlag &Flag) const
 Allows for run-time checking the type of the network (see the TGraphFlag for flags). More...
 
TNodeNetoperator= (const TNodeNet &NodeNet)
 
int GetNodes () const
 Returns the number of nodes in the network. More...
 
int AddNode (int NId=-1)
 Adds a node of ID NId to the network. More...
 
int AddNode (int NId, const TNodeData &NodeDat)
 Adds a node of ID NId and node data NodeDat to the network. More...
 
int AddNode (const TNodeI &NodeI)
 Adds a node NodeI and its node data to the network. More...
 
void DelNode (const int &NId)
 Deletes node of ID NId from the network. More...
 
void DelNode (const TNode &NodeI)
 Deletes node of ID NodeI.GetId() from the network. 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 network. More...
 
TNodeI EndNI () const
 Returns an iterator referring to the past-the-end node in the network. More...
 
TNodeI GetNI (const int &NId) const
 Returns an iterator referring to the node of ID NId in the network. More...
 
const TNodeGetNode (const int &NId) const
 Returns node element for the node of ID NId in the network. More...
 
void SetNDat (const int &NId, const TNodeData &NodeDat)
 Sets node data for the node of ID NId in the network. More...
 
TNodeData & GetNDat (const int &NId)
 Returns node data for the node of ID NId in the network. More...
 
const TNodeData & GetNDat (const int &NId) const
 Returns node data for the node of ID NId in the network. More...
 
int GetMxNId () const
 Returns an ID that is larger than any node ID in the network. More...
 
int GetEdges () const
 Returns the number of edges in the network. More...
 
int AddEdge (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the network. More...
 
int AddEdge (const TEdgeI &EdgeI)
 Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the network. More...
 
void DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true)
 Deletes an edge from node IDs SrcNId to DstNId from the network. 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 network. More...
 
TEdgeI BegEI () const
 Returns an iterator referring to the first edge in the network. More...
 
TEdgeI EndEI () const
 Returns an iterator referring to the past-the-end edge in the network. 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 network. More...
 
int GetRndNId (TRnd &Rnd=TInt::Rnd)
 Returns an ID of a random node in the network. More...
 
TNodeI GetRndNI (TRnd &Rnd=TInt::Rnd)
 Returns an interator referring to a random node in the network. More...
 
void GetNIdV (TIntV &NIdV) const
 Gets a vector IDs of all nodes in the network. More...
 
bool Empty () const
 Tests whether the network is empty (has zero nodes). More...
 
void Clr (const bool &DoDel=true, const bool &ResetDat=true)
 Deletes all nodes and edges from the network. More...
 
void Reserve (const int &Nodes, const int &Edges)
 Reserves memory for a network of Nodes nodes and Edges edges. More...
 
void SortNIdById (const bool &Asc=true)
 Sorts nodes by node IDs. More...
 
void SortNIdByDat (const bool &Asc=true)
 Sorts nodes by node data. More...
 
void Defrag (const bool &OnlyNodeLinks=false)
 Defragments the network. More...
 
bool IsOk (const bool &ThrowExcept=true) const
 Checks the network data structure for internal consistency. More...
 

Static Public Member Functions

static PNet New ()
 Static constructor that returns a pointer to the network. Call: TPt <TNodeNet<TNodeData> > Net = TNodeNet<TNodeData>::New(). More...
 
static PNet Load (TSIn &SIn)
 Static constructor that loads the network from a stream SIn and returns a pointer to it. More...
 

Protected Member Functions

TNodeGetNode (const int &NId)
 

Protected Attributes

TCRef CRef
 
TInt MxNId
 
THash< TInt, TNodeNodeH
 

Friends

class TPt< TNodeNet< TNodeData > >
 

Detailed Description

template<class TNodeData>
class TNodeNet< TNodeData >

Node Network (directed graph, TNGraph with data on nodes only).

Definition at line 14 of file network.h.

Member Typedef Documentation

template<class TNodeData>
typedef TPt<TNet> TNodeNet< TNodeData >::PNet

Definition at line 18 of file network.h.

template<class TNodeData>
typedef TNodeNet<TNodeData> TNodeNet< TNodeData >::TNet

Definition at line 17 of file network.h.

template<class TNodeData>
typedef TNodeData TNodeNet< TNodeData >::TNodeDat

Definition at line 16 of file network.h.

Constructor & Destructor Documentation

template<class TNodeData>
TNodeNet< TNodeData >::TNodeNet ( )
inline

Definition at line 138 of file network.h.

138 : CRef(), MxNId(0), NodeH() { }
THash< TInt, TNode > NodeH
Definition: network.h:135
TCRef CRef
Definition: network.h:133
TInt MxNId
Definition: network.h:134
template<class TNodeData>
TNodeNet< TNodeData >::TNodeNet ( const int &  Nodes,
const int &  Edges 
)
inlineexplicit

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

Definition at line 140 of file network.h.

140 : MxNId(0) { Reserve(Nodes, Edges); }
TInt MxNId
Definition: network.h:134
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a network of Nodes nodes and Edges edges.
Definition: network.h:240
template<class TNodeData>
TNodeNet< TNodeData >::TNodeNet ( const TNodeNet< TNodeData > &  NodeNet)
inline

Definition at line 141 of file network.h.

141 : MxNId(NodeNet.MxNId), NodeH(NodeNet.NodeH) { }
THash< TInt, TNode > NodeH
Definition: network.h:135
TInt MxNId
Definition: network.h:134
template<class TNodeData>
TNodeNet< TNodeData >::TNodeNet ( TSIn SIn)
inline

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

Definition at line 143 of file network.h.

143 : MxNId(SIn), NodeH(SIn) { }
THash< TInt, TNode > NodeH
Definition: network.h:135
TInt MxNId
Definition: network.h:134
template<class TNodeData>
virtual TNodeNet< TNodeData >::~TNodeNet ( )
inlinevirtual

Definition at line 144 of file network.h.

144 { }

Member Function Documentation

template<class TNodeData >
int TNodeNet< TNodeData >::AddEdge ( const int &  SrcNId,
const int &  DstNId 
)

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

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 TNodeNet have no IDs we return -1. Function aborts if SrcNId or DstNId are not nodes in the network.

Definition at line 331 of file network.h.

331  {
332  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
333  if (IsEdge(SrcNId, DstNId)) { return -2; }
334  GetNode(SrcNId).OutNIdV.AddSorted(DstNId);
335  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
336  return -1; // edge id
337 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TNode & GetNode(const int &NId)
Definition: network.h:130
TIntV InNIdV
Definition: network.h:24
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1027
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
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 network.
Definition: network.h:351
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TIntV OutNIdV
Definition: network.h:24
template<class TNodeData>
int TNodeNet< TNodeData >::AddEdge ( const TEdgeI EdgeI)
inline

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

Definition at line 209 of file network.h.

209 { 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 network.
Definition: network.h:331
template<class TNodeData >
int TNodeNet< TNodeData >::AddNode ( int  NId = -1)

Adds a node of ID NId to the network.

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 273 of file network.h.

273  {
274  if (NId == -1) {
275  NId = MxNId; MxNId++;
276  } else {
277  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
278  MxNId = TMath::Mx(NId+1, MxNId());
279  }
280  NodeH.AddDat(NId, TNode(NId));
281  return NId;
282 }
#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: network.h:135
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
TInt MxNId
Definition: network.h:134
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class TNodeData>
int TNodeNet< TNodeData >::AddNode ( int  NId,
const TNodeData &  NodeDat 
)

Adds a node of ID NId and node data NodeDat to the network.

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 285 of file network.h.

285  {
286  if (NId == -1) {
287  NId = MxNId; MxNId++;
288  } else {
289  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
290  MxNId = TMath::Mx(NId+1, MxNId());
291  }
292  NodeH.AddDat(NId, TNode(NId, NodeDat));
293  return NId;
294 }
#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: network.h:135
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
TInt MxNId
Definition: network.h:134
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class TNodeData>
int TNodeNet< TNodeData >::AddNode ( const TNodeI NodeI)
inline

Adds a node NodeI and its node data to the network.

Definition at line 171 of file network.h.

171 { return AddNode(NodeI.GetId(), NodeI.GetDat()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:273
template<class TNodeData>
TEdgeI TNodeNet< TNodeData >::BegEI ( ) const
inline

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

Definition at line 219 of file network.h.

219 { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0) NI++; return TEdgeI(NI, EndNI()); }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:181
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:71
template<class TNodeData>
TNodeI TNodeNet< TNodeData >::BegNI ( ) const
inline

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

Definition at line 181 of file network.h.

181 { return TNodeI(NodeH.BegI(), this); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TNode > NodeH
Definition: network.h:135
template<class TNodeData>
void TNodeNet< TNodeData >::Clr ( const bool &  DoDel = true,
const bool &  ResetDat = true 
)
inline

Deletes all nodes and edges from the network.

Definition at line 237 of file network.h.

237  {
238  MxNId = 0; NodeH.Clr(DoDel, -1, ResetDat); }
THash< TInt, TNode > NodeH
Definition: network.h:135
TInt MxNId
Definition: network.h:134
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
template<class TNodeData >
void TNodeNet< TNodeData >::Defrag ( const bool &  OnlyNodeLinks = false)

Defragments the network.

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

Definition at line 373 of file network.h.

373  {
374  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n); ) {
375  TNode& Node = NodeH[n];
376  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
377  }
378  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) {
379  NodeH.Defrag(); }
380 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
THash< TInt, TNode > NodeH
Definition: network.h:135
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
template<class TNodeData >
void TNodeNet< TNodeData >::DelEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
)

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

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

Definition at line 340 of file network.h.

340  {
341  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
342  GetNode(SrcNId).OutNIdV.DelIfIn(DstNId);
343  GetNode(DstNId).InNIdV.DelIfIn(SrcNId);
344  if (! IsDir) {
345  GetNode(DstNId).OutNIdV.DelIfIn(SrcNId);
346  GetNode(SrcNId).InNIdV.DelIfIn(DstNId);
347  }
348 }
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1115
#define IAssertR(Cond, Reason)
Definition: bd.h:265
TNode & GetNode(const int &NId)
Definition: network.h:130
TIntV InNIdV
Definition: network.h:24
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TIntV OutNIdV
Definition: network.h:24
template<class TNodeData >
void TNodeNet< TNodeData >::DelNode ( const int &  NId)

Deletes node of ID NId from the network.

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

Definition at line 297 of file network.h.

297  {
298  { TNode& Node = GetNode(NId);
299  for (int e = 0; e < Node.GetOutDeg(); e++) {
300  const int nbr = Node.GetOutNId(e);
301  if (nbr == NId) { continue; }
302  TNode& N = GetNode(nbr);
303  int n = N.InNIdV.SearchBin(NId);
304  if (n!= -1) { N.InNIdV.Del(n); }
305  }
306  for (int e = 0; e < Node.GetInDeg(); e++) {
307  const int nbr = Node.GetInNId(e);
308  if (nbr == NId) { continue; }
309  TNode& N = GetNode(nbr);
310  int n = N.OutNIdV.SearchBin(NId);
311  if (n!= -1) { N.OutNIdV.Del(n); }
312  } }
313  NodeH.DelKey(NId);
314 }
TNode & GetNode(const int &NId)
Definition: network.h:130
THash< TInt, TNode > NodeH
Definition: network.h:135
void DelKey(const TKey &Key)
Definition: hash.h:358
template<class TNodeData>
void TNodeNet< TNodeData >::DelNode ( const TNode NodeI)
inline

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

Definition at line 177 of file network.h.

177 { DelNode(NodeI.GetId()); }
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.h:297
template<class TNodeData>
bool TNodeNet< TNodeData >::Empty ( ) const
inline

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

Definition at line 235 of file network.h.

235 { return GetNodes()==0; }
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
template<class TNodeData>
TEdgeI TNodeNet< TNodeData >::EndEI ( ) const
inline

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

Definition at line 221 of file network.h.

221 { return TEdgeI(EndNI(), EndNI()); }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
template<class TNodeData>
TNodeI TNodeNet< TNodeData >::EndNI ( ) const
inline

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

Definition at line 183 of file network.h.

183 { return TNodeI(NodeH.EndI(), this); }
THash< TInt, TNode > NodeH
Definition: network.h:135
TIter EndI() const
Definition: hash.h:176
template<class TNodeData >
int TNodeNet< TNodeData >::GetEdges ( ) const

Returns the number of edges in the network.

Definition at line 323 of file network.h.

323  {
324  int edges=0;
325  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N);) {
326  edges+=NodeH[N].GetOutDeg(); }
327  return edges;
328 }
THash< TInt, TNode > NodeH
Definition: network.h:135
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
template<class TNodeData>
TEdgeI TNodeNet< TNodeData >::GetEI ( const int &  EId) const

Not supported/implemented!

template<class TNodeData >
TNodeNet< TNodeData >::TEdgeI TNodeNet< TNodeData >::GetEI ( const int &  SrcNId,
const int &  DstNId 
) const

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

Definition at line 365 of file network.h.

365  {
366  const TNodeI SrcNI = GetNI(SrcNId);
367  const int NodeN = SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
368  if (NodeN == -1) { return EndEI(); }
369  return TEdgeI(SrcNI, EndNI(), NodeN);
370 }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:183
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:185
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:221
template<class TNodeData>
int TNodeNet< TNodeData >::GetMxNId ( ) const
inline

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

Definition at line 195 of file network.h.

195 { return MxNId; }
TInt MxNId
Definition: network.h:134
template<class TNodeData>
TNodeData& TNodeNet< TNodeData >::GetNDat ( const int &  NId)
inline

Returns node data for the node of ID NId in the network.

Definition at line 191 of file network.h.

191 { return NodeH.GetDat(NId).NodeDat; }
THash< TInt, TNode > NodeH
Definition: network.h:135
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeData NodeDat
Definition: network.h:23
template<class TNodeData>
const TNodeData& TNodeNet< TNodeData >::GetNDat ( const int &  NId) const
inline

Returns node data for the node of ID NId in the network.

Definition at line 193 of file network.h.

193 { return NodeH.GetDat(NId).NodeDat; }
THash< TInt, TNode > NodeH
Definition: network.h:135
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeData NodeDat
Definition: network.h:23
template<class TNodeData>
TNodeI TNodeNet< TNodeData >::GetNI ( const int &  NId) const
inline

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

Definition at line 185 of file network.h.

185 { return TNodeI(NodeH.GetI(NId), this); }
THash< TInt, TNode > NodeH
Definition: network.h:135
TIter GetI(const TKey &Key) const
Definition: hash.h:178
template<class TNodeData >
void TNodeNet< TNodeData >::GetNIdV ( TIntV NIdV) const

Gets a vector IDs of all nodes in the network.

Definition at line 358 of file network.h.

358  {
359  NIdV.Reserve(GetNodes(), 0);
360  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
361  NIdV.Add(NodeH.GetKey(N)); }
362 }
THash< TInt, TNode > NodeH
Definition: network.h:135
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:157
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:506
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
template<class TNodeData>
TNode& TNodeNet< TNodeData >::GetNode ( const int &  NId)
inlineprotected

Definition at line 130 of file network.h.

130 { return NodeH.GetDat(NId); }
THash< TInt, TNode > NodeH
Definition: network.h:135
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
template<class TNodeData>
const TNode& TNodeNet< TNodeData >::GetNode ( const int &  NId) const
inline

Returns node element for the node of ID NId in the network.

Definition at line 187 of file network.h.

187 { return NodeH.GetDat(NId); }
THash< TInt, TNode > NodeH
Definition: network.h:135
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
template<class TNodeData>
int TNodeNet< TNodeData >::GetNodes ( ) const
inline

Returns the number of nodes in the network.

Definition at line 157 of file network.h.

157 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: network.h:135
int Len() const
Definition: hash.h:186
template<class TNodeData>
TNodeI TNodeNet< TNodeData >::GetRndNI ( TRnd Rnd = TInt::Rnd)
inline

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

Definition at line 230 of file network.h.

230 { return GetNI(GetRndNId(Rnd)); }
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:185
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the network.
Definition: network.h:228
template<class TNodeData>
int TNodeNet< TNodeData >::GetRndNId ( TRnd Rnd = TInt::Rnd)
inline

Returns an ID of a random node in the network.

Definition at line 228 of file network.h.

228 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: network.h:135
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
template<class TNodeData >
bool TNodeNet< TNodeData >::HasFlag ( const TGraphFlag Flag) const

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

Definition at line 268 of file network.h.

268  {
269  return HasGraphFlag(typename TNet, Flag);
270 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:38
TNodeNet< TNodeData > TNet
Definition: network.h:17
template<class TNodeData >
bool TNodeNet< TNodeData >::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 network.

Definition at line 351 of file network.h.

351  {
352  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
353  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
354  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
355 }
bool IsOutNId(const int &NId) const
Definition: network.h:42
TNode & GetNode(const int &NId)
Definition: network.h:130
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
template<class TNodeData>
bool TNodeNet< TNodeData >::IsNode ( const int &  NId) const
inline

Tests whether ID NId is a node.

Definition at line 179 of file network.h.

179 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: network.h:135
bool IsKey(const TKey &Key) const
Definition: hash.h:216
template<class TNodeData >
bool TNodeNet< TNodeData >::IsOk ( const bool &  ThrowExcept = true) const

Checks the network data structure for internal consistency.

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

Definition at line 383 of file network.h.

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

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

Definition at line 150 of file network.h.

150 { return PNet(new TNodeNet(SIn)); }
TNodeNet()
Definition: network.h:138
TPt< TNet > PNet
Definition: network.h:18
template<class TNodeData>
static PNet TNodeNet< TNodeData >::New ( )
inlinestatic

Static constructor that returns a pointer to the network. Call: TPt <TNodeNet<TNodeData> > Net = TNodeNet<TNodeData>::New().

Definition at line 148 of file network.h.

148 { return PNet(new TNodeNet()); }
TNodeNet()
Definition: network.h:138
TPt< TNet > PNet
Definition: network.h:18
template<class TNodeData>
TNodeNet& TNodeNet< TNodeData >::operator= ( const TNodeNet< TNodeData > &  NodeNet)
inline

Definition at line 153 of file network.h.

153  {
154  if (this!=&NodeNet) { NodeH=NodeNet.NodeH; MxNId=NodeNet.MxNId; } return *this; }
THash< TInt, TNode > NodeH
Definition: network.h:135
TInt MxNId
Definition: network.h:134
template<class TNodeData>
void TNodeNet< TNodeData >::Reserve ( const int &  Nodes,
const int &  Edges 
)
inline

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

Definition at line 240 of file network.h.

240 { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
THash< TInt, TNode > NodeH
Definition: network.h:135
void Gen(const int &ExpectVals)
Definition: hash.h:180
template<class TNodeData>
virtual void TNodeNet< TNodeData >::Save ( TSOut SOut) const
inlinevirtual

Saves the network to a (binary) stream SOut.

Reimplemented in TTimeNet.

Definition at line 146 of file network.h.

146 { MxNId.Save(SOut); NodeH.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: network.h:135
TInt MxNId
Definition: network.h:134
template<class TNodeData>
void TNodeNet< TNodeData >::SetNDat ( const int &  NId,
const TNodeData &  NodeDat 
)

Sets node data for the node of ID NId in the network.

Definition at line 317 of file network.h.

317  {
318  IAssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist.", NId).CStr());
319  NodeH.GetDat(NId).NodeDat = NodeDat;
320 }
#define IAssertR(Cond, Reason)
Definition: bd.h:265
THash< TInt, TNode > NodeH
Definition: network.h:135
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeData NodeDat
Definition: network.h:23
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:179
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
template<class TNodeData>
void TNodeNet< TNodeData >::SortNIdByDat ( const bool &  Asc = true)
inline

Sorts nodes by node data.

Definition at line 244 of file network.h.

244 { NodeH.SortByDat(Asc); }
THash< TInt, TNode > NodeH
Definition: network.h:135
void SortByDat(const bool &Asc=true)
Definition: hash.h:246
template<class TNodeData>
void TNodeNet< TNodeData >::SortNIdById ( const bool &  Asc = true)
inline

Sorts nodes by node IDs.

Definition at line 242 of file network.h.

242 { NodeH.SortByKey(Asc); }
THash< TInt, TNode > NodeH
Definition: network.h:135
void SortByKey(const bool &Asc=true)
Definition: hash.h:245

Friends And Related Function Documentation

template<class TNodeData>
friend class TPt< TNodeNet< TNodeData > >
friend

Definition at line 258 of file network.h.

Member Data Documentation

template<class TNodeData>
TCRef TNodeNet< TNodeData >::CRef
protected

Definition at line 133 of file network.h.

template<class TNodeData>
TInt TNodeNet< TNodeData >::MxNId
protected

Definition at line 134 of file network.h.

template<class TNodeData>
THash<TInt, TNode> TNodeNet< TNodeData >::NodeH
protected

Definition at line 135 of file network.h.


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