SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
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 LoadShM(TShMIn& ShMIn) {
47  Id = TInt(ShMIn);
48  NIdV.LoadShM(ShMIn);
49  }
50  void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); }
51  int GetId() const { return Id; }
52  int GetDeg() const { return NIdV.Len(); }
53  int GetInDeg() const { return GetDeg(); }
54  int GetOutDeg() const { return GetDeg(); }
55  int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
56  int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
57  int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
58  bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
59  bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
60  bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
61  void PackOutNIdV() { NIdV.Pack(); }
62  void PackNIdV() { NIdV.Pack(); }
63  void SortNIdV() { NIdV.Sort();}
64  friend class TUNGraph;
65  friend class TUNGraphMtx;
66  };
68  class TNodeI {
69  private:
72  public:
73  TNodeI() : NodeHI() { }
74  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
75  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
76  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
77 
79  TNodeI& operator++ (int) { NodeHI++; return *this; }
81  TNodeI& operator-- (int) { NodeHI--; return *this; }
82 
83 
84  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
85  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
86 
88  int GetId() const { return NodeHI.GetDat().GetId(); }
90  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
92  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
94  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
96  void SortNIdV() { NodeHI.GetDat().SortNIdV(); }
98 
101  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
103 
106  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
108 
111  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
113  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
115  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
117  bool IsNbrNId(const int& NId) const { return NodeHI.GetDat().IsNbrNId(NId); }
118  friend class TUNGraph;
119  };
121  class TEdgeI {
122  private:
124  int CurEdge;
125  public:
126  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
127  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
128  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
129  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
131  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; }
132  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
133  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
135  int GetId() const { return -1; }
137  int GetSrcNId() const { return CurNode.GetId(); }
139  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
140  friend class TUNGraph;
141  };
142 private:
146 private:
148  public:
150  void operator() (TNode* Node, TShMIn& ShMIn) { Node->LoadShM(ShMIn);}
151  };
152 private:
153  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
154  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
155  void LoadGraphShM(TShMIn& ShMIn) {
156  MxNId = TInt(ShMIn);
157  NEdges = TInt(ShMIn);
159  NodeH.LoadShM(ShMIn, Fn);
160  }
161 public:
162  TUNGraph() : CRef(), MxNId(0), NEdges(0), NodeH() { }
164  explicit TUNGraph(const int& Nodes, const int& Edges) : MxNId(0), NEdges(0) { Reserve(Nodes, Edges); }
165  TUNGraph(const TUNGraph& Graph) : MxNId(Graph.MxNId), NEdges(Graph.NEdges), NodeH(Graph.NodeH) { }
167  TUNGraph(TSIn& SIn) : MxNId(SIn), NEdges(SIn), NodeH(SIn) { }
169 
170  void Save(TSOut& SOut) const { MxNId.Save(SOut); NEdges.Save(SOut); NodeH.Save(SOut); }
172  static PUNGraph New() { return new TUNGraph(); }
174 
176  static PUNGraph New(const int& Nodes, const int& Edges) { return new TUNGraph(Nodes, Edges); }
178  static PUNGraph Load(TSIn& SIn) { return PUNGraph(new TUNGraph(SIn)); }
180 
182  static PUNGraph LoadShM(TShMIn& ShMIn) {
183  TUNGraph* Graph = new TUNGraph();
184  Graph->LoadGraphShM(ShMIn);
185  return PUNGraph(Graph);
186  }
187  bool HasFlag(const TGraphFlag& Flag) const;
188  TUNGraph& operator = (const TUNGraph& Graph) {
189  if (this!=&Graph) { MxNId=Graph.MxNId; NEdges=Graph.NEdges; NodeH=Graph.NodeH; } return *this; }
190 
192  int GetNodes() const { return NodeH.Len(); }
194 
198  int AddNode(int NId = -1);
200 
204  int AddNodeUnchecked(int NId = -1);
206  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId()); }
208 
217  int AddNode(const int& NId, const TIntV& NbrNIdV);
219 
227  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId);
229 
231  void DelNode(const int& NId);
233  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
235  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
237  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
239  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
241  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
243  int GetMxNId() const { return MxNId; }
244 
246  int GetEdges() const;
248 
252  int AddEdge(const int& SrcNId, const int& DstNId);
254 
261  int AddEdgeUnchecked(const int& SrcNId, const int& DstNId);
263  int AddEdge2(const int& SrcNId, const int& DstNId);
265  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
267 
270  void DelEdge(const int& SrcNId, const int& DstNId);
272  bool IsEdge(const int& SrcNId, const int& DstNId) const;
274  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; }
276  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
278  TEdgeI GetEI(const int& EId) const;
280 
282  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
283 
285  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
287  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
289  void GetNIdV(TIntV& NIdV) const;
290 
292  bool Empty() const { return GetNodes()==0; }
294  void Clr() { MxNId=0; NEdges=0; NodeH.Clr(); }
296  void SortNodeAdjV() { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { NI.SortNIdV();} }
298  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) NodeH.Gen(Nodes/2); }
300  void ReserveNIdDeg(const int& NId, const int& Deg) { GetNode(NId).NIdV.Reserve(Deg); }
302 
306  void Defrag(const bool& OnlyNodeLinks=false);
308 
310  bool IsOk(const bool& ThrowExcept=true) const;
312  void Dump(FILE *OutF=stdout) const;
314 
320  static PUNGraph GetSmallGraph();
321 
322  friend class TUNGraphMtx;
323  friend class TPt<TUNGraph>;
324 };
325 
326 //#//////////////////////////////////////////////
328 
342 class TNGraph {
343 public:
344  typedef TNGraph TNet;
346 public:
347  class TNode {
348  private:
351  public:
352  TNode() : Id(-1), InNIdV(), OutNIdV() { }
353  TNode(const int& NId) : Id(NId), InNIdV(), OutNIdV() { }
354  TNode(const TNode& Node) : Id(Node.Id), InNIdV(Node.InNIdV), OutNIdV(Node.OutNIdV) { }
355  TNode(TSIn& SIn) : Id(SIn), InNIdV(SIn), OutNIdV(SIn) { }
356  void Save(TSOut& SOut) const { Id.Save(SOut); InNIdV.Save(SOut); OutNIdV.Save(SOut); }
357  int GetId() const { return Id; }
358  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
359  int GetInDeg() const { return InNIdV.Len(); }
360  int GetOutDeg() const { return OutNIdV.Len(); }
361  int GetInNId(const int& NodeN) const { return InNIdV[NodeN]; }
362  int GetOutNId(const int& NodeN) const { return OutNIdV[NodeN]; }
363  int GetNbrNId(const int& NodeN) const { return NodeN<GetOutDeg()?GetOutNId(NodeN):GetInNId(NodeN-GetOutDeg()); }
364  bool IsInNId(const int& NId) const { return InNIdV.SearchBin(NId) != -1; }
365  bool IsOutNId(const int& NId) const { return OutNIdV.SearchBin(NId) != -1; }
366  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
367  void PackOutNIdV() { OutNIdV.Pack(); }
368  void PackNIdV() { InNIdV.Pack(); }
369  void SortNIdV() { InNIdV.Sort(); OutNIdV.Sort();}
370  void LoadShM(TShMIn& ShMIn) {
371  Id = TInt(ShMIn);
372  InNIdV.LoadShM(ShMIn);
373  OutNIdV.LoadShM(ShMIn);
374  }
375  friend class TNGraph;
376  friend class TNGraphMtx;
377  };
379  class TNodeI {
380  private:
383  public:
384  TNodeI() : NodeHI() { }
385  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
386  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
387  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
389  TNodeI& operator++ (int) { NodeHI++; return *this; }
391  TNodeI& operator-- (int) { NodeHI--; return *this; }
392 
393  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
394  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
396  int GetId() const { return NodeHI.GetDat().GetId(); }
398  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
400  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
402  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
404  void SortNIdV() { NodeHI.GetDat().SortNIdV(); }
406 
408  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
410 
412  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
414 
416  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
418  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
420  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
422  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
423  friend class TNGraph;
424  };
426  class TEdgeI {
427  private:
429  int CurEdge;
430  public:
431  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
432  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
433  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
434  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
437  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
438  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
439  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
441  int GetId() const { return -1; }
443  int GetSrcNId() const { return CurNode.GetId(); }
445  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
446  friend class TNGraph;
447  };
448 private:
452 private:
454  public:
456  void operator() (TNode* Node, TShMIn& ShMIn) {Node->LoadShM(ShMIn);}
457  };
458 private:
459  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
460  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
461  void LoadGraphShM(TShMIn& ShMIn) {
462  MxNId = TInt(ShMIn);
464  NodeH.LoadShM(ShMIn, Fn);
465  }
466 
467 public:
468  TNGraph() : CRef(), MxNId(0), NodeH() { }
470  explicit TNGraph(const int& Nodes, const int& Edges) : MxNId(0) { Reserve(Nodes, Edges); }
471  TNGraph(const TNGraph& Graph) : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
473  TNGraph(TSIn& SIn) : MxNId(SIn), NodeH(SIn) { }
475  void Save(TSOut& SOut) const { MxNId.Save(SOut); NodeH.Save(SOut); }
477  static PNGraph New() { return new TNGraph(); }
479 
481  static PNGraph New(const int& Nodes, const int& Edges) { return new TNGraph(Nodes, Edges); }
483  static PNGraph Load(TSIn& SIn) { return PNGraph(new TNGraph(SIn)); }
485 
488  static PNGraph LoadShM(TShMIn& ShMIn) {
489  TNGraph* Graph = new TNGraph();
490  Graph->LoadGraphShM(ShMIn);
491  return PNGraph(Graph);
492  }
494  bool HasFlag(const TGraphFlag& Flag) const;
495  TNGraph& operator = (const TNGraph& Graph) {
496  if (this!=&Graph) { MxNId=Graph.MxNId; NodeH=Graph.NodeH; } return *this; }
497 
499  int GetNodes() const { return NodeH.Len(); }
501 
505  int AddNode(int NId = -1);
507 
511  int AddNodeUnchecked(int NId = -1);
513  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
515 
524  int AddNode(const int& NId, const TIntV& InNIdV, const TIntV& OutNIdV);
526 
534  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& SrcVId, const int& DstVId);
536 
538  void DelNode(const int& NId);
540  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
542  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
544  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
546  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
548  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
549  // GetNodeC() has been commented out. It was a quick shortcut, do not use.
550  //const TNode& GetNodeC(const int& NId) const { return NodeH.GetDat(NId); }
552  int GetMxNId() const { return MxNId; }
553 
555  int GetEdges() const;
557 
563  int AddEdge(const int& SrcNId, const int& DstNId);
565 
572  int AddEdgeUnchecked(const int& SrcNId, const int& DstNId);
574  int AddEdge2(const int& SrcNId, const int& DstNId);
576  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
578 
582  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
584  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const;
586  TEdgeI BegEI() const { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0){NI++;} return TEdgeI(NI, EndNI()); }
588  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
590  TEdgeI GetEI(const int& EId) const; // not supported
592  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
593 
595  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
597  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
599  void GetNIdV(TIntV& NIdV) const;
600 
602  bool Empty() const { return GetNodes()==0; }
604  void Clr() { MxNId=0; NodeH.Clr(); }
606  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
608  void ReserveNIdInDeg(const int& NId, const int& InDeg) { GetNode(NId).InNIdV.Reserve(InDeg); }
610  void ReserveNIdOutDeg(const int& NId, const int& OutDeg) { GetNode(NId).OutNIdV.Reserve(OutDeg); }
612  void SortNodeAdjV() { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { NI.SortNIdV();} }
614 
619  void Defrag(const bool& OnlyNodeLinks=false);
621 
624  bool IsOk(const bool& ThrowExcept=true) const;
626  void Dump(FILE *OutF=stdout) const;
628 
632  static PNGraph GetSmallGraph();
633  friend class TPt<TNGraph>;
634  friend class TNGraphMtx;
635 };
636 
637 // set flags
638 namespace TSnap {
639 template <> struct IsDirected<TNGraph> { enum { Val = 1 }; };
640 }
641 
642 //#//////////////////////////////////////////////
644 
657 class TNEGraph {
658 public:
659  typedef TNEGraph TNet;
661 public:
662  class TNode {
663  private:
666  public:
667  TNode() : Id(-1), InEIdV(), OutEIdV() { }
668  TNode(const int& NId) : Id(NId), InEIdV(), OutEIdV() { }
669  TNode(const TNode& Node) : Id(Node.Id), InEIdV(Node.InEIdV), OutEIdV(Node.OutEIdV) { }
670  TNode(TSIn& SIn) : Id(SIn), InEIdV(SIn), OutEIdV(SIn) { }
671  void Save(TSOut& SOut) const { Id.Save(SOut); InEIdV.Save(SOut); OutEIdV.Save(SOut); }
672  int GetId() const { return Id; }
673  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
674  int GetInDeg() const { return InEIdV.Len(); }
675  int GetOutDeg() const { return OutEIdV.Len(); }
676  int GetInEId(const int& EdgeN) const { return InEIdV[EdgeN]; }
677  int GetOutEId(const int& EdgeN) const { return OutEIdV[EdgeN]; }
678  int GetNbrEId(const int& EdgeN) const { return EdgeN<GetOutDeg()?GetOutEId(EdgeN):GetInEId(EdgeN-GetOutDeg()); }
679  bool IsInEId(const int& EId) const { return InEIdV.SearchBin(EId) != -1; }
680  bool IsOutEId(const int& EId) const { return OutEIdV.SearchBin(EId) != -1; }
681  friend class TNEGraph;
682  };
683  class TEdge {
684  private:
686  public:
687  TEdge() : Id(-1), SrcNId(-1), DstNId(-1) { }
688  TEdge(const int& EId, const int& SourceNId, const int& DestNId) : Id(EId), SrcNId(SourceNId), DstNId(DestNId) { }
689  TEdge(const TEdge& Edge) : Id(Edge.Id), SrcNId(Edge.SrcNId), DstNId(Edge.DstNId) { }
690  TEdge(TSIn& SIn) : Id(SIn), SrcNId(SIn), DstNId(SIn) { }
691  void Save(TSOut& SOut) const { Id.Save(SOut); SrcNId.Save(SOut); DstNId.Save(SOut); }
692  int GetId() const { return Id; }
693  int GetSrcNId() const { return SrcNId; }
694  int GetDstNId() const { return DstNId; }
695  friend class TNEGraph;
696  };
698  class TNodeI {
699  private:
702  const TNEGraph *Graph;
703  public:
704  TNodeI() : NodeHI(), Graph(NULL) { }
705  TNodeI(const THashIter& NodeHIter, const TNEGraph* GraphPt) : NodeHI(NodeHIter), Graph(GraphPt) { }
706  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI), Graph(NodeI.Graph) { }
707  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; Graph=NodeI.Graph; return *this; }
709  TNodeI& operator++ (int) { NodeHI++; return *this; }
711  TNodeI& operator-- (int) { NodeHI--; return *this; }
712 
713  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
714  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
716  int GetId() const { return NodeHI.GetDat().GetId(); }
718  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
720  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
722  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
724 
726  int GetInNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetInEId(EdgeN)).GetSrcNId(); }
728 
730  int GetOutNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetOutEId(EdgeN)).GetDstNId(); }
732 
734  int GetNbrNId(const int& EdgeN) const { const TEdge& E = Graph->GetEdge(NodeHI.GetDat().GetNbrEId(EdgeN));
735  return GetId()==E.GetSrcNId() ? E.GetDstNId():E.GetSrcNId(); }
737  bool IsInNId(const int& NId) const;
739  bool IsOutNId(const int& NId) const;
741  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
742  // edges
744  int GetInEId(const int& EdgeN) const { return NodeHI.GetDat().GetInEId(EdgeN); }
746  int GetOutEId(const int& EdgeN) const { return NodeHI.GetDat().GetOutEId(EdgeN); }
748  int GetNbrEId(const int& EdgeN) const { return NodeHI.GetDat().GetNbrEId(EdgeN); }
750  bool IsInEId(const int& EId) const { return NodeHI.GetDat().IsInEId(EId); }
752  bool IsOutEId(const int& EId) const { return NodeHI.GetDat().IsOutEId(EId); }
754  bool IsNbrEId(const int& EId) const { return IsInEId(EId) || IsOutEId(EId); }
755  friend class TNEGraph;
756  };
758  class TEdgeI {
759  private:
762  const TNEGraph *Graph;
763  public:
764  TEdgeI() : EdgeHI(), Graph(NULL) { }
765  TEdgeI(const THashIter& EdgeHIter, const TNEGraph *GraphPt) : EdgeHI(EdgeHIter), Graph(GraphPt) { }
766  TEdgeI(const TEdgeI& EdgeI) : EdgeHI(EdgeI.EdgeHI), Graph(EdgeI.Graph) { }
767  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { EdgeHI=EdgeI.EdgeHI; Graph=EdgeI.Graph; } return *this; }
769  TEdgeI& operator++ (int) { EdgeHI++; return *this; }
770  bool operator < (const TEdgeI& EdgeI) const { return EdgeHI < EdgeI.EdgeHI; }
771  bool operator == (const TEdgeI& EdgeI) const { return EdgeHI == EdgeI.EdgeHI; }
773  int GetId() const { return EdgeHI.GetDat().GetId(); }
775  int GetSrcNId() const { return EdgeHI.GetDat().GetSrcNId(); }
777  int GetDstNId() const { return EdgeHI.GetDat().GetDstNId(); }
778  friend class TNEGraph;
779  };
780 
781 private:
782  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
783  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
784  TEdge& GetEdge(const int& EId) { return EdgeH.GetDat(EId); }
785  const TEdge& GetEdge(const int& EId) const { return EdgeH.GetDat(EId); }
786 private:
791 public:
792  TNEGraph() : CRef(), MxNId(0), MxEId(0) { }
794  explicit TNEGraph(const int& Nodes, const int& Edges) : CRef(), MxNId(0), MxEId(0) { Reserve(Nodes, Edges); }
795  TNEGraph(const TNEGraph& Graph) : MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { }
797  TNEGraph(TSIn& SIn) : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { }
799  void Save(TSOut& SOut) const { MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut); }
801  static PNEGraph New() { return PNEGraph(new TNEGraph()); }
803 
805  static PNEGraph New(const int& Nodes, const int& Edges) { return PNEGraph(new TNEGraph(Nodes, Edges)); }
807  static PNEGraph Load(TSIn& SIn) { return PNEGraph(new TNEGraph(SIn)); }
809  bool HasFlag(const TGraphFlag& Flag) const;
810  TNEGraph& operator = (const TNEGraph& Graph) { if (this!=&Graph) {
811  MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; } return *this; }
812 
814  int GetNodes() const { return NodeH.Len(); }
816 
820  int AddNode(int NId = -1);
822  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
824 
826  void DelNode(const int& NId);
828  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
830  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
832  TNodeI BegNI() const { return TNodeI(NodeH.BegI(), this); }
834  TNodeI EndNI() const { return TNodeI(NodeH.EndI(), this); }
836  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId), this); }
838  int GetMxNId() const { return MxNId; }
839 
841  int GetEdges() const { return EdgeH.Len(); }
843 
848  int AddEdge(const int& SrcNId, const int& DstNId, int EId = -1);
850  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); }
852  void DelEdge(const int& EId);
854 
858  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
860  bool IsEdge(const int& EId) const { return EdgeH.IsKey(EId); }
862  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const { int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); }
864  bool IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir = true) const;
866  int GetEId(const int& SrcNId, const int& DstNId) const { int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; }
868  TEdgeI BegEI() const { return TEdgeI(EdgeH.BegI(), this); }
870  TEdgeI EndEI() const { return TEdgeI(EdgeH.EndI(), this); }
872  TEdgeI GetEI(const int& EId) const { return TEdgeI(EdgeH.GetI(EId), this); }
874  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const { return GetEI(GetEId(SrcNId, DstNId)); }
875 
877  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
879  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
881  int GetRndEId(TRnd& Rnd=TInt::Rnd) { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); }
883  TEdgeI GetRndEI(TRnd& Rnd=TInt::Rnd) { return GetEI(GetRndEId(Rnd)); }
885  void GetNIdV(TIntV& NIdV) const;
887  void GetEIdV(TIntV& EIdV) const;
888 
890  bool Empty() const { return GetNodes()==0; }
892  void Clr() { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); }
894  void Reserve(const int& Nodes, const int& Edges) {
895  if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } }
897 
902  void Defrag(const bool& OnlyNodeLinks=false);
904 
907  bool IsOk(const bool& ThrowExcept=true) const;
909  void Dump(FILE *OutF=stdout) const;
911 
915  static PNEGraph GetSmallGraph();
916  friend class TPt<TNEGraph>;
917 };
918 
919 // set flags
920 namespace TSnap {
921 template <> struct IsMultiGraph<TNEGraph> { enum { Val = 1 }; };
922 template <> struct IsDirected<TNEGraph> { enum { Val = 1 }; };
923 }
924 
925 //#//////////////////////////////////////////////
927 
928 class TBPGraph {
929 public:
930  typedef TBPGraph TNet;
932  typedef enum { bgsUndef, bgsLeft, bgsRight, bgsBoth } TNodeTy; // left or right hand side node
933 public:
934  class TNode {
935  private:
938  TNodeTy NodeTy; // remove
939  public:
940  TNode() : Id(-1), NIdV(), NodeTy(bgsUndef) { }
941  TNode(const int& NId) : Id(NId), NIdV(), NodeTy(true?bgsLeft:bgsRight) { }
942  TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV), NodeTy(Node.NodeTy) { }
943  TNode(TSIn& SIn) : Id(SIn), NIdV(SIn), NodeTy(bgsUndef) { TInt Ty(SIn); NodeTy=(TNodeTy)Ty.Val; }
944  void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); TInt(NodeTy).Save(SOut); }
945  int GetId() const { return Id; }
946  int GetDeg() const { return NIdV.Len(); }
947  int GetInDeg() const { return GetDeg(); }
948  int GetOutDeg() const { return GetDeg(); }
949  int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
950  int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
951  int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
952  bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
953  bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
954  bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
955  void PackOutNIdV() { NIdV.Pack(); }
956  void PackNIdV() { NIdV.Pack(); }
957  friend class TBPGraph;
958  };
960  class TNodeI {
961  private:
963  THashIter LeftHI, RightHI; // iterator over left and right hand-side nodes
964  private:
965  inline THashIter HI() const { return ! LeftHI.IsEnd()?LeftHI:RightHI; }
966  public:
967  TNodeI() : LeftHI(), RightHI() { }
968  TNodeI(const THashIter& LeftHIter, const THashIter& RightHIter) : LeftHI(LeftHIter), RightHI(RightHIter) { }
969  TNodeI(const TNodeI& NodeI) : LeftHI(NodeI.LeftHI), RightHI(NodeI.RightHI) { }
970  TNodeI& operator = (const TNodeI& NodeI) { LeftHI = NodeI.LeftHI; RightHI=NodeI.RightHI; return *this; }
973  if (! LeftHI.IsEnd()) {
974  LeftHI++; }
975  else if (! RightHI.IsEnd()) {
976  RightHI++; }
977  return *this; }
978  bool operator < (const TNodeI& NodeI) const { return LeftHI < NodeI.LeftHI || (LeftHI==NodeI.LeftHI && RightHI < NodeI.RightHI); }
979  bool operator == (const TNodeI& NodeI) const { return LeftHI==NodeI.LeftHI && RightHI==NodeI.RightHI; }
981  int GetId() const { return HI().GetDat().GetId(); }
983  bool IsLeft() const { return ! LeftHI.IsEnd(); }
985  bool IsRight() const { return ! IsLeft(); }
987  int GetDeg() const { return HI().GetDat().GetDeg(); }
989  int GetInDeg() const { return HI().GetDat().GetInDeg(); }
991  int GetOutDeg() const { return HI().GetDat().GetOutDeg(); }
993 
994  int GetInNId(const int& NodeN) const { return HI().GetDat().GetInNId(NodeN); }
996 
997  int GetOutNId(const int& NodeN) const { return HI().GetDat().GetOutNId(NodeN); }
999 
1000  int GetNbrNId(const int& NodeN) const { return HI().GetDat().GetNbrNId(NodeN); }
1002  bool IsInNId(const int& NId) const { return HI().GetDat().IsInNId(NId); }
1004  bool IsOutNId(const int& NId) const { return HI().GetDat().IsOutNId(NId); }
1006  bool IsNbrNId(const int& NId) const { return HI().GetDat().IsNbrNId(NId); }
1007  friend class TBPGraph;
1008  };
1010  class TEdgeI {
1011  private:
1012  TNodeI CurNode, EndNode; // end node on the 'left'
1013  int CurEdge;
1014  public:
1015  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
1016  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
1017  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
1018  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
1021  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
1022  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
1023  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
1025  int GetId() const { return -1; }
1027  int GetSrcNId() const { return CurNode.GetId(); }
1029  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
1031  int GetLNId() const { return GetSrcNId(); }
1033  int GetRNId() const { return GetDstNId(); }
1034  friend class TBPGraph;
1035  };
1036 private:
1038  TInt MxNId; // maximum node ID in the graph
1039  THash<TInt, TNode> LeftH; // 'left' nodes
1040  THash<TInt, TNode> RightH; // 'right' nodes
1041 private:
1042  //TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
1043  //const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
1044 public:
1045  TBPGraph() : CRef(), MxNId(0), LeftH(), RightH() { }
1047  explicit TBPGraph(const int& Nodes, const int& Edges) : MxNId(0) { }
1048  TBPGraph(const TBPGraph& BPGraph) : MxNId(BPGraph.MxNId), LeftH(BPGraph.LeftH), RightH(BPGraph.RightH) { }
1050  TBPGraph(TSIn& SIn) : MxNId(SIn), LeftH(SIn), RightH(SIn) { }
1052  void Save(TSOut& SOut) const { MxNId.Save(SOut); LeftH.Save(SOut); RightH.Save(SOut); }
1054  static PBPGraph New() { return new TBPGraph(); }
1056 
1057  static PBPGraph New(const int& Nodes, const int& Edges) { return new TBPGraph(Nodes, Edges); }
1059  static PBPGraph Load(TSIn& SIn) { return PBPGraph(new TBPGraph(SIn)); }
1061  bool HasFlag(const TGraphFlag& Flag) const;
1062  TBPGraph& operator = (const TBPGraph& BPGraph) {
1063  if (this!=&BPGraph) { MxNId=BPGraph.MxNId; LeftH=BPGraph.LeftH; RightH=BPGraph.RightH; } return *this; }
1064 
1066  int GetNodes() const { return GetLNodes() + GetRNodes(); }
1068  int GetLNodes() const { return LeftH.Len(); }
1070  int GetRNodes() const { return RightH.Len(); }
1072 
1073  int AddNode(int NId = -1, const bool& LeftNode=true);
1075  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId(), NodeI.IsLeft()); }
1077 
1078  void DelNode(const int& NId);
1080  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
1082  bool IsNode(const int& NId) const { return IsLNode(NId) || IsRNode(NId); }
1084  bool IsLNode(const int& NId) const { return LeftH.IsKey(NId); }
1086  bool IsRNode(const int& NId) const { return RightH.IsKey(NId); }
1088  int GetMxNId() const { return MxNId; }
1089 
1091  TNodeI BegNI() const { return TNodeI(LeftH.BegI(), RightH.BegI()); }
1093  TNodeI EndNI() const { return TNodeI(LeftH.EndI(), RightH.EndI()); }
1095  TNodeI GetNI(const int& NId) const { return IsLNode(NId) ? TNodeI(LeftH.GetI(NId), RightH.EndI()) : TNodeI(LeftH.EndI(), RightH.GetI(NId)); }
1097  TNodeI BegLNI() const { return TNodeI(LeftH.BegI(), RightH.EndI()); }
1099  TNodeI EndLNI() const { return EndNI(); }
1101  TNodeI BegRNI() const { return TNodeI(LeftH.EndI(), RightH.BegI()); }
1103  TNodeI EndRNI() const { return EndNI(); }
1104 
1106  int GetEdges() const;
1108 
1109  int AddEdge(const int& LeftNId, const int& RightNId);
1111  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
1113 
1114  void DelEdge(const int& LeftNId, const int& RightNId);
1116  bool IsEdge(const int& LeftNId, const int& RightNId) const;
1118  TEdgeI BegEI() const { TNodeI NI=BegLNI(); while (NI<EndLNI() && (NI.GetOutDeg()==0 || NI.GetId()>NI.GetOutNId(0))) { NI++; } return TEdgeI(NI, EndLNI()); }
1120  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
1122  TEdgeI GetEI(const int& EId) const;
1124 
1125  TEdgeI GetEI(const int& LeftNId, const int& RightNId) const;
1126 
1128  int GetRndNId(TRnd& Rnd=TInt::Rnd);
1130  int GetRndLNId(TRnd& Rnd=TInt::Rnd);
1132  int GetRndRNId(TRnd& Rnd=TInt::Rnd);
1134  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
1136  void GetNIdV(TIntV& NIdV) const;
1138  void GetLNIdV(TIntV& NIdV) const;
1140  void GetRNIdV(TIntV& NIdV) const;
1141 
1143  bool Empty() const { return GetNodes()==0; }
1145  void Clr() { MxNId=0; LeftH.Clr(); RightH.Clr(); }
1147  void Reserve(const int& Nodes, const int& Edges);
1149 
1150  void Defrag(const bool& OnlyNodeLinks=false);
1152 
1153  bool IsOk(const bool& ThrowExcept=true) const;
1155  void Dump(FILE *OutF=stdout) const;
1157 
1158  static PBPGraph GetSmallGraph();
1159 
1160  friend class TPt<TBPGraph>;
1161 };
1162 
1163 // set flags
1164 namespace TSnap {
1165 template <> struct IsBipart<TBPGraph> { enum { Val = 1 }; };
1166 }
1167 
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: graph.cpp:232
Definition: bd.h:440
static PBPGraph GetSmallGraph()
Returns a small graph on 2 'left', 3 'right' nodes and 4 edges.
Definition: graph.cpp:868
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetLNId() and EdgeI.GetRNId() to the graph.
Definition: graph.h:1111
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:121
void LoadGraphShM(TShMIn &ShMIn)
Definition: graph.h:155
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:416
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:978
TBPGraph(const TBPGraph &BPGraph)
!! Reserve(Nodes, Edges); }
Definition: graph.h:1048
int GetDeg() const
Definition: graph.h:946
static PNGraph LoadShM(TShMIn &ShMIn)
Static constructor that loads the graph from a shared memory stream and returns pointer to it...
Definition: graph.h:488
int GetId() const
Gets edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:1025
TPt< TBPGraph > PNet
Definition: graph.h:931
TUNGraph(const TUNGraph &Graph)
Definition: graph.h:165
THashIter NodeHI
Definition: graph.h:71
int AddNode(const TNodeI &NodeId)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:513
int GetOutDeg() const
Definition: graph.h:360
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a biparite graph of Nodes nodes and Edges edges.
Definition: graph.cpp:790
int GetSrcNId() const
Definition: graph.h:693
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:376
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:176
TNodeI CurNode
Definition: graph.h:428
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.cpp:468
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:497
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:516
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:544
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:586
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:276
void GetRNIdV(TIntV &NIdV) const
Gets a vector IDs of all 'right' nodes in the bipartite graph.
Definition: graph.cpp:784
TNode(const int &NId)
Definition: graph.h:43
bool IsOutNId(const int &NId) const
Definition: graph.h:60
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:133
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:81
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:1020
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:970
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:1057
TNode(const TNode &Node)
Definition: graph.h:942
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:890
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:979
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:1095
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:989
static PUNGraph GetSmallGraph()
Returns a small graph on 5 nodes and 5 edges.
Definition: graph.cpp:221
Definition: dt.h:11
TNEGraph & operator=(const TNEGraph &Graph)
Definition: graph.h:810
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:70
TNode & GetNode(const int &NId)
Definition: graph.h:153
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:771
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:117
int GetNbrEId(const int &EdgeN) const
Definition: graph.h:678
void LoadShM(TShMIn &ShMIn)
Definition: graph.h:46
TEdge(const int &EId, const int &SourceNId, const int &DestNId)
Definition: graph.h:688
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.cpp:754
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:1082
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:437
TNodeTy NodeTy
Definition: graph.h:938
int Val
Definition: dt.h:1136
const TNEGraph * Graph
Definition: graph.h:762
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
void Save(TSOut &SOut) const
Definition: dt.h:1150
void ReserveNIdInDeg(const int &NId, const int &InDeg)
Reserves memory for node ID NId having InDeg in-edges.
Definition: graph.h:608
THashIter HI() const
Definition: graph.h:965
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:868
TNEGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:794
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:1075
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:153
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:588
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:722
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:139
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:243
void PackNIdV()
Definition: graph.h:62
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:418
bool IsLNode(const int &NId) const
Tests whether ID NId is a 'left' side node.
Definition: graph.h:1084
TNEGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:797
TUNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:167
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:434
Tests (at compile time) if the graph is directed.
Definition: gbase.h:28
void LoadShM(TShMIn &ShMIn)
Definition: graph.h:370
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:1088
int GetNbrNId(const int &NodeN) const
Definition: graph.h:951
TIter BegI() const
Definition: hash.h:213
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:960
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:1029
THash< TInt, TNode > NodeH
Definition: graph.h:451
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TNodeTy
Definition: graph.h:932
int AddEdge(const TEdgeI &EdgeI)
Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph.
Definition: graph.h:576
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool IsInEId(const int &EId) const
Definition: graph.h:679
int GetId() const
Definition: graph.h:672
TPt< TNEGraph > PNet
Definition: graph.h:660
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:76
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:770
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:68
int GetId() const
Gets edge ID.
Definition: graph.h:773
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:1093
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:1023
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:330
void Save(TSOut &SOut) const
Definition: hash.h:183
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:1091
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:862
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:711
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:586
int GetInDeg() const
Definition: graph.h:674
TInt NEdges
Definition: graph.h:144
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:436
TInt SrcNId
Definition: graph.h:685
void Save(TSOut &SOut) const
Definition: graph.h:691
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:391
int GetOutEId(const int &EdgeN) const
Definition: graph.h:677
int AddNode(int NId=-1, const bool &LeftNode=true)
Adds a node of ID NId to the graph.
Definition: graph.cpp:671
void LoadGraphShM(TShMIn &ShMIn)
Definition: graph.h:461
void PackOutNIdV()
Definition: graph.h:61
Directed multigraph.
Definition: graph.h:657
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:870
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:1120
THashIter NodeHI
Definition: graph.h:382
int GetInDeg() const
Definition: graph.h:53
void PackOutNIdV()
Definition: graph.h:367
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TCRef CRef
Definition: graph.h:787
Definition: fl.h:384
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:207
static PNEGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:807
TNGraph()
Definition: graph.h:468
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:758
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:799
TNode(const int &NId)
Definition: graph.h:353
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:137
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:391
TCRef CRef
Definition: graph.h:1037
TNodeI EndNode
Definition: graph.h:123
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:170
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:422
const TNode & GetNode(const int &NId) const
Definition: graph.h:154
TIter EndI() const
Definition: hash.h:218
TNEGraph TNet
Definition: graph.h:659
THashIter NodeHI
Definition: graph.h:701
void SortNodeAdjV()
Sorts the adjacency lists of each node.
Definition: graph.h:296
static TRnd Rnd
Definition: dt.h:1143
int GetId() const
Definition: graph.h:51
bool IsInNId(const int &NId) const
Definition: graph.h:59
int GetRndLNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random 'left' node in the graph.
Definition: graph.cpp:762
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:709
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:1047
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:754
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:832
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:441
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:828
THashIter EdgeHI
Definition: graph.h:761
void LoadShM(TShMIn &ShMIn)
Load THash from shared memory file. Copying/Deleting Keys is illegal.
Definition: hash.h:157
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:192
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:1002
TNodeI(const THashIter &NodeHIter, const TNEGraph *GraphPt)
Definition: graph.h:705
THash< TInt, TNode > NodeH
Definition: graph.h:789
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: graph.h:726
TNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:473
TNodeI EndNode
Definition: graph.h:428
int GetInNId(const int &NodeN) const
Definition: graph.h:949
int GetOutDeg() const
Definition: graph.h:675
TNode & GetNode(const int &NId)
Definition: graph.h:459
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition: graph.h:850
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:954
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:285
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:769
int GetId() const
Returns ID of the current node.
Definition: graph.h:981
int GetOutNId(const int &NodeN) const
Definition: graph.h:950
int GetInDeg() const
Definition: graph.h:359
int GetDstNId() const
Gets destination of an edge.
Definition: graph.h:777
THash< TInt, TEdge > EdgeH
Definition: graph.h:790
int GetDstNId() const
Definition: graph.h:694
int GetLNodes() const
Returns the number of nodes on the 'left' side of the biparite graph.
Definition: graph.h:1068
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:128
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:132
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:1016
static PBPGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:1059
TCRef CRef
Definition: graph.h:143
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:294
int GetOutEId(const int &EdgeN) const
Returns ID of EdgeN-th out-edge.
Definition: graph.h:746
void SortNodeAdjV()
Sorts the adjacency lists of each node.
Definition: graph.h:612
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:1000
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:90
void Clr()
Deletes all nodes and edges from the bipartite graph.
Definition: graph.h:1145
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:834
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TNode(const int &NId)
Definition: graph.h:941
int GetInEId(const int &EdgeN) const
Definition: graph.h:676
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:836
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:991
int GetId() const
Definition: graph.h:357
int GetNbrNId(const int &EdgeN) const
Returns ID of EdgeN-th neighboring node.
Definition: graph.h:734
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:564
TEdgeI GetEI(const int &SrcNId, const int &DstNId) const
Returns an iterator referring to edge (SrcNId, DstNId) in the graph.
Definition: graph.h:874
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:814
static PUNGraph LoadShM(TShMIn &ShMIn)
Static constructor that loads the graph from shared memory.
Definition: graph.h:182
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:79
THashIter LeftHI
Definition: graph.h:963
void SortNIdV()
Sorts the adjacency lists of the current node.
Definition: graph.h:404
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:94
bool Empty() const
Tests whether the bipartite graph is empty (has zero nodes).
Definition: graph.h:1143
void PackNIdV()
Definition: graph.h:368
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TEdge & GetEdge(const int &EId)
Definition: graph.h:784
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:1031
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:381
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.cpp:477
TNodeI EndRNI() const
Returns an iterator referring to the past-the-end 'right' node in the graph.
Definition: graph.h:1103
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:389
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
bool IsOutNId(const int &NId) const
Definition: graph.h:954
void Gen(const int &ExpectVals)
Definition: hash.h:222
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:426
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:698
TInt MxNId
Definition: graph.h:144
TNodeI(const THashIter &LeftHIter, const THashIter &RightHIter)
Definition: graph.h:968
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
static PNEGraph GetSmallGraph()
Returns a small multigraph on 5 nodes and 6 edges.
Definition: graph.cpp:660
int GetNodes() const
Returns the total number of nodes in the graph.
Definition: graph.h:1066
bool IsInNId(const int &NId) const
Definition: graph.h:953
static PNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:483
Definition: gsvd.h:5
TEdgeI(const THashIter &EdgeHIter, const TNEGraph *GraphPt)
Definition: graph.h:765
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:233
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:345
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:298
TNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:470
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:292
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:595
TNodeI BegLNI() const
Returns an iterator referring to the first 'left' node in the graph.
Definition: graph.h:1097
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: graph.h:744
TNode(TSIn &SIn)
Definition: graph.h:670
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:420
TNode(TSIn &SIn)
Definition: graph.h:355
int GetRNodes() const
Returns the number of nodes on the 'right' side of the biparite graph.
Definition: graph.h:1070
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:241
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: graph.cpp:464
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:700
Tests (at compile time) if the graph is a multigraph with multiple edges between the same nodes...
Definition: gbase.h:30
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:542
TNodeI(const TNodeI &NodeI)
Definition: graph.h:75
TNode(const TNode &Node)
Definition: graph.h:669
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: graph.h:730
TInt DstNId
Definition: graph.h:685
void operator()(TNode *Node, TShMIn &ShMIn)
Definition: graph.h:150
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:735
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:1022
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:265
TInt MxNId
Definition: graph.h:1038
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:274
void PackOutNIdV()
Definition: graph.h:955
void GetLNIdV(TIntV &NIdV) const
Gets a vector IDs of all 'left' nodes in the bipartite graph.
Definition: graph.cpp:778
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:172
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:432
TPt< TNGraph > PNGraph
Pointer to a directed graph (TNGraph)
Definition: graph.h:16
TNodeI CurNode
Definition: graph.h:123
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:713
TBPGraph TNet
Definition: graph.h:930
int GetOutNId(const int &NodeN) const
Definition: graph.h:362
TBPGraph & operator=(const TBPGraph &BPGraph)
Definition: graph.h:1062
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:486
int AddNode(const TNodeI &NodeId)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:822
void DelEdge(const int &SrcNId, const int &DstNId)
Deletes an edge between node IDs SrcNId and DstNId from the graph.
Definition: graph.cpp:124
TNodeI BegRNI() const
Returns an iterator referring to the first 'right' node in the graph.
Definition: graph.h:1101
int GetOutNId(const int &NodeN) const
Definition: graph.h:56
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the network, noop if the node already exists.
Definition: graph.cpp:20
TIntV OutEIdV
Definition: graph.h:665
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: graph.cpp:639
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:287
void Save(TSOut &SOut) const
Definition: graph.h:944
void Save(TSOut &SOut) const
Definition: graph.h:50
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:1099
void SortNIdV()
Definition: graph.h:63
int GetRNId() const
Gets the ID of the node on the 'right' side of the edge.
Definition: graph.h:1033
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:597
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:387
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:714
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:398
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:85
Definition: fl.h:128
TNGraph(const TNGraph &Graph)
Definition: graph.h:471
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:962
TIntV NIdV
Definition: graph.h:937
int GetDeg() const
Definition: graph.h:673
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:1006
void operator()(TNode *Node, TShMIn &ShMIn)
Definition: graph.h:456
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1519
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:382
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:393
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:987
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:604
TUNGraph()
Definition: graph.h:162
Definition: dt.h:1134
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:552
bool IsNbrNId(const int &NId) const
Definition: graph.h:952
TNEGraph(const TNEGraph &Graph)
Definition: graph.h:795
TNode & GetNode(const int &NId)
Definition: graph.h:782
static PUNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:178
static PNEGraph New()
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New().
Definition: graph.h:801
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:475
void Save(TSOut &SOut) const
Definition: graph.h:671
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:705
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:719
THashIter RightHI
Definition: graph.h:963
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:1027
TEdge(const TEdge &Edge)
Definition: graph.h:689
Directed graph.
Definition: graph.h:342
bool IsNbrNId(const int &NId) const
Definition: graph.h:58
void LoadShM(TShMIn &ShMIn)
Constructs the vector from a shared memory input.
Definition: ds.h:932
int GetNbrNId(const int &NodeN) const
Definition: graph.h:57
TEdgeI GetEI(const int &EId) const
Returns an iterator referring to edge with edge ID EId.
Definition: graph.h:872
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:682
TNode(const TNode &Node)
Definition: graph.h:354
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:1018
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:860
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:546
int GetId() const
Returns ID of the current node.
Definition: graph.h:396
TBPGraph()
Definition: graph.h:1045
static PNGraph GetSmallGraph()
Returns a small graph on 5 nodes and 6 edges.
Definition: graph.cpp:454
TNode(const int &NId)
Definition: graph.h:668
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:106
int GetRndRNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random 'right' node in the graph.
Definition: graph.cpp:766
const TNode & GetNode(const int &NId) const
Definition: graph.h:783
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:135
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:894
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
Bipartite graph.
Definition: graph.h:928
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:866
int GetSrcNId() const
Gets the source of an edge.
Definition: graph.h:775
int AddEdge2(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph. If nodes do not exists, create them.
Definition: graph.cpp:112
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:707
THash< TInt, TNode > NodeH
Definition: graph.h:145
TEdge(TSIn &SIn)
Definition: graph.h:690
TNode(const TNode &Node)
Definition: graph.h:44
bool IsOutEId(const int &EId) const
Definition: graph.h:680
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:481
void GetEIdV(TIntV &EIdV) const
Gets a vector IDs of all edges in the graph.
Definition: graph.cpp:570
TNode(TSIn &SIn)
Definition: graph.h:943
void Pack()
Reduces vector capacity (frees memory) to match its size.
Definition: ds.h:1057
int GetId() const
Definition: graph.h:945
int GetRndEId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random edge in the graph.
Definition: graph.h:881
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:1086
bool IsRight() const
Tests whether the node is right hand side node.
Definition: graph.h:985
TCRef CRef
Definition: graph.h:449
THash< TInt, TNode > RightH
Definition: graph.h:1040
Definition: hash.h:97
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:385
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:805
TPt< TNGraph > PNet
Definition: graph.h:345
int GetNbrEId(const int &EdgeN) const
Returns ID of EdgeN-th in or out-edge.
Definition: graph.h:748
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.h:841
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:92
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:160
const TEdge & GetEdge(const int &EId) const
Definition: graph.h:785
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:239
int GetOutDeg() const
Definition: graph.h:948
const TNEGraph * Graph
Definition: graph.h:702
TIntV InNIdV
Definition: graph.h:350
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:1004
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:877
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:606
static PBPGraph New()
Static constructor that returns a pointer to the graph. Call: PBPGraph BPGraph = TBPGraph::New();.
Definition: graph.h:1054
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:111
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
THash< TInt, TNode > LeftH
Definition: graph.h:1039
TNode(TSIn &SIn)
Definition: graph.h:45
TInt MxNId
Definition: graph.h:788
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:235
TInt MxNId
Definition: graph.h:450
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:997
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:879
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:444
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:394
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:892
TNodeI CurNode
Definition: graph.h:1012
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the bipartite graph.
Definition: graph.cpp:770
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:294
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:972
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: graph.cpp:170
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:543
TEdgeI GetRndEI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random edge in the graph.
Definition: graph.h:883
TIntV OutNIdV
Definition: graph.h:350
int GetOutDeg() const
Definition: graph.h:54
const TNode & GetNode(const int &NId) const
Definition: graph.h:460
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the network, noop if the node already exists.
Definition: graph.cpp:247
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
void SortNIdV()
Sorts the adjacency lists of the current node.
Definition: graph.h:96
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:577
void SortNIdV()
Definition: graph.h:369
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:1118
TNodeI EndNode
Definition: graph.h:1012
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:400
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:540
void Dump(FILE *OutF=stdout) const
Print the biparite graph in a human readable form to an output stream OutF.
Definition: graph.cpp:845
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:1080
int GetSrcNId() const
Returns the source node of the edge.
Definition: graph.h:443
int GetId() const
Returns ID of the current node.
Definition: graph.h:716
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the biparite graph.
Definition: graph.cpp:794
int GetId() const
Returns ID of the current node.
Definition: graph.h:88
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:137
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:1017
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:408
void PackNIdV()
Definition: graph.h:956
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:115
void DelEdge(const int &EId)
Deletes an edge with edge ID EId from the graph.
Definition: graph.cpp:527
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
int GetInNId(const int &NodeN) const
Definition: graph.h:55
bool IsOutEId(const int &EId) const
Tests whether the edge with ID EId is an out-edge of current node.
Definition: graph.h:752
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:237
int GetInNId(const int &NodeN) const
Definition: graph.h:361
int GetDeg() const
Definition: graph.h:358
int GetDeg() const
Definition: graph.h:52
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:438
void ReserveNIdDeg(const int &NId, const int &Deg)
Reserves memory for node ID NId having Deg edges.
Definition: graph.h:300
bool IsOutNId(const int &NId) const
Definition: graph.h:365
int GetNbrNId(const int &NodeN) const
Definition: graph.h:363
int Len() const
Definition: hash.h:228
TNodeI(const TNodeI &NodeI)
Definition: graph.h:969
TInt MxEId
Definition: graph.h:788
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:206
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:74
TNodeI(const TNodeI &NodeI)
Definition: graph.h:706
bool IsInEId(const int &EId) const
Tests whether the edge with ID EId is an in-edge of current node.
Definition: graph.h:750
bool IsNbrNId(const int &NId) const
Definition: graph.h:366
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:439
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:1134
bool IsOk(const bool &ThrowExcept=true) const
Checks the bipartite graph data structure for internal consistency.
Definition: graph.cpp:803
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:67
Tests (at compile time) if the graph is a bipartite graph type.
Definition: gbase.h:38
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:767
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:766
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:1010
void Save(TSOut &SOut) const
Definition: graph.h:356
TNGraph & operator=(const TNGraph &Graph)
Definition: graph.h:495
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:698
int AddEdge2(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph. If nodes do not exist, create them.
Definition: graph.cpp:336
int GetDstNId() const
Returns the destination node of the edge.
Definition: graph.h:445
bool IsInNId(const int &NId) const
Definition: graph.h:364
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:84
int GetInDeg() const
Definition: graph.h:947
TUNGraph & operator=(const TUNGraph &Graph)
Definition: graph.h:188
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:412
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:718
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:602
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:131
THash< TInt, TEdge >::TIter THashIter
Definition: graph.h:760
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:720
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:127
bool IsLeft() const
Tests whether the node is left hand side node.
Definition: graph.h:983
TUNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:164
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:830
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:433
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:129
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:101
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:1052
TBPGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:1050
TNodeI(const TNodeI &NodeI)
Definition: graph.h:386
void ReserveNIdOutDeg(const int &NId, const int &OutDeg)
Reserves memory for node ID NId having OutDeg out-edges.
Definition: graph.h:610
TNEGraph()
Definition: graph.h:792
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:103
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:741
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:838
TIter GetI(const TKey &Key) const
Definition: hash.h:220
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:994
TIntV InEIdV
Definition: graph.h:665
int GetId() const
Definition: graph.h:692
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:113
TNGraph TNet
Definition: graph.h:344