SNAP Library 2.3, User Reference  2014-06-16 11:58:46
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
graph.h
Go to the documentation of this file.
1 //#//////////////////////////////////////////////
3 
4 class TUNGraph;
5 class TBPGraph;
6 //TODO:class TUNEGraph; -- undirected multigraph
7 
9 typedef TPt<TUNGraph> PUNGraph;
12 
13 //#//////////////////////////////////////////////
15 class TNGraph;
16 class TNEGraph;
17 
19 typedef TPt<TNGraph> PNGraph;
22 
23 //#//////////////////////////////////////////////
25 
32 class TUNGraph {
33 public:
34  typedef TUNGraph TNet;
36 public:
37  class TNode {
38  private:
41  public:
42  TNode() : Id(-1), NIdV() { }
43  TNode(const int& NId) : Id(NId), NIdV() { }
44  TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV) { }
45  TNode(TSIn& SIn) : Id(SIn), NIdV(SIn) { }
46  void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); }
47  int GetId() const { return Id; }
48  int GetDeg() const { return NIdV.Len(); }
49  int GetInDeg() const { return GetDeg(); }
50  int GetOutDeg() const { return GetDeg(); }
51  int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
52  int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
53  int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
54  bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
55  bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
56  bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
57  void PackOutNIdV() { NIdV.Pack(); }
58  void PackNIdV() { NIdV.Pack(); }
59  friend class TUNGraph;
60  friend class TUNGraphMtx;
61  };
63  class TNodeI {
64  private:
67  public:
68  TNodeI() : NodeHI() { }
69  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
70  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
71  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
72 
74  TNodeI& operator++ (int) { NodeHI++; return *this; }
75 
76  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
77  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
78 
80  int GetId() const { return NodeHI.GetDat().GetId(); }
82  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
84  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
86  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
88 
91  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
93 
96  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
98 
101  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
103  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
105  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
107  bool IsNbrNId(const int& NId) const { return NodeHI.GetDat().IsNbrNId(NId); }
108  friend class TUNGraph;
109  };
111  class TEdgeI {
112  private:
114  int CurEdge;
115  public:
116  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
117  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
118  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
119  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
121  TEdgeI& operator++ (int) { do { CurEdge++; if (CurEdge >= CurNode.GetOutDeg()) { CurEdge=0; CurNode++; while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } } while (CurNode < EndNode && GetSrcNId()>GetDstNId()); return *this; }
122  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
123  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
125  int GetId() const { return -1; }
127  int GetSrcNId() const { return CurNode.GetId(); }
129  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
130  friend class TUNGraph;
131  };
132 private:
136 private:
137  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
138  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
139 public:
140  TUNGraph() : CRef(), MxNId(0), NEdges(0), NodeH() { }
142  explicit TUNGraph(const int& Nodes, const int& Edges) : MxNId(0), NEdges(0) { Reserve(Nodes, Edges); }
143  TUNGraph(const TUNGraph& Graph) : MxNId(Graph.MxNId), NEdges(Graph.NEdges), NodeH(Graph.NodeH) { }
145  TUNGraph(TSIn& SIn) : MxNId(SIn), NEdges(SIn), NodeH(SIn) { }
147  void Save(TSOut& SOut) const { MxNId.Save(SOut); NEdges.Save(SOut); NodeH.Save(SOut); }
149  static PUNGraph New() { return new TUNGraph(); }
151 
153  static PUNGraph New(const int& Nodes, const int& Edges) { return new TUNGraph(Nodes, Edges); }
155  static PUNGraph Load(TSIn& SIn) { return PUNGraph(new TUNGraph(SIn)); }
157  bool HasFlag(const TGraphFlag& Flag) const;
158  TUNGraph& operator = (const TUNGraph& Graph) {
159  if (this!=&Graph) { MxNId=Graph.MxNId; NEdges=Graph.NEdges; NodeH=Graph.NodeH; } return *this; }
160 
162  int GetNodes() const { return NodeH.Len(); }
164 
168  int AddNode(int NId = -1);
170  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId()); }
172 
181  int AddNode(const int& NId, const TIntV& NbrNIdV);
183 
191  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId);
193 
195  void DelNode(const int& NId);
197  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
199  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
201  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
203  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
205  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
207  int GetMxNId() const { return MxNId; }
208 
210  int GetEdges() const;
212 
216  int AddEdge(const int& SrcNId, const int& DstNId);
218  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
220 
223  void DelEdge(const int& SrcNId, const int& DstNId);
225  bool IsEdge(const int& SrcNId, const int& DstNId) const;
227  TEdgeI BegEI() const { TNodeI NI = BegNI(); TEdgeI EI(NI, EndNI(), 0); if (GetNodes() != 0 && (NI.GetOutDeg()==0 || NI.GetId()>NI.GetOutNId(0))) { EI++; } return EI; }
229  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
231  TEdgeI GetEI(const int& EId) const;
233 
235  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
236 
238  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
240  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
242  void GetNIdV(TIntV& NIdV) const;
243 
245  bool Empty() const { return GetNodes()==0; }
247  void Clr() { MxNId=0; NEdges=0; NodeH.Clr(); }
249  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) NodeH.Gen(Nodes/2); }
251  void ReserveNIdDeg(const int& NId, const int& Deg) { GetNode(NId).NIdV.Reserve(Deg); }
253 
257  void Defrag(const bool& OnlyNodeLinks=false);
259 
261  bool IsOk(const bool& ThrowExcept=true) const;
263  void Dump(FILE *OutF=stdout) const;
265 
271  static PUNGraph GetSmallGraph();
272 
273  friend class TUNGraphMtx;
274  friend class TPt<TUNGraph>;
275 };
276 
277 //#//////////////////////////////////////////////
279 
293 class TNGraph {
294 public:
295  typedef TNGraph TNet;
297 public:
298  class TNode {
299  private:
302  public:
303  TNode() : Id(-1), InNIdV(), OutNIdV() { }
304  TNode(const int& NId) : Id(NId), InNIdV(), OutNIdV() { }
305  TNode(const TNode& Node) : Id(Node.Id), InNIdV(Node.InNIdV), OutNIdV(Node.OutNIdV) { }
306  TNode(TSIn& SIn) : Id(SIn), InNIdV(SIn), OutNIdV(SIn) { }
307  void Save(TSOut& SOut) const { Id.Save(SOut); InNIdV.Save(SOut); OutNIdV.Save(SOut); }
308  int GetId() const { return Id; }
309  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
310  int GetInDeg() const { return InNIdV.Len(); }
311  int GetOutDeg() const { return OutNIdV.Len(); }
312  int GetInNId(const int& NodeN) const { return InNIdV[NodeN]; }
313  int GetOutNId(const int& NodeN) const { return OutNIdV[NodeN]; }
314  int GetNbrNId(const int& NodeN) const { return NodeN<GetOutDeg()?GetOutNId(NodeN):GetInNId(NodeN-GetOutDeg()); }
315  bool IsInNId(const int& NId) const { return InNIdV.SearchBin(NId) != -1; }
316  bool IsOutNId(const int& NId) const { return OutNIdV.SearchBin(NId) != -1; }
317  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
318  void PackOutNIdV() { OutNIdV.Pack(); }
319  void PackNIdV() { InNIdV.Pack(); }
320  friend class TNGraph;
321  friend class TNGraphMtx;
322  };
324  class TNodeI {
325  private:
328  public:
329  TNodeI() : NodeHI() { }
330  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
331  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
332  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
334  TNodeI& operator++ (int) { NodeHI++; return *this; }
335  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
336  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
338  int GetId() const { return NodeHI.GetDat().GetId(); }
340  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
342  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
344  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
346 
348  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
350 
352  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
354 
356  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
358  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
360  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
362  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
363  friend class TNGraph;
364  };
366  class TEdgeI {
367  private:
369  int CurEdge;
370  public:
371  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
372  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
373  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
374  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
377  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
378  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
379  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
381  int GetId() const { return -1; }
383  int GetSrcNId() const { return CurNode.GetId(); }
385  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
386  friend class TNGraph;
387  };
388 private:
392 private:
393  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
394  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
395 public:
396  TNGraph() : CRef(), MxNId(0), NodeH() { }
398  explicit TNGraph(const int& Nodes, const int& Edges) : MxNId(0) { Reserve(Nodes, Edges); }
399  TNGraph(const TNGraph& Graph) : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
401  TNGraph(TSIn& SIn) : MxNId(SIn), NodeH(SIn) { }
403  void Save(TSOut& SOut) const { MxNId.Save(SOut); NodeH.Save(SOut); }
405  static PNGraph New() { return new TNGraph(); }
407 
409  static PNGraph New(const int& Nodes, const int& Edges) { return new TNGraph(Nodes, Edges); }
411  static PNGraph Load(TSIn& SIn) { return PNGraph(new TNGraph(SIn)); }
413  bool HasFlag(const TGraphFlag& Flag) const;
414  TNGraph& operator = (const TNGraph& Graph) {
415  if (this!=&Graph) { MxNId=Graph.MxNId; NodeH=Graph.NodeH; } return *this; }
416 
418  int GetNodes() const { return NodeH.Len(); }
420 
424  int AddNode(int NId = -1);
426  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
428 
437  int AddNode(const int& NId, const TIntV& InNIdV, const TIntV& OutNIdV);
439 
447  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& SrcVId, const int& DstVId);
449 
451  void DelNode(const int& NId);
453  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
455  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
457  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
459  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
461  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
462  // GetNodeC() has been commented out. It was a quick shortcut, do not use.
463  //const TNode& GetNodeC(const int& NId) const { return NodeH.GetDat(NId); }
465  int GetMxNId() const { return MxNId; }
466 
468  int GetEdges() const;
470 
476  int AddEdge(const int& SrcNId, const int& DstNId);
478  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
480 
484  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
486  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const;
488  TEdgeI BegEI() const { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0){NI++;} return TEdgeI(NI, EndNI()); }
490  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
492  TEdgeI GetEI(const int& EId) const; // not supported
494  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
495 
497  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
499  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
501  void GetNIdV(TIntV& NIdV) const;
502 
504  bool Empty() const { return GetNodes()==0; }
506  void Clr() { MxNId=0; NodeH.Clr(); }
508  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
510  void ReserveNIdInDeg(const int& NId, const int& InDeg) { GetNode(NId).InNIdV.Reserve(InDeg); }
512  void ReserveNIdOutDeg(const int& NId, const int& OutDeg) { GetNode(NId).OutNIdV.Reserve(OutDeg); }
514 
519  void Defrag(const bool& OnlyNodeLinks=false);
521 
524  bool IsOk(const bool& ThrowExcept=true) const;
526  void Dump(FILE *OutF=stdout) const;
528 
532  static PNGraph GetSmallGraph();
533  friend class TPt<TNGraph>;
534  friend class TNGraphMtx;
535 };
536 
537 // set flags
538 namespace TSnap {
539 template <> struct IsDirected<TNGraph> { enum { Val = 1 }; };
540 }
541 
542 //#//////////////////////////////////////////////
544 
557 class TNEGraph {
558 public:
559  typedef TNEGraph TNet;
561 public:
562  class TNode {
563  private:
566  public:
567  TNode() : Id(-1), InEIdV(), OutEIdV() { }
568  TNode(const int& NId) : Id(NId), InEIdV(), OutEIdV() { }
569  TNode(const TNode& Node) : Id(Node.Id), InEIdV(Node.InEIdV), OutEIdV(Node.OutEIdV) { }
570  TNode(TSIn& SIn) : Id(SIn), InEIdV(SIn), OutEIdV(SIn) { }
571  void Save(TSOut& SOut) const { Id.Save(SOut); InEIdV.Save(SOut); OutEIdV.Save(SOut); }
572  int GetId() const { return Id; }
573  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
574  int GetInDeg() const { return InEIdV.Len(); }
575  int GetOutDeg() const { return OutEIdV.Len(); }
576  int GetInEId(const int& EdgeN) const { return InEIdV[EdgeN]; }
577  int GetOutEId(const int& EdgeN) const { return OutEIdV[EdgeN]; }
578  int GetNbrEId(const int& EdgeN) const { return EdgeN<GetOutDeg()?GetOutEId(EdgeN):GetInEId(EdgeN-GetOutDeg()); }
579  bool IsInEId(const int& EId) const { return InEIdV.SearchBin(EId) != -1; }
580  bool IsOutEId(const int& EId) const { return OutEIdV.SearchBin(EId) != -1; }
581  friend class TNEGraph;
582  };
583  class TEdge {
584  private:
586  public:
587  TEdge() : Id(-1), SrcNId(-1), DstNId(-1) { }
588  TEdge(const int& EId, const int& SourceNId, const int& DestNId) : Id(EId), SrcNId(SourceNId), DstNId(DestNId) { }
589  TEdge(const TEdge& Edge) : Id(Edge.Id), SrcNId(Edge.SrcNId), DstNId(Edge.DstNId) { }
590  TEdge(TSIn& SIn) : Id(SIn), SrcNId(SIn), DstNId(SIn) { }
591  void Save(TSOut& SOut) const { Id.Save(SOut); SrcNId.Save(SOut); DstNId.Save(SOut); }
592  int GetId() const { return Id; }
593  int GetSrcNId() const { return SrcNId; }
594  int GetDstNId() const { return DstNId; }
595  friend class TNEGraph;
596  };
598  class TNodeI {
599  private:
602  const TNEGraph *Graph;
603  public:
604  TNodeI() : NodeHI(), Graph(NULL) { }
605  TNodeI(const THashIter& NodeHIter, const TNEGraph* GraphPt) : NodeHI(NodeHIter), Graph(GraphPt) { }
606  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI), Graph(NodeI.Graph) { }
607  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; Graph=NodeI.Graph; return *this; }
609  TNodeI& operator++ (int) { NodeHI++; return *this; }
610  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
611  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
613  int GetId() const { return NodeHI.GetDat().GetId(); }
615  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
617  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
619  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
621 
623  int GetInNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetInEId(EdgeN)).GetSrcNId(); }
625 
627  int GetOutNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetOutEId(EdgeN)).GetDstNId(); }
629 
631  int GetNbrNId(const int& EdgeN) const { const TEdge& E = Graph->GetEdge(NodeHI.GetDat().GetNbrEId(EdgeN));
632  return GetId()==E.GetSrcNId() ? E.GetDstNId():E.GetSrcNId(); }
634  bool IsInNId(const int& NId) const;
636  bool IsOutNId(const int& NId) const;
638  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
639  // edges
641  int GetInEId(const int& EdgeN) const { return NodeHI.GetDat().GetInEId(EdgeN); }
643  int GetOutEId(const int& EdgeN) const { return NodeHI.GetDat().GetOutEId(EdgeN); }
645  int GetNbrEId(const int& EdgeN) const { return NodeHI.GetDat().GetNbrEId(EdgeN); }
647  bool IsInEId(const int& EId) const { return NodeHI.GetDat().IsInEId(EId); }
649  bool IsOutEId(const int& EId) const { return NodeHI.GetDat().IsOutEId(EId); }
651  bool IsNbrEId(const int& EId) const { return IsInEId(EId) || IsOutEId(EId); }
652  friend class TNEGraph;
653  };
655  class TEdgeI {
656  private:
659  const TNEGraph *Graph;
660  public:
661  TEdgeI() : EdgeHI(), Graph(NULL) { }
662  TEdgeI(const THashIter& EdgeHIter, const TNEGraph *GraphPt) : EdgeHI(EdgeHIter), Graph(GraphPt) { }
663  TEdgeI(const TEdgeI& EdgeI) : EdgeHI(EdgeI.EdgeHI), Graph(EdgeI.Graph) { }
664  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { EdgeHI=EdgeI.EdgeHI; Graph=EdgeI.Graph; } return *this; }
666  TEdgeI& operator++ (int) { EdgeHI++; return *this; }
667  bool operator < (const TEdgeI& EdgeI) const { return EdgeHI < EdgeI.EdgeHI; }
668  bool operator == (const TEdgeI& EdgeI) const { return EdgeHI == EdgeI.EdgeHI; }
670  int GetId() const { return EdgeHI.GetDat().GetId(); }
672  int GetSrcNId() const { return EdgeHI.GetDat().GetSrcNId(); }
674  int GetDstNId() const { return EdgeHI.GetDat().GetDstNId(); }
675  friend class TNEGraph;
676  };
677 
678 private:
679  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
680  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
681  TEdge& GetEdge(const int& EId) { return EdgeH.GetDat(EId); }
682  const TEdge& GetEdge(const int& EId) const { return EdgeH.GetDat(EId); }
683 private:
688 public:
689  TNEGraph() : CRef(), MxNId(0), MxEId(0) { }
691  explicit TNEGraph(const int& Nodes, const int& Edges) : CRef(), MxNId(0), MxEId(0) { Reserve(Nodes, Edges); }
692  TNEGraph(const TNEGraph& Graph) : MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { }
694  TNEGraph(TSIn& SIn) : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { }
696  void Save(TSOut& SOut) const { MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut); }
698  static PNEGraph New() { return PNEGraph(new TNEGraph()); }
700 
702  static PNEGraph New(const int& Nodes, const int& Edges) { return PNEGraph(new TNEGraph(Nodes, Edges)); }
704  static PNEGraph Load(TSIn& SIn) { return PNEGraph(new TNEGraph(SIn)); }
706  bool HasFlag(const TGraphFlag& Flag) const;
707  TNEGraph& operator = (const TNEGraph& Graph) { if (this!=&Graph) {
708  MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; } return *this; }
709 
711  int GetNodes() const { return NodeH.Len(); }
713 
717  int AddNode(int NId = -1);
719  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
721 
723  void DelNode(const int& NId);
725  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
727  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
729  TNodeI BegNI() const { return TNodeI(NodeH.BegI(), this); }
731  TNodeI EndNI() const { return TNodeI(NodeH.EndI(), this); }
733  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId), this); }
735  int GetMxNId() const { return MxNId; }
736 
738  int GetEdges() const { return EdgeH.Len(); }
740 
745  int AddEdge(const int& SrcNId, const int& DstNId, int EId = -1);
747  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); }
749  void DelEdge(const int& EId);
751 
755  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
757  bool IsEdge(const int& EId) const { return EdgeH.IsKey(EId); }
759  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const { int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); }
761  bool IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir = true) const;
763  int GetEId(const int& SrcNId, const int& DstNId) const { int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; }
765  TEdgeI BegEI() const { return TEdgeI(EdgeH.BegI(), this); }
767  TEdgeI EndEI() const { return TEdgeI(EdgeH.EndI(), this); }
769  TEdgeI GetEI(const int& EId) const { return TEdgeI(EdgeH.GetI(EId), this); }
771  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const { return GetEI(GetEId(SrcNId, DstNId)); }
772 
774  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
776  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
778  int GetRndEId(TRnd& Rnd=TInt::Rnd) { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); }
780  TEdgeI GetRndEI(TRnd& Rnd=TInt::Rnd) { return GetEI(GetRndEId(Rnd)); }
782  void GetNIdV(TIntV& NIdV) const;
784  void GetEIdV(TIntV& EIdV) const;
785 
787  bool Empty() const { return GetNodes()==0; }
789  void Clr() { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); }
791  void Reserve(const int& Nodes, const int& Edges) {
792  if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } }
794 
799  void Defrag(const bool& OnlyNodeLinks=false);
801 
804  bool IsOk(const bool& ThrowExcept=true) const;
806  void Dump(FILE *OutF=stdout) const;
808 
812  static PNEGraph GetSmallGraph();
813  friend class TPt<TNEGraph>;
814 };
815 
816 // set flags
817 namespace TSnap {
818 template <> struct IsMultiGraph<TNEGraph> { enum { Val = 1 }; };
819 template <> struct IsDirected<TNEGraph> { enum { Val = 1 }; };
820 }
821 
822 //#//////////////////////////////////////////////
824 
825 class TBPGraph {
826 public:
827  typedef TBPGraph TNet;
829  typedef enum { bgsUndef, bgsLeft, bgsRight, bgsBoth } TNodeTy; // left or right hand side node
830 public:
831  class TNode {
832  private:
835  TNodeTy NodeTy; // remove
836  public:
837  TNode() : Id(-1), NIdV(), NodeTy(bgsUndef) { }
838  TNode(const int& NId) : Id(NId), NIdV(), NodeTy(true?bgsLeft:bgsRight) { }
839  TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV), NodeTy(Node.NodeTy) { }
840  TNode(TSIn& SIn) : Id(SIn), NIdV(SIn), NodeTy(bgsUndef) { TInt Ty(SIn); NodeTy=(TNodeTy)Ty.Val; }
841  void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); TInt(NodeTy).Save(SOut); }
842  int GetId() const { return Id; }
843  int GetDeg() const { return NIdV.Len(); }
844  int GetInDeg() const { return GetDeg(); }
845  int GetOutDeg() const { return GetDeg(); }
846  int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
847  int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
848  int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
849  bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
850  bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
851  bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
852  void PackOutNIdV() { NIdV.Pack(); }
853  void PackNIdV() { NIdV.Pack(); }
854  friend class TBPGraph;
855  };
857  class TNodeI {
858  private:
860  THashIter LeftHI, RightHI; // iterator over left and right hand-side nodes
861  private:
862  inline THashIter HI() const { return ! LeftHI.IsEnd()?LeftHI:RightHI; }
863  public:
864  TNodeI() : LeftHI(), RightHI() { }
865  TNodeI(const THashIter& LeftHIter, const THashIter& RightHIter) : LeftHI(LeftHIter), RightHI(RightHIter) { }
866  TNodeI(const TNodeI& NodeI) : LeftHI(NodeI.LeftHI), RightHI(NodeI.RightHI) { }
867  TNodeI& operator = (const TNodeI& NodeI) { LeftHI = NodeI.LeftHI; RightHI=NodeI.RightHI; return *this; }
870  if (! LeftHI.IsEnd()) {
871  LeftHI++; }
872  else if (! RightHI.IsEnd()) {
873  RightHI++; }
874  return *this; }
875  bool operator < (const TNodeI& NodeI) const { return LeftHI < NodeI.LeftHI || (LeftHI==NodeI.LeftHI && RightHI < NodeI.RightHI); }
876  bool operator == (const TNodeI& NodeI) const { return LeftHI==NodeI.LeftHI && RightHI==NodeI.RightHI; }
878  int GetId() const { return HI().GetDat().GetId(); }
880  bool IsLeft() const { return ! LeftHI.IsEnd(); }
882  bool IsRight() const { return ! IsLeft(); }
884  int GetDeg() const { return HI().GetDat().GetDeg(); }
886  int GetInDeg() const { return HI().GetDat().GetInDeg(); }
888  int GetOutDeg() const { return HI().GetDat().GetOutDeg(); }
890 
891  int GetInNId(const int& NodeN) const { return HI().GetDat().GetInNId(NodeN); }
893 
894  int GetOutNId(const int& NodeN) const { return HI().GetDat().GetOutNId(NodeN); }
896 
897  int GetNbrNId(const int& NodeN) const { return HI().GetDat().GetNbrNId(NodeN); }
899  bool IsInNId(const int& NId) const { return HI().GetDat().IsInNId(NId); }
901  bool IsOutNId(const int& NId) const { return HI().GetDat().IsOutNId(NId); }
903  bool IsNbrNId(const int& NId) const { return HI().GetDat().IsNbrNId(NId); }
904  friend class TBPGraph;
905  };
907  class TEdgeI {
908  private:
909  TNodeI CurNode, EndNode; // end node on the 'left'
910  int CurEdge;
911  public:
912  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
913  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
914  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
915  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
918  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
919  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
920  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
922  int GetId() const { return -1; }
924  int GetSrcNId() const { return CurNode.GetId(); }
926  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
928  int GetLNId() const { return GetSrcNId(); }
930  int GetRNId() const { return GetDstNId(); }
931  friend class TBPGraph;
932  };
933 private:
935  TInt MxNId; // maximum node ID in the graph
936  THash<TInt, TNode> LeftH; // 'left' nodes
937  THash<TInt, TNode> RightH; // 'right' nodes
938 private:
939  //TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
940  //const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
941 public:
942  TBPGraph() : CRef(), MxNId(0), LeftH(), RightH() { }
944  explicit TBPGraph(const int& Nodes, const int& Edges) : MxNId(0) { }
945  TBPGraph(const TBPGraph& BPGraph) : MxNId(BPGraph.MxNId), LeftH(BPGraph.LeftH), RightH(BPGraph.RightH) { }
947  TBPGraph(TSIn& SIn) : MxNId(SIn), LeftH(SIn), RightH(SIn) { }
949  void Save(TSOut& SOut) const { MxNId.Save(SOut); LeftH.Save(SOut); RightH.Save(SOut); }
951  static PBPGraph New() { return new TBPGraph(); }
953 
954  static PBPGraph New(const int& Nodes, const int& Edges) { return new TBPGraph(Nodes, Edges); }
956  static PBPGraph Load(TSIn& SIn) { return PBPGraph(new TBPGraph(SIn)); }
958  bool HasFlag(const TGraphFlag& Flag) const;
959  TBPGraph& operator = (const TBPGraph& BPGraph) {
960  if (this!=&BPGraph) { MxNId=BPGraph.MxNId; LeftH=BPGraph.LeftH; RightH=BPGraph.RightH; } return *this; }
961 
963  int GetNodes() const { return GetLNodes() + GetRNodes(); }
965  int GetLNodes() const { return LeftH.Len(); }
967  int GetRNodes() const { return RightH.Len(); }
969 
970  int AddNode(int NId = -1, const bool& LeftNode=true);
972  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId(), NodeI.IsLeft()); }
974 
975  void DelNode(const int& NId);
977  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
979  bool IsNode(const int& NId) const { return IsLNode(NId) || IsRNode(NId); }
981  bool IsLNode(const int& NId) const { return LeftH.IsKey(NId); }
983  bool IsRNode(const int& NId) const { return RightH.IsKey(NId); }
985  int GetMxNId() const { return MxNId; }
986 
988  TNodeI BegNI() const { return TNodeI(LeftH.BegI(), RightH.BegI()); }
990  TNodeI EndNI() const { return TNodeI(LeftH.EndI(), RightH.EndI()); }
992  TNodeI GetNI(const int& NId) const { return IsLNode(NId) ? TNodeI(LeftH.GetI(NId), RightH.EndI()) : TNodeI(LeftH.EndI(), RightH.GetI(NId)); }
994  TNodeI BegLNI() const { return TNodeI(LeftH.BegI(), RightH.EndI()); }
996  TNodeI EndLNI() const { return EndNI(); }
998  TNodeI BegRNI() const { return TNodeI(LeftH.EndI(), RightH.BegI()); }
1000  TNodeI EndRNI() const { return EndNI(); }
1001 
1003  int GetEdges() const;
1005 
1006  int AddEdge(const int& LeftNId, const int& RightNId);
1008  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
1010 
1011  void DelEdge(const int& LeftNId, const int& RightNId);
1013  bool IsEdge(const int& LeftNId, const int& RightNId) const;
1015  TEdgeI BegEI() const { TNodeI NI=BegLNI(); while (NI<EndLNI() && (NI.GetOutDeg()==0 || NI.GetId()>NI.GetOutNId(0))) { NI++; } return TEdgeI(NI, EndLNI()); }
1017  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
1019  TEdgeI GetEI(const int& EId) const;
1021 
1022  TEdgeI GetEI(const int& LeftNId, const int& RightNId) const;
1023 
1025  int GetRndNId(TRnd& Rnd=TInt::Rnd);
1027  int GetRndLNId(TRnd& Rnd=TInt::Rnd);
1029  int GetRndRNId(TRnd& Rnd=TInt::Rnd);
1031  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
1033  void GetNIdV(TIntV& NIdV) const;
1035  void GetLNIdV(TIntV& NIdV) const;
1037  void GetRNIdV(TIntV& NIdV) const;
1038 
1040  bool Empty() const { return GetNodes()==0; }
1042  void Clr() { MxNId=0; LeftH.Clr(); RightH.Clr(); }
1044  void Reserve(const int& Nodes, const int& Edges);
1046 
1047  void Defrag(const bool& OnlyNodeLinks=false);
1049 
1050  bool IsOk(const bool& ThrowExcept=true) const;
1052  void Dump(FILE *OutF=stdout) const;
1054 
1055  static PBPGraph GetSmallGraph();
1056 
1057  friend class TPt<TBPGraph>;
1058 };
1059 
1060 // set flags
1061 namespace TSnap {
1062 template <> struct IsBipart<TBPGraph> { enum { Val = 1 }; };
1063 }
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: graph.cpp:204
Definition: bd.h:440
static PBPGraph GetSmallGraph()
Returns a small graph on 2 'left', 3 'right' nodes and 4 edges.
Definition: graph.cpp:818
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetLNId() and EdgeI.GetRNId() to the graph.
Definition: graph.h:1008
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:111
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:356
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:875
TBPGraph(const TBPGraph &BPGraph)
!! Reserve(Nodes, Edges); }
Definition: graph.h:945
int GetDeg() const
Definition: graph.h:843
int GetId() const
Gets edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:922
TPt< TBPGraph > PNet
Definition: graph.h:828
TUNGraph(const TUNGraph &Graph)
Definition: graph.h:143
THashIter NodeHI
Definition: graph.h:66
int AddNode(const TNodeI &NodeId)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:426
int GetOutDeg() const
Definition: graph.h:311
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a biparite graph of Nodes nodes and Edges edges.
Definition: graph.cpp:740
int GetSrcNId() const
Definition: graph.h:593
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:326
static PUNGraph New(const int &Nodes, const int &Edges)
Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and...
Definition: graph.h:153
TNodeI CurNode
Definition: graph.h:368
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.cpp:418
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:447
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
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:457
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:536
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:229
void GetRNIdV(TIntV &NIdV) const
Gets a vector IDs of all 'right' nodes in the bipartite graph.
Definition: graph.cpp:734
TNode(const int &NId)
Definition: graph.h:43
bool IsOutNId(const int &NId) const
Definition: graph.h:56
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:123
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:917
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:867
static PBPGraph New(const int &Nodes, const int &Edges)
Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and...
Definition: graph.h:954
TNode(const TNode &Node)
Definition: graph.h:839
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:787
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:876
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:992
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:886
static PUNGraph GetSmallGraph()
Returns a small graph on 5 nodes and 5 edges.
Definition: graph.cpp:193
Definition: dt.h:11
TNEGraph & operator=(const TNEGraph &Graph)
Definition: graph.h:707
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:65
TNode & GetNode(const int &NId)
Definition: graph.h:137
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:668
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:107
int GetNbrEId(const int &EdgeN) const
Definition: graph.h:578
TEdge(const int &EId, const int &SourceNId, const int &DestNId)
Definition: graph.h:588
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.cpp:704
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:979
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:387
TNodeTy NodeTy
Definition: graph.h:835
int Val
Definition: dt.h:1043
const TNEGraph * Graph
Definition: graph.h:659
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:405
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:461
void Save(TSOut &SOut) const
Definition: dt.h:1057
void ReserveNIdInDeg(const int &NId, const int &InDeg)
Reserves memory for node ID NId having InDeg in-edges.
Definition: graph.h:510
THashIter HI() const
Definition: graph.h:862
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:765
TNEGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:691
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:972
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:125
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:490
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:619
int GetDstNId() const
Returns the destination of the edge. Since the graph is undirected, this is the node with a greater I...
Definition: graph.h:129
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:207
void PackNIdV()
Definition: graph.h:58
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:358
bool IsLNode(const int &NId) const
Tests whether ID NId is a 'left' side node.
Definition: graph.h:981
TNEGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:694
TUNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:145
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:374
Tests (at compile time) if the graph is directed.
Definition: gbase.h:25
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:985
int GetNbrNId(const int &NodeN) const
Definition: graph.h:848
TIter BegI() const
Definition: hash.h:171
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:278
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:857
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
int GetDstNId() const
Gets destination ('right' side) of an edge. Since the graph is undirected this is the node with great...
Definition: graph.h:926
THash< TInt, TNode > NodeH
Definition: graph.h:391
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:74
TNodeTy
Definition: graph.h:829
int AddEdge(const TEdgeI &EdgeI)
Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph.
Definition: graph.h:478
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
bool IsInEId(const int &EId) const
Definition: graph.h:579
int GetId() const
Definition: graph.h:572
TPt< TNEGraph > PNet
Definition: graph.h:560
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:71
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:667
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
int GetId() const
Gets edge ID.
Definition: graph.h:670
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:990
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:920
void Save(TSOut &SOut) const
Definition: hash.h:141
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:988
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.
Definition: graph.h:759
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:488
int GetInDeg() const
Definition: graph.h:574
TInt NEdges
Definition: graph.h:134
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:376
TInt SrcNId
Definition: graph.h:585
void Save(TSOut &SOut) const
Definition: graph.h:591
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:341
int GetOutEId(const int &EdgeN) const
Definition: graph.h:577
int AddNode(int NId=-1, const bool &LeftNode=true)
Adds a node of ID NId to the graph.
Definition: graph.cpp:621
void PackOutNIdV()
Definition: graph.h:57
Directed multigraph.
Definition: graph.h:557
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:767
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:418
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:1017
THashIter NodeHI
Definition: graph.h:327
int GetInDeg() const
Definition: graph.h:49
void PackOutNIdV()
Definition: graph.h:318
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:208
TCRef CRef
Definition: graph.h:684
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:179
static PNEGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:704
TNGraph()
Definition: graph.h:396
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:655
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:696
TNode(const int &NId)
Definition: graph.h:304
int GetSrcNId() const
Returns the source of the edge. Since the graph is undirected, this is the node with a smaller ID of ...
Definition: graph.h:127
TCRef CRef
Definition: graph.h:934
TNodeI EndNode
Definition: graph.h:113
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:147
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:362
const TNode & GetNode(const int &NId) const
Definition: graph.h:138
TIter EndI() const
Definition: hash.h:176
TNEGraph TNet
Definition: graph.h:559
THashIter NodeHI
Definition: graph.h:601
static TRnd Rnd
Definition: dt.h:1050
int GetId() const
Definition: graph.h:47
bool IsInNId(const int &NId) const
Definition: graph.h:55
int GetRndLNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random 'left' node in the graph.
Definition: graph.cpp:712
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:609
TBPGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes (LeftNodes+RightNodes) nodes and Edges e...
Definition: graph.h:944
bool IsNbrEId(const int &EId) const
Tests whether the edge with ID EId is an in or out-edge of current node.
Definition: graph.h:651
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:729
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:381
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:725
THashIter EdgeHI
Definition: graph.h:658
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:162
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:899
TNodeI(const THashIter &NodeHIter, const TNEGraph *GraphPt)
Definition: graph.h:605
THash< TInt, TNode > NodeH
Definition: graph.h:686
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: graph.h:623
TNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:401
TNodeI EndNode
Definition: graph.h:368
int GetInNId(const int &NodeN) const
Definition: graph.h:846
int GetOutDeg() const
Definition: graph.h:575
TNode & GetNode(const int &NId)
Definition: graph.h:393
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition: graph.h:747
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:885
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:238
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:666
int GetId() const
Returns ID of the current node.
Definition: graph.h:878
int GetOutNId(const int &NodeN) const
Definition: graph.h:847
int GetInDeg() const
Definition: graph.h:310
int GetDstNId() const
Gets destination of an edge.
Definition: graph.h:674
THash< TInt, TEdge > EdgeH
Definition: graph.h:687
int GetDstNId() const
Definition: graph.h:594
int GetLNodes() const
Returns the number of nodes on the 'left' side of the biparite graph.
Definition: graph.h:965
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:118
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:122
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:913
static PBPGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:956
TCRef CRef
Definition: graph.h:133
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:259
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: graph.h:643
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:897
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:82
void Clr()
Deletes all nodes and edges from the bipartite graph.
Definition: graph.h:1042
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:731
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TNode(const int &NId)
Definition: graph.h:838
int GetInEId(const int &EdgeN) const
Definition: graph.h:576
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:733
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:888
int GetId() const
Definition: graph.h:308
int GetNbrNId(const int &EdgeN) const
Returns ID of EdgeN-th neighboring node.
Definition: graph.h:631
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:514
TEdgeI GetEI(const int &SrcNId, const int &DstNId) const
Returns an iterator referring to edge (SrcNId, DstNId) in the graph.
Definition: graph.h:771
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:711
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:74
THashIter LeftHI
Definition: graph.h:860
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:86
bool Empty() const
Tests whether the bipartite graph is empty (has zero nodes).
Definition: graph.h:1040
void PackNIdV()
Definition: graph.h:319
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TEdge & GetEdge(const int &EId)
Definition: graph.h:681
Undirected graph.
Definition: graph.h:32
int GetLNId() const
Gets the ID of the node on the 'left' side of the edge.
Definition: graph.h:928
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:326
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.cpp:427
TNodeI EndRNI() const
Returns an iterator referring to the past-the-end 'right' node in the graph.
Definition: graph.h:1000
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:334
bool IsOutNId(const int &NId) const
Definition: graph.h:851
void Gen(const int &ExpectVals)
Definition: hash.h:180
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:366
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:648
TInt MxNId
Definition: graph.h:134
TNodeI(const THashIter &LeftHIter, const THashIter &RightHIter)
Definition: graph.h:865
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:286
static PNEGraph GetSmallGraph()
Returns a small multigraph on 5 nodes and 6 edges.
Definition: graph.cpp:610
int GetNodes() const
Returns the total number of nodes in the graph.
Definition: graph.h:963
bool IsInNId(const int &NId) const
Definition: graph.h:850
static PNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:411
Definition: gsvd.h:5
TEdgeI(const THashIter &EdgeHIter, const TNEGraph *GraphPt)
Definition: graph.h:662
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:197
void DelEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true)
Deletes an edge from node IDs SrcNId to DstNId from the graph.
Definition: graph.cpp:295
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:313
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:249
TNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:398
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:245
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:497
TNodeI BegLNI() const
Returns an iterator referring to the first 'left' node in the graph.
Definition: graph.h:994
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: graph.h:641
TNode(TSIn &SIn)
Definition: graph.h:570
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:360
TNode(TSIn &SIn)
Definition: graph.h:306
int GetRNodes() const
Returns the number of nodes on the 'right' side of the biparite graph.
Definition: graph.h:967
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:205
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: graph.cpp:414
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:600
Tests (at compile time) if the graph is a multigraph with multiple edges between the same nodes...
Definition: gbase.h:27
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:455
TNodeI(const TNodeI &NodeI)
Definition: graph.h:70
TNode(const TNode &Node)
Definition: graph.h:569
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: graph.h:627
TInt DstNId
Definition: graph.h:585
bool IsEdge(const int &LeftNId, const int &RightNId) const
Tests whether an edge between node IDs LeftNId and RightNId exists in the graph.
Definition: graph.cpp:685
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:919
TIntV NIdV
Definition: graph.h:40
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition: graph.h:218
TInt MxNId
Definition: graph.h:935
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:227
void PackOutNIdV()
Definition: graph.h:852
void GetLNIdV(TIntV &NIdV) const
Gets a vector IDs of all 'left' nodes in the bipartite graph.
Definition: graph.cpp:728
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:149
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:372
TPt< TNGraph > PNGraph
Pointer to a directed graph (TNGraph)
Definition: graph.h:16
TNodeI CurNode
Definition: graph.h:113
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:610
TBPGraph TNet
Definition: graph.h:827
int GetOutNId(const int &NodeN) const
Definition: graph.h:313
TBPGraph & operator=(const TBPGraph &BPGraph)
Definition: graph.h:959
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:436
int AddNode(const TNodeI &NodeId)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:719
void DelEdge(const int &SrcNId, const int &DstNId)
Deletes an edge between node IDs SrcNId and DstNId from the graph.
Definition: graph.cpp:95
TNodeI BegRNI() const
Returns an iterator referring to the first 'right' node in the graph.
Definition: graph.h:998
int GetOutNId(const int &NodeN) const
Definition: graph.h:52
TIntV OutEIdV
Definition: graph.h:565
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:589
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:240
void Save(TSOut &SOut) const
Definition: graph.h:841
void Save(TSOut &SOut) const
Definition: graph.h:46
TUNGraph TNet
Definition: graph.h:34
TNodeI EndLNI() const
Returns an iterator referring to the past-the-end 'left' node in the graph.
Definition: graph.h:996
int GetRNId() const
Gets the ID of the node on the 'right' side of the edge.
Definition: graph.h:930
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:499
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:332
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:611
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:340
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:77
Definition: fl.h:128
TNGraph(const TNGraph &Graph)
Definition: graph.h:399
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:859
TIntV NIdV
Definition: graph.h:834
int GetDeg() const
Definition: graph.h:573
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:903
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1418
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:332
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:84
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:335
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:884
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:506
TUNGraph()
Definition: graph.h:140
Definition: dt.h:1041
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:465
bool IsNbrNId(const int &NId) const
Definition: graph.h:849
TNEGraph(const TNEGraph &Graph)
Definition: graph.h:692
TNode & GetNode(const int &NId)
Definition: graph.h:679
static PUNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:155
static PNEGraph New()
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New().
Definition: graph.h:698
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:403
void Save(TSOut &SOut) const
Definition: graph.h:571
int AddEdge(const int &LeftNId, const int &RightNId)
Adds an edge between a node LeftNId on the left and a node RightNId on the right side of the bipartit...
Definition: graph.cpp:655
void DelEdge(const int &LeftNId, const int &RightNId)
Deletes an edge between a node LeftNId on the left and a node RightNId on the right side of the bipar...
Definition: graph.cpp:669
THashIter RightHI
Definition: graph.h:860
int GetSrcNId() const
Gets the source ('left' side) of an edge. Since the graph is undirected this is the node with smaller...
Definition: graph.h:924
TEdge(const TEdge &Edge)
Definition: graph.h:589
Directed graph.
Definition: graph.h:293
bool IsNbrNId(const int &NId) const
Definition: graph.h:54
int GetNbrNId(const int &NodeN) const
Definition: graph.h:53
TEdgeI GetEI(const int &EId) const
Returns an iterator referring to edge with edge ID EId.
Definition: graph.h:769
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:632
TNode(const TNode &Node)
Definition: graph.h:305
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:915
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:757
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:459
int GetId() const
Returns ID of the current node.
Definition: graph.h:338
TBPGraph()
Definition: graph.h:942
static PNGraph GetSmallGraph()
Returns a small graph on 5 nodes and 6 edges.
Definition: graph.cpp:404
TNode(const int &NId)
Definition: graph.h:568
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:96
int GetRndRNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random 'right' node in the graph.
Definition: graph.cpp:716
const TNode & GetNode(const int &NId) const
Definition: graph.h:680
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:125
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:791
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:344
Bipartite graph.
Definition: graph.h:825
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:763
int GetSrcNId() const
Gets the source of an edge.
Definition: graph.h:672
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:607
THash< TInt, TNode > NodeH
Definition: graph.h:135
TEdge(TSIn &SIn)
Definition: graph.h:590
TNode(const TNode &Node)
Definition: graph.h:44
bool IsOutEId(const int &EId) const
Definition: graph.h:580
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...
Definition: graph.h:409
void GetEIdV(TIntV &EIdV) const
Gets a vector IDs of all edges in the graph.
Definition: graph.cpp:520
TNode(TSIn &SIn)
Definition: graph.h:840
void Pack()
The vector reduces its capacity (frees memory) to match its size.
Definition: ds.h:987
int GetId() const
Definition: graph.h:842
int GetRndEId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random edge in the graph.
Definition: graph.h:778
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types.
bool IsRNode(const int &NId) const
Tests whether ID NId is a 'right' side node.
Definition: graph.h:983
bool IsRight() const
Tests whether the node is right hand side node.
Definition: graph.h:882
TCRef CRef
Definition: graph.h:389
THash< TInt, TNode > RightH
Definition: graph.h:937
Definition: hash.h:88
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:330
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...
Definition: graph.h:702
TPt< TNGraph > PNet
Definition: graph.h:296
int GetNbrEId(const int &EdgeN) const
Returns ID of EdgeN-th in or out-edge.
Definition: graph.h:645
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.h:738
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:84
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:324
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:132
const TEdge & GetEdge(const int &EId) const
Definition: graph.h:682
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:203
int GetOutDeg() const
Definition: graph.h:845
const TNEGraph * Graph
Definition: graph.h:602
TIntV InNIdV
Definition: graph.h:301
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:901
TPt< TUNGraph > PNet
Definition: graph.h:35
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:774
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:508
static PBPGraph New()
Static constructor that returns a pointer to the graph. Call: PBPGraph BPGraph = TBPGraph::New();.
Definition: graph.h:951
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:101
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
THash< TInt, TNode > LeftH
Definition: graph.h:936
TNode(TSIn &SIn)
Definition: graph.h:45
TInt MxNId
Definition: graph.h:685
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:199
TInt MxNId
Definition: graph.h:390
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:894
TPt< TBPGraph > PBPGraph
Pointer to a bipartitegraph graph (TBPGraph)
Definition: graph.h:11
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: graph.cpp:3
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:776
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
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:336
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:789
TNodeI CurNode
Definition: graph.h:909
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the bipartite graph.
Definition: graph.cpp:720
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:247
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:869
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:142
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:506
TEdgeI GetRndEI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random edge in the graph.
Definition: graph.h:780
TIntV OutNIdV
Definition: graph.h:301
int GetOutDeg() const
Definition: graph.h:50
const TNode & GetNode(const int &NId) const
Definition: graph.h:394
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:527
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:1015
TNodeI EndNode
Definition: graph.h:909
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:342
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:453
void Dump(FILE *OutF=stdout) const
Print the biparite graph in a human readable form to an output stream OutF.
Definition: graph.cpp:795
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:977
int GetSrcNId() const
Returns the source node of the edge.
Definition: graph.h:383
int GetId() const
Returns ID of the current node.
Definition: graph.h:613
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the biparite graph.
Definition: graph.cpp:744
int GetId() const
Returns ID of the current node.
Definition: graph.h:80
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition: graph.cpp:108
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:914
bool IsKey(const TKey &Key) const
Definition: hash.h:216
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:348
void PackNIdV()
Definition: graph.h:853
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:105
void DelEdge(const int &EId)
Deletes an edge with edge ID EId from the graph.
Definition: graph.cpp:477
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
int GetInNId(const int &NodeN) const
Definition: graph.h:51
bool IsOutEId(const int &EId) const
Tests whether the edge with ID EId is an out-edge of current node.
Definition: graph.h:649
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:201
int GetInNId(const int &NodeN) const
Definition: graph.h:312
int GetDeg() const
Definition: graph.h:309
int GetDeg() const
Definition: graph.h:48
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:378
void ReserveNIdDeg(const int &NId, const int &Deg)
Reserves memory for node ID NId having Deg edges.
Definition: graph.h:251
bool IsOutNId(const int &NId) const
Definition: graph.h:316
int GetNbrNId(const int &NodeN) const
Definition: graph.h:314
int Len() const
Definition: hash.h:186
TNodeI(const TNodeI &NodeI)
Definition: graph.h:866
TInt MxEId
Definition: graph.h:685
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:170
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:69
TNodeI(const TNodeI &NodeI)
Definition: graph.h:606
bool IsInEId(const int &EId) const
Tests whether the edge with ID EId is an in-edge of current node.
Definition: graph.h:647
bool IsNbrNId(const int &NId) const
Definition: graph.h:317
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:379
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:1031
bool IsOk(const bool &ThrowExcept=true) const
Checks the bipartite graph data structure for internal consistency.
Definition: graph.cpp:753
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:59
Tests (at compile time) if the graph is a bipartite graph type.
Definition: gbase.h:35
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:664
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:663
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:907
void Save(TSOut &SOut) const
Definition: graph.h:307
TNGraph & operator=(const TNGraph &Graph)
Definition: graph.h:414
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:598
int GetDstNId() const
Returns the destination node of the edge.
Definition: graph.h:385
bool IsInNId(const int &NId) const
Definition: graph.h:315
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:76
int GetInDeg() const
Definition: graph.h:844
TUNGraph & operator=(const TUNGraph &Graph)
Definition: graph.h:158
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:352
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:615
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:504
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:121
THash< TInt, TEdge >::TIter THashIter
Definition: graph.h:657
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:617
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:117
bool IsLeft() const
Tests whether the node is left hand side node.
Definition: graph.h:880
TUNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:142
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:727
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:373
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:119
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:91
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:949
TBPGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:947
TNodeI(const TNodeI &NodeI)
Definition: graph.h:331
void ReserveNIdOutDeg(const int &NId, const int &OutDeg)
Reserves memory for node ID NId having OutDeg out-edges.
Definition: graph.h:512
TNEGraph()
Definition: graph.h:689
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:638
TPt< TUNGraph > PUNGraph
Pointer to an undirected graph (TUNGraph)
Definition: graph.h:5
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:735
TIter GetI(const TKey &Key) const
Definition: hash.h:178
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:891
TIntV InEIdV
Definition: graph.h:565
int GetId() const
Definition: graph.h:592
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:103
TNGraph TNet
Definition: graph.h:295