SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  void SortNIdV() { NIdV.Sort();}
60  friend class TUNGraph;
61  friend class TUNGraphMtx;
62  };
64  class TNodeI {
65  private:
68  public:
69  TNodeI() : NodeHI() { }
70  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
71  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
72  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
73 
75  TNodeI& operator++ (int) { NodeHI++; return *this; }
77  TNodeI& operator-- (int) { NodeHI--; return *this; }
78 
79 
80  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
81  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
82 
84  int GetId() const { return NodeHI.GetDat().GetId(); }
86  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
88  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
90  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
92  void SortNIdV() { NodeHI.GetDat().SortNIdV(); }
94 
97  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
99 
102  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
104 
107  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
109  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
111  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
113  bool IsNbrNId(const int& NId) const { return NodeHI.GetDat().IsNbrNId(NId); }
114  friend class TUNGraph;
115  };
117  class TEdgeI {
118  private:
120  int CurEdge;
121  public:
122  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
123  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
124  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
125  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
127  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; }
128  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
129  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
131  int GetId() const { return -1; }
133  int GetSrcNId() const { return CurNode.GetId(); }
135  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
136  friend class TUNGraph;
137  };
138 private:
142 private:
143  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
144  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
145 public:
146  TUNGraph() : CRef(), MxNId(0), NEdges(0), NodeH() { }
148  explicit TUNGraph(const int& Nodes, const int& Edges) : MxNId(0), NEdges(0) { Reserve(Nodes, Edges); }
149  TUNGraph(const TUNGraph& Graph) : MxNId(Graph.MxNId), NEdges(Graph.NEdges), NodeH(Graph.NodeH) { }
151  TUNGraph(TSIn& SIn) : MxNId(SIn), NEdges(SIn), NodeH(SIn) { }
153  void Save(TSOut& SOut) const { MxNId.Save(SOut); NEdges.Save(SOut); NodeH.Save(SOut); }
155  static PUNGraph New() { return new TUNGraph(); }
157 
159  static PUNGraph New(const int& Nodes, const int& Edges) { return new TUNGraph(Nodes, Edges); }
161  static PUNGraph Load(TSIn& SIn) { return PUNGraph(new TUNGraph(SIn)); }
163  bool HasFlag(const TGraphFlag& Flag) const;
164  TUNGraph& operator = (const TUNGraph& Graph) {
165  if (this!=&Graph) { MxNId=Graph.MxNId; NEdges=Graph.NEdges; NodeH=Graph.NodeH; } return *this; }
166 
168  int GetNodes() const { return NodeH.Len(); }
170 
174  int AddNode(int NId = -1);
176  int AddNodeUnchecked(int NId = -1);
178  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId()); }
180 
189  int AddNode(const int& NId, const TIntV& NbrNIdV);
191 
199  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId);
201 
203  void DelNode(const int& NId);
205  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
207  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
209  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
211  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
213  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
215  int GetMxNId() const { return MxNId; }
216 
218  int GetEdges() const;
220 
224  int AddEdge(const int& SrcNId, const int& DstNId);
226  int AddEdgeUnchecked(const int& SrcNId, const int& DstNId);
228  int AddEdge2(const int& SrcNId, const int& DstNId);
230  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
232 
235  void DelEdge(const int& SrcNId, const int& DstNId);
237  bool IsEdge(const int& SrcNId, const int& DstNId) const;
239  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; }
241  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
243  TEdgeI GetEI(const int& EId) const;
245 
247  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
248 
250  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
252  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
254  void GetNIdV(TIntV& NIdV) const;
255 
257  bool Empty() const { return GetNodes()==0; }
259  void Clr() { MxNId=0; NEdges=0; NodeH.Clr(); }
261  void SortNodeAdjV() { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { NI.SortNIdV();} }
263  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) NodeH.Gen(Nodes/2); }
265  void ReserveNIdDeg(const int& NId, const int& Deg) { GetNode(NId).NIdV.Reserve(Deg); }
267 
271  void Defrag(const bool& OnlyNodeLinks=false);
273 
275  bool IsOk(const bool& ThrowExcept=true) const;
277  void Dump(FILE *OutF=stdout) const;
279 
285  static PUNGraph GetSmallGraph();
286 
287  friend class TUNGraphMtx;
288  friend class TPt<TUNGraph>;
289 };
290 
291 //#//////////////////////////////////////////////
293 
307 class TNGraph {
308 public:
309  typedef TNGraph TNet;
311 public:
312  class TNode {
313  private:
316  public:
317  TNode() : Id(-1), InNIdV(), OutNIdV() { }
318  TNode(const int& NId) : Id(NId), InNIdV(), OutNIdV() { }
319  TNode(const TNode& Node) : Id(Node.Id), InNIdV(Node.InNIdV), OutNIdV(Node.OutNIdV) { }
320  TNode(TSIn& SIn) : Id(SIn), InNIdV(SIn), OutNIdV(SIn) { }
321  void Save(TSOut& SOut) const { Id.Save(SOut); InNIdV.Save(SOut); OutNIdV.Save(SOut); }
322  int GetId() const { return Id; }
323  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
324  int GetInDeg() const { return InNIdV.Len(); }
325  int GetOutDeg() const { return OutNIdV.Len(); }
326  int GetInNId(const int& NodeN) const { return InNIdV[NodeN]; }
327  int GetOutNId(const int& NodeN) const { return OutNIdV[NodeN]; }
328  int GetNbrNId(const int& NodeN) const { return NodeN<GetOutDeg()?GetOutNId(NodeN):GetInNId(NodeN-GetOutDeg()); }
329  bool IsInNId(const int& NId) const { return InNIdV.SearchBin(NId) != -1; }
330  bool IsOutNId(const int& NId) const { return OutNIdV.SearchBin(NId) != -1; }
331  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
332  void PackOutNIdV() { OutNIdV.Pack(); }
333  void PackNIdV() { InNIdV.Pack(); }
334  void SortNIdV() { InNIdV.Sort(); OutNIdV.Sort();}
335  friend class TNGraph;
336  friend class TNGraphMtx;
337  };
339  class TNodeI {
340  private:
343  public:
344  TNodeI() : NodeHI() { }
345  TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
346  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
347  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
349  TNodeI& operator++ (int) { NodeHI++; return *this; }
351  TNodeI& operator-- (int) { NodeHI--; return *this; }
352 
353  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
354  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
356  int GetId() const { return NodeHI.GetDat().GetId(); }
358  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
360  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
362  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
364  void SortNIdV() { NodeHI.GetDat().SortNIdV(); }
366 
368  int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
370 
372  int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
374 
376  int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
378  bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
380  bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
382  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
383  friend class TNGraph;
384  };
386  class TEdgeI {
387  private:
389  int CurEdge;
390  public:
391  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
392  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
393  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
394  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
397  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
398  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
399  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
401  int GetId() const { return -1; }
403  int GetSrcNId() const { return CurNode.GetId(); }
405  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
406  friend class TNGraph;
407  };
408 private:
412 private:
413  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
414  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
415 public:
416  TNGraph() : CRef(), MxNId(0), NodeH() { }
418  explicit TNGraph(const int& Nodes, const int& Edges) : MxNId(0) { Reserve(Nodes, Edges); }
419  TNGraph(const TNGraph& Graph) : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
421  TNGraph(TSIn& SIn) : MxNId(SIn), NodeH(SIn) { }
423  void Save(TSOut& SOut) const { MxNId.Save(SOut); NodeH.Save(SOut); }
425  static PNGraph New() { return new TNGraph(); }
427 
429  static PNGraph New(const int& Nodes, const int& Edges) { return new TNGraph(Nodes, Edges); }
431  static PNGraph Load(TSIn& SIn) { return PNGraph(new TNGraph(SIn)); }
433  bool HasFlag(const TGraphFlag& Flag) const;
434  TNGraph& operator = (const TNGraph& Graph) {
435  if (this!=&Graph) { MxNId=Graph.MxNId; NodeH=Graph.NodeH; } return *this; }
436 
438  int GetNodes() const { return NodeH.Len(); }
440 
444  int AddNode(int NId = -1);
446  int AddNodeUnchecked(int NId = -1);
448  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
450 
459  int AddNode(const int& NId, const TIntV& InNIdV, const TIntV& OutNIdV);
461 
469  int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& SrcVId, const int& DstVId);
471 
473  void DelNode(const int& NId);
475  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
477  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
479  TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
481  TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
483  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
484  // GetNodeC() has been commented out. It was a quick shortcut, do not use.
485  //const TNode& GetNodeC(const int& NId) const { return NodeH.GetDat(NId); }
487  int GetMxNId() const { return MxNId; }
488 
490  int GetEdges() const;
492 
498  int AddEdge(const int& SrcNId, const int& DstNId);
500  int AddEdgeUnchecked(const int& SrcNId, const int& DstNId);
502  int AddEdge2(const int& SrcNId, const int& DstNId);
504  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
506 
510  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
512  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const;
514  TEdgeI BegEI() const { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0){NI++;} return TEdgeI(NI, EndNI()); }
516  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
518  TEdgeI GetEI(const int& EId) const; // not supported
520  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
521 
523  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
525  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
527  void GetNIdV(TIntV& NIdV) const;
528 
530  bool Empty() const { return GetNodes()==0; }
532  void Clr() { MxNId=0; NodeH.Clr(); }
534  void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
536  void ReserveNIdInDeg(const int& NId, const int& InDeg) { GetNode(NId).InNIdV.Reserve(InDeg); }
538  void ReserveNIdOutDeg(const int& NId, const int& OutDeg) { GetNode(NId).OutNIdV.Reserve(OutDeg); }
540  void SortNodeAdjV() { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { NI.SortNIdV();} }
542 
547  void Defrag(const bool& OnlyNodeLinks=false);
549 
552  bool IsOk(const bool& ThrowExcept=true) const;
554  void Dump(FILE *OutF=stdout) const;
556 
560  static PNGraph GetSmallGraph();
561  friend class TPt<TNGraph>;
562  friend class TNGraphMtx;
563 };
564 
565 // set flags
566 namespace TSnap {
567 template <> struct IsDirected<TNGraph> { enum { Val = 1 }; };
568 }
569 
570 //#//////////////////////////////////////////////
572 
585 class TNEGraph {
586 public:
587  typedef TNEGraph TNet;
589 public:
590  class TNode {
591  private:
594  public:
595  TNode() : Id(-1), InEIdV(), OutEIdV() { }
596  TNode(const int& NId) : Id(NId), InEIdV(), OutEIdV() { }
597  TNode(const TNode& Node) : Id(Node.Id), InEIdV(Node.InEIdV), OutEIdV(Node.OutEIdV) { }
598  TNode(TSIn& SIn) : Id(SIn), InEIdV(SIn), OutEIdV(SIn) { }
599  void Save(TSOut& SOut) const { Id.Save(SOut); InEIdV.Save(SOut); OutEIdV.Save(SOut); }
600  int GetId() const { return Id; }
601  int GetDeg() const { return GetInDeg() + GetOutDeg(); }
602  int GetInDeg() const { return InEIdV.Len(); }
603  int GetOutDeg() const { return OutEIdV.Len(); }
604  int GetInEId(const int& EdgeN) const { return InEIdV[EdgeN]; }
605  int GetOutEId(const int& EdgeN) const { return OutEIdV[EdgeN]; }
606  int GetNbrEId(const int& EdgeN) const { return EdgeN<GetOutDeg()?GetOutEId(EdgeN):GetInEId(EdgeN-GetOutDeg()); }
607  bool IsInEId(const int& EId) const { return InEIdV.SearchBin(EId) != -1; }
608  bool IsOutEId(const int& EId) const { return OutEIdV.SearchBin(EId) != -1; }
609  friend class TNEGraph;
610  };
611  class TEdge {
612  private:
614  public:
615  TEdge() : Id(-1), SrcNId(-1), DstNId(-1) { }
616  TEdge(const int& EId, const int& SourceNId, const int& DestNId) : Id(EId), SrcNId(SourceNId), DstNId(DestNId) { }
617  TEdge(const TEdge& Edge) : Id(Edge.Id), SrcNId(Edge.SrcNId), DstNId(Edge.DstNId) { }
618  TEdge(TSIn& SIn) : Id(SIn), SrcNId(SIn), DstNId(SIn) { }
619  void Save(TSOut& SOut) const { Id.Save(SOut); SrcNId.Save(SOut); DstNId.Save(SOut); }
620  int GetId() const { return Id; }
621  int GetSrcNId() const { return SrcNId; }
622  int GetDstNId() const { return DstNId; }
623  friend class TNEGraph;
624  };
626  class TNodeI {
627  private:
630  const TNEGraph *Graph;
631  public:
632  TNodeI() : NodeHI(), Graph(NULL) { }
633  TNodeI(const THashIter& NodeHIter, const TNEGraph* GraphPt) : NodeHI(NodeHIter), Graph(GraphPt) { }
634  TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI), Graph(NodeI.Graph) { }
635  TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; Graph=NodeI.Graph; return *this; }
637  TNodeI& operator++ (int) { NodeHI++; return *this; }
639  TNodeI& operator-- (int) { NodeHI--; return *this; }
640 
641  bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
642  bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
644  int GetId() const { return NodeHI.GetDat().GetId(); }
646  int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
648  int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
650  int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
652 
654  int GetInNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetInEId(EdgeN)).GetSrcNId(); }
656 
658  int GetOutNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetOutEId(EdgeN)).GetDstNId(); }
660 
662  int GetNbrNId(const int& EdgeN) const { const TEdge& E = Graph->GetEdge(NodeHI.GetDat().GetNbrEId(EdgeN));
663  return GetId()==E.GetSrcNId() ? E.GetDstNId():E.GetSrcNId(); }
665  bool IsInNId(const int& NId) const;
667  bool IsOutNId(const int& NId) const;
669  bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
670  // edges
672  int GetInEId(const int& EdgeN) const { return NodeHI.GetDat().GetInEId(EdgeN); }
674  int GetOutEId(const int& EdgeN) const { return NodeHI.GetDat().GetOutEId(EdgeN); }
676  int GetNbrEId(const int& EdgeN) const { return NodeHI.GetDat().GetNbrEId(EdgeN); }
678  bool IsInEId(const int& EId) const { return NodeHI.GetDat().IsInEId(EId); }
680  bool IsOutEId(const int& EId) const { return NodeHI.GetDat().IsOutEId(EId); }
682  bool IsNbrEId(const int& EId) const { return IsInEId(EId) || IsOutEId(EId); }
683  friend class TNEGraph;
684  };
686  class TEdgeI {
687  private:
690  const TNEGraph *Graph;
691  public:
692  TEdgeI() : EdgeHI(), Graph(NULL) { }
693  TEdgeI(const THashIter& EdgeHIter, const TNEGraph *GraphPt) : EdgeHI(EdgeHIter), Graph(GraphPt) { }
694  TEdgeI(const TEdgeI& EdgeI) : EdgeHI(EdgeI.EdgeHI), Graph(EdgeI.Graph) { }
695  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { EdgeHI=EdgeI.EdgeHI; Graph=EdgeI.Graph; } return *this; }
697  TEdgeI& operator++ (int) { EdgeHI++; return *this; }
698  bool operator < (const TEdgeI& EdgeI) const { return EdgeHI < EdgeI.EdgeHI; }
699  bool operator == (const TEdgeI& EdgeI) const { return EdgeHI == EdgeI.EdgeHI; }
701  int GetId() const { return EdgeHI.GetDat().GetId(); }
703  int GetSrcNId() const { return EdgeHI.GetDat().GetSrcNId(); }
705  int GetDstNId() const { return EdgeHI.GetDat().GetDstNId(); }
706  friend class TNEGraph;
707  };
708 
709 private:
710  TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
711  const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
712  TEdge& GetEdge(const int& EId) { return EdgeH.GetDat(EId); }
713  const TEdge& GetEdge(const int& EId) const { return EdgeH.GetDat(EId); }
714 private:
719 public:
720  TNEGraph() : CRef(), MxNId(0), MxEId(0) { }
722  explicit TNEGraph(const int& Nodes, const int& Edges) : CRef(), MxNId(0), MxEId(0) { Reserve(Nodes, Edges); }
723  TNEGraph(const TNEGraph& Graph) : MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { }
725  TNEGraph(TSIn& SIn) : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { }
727  void Save(TSOut& SOut) const { MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut); }
729  static PNEGraph New() { return PNEGraph(new TNEGraph()); }
731 
733  static PNEGraph New(const int& Nodes, const int& Edges) { return PNEGraph(new TNEGraph(Nodes, Edges)); }
735  static PNEGraph Load(TSIn& SIn) { return PNEGraph(new TNEGraph(SIn)); }
737  bool HasFlag(const TGraphFlag& Flag) const;
738  TNEGraph& operator = (const TNEGraph& Graph) { if (this!=&Graph) {
739  MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; } return *this; }
740 
742  int GetNodes() const { return NodeH.Len(); }
744 
748  int AddNode(int NId = -1);
750  int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
752 
754  void DelNode(const int& NId);
756  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
758  bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
760  TNodeI BegNI() const { return TNodeI(NodeH.BegI(), this); }
762  TNodeI EndNI() const { return TNodeI(NodeH.EndI(), this); }
764  TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId), this); }
766  int GetMxNId() const { return MxNId; }
767 
769  int GetEdges() const { return EdgeH.Len(); }
771 
776  int AddEdge(const int& SrcNId, const int& DstNId, int EId = -1);
778  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); }
780  void DelEdge(const int& EId);
782 
786  void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
788  bool IsEdge(const int& EId) const { return EdgeH.IsKey(EId); }
790  bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const { int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); }
792  bool IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir = true) const;
794  int GetEId(const int& SrcNId, const int& DstNId) const { int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; }
796  TEdgeI BegEI() const { return TEdgeI(EdgeH.BegI(), this); }
798  TEdgeI EndEI() const { return TEdgeI(EdgeH.EndI(), this); }
800  TEdgeI GetEI(const int& EId) const { return TEdgeI(EdgeH.GetI(EId), this); }
802  TEdgeI GetEI(const int& SrcNId, const int& DstNId) const { return GetEI(GetEId(SrcNId, DstNId)); }
803 
805  int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
807  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
809  int GetRndEId(TRnd& Rnd=TInt::Rnd) { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); }
811  TEdgeI GetRndEI(TRnd& Rnd=TInt::Rnd) { return GetEI(GetRndEId(Rnd)); }
813  void GetNIdV(TIntV& NIdV) const;
815  void GetEIdV(TIntV& EIdV) const;
816 
818  bool Empty() const { return GetNodes()==0; }
820  void Clr() { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); }
822  void Reserve(const int& Nodes, const int& Edges) {
823  if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } }
825 
830  void Defrag(const bool& OnlyNodeLinks=false);
832 
835  bool IsOk(const bool& ThrowExcept=true) const;
837  void Dump(FILE *OutF=stdout) const;
839 
843  static PNEGraph GetSmallGraph();
844  friend class TPt<TNEGraph>;
845 };
846 
847 // set flags
848 namespace TSnap {
849 template <> struct IsMultiGraph<TNEGraph> { enum { Val = 1 }; };
850 template <> struct IsDirected<TNEGraph> { enum { Val = 1 }; };
851 }
852 
853 //#//////////////////////////////////////////////
855 
856 class TBPGraph {
857 public:
858  typedef TBPGraph TNet;
860  typedef enum { bgsUndef, bgsLeft, bgsRight, bgsBoth } TNodeTy; // left or right hand side node
861 public:
862  class TNode {
863  private:
866  TNodeTy NodeTy; // remove
867  public:
868  TNode() : Id(-1), NIdV(), NodeTy(bgsUndef) { }
869  TNode(const int& NId) : Id(NId), NIdV(), NodeTy(true?bgsLeft:bgsRight) { }
870  TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV), NodeTy(Node.NodeTy) { }
871  TNode(TSIn& SIn) : Id(SIn), NIdV(SIn), NodeTy(bgsUndef) { TInt Ty(SIn); NodeTy=(TNodeTy)Ty.Val; }
872  void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); TInt(NodeTy).Save(SOut); }
873  int GetId() const { return Id; }
874  int GetDeg() const { return NIdV.Len(); }
875  int GetInDeg() const { return GetDeg(); }
876  int GetOutDeg() const { return GetDeg(); }
877  int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
878  int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
879  int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
880  bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
881  bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
882  bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
883  void PackOutNIdV() { NIdV.Pack(); }
884  void PackNIdV() { NIdV.Pack(); }
885  friend class TBPGraph;
886  };
888  class TNodeI {
889  private:
891  THashIter LeftHI, RightHI; // iterator over left and right hand-side nodes
892  private:
893  inline THashIter HI() const { return ! LeftHI.IsEnd()?LeftHI:RightHI; }
894  public:
895  TNodeI() : LeftHI(), RightHI() { }
896  TNodeI(const THashIter& LeftHIter, const THashIter& RightHIter) : LeftHI(LeftHIter), RightHI(RightHIter) { }
897  TNodeI(const TNodeI& NodeI) : LeftHI(NodeI.LeftHI), RightHI(NodeI.RightHI) { }
898  TNodeI& operator = (const TNodeI& NodeI) { LeftHI = NodeI.LeftHI; RightHI=NodeI.RightHI; return *this; }
901  if (! LeftHI.IsEnd()) {
902  LeftHI++; }
903  else if (! RightHI.IsEnd()) {
904  RightHI++; }
905  return *this; }
906  bool operator < (const TNodeI& NodeI) const { return LeftHI < NodeI.LeftHI || (LeftHI==NodeI.LeftHI && RightHI < NodeI.RightHI); }
907  bool operator == (const TNodeI& NodeI) const { return LeftHI==NodeI.LeftHI && RightHI==NodeI.RightHI; }
909  int GetId() const { return HI().GetDat().GetId(); }
911  bool IsLeft() const { return ! LeftHI.IsEnd(); }
913  bool IsRight() const { return ! IsLeft(); }
915  int GetDeg() const { return HI().GetDat().GetDeg(); }
917  int GetInDeg() const { return HI().GetDat().GetInDeg(); }
919  int GetOutDeg() const { return HI().GetDat().GetOutDeg(); }
921 
922  int GetInNId(const int& NodeN) const { return HI().GetDat().GetInNId(NodeN); }
924 
925  int GetOutNId(const int& NodeN) const { return HI().GetDat().GetOutNId(NodeN); }
927 
928  int GetNbrNId(const int& NodeN) const { return HI().GetDat().GetNbrNId(NodeN); }
930  bool IsInNId(const int& NId) const { return HI().GetDat().IsInNId(NId); }
932  bool IsOutNId(const int& NId) const { return HI().GetDat().IsOutNId(NId); }
934  bool IsNbrNId(const int& NId) const { return HI().GetDat().IsNbrNId(NId); }
935  friend class TBPGraph;
936  };
938  class TEdgeI {
939  private:
940  TNodeI CurNode, EndNode; // end node on the 'left'
941  int CurEdge;
942  public:
943  TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
944  TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
945  TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
946  TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
949  while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } } return *this; }
950  bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
951  bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
953  int GetId() const { return -1; }
955  int GetSrcNId() const { return CurNode.GetId(); }
957  int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
959  int GetLNId() const { return GetSrcNId(); }
961  int GetRNId() const { return GetDstNId(); }
962  friend class TBPGraph;
963  };
964 private:
966  TInt MxNId; // maximum node ID in the graph
967  THash<TInt, TNode> LeftH; // 'left' nodes
968  THash<TInt, TNode> RightH; // 'right' nodes
969 private:
970  //TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
971  //const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
972 public:
973  TBPGraph() : CRef(), MxNId(0), LeftH(), RightH() { }
975  explicit TBPGraph(const int& Nodes, const int& Edges) : MxNId(0) { }
976  TBPGraph(const TBPGraph& BPGraph) : MxNId(BPGraph.MxNId), LeftH(BPGraph.LeftH), RightH(BPGraph.RightH) { }
978  TBPGraph(TSIn& SIn) : MxNId(SIn), LeftH(SIn), RightH(SIn) { }
980  void Save(TSOut& SOut) const { MxNId.Save(SOut); LeftH.Save(SOut); RightH.Save(SOut); }
982  static PBPGraph New() { return new TBPGraph(); }
984 
985  static PBPGraph New(const int& Nodes, const int& Edges) { return new TBPGraph(Nodes, Edges); }
987  static PBPGraph Load(TSIn& SIn) { return PBPGraph(new TBPGraph(SIn)); }
989  bool HasFlag(const TGraphFlag& Flag) const;
990  TBPGraph& operator = (const TBPGraph& BPGraph) {
991  if (this!=&BPGraph) { MxNId=BPGraph.MxNId; LeftH=BPGraph.LeftH; RightH=BPGraph.RightH; } return *this; }
992 
994  int GetNodes() const { return GetLNodes() + GetRNodes(); }
996  int GetLNodes() const { return LeftH.Len(); }
998  int GetRNodes() const { return RightH.Len(); }
1000 
1001  int AddNode(int NId = -1, const bool& LeftNode=true);
1003  int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId(), NodeI.IsLeft()); }
1005 
1006  void DelNode(const int& NId);
1008  void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
1010  bool IsNode(const int& NId) const { return IsLNode(NId) || IsRNode(NId); }
1012  bool IsLNode(const int& NId) const { return LeftH.IsKey(NId); }
1014  bool IsRNode(const int& NId) const { return RightH.IsKey(NId); }
1016  int GetMxNId() const { return MxNId; }
1017 
1019  TNodeI BegNI() const { return TNodeI(LeftH.BegI(), RightH.BegI()); }
1021  TNodeI EndNI() const { return TNodeI(LeftH.EndI(), RightH.EndI()); }
1023  TNodeI GetNI(const int& NId) const { return IsLNode(NId) ? TNodeI(LeftH.GetI(NId), RightH.EndI()) : TNodeI(LeftH.EndI(), RightH.GetI(NId)); }
1025  TNodeI BegLNI() const { return TNodeI(LeftH.BegI(), RightH.EndI()); }
1027  TNodeI EndLNI() const { return EndNI(); }
1029  TNodeI BegRNI() const { return TNodeI(LeftH.EndI(), RightH.BegI()); }
1031  TNodeI EndRNI() const { return EndNI(); }
1032 
1034  int GetEdges() const;
1036 
1037  int AddEdge(const int& LeftNId, const int& RightNId);
1039  int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
1041 
1042  void DelEdge(const int& LeftNId, const int& RightNId);
1044  bool IsEdge(const int& LeftNId, const int& RightNId) const;
1046  TEdgeI BegEI() const { TNodeI NI=BegLNI(); while (NI<EndLNI() && (NI.GetOutDeg()==0 || NI.GetId()>NI.GetOutNId(0))) { NI++; } return TEdgeI(NI, EndLNI()); }
1048  TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
1050  TEdgeI GetEI(const int& EId) const;
1052 
1053  TEdgeI GetEI(const int& LeftNId, const int& RightNId) const;
1054 
1056  int GetRndNId(TRnd& Rnd=TInt::Rnd);
1058  int GetRndLNId(TRnd& Rnd=TInt::Rnd);
1060  int GetRndRNId(TRnd& Rnd=TInt::Rnd);
1062  TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
1064  void GetNIdV(TIntV& NIdV) const;
1066  void GetLNIdV(TIntV& NIdV) const;
1068  void GetRNIdV(TIntV& NIdV) const;
1069 
1071  bool Empty() const { return GetNodes()==0; }
1073  void Clr() { MxNId=0; LeftH.Clr(); RightH.Clr(); }
1075  void Reserve(const int& Nodes, const int& Edges);
1077 
1078  void Defrag(const bool& OnlyNodeLinks=false);
1080 
1081  bool IsOk(const bool& ThrowExcept=true) const;
1083  void Dump(FILE *OutF=stdout) const;
1085 
1086  static PBPGraph GetSmallGraph();
1087 
1088  friend class TPt<TBPGraph>;
1089 };
1090 
1091 // set flags
1092 namespace TSnap {
1093 template <> struct IsBipart<TBPGraph> { enum { Val = 1 }; };
1094 }
1095 
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:1039
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:117
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:376
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:906
TBPGraph(const TBPGraph &BPGraph)
!! Reserve(Nodes, Edges); }
Definition: graph.h:976
int GetDeg() const
Definition: graph.h:874
int GetId() const
Gets edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:953
TPt< TBPGraph > PNet
Definition: graph.h:859
TUNGraph(const TUNGraph &Graph)
Definition: graph.h:149
THashIter NodeHI
Definition: graph.h:67
int AddNode(const TNodeI &NodeId)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:448
int GetOutDeg() const
Definition: graph.h:325
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:621
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:159
TNodeI CurNode
Definition: graph.h:388
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:479
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:241
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:56
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:129
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:77
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:948
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:898
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:985
TNode(const TNode &Node)
Definition: graph.h:870
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:818
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:907
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:1023
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:917
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:738
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:66
TNode & GetNode(const int &NId)
Definition: graph.h:143
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:699
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:113
int GetNbrEId(const int &EdgeN) const
Definition: graph.h:606
TEdge(const int &EId, const int &SourceNId, const int &DestNId)
Definition: graph.h:616
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:1010
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:866
int Val
Definition: dt.h:1046
const TNEGraph * Graph
Definition: graph.h:690
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:483
void Save(TSOut &SOut) const
Definition: dt.h:1060
void ReserveNIdInDeg(const int &NId, const int &InDeg)
Reserves memory for node ID NId having InDeg in-edges.
Definition: graph.h:536
THashIter HI() const
Definition: graph.h:893
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:796
TNEGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:722
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:1003
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:516
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:650
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:135
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:215
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:378
bool IsLNode(const int &NId) const
Tests whether ID NId is a 'left' side node.
Definition: graph.h:1012
TNEGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:725
TUNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:151
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:394
Tests (at compile time) if the graph is directed.
Definition: gbase.h:28
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:1016
int GetNbrNId(const int &NodeN) const
Definition: graph.h:879
TIter BegI() const
Definition: hash.h:171
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:888
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:957
THash< TInt, TNode > NodeH
Definition: graph.h:411
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TNodeTy
Definition: graph.h:860
int AddEdge(const TEdgeI &EdgeI)
Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph.
Definition: graph.h:504
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
bool IsInEId(const int &EId) const
Definition: graph.h:607
int GetId() const
Definition: graph.h:600
TPt< TNEGraph > PNet
Definition: graph.h:588
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:72
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:698
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
int GetId() const
Gets edge ID.
Definition: graph.h:701
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:1021
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:951
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph without performing checks.
Definition: graph.cpp:330
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:1019
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:790
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:639
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:514
int GetInDeg() const
Definition: graph.h:602
TInt NEdges
Definition: graph.h:140
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:396
TInt SrcNId
Definition: graph.h:613
void Save(TSOut &SOut) const
Definition: graph.h:619
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:605
int AddNode(int NId=-1, const bool &LeftNode=true)
Adds a node of ID NId to the graph.
Definition: graph.cpp:671
void PackOutNIdV()
Definition: graph.h:57
Directed multigraph.
Definition: graph.h:585
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:798
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:1048
THashIter NodeHI
Definition: graph.h:342
int GetInDeg() const
Definition: graph.h:49
void PackOutNIdV()
Definition: graph.h:332
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TCRef CRef
Definition: graph.h:715
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:735
TNGraph()
Definition: graph.h:416
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:686
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:727
TNode(const int &NId)
Definition: graph.h:318
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:133
TNodeI & operator--(int)
Decrement iterator.
Definition: graph.h:351
TCRef CRef
Definition: graph.h:965
TNodeI EndNode
Definition: graph.h:119
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:153
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:382
const TNode & GetNode(const int &NId) const
Definition: graph.h:144
TIter EndI() const
Definition: hash.h:176
TNEGraph TNet
Definition: graph.h:587
THashIter NodeHI
Definition: graph.h:629
void SortNodeAdjV()
Sorts the adjacency lists of each node.
Definition: graph.h:261
static TRnd Rnd
Definition: dt.h:1053
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:762
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:637
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:975
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:682
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:760
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:401
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:756
THashIter EdgeHI
Definition: graph.h:689
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:930
TNodeI(const THashIter &NodeHIter, const TNEGraph *GraphPt)
Definition: graph.h:633
THash< TInt, TNode > NodeH
Definition: graph.h:717
int GetInNId(const int &EdgeN) const
Returns ID of EdgeN-th in-node (the node pointing to the current node).
Definition: graph.h:654
TNGraph(TSIn &SIn)
Constructor that loads the graph from a (binary) stream SIn.
Definition: graph.h:421
TNodeI EndNode
Definition: graph.h:388
int GetInNId(const int &NodeN) const
Definition: graph.h:877
int GetOutDeg() const
Definition: graph.h:603
TNode & GetNode(const int &NId)
Definition: graph.h:413
int AddEdge(const TEdgeI &EdgeI)
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition: graph.h:778
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:903
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:250
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:697
int GetId() const
Returns ID of the current node.
Definition: graph.h:909
int GetOutNId(const int &NodeN) const
Definition: graph.h:878
int GetInDeg() const
Definition: graph.h:324
int GetDstNId() const
Gets destination of an edge.
Definition: graph.h:705
THash< TInt, TEdge > EdgeH
Definition: graph.h:718
int GetDstNId() const
Definition: graph.h:622
int GetLNodes() const
Returns the number of nodes on the 'left' side of the biparite graph.
Definition: graph.h:996
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:124
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:128
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:944
static PBPGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:987
TCRef CRef
Definition: graph.h:139
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:674
void SortNodeAdjV()
Sorts the adjacency lists of each node.
Definition: graph.h:540
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:928
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:86
void Clr()
Deletes all nodes and edges from the bipartite graph.
Definition: graph.h:1073
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:762
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TNode(const int &NId)
Definition: graph.h:869
int GetInEId(const int &EdgeN) const
Definition: graph.h:604
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:764
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:919
int GetId() const
Definition: graph.h:322
int GetNbrNId(const int &EdgeN) const
Returns ID of EdgeN-th neighboring node.
Definition: graph.h:662
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:802
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:742
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:75
THashIter LeftHI
Definition: graph.h:891
void SortNIdV()
Sorts the adjacency lists of the current node.
Definition: graph.h:364
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:90
bool Empty() const
Tests whether the bipartite graph is empty (has zero nodes).
Definition: graph.h:1071
void PackNIdV()
Definition: graph.h:333
TPt< TNEGraph > PNEGraph
Pointer to a directed multigraph (TNEGraph)
Definition: graph.h:21
TEdge & GetEdge(const int &EId)
Definition: graph.h:712
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:959
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:341
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:1031
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:349
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
bool IsOutNId(const int &NId) const
Definition: graph.h:882
void Gen(const int &ExpectVals)
Definition: hash.h:180
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:386
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:698
TInt MxNId
Definition: graph.h:140
TNodeI(const THashIter &LeftHIter, const THashIter &RightHIter)
Definition: graph.h:896
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
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:994
bool IsInNId(const int &NId) const
Definition: graph.h:881
static PNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:431
Definition: gsvd.h:5
TEdgeI(const THashIter &EdgeHIter, const TNEGraph *GraphPt)
Definition: graph.h:693
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:205
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:263
TNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:418
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:257
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the graph.
Definition: graph.h:523
TNodeI BegLNI() const
Returns an iterator referring to the first 'left' node in the graph.
Definition: graph.h:1025
int GetInEId(const int &EdgeN) const
Returns ID of EdgeN-th in-edge.
Definition: graph.h:672
TNode(TSIn &SIn)
Definition: graph.h:598
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:380
TNode(TSIn &SIn)
Definition: graph.h:320
int GetRNodes() const
Returns the number of nodes on the 'right' side of the biparite graph.
Definition: graph.h:998
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:213
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:628
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:477
TNodeI(const TNodeI &NodeI)
Definition: graph.h:71
TNode(const TNode &Node)
Definition: graph.h:597
int GetOutNId(const int &EdgeN) const
Returns ID of EdgeN-th out-node (the node the current node points to).
Definition: graph.h:658
TInt DstNId
Definition: graph.h:613
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:950
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:230
TInt MxNId
Definition: graph.h:966
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:239
void PackOutNIdV()
Definition: graph.h:883
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:155
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:392
TPt< TNGraph > PNGraph
Pointer to a directed graph (TNGraph)
Definition: graph.h:16
TNodeI CurNode
Definition: graph.h:119
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:641
TBPGraph TNet
Definition: graph.h:858
int GetOutNId(const int &NodeN) const
Definition: graph.h:327
TBPGraph & operator=(const TBPGraph &BPGraph)
Definition: graph.h:990
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:750
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:1029
int GetOutNId(const int &NodeN) const
Definition: graph.h:52
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the graph without performing checks.
Definition: graph.cpp:20
TIntV OutEIdV
Definition: graph.h:593
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:252
void Save(TSOut &SOut) const
Definition: graph.h:872
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:1027
void SortNIdV()
Definition: graph.h:59
int GetRNId() const
Gets the ID of the node on the 'right' side of the edge.
Definition: graph.h:961
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:525
TNodeI & operator=(const TNodeI &NodeI)
Definition: graph.h:347
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:642
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:358
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:81
Definition: fl.h:128
TNGraph(const TNGraph &Graph)
Definition: graph.h:419
THash< TInt, TNode >::TIter THashIter
Definition: graph.h:890
TIntV NIdV
Definition: graph.h:865
int GetDeg() const
Definition: graph.h:601
bool IsNbrNId(const int &NId) const
Tests whether node with ID NId is a neighbor of the current node.
Definition: graph.h:934
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1454
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:353
int GetDeg() const
Returns degree of the current node.
Definition: graph.h:915
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:532
TUNGraph()
Definition: graph.h:146
Definition: dt.h:1044
int GetMxNId() const
Returns an ID that is larger than any node ID in the graph.
Definition: graph.h:487
bool IsNbrNId(const int &NId) const
Definition: graph.h:880
TNEGraph(const TNEGraph &Graph)
Definition: graph.h:723
TNode & GetNode(const int &NId)
Definition: graph.h:710
static PUNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
Definition: graph.h:161
static PNEGraph New()
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New().
Definition: graph.h:729
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:423
void Save(TSOut &SOut) const
Definition: graph.h:599
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:891
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:955
TEdge(const TEdge &Edge)
Definition: graph.h:617
Directed graph.
Definition: graph.h:307
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:800
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: graph.cpp:682
TNode(const TNode &Node)
Definition: graph.h:319
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:946
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: graph.h:788
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:481
int GetId() const
Returns ID of the current node.
Definition: graph.h:356
TBPGraph()
Definition: graph.h:973
static PNGraph GetSmallGraph()
Returns a small graph on 5 nodes and 6 edges.
Definition: graph.cpp:454
TNode(const int &NId)
Definition: graph.h:596
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:102
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:711
int GetId() const
Returns edge ID. Always returns -1 since only edges in multigraphs have explicit IDs.
Definition: graph.h:131
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:822
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:362
Bipartite graph.
Definition: graph.h:856
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:794
int GetSrcNId() const
Gets the source of an edge.
Definition: graph.h:703
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:635
THash< TInt, TNode > NodeH
Definition: graph.h:141
TEdge(TSIn &SIn)
Definition: graph.h:618
TNode(const TNode &Node)
Definition: graph.h:44
bool IsOutEId(const int &EId) const
Definition: graph.h:608
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:429
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:871
void Pack()
Reduces vector capacity (frees memory) to match its size.
Definition: ds.h:1005
int GetId() const
Definition: graph.h:873
int GetRndEId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random edge in the graph.
Definition: graph.h:809
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:1014
bool IsRight() const
Tests whether the node is right hand side node.
Definition: graph.h:913
TCRef CRef
Definition: graph.h:409
THash< TInt, TNode > RightH
Definition: graph.h:968
Definition: hash.h:88
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:345
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:733
TPt< TNGraph > PNet
Definition: graph.h:310
int GetNbrEId(const int &EdgeN) const
Returns ID of EdgeN-th in or out-edge.
Definition: graph.h:676
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.h:769
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:88
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:339
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:160
const TEdge & GetEdge(const int &EId) const
Definition: graph.h:713
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
int GetOutDeg() const
Definition: graph.h:876
const TNEGraph * Graph
Definition: graph.h:630
TIntV InNIdV
Definition: graph.h:315
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:932
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:805
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:534
static PBPGraph New()
Static constructor that returns a pointer to the graph. Call: PBPGraph BPGraph = TBPGraph::New();.
Definition: graph.h:982
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:107
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
THash< TInt, TNode > LeftH
Definition: graph.h:967
TNode(TSIn &SIn)
Definition: graph.h:45
TInt MxNId
Definition: graph.h:716
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:207
TInt MxNId
Definition: graph.h:410
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:925
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:807
int GetRndKeyId(TRnd &Rnd) const
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
Definition: hash.h:402
bool operator==(const TNodeI &NodeI) const
Definition: graph.h:354
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:820
TNodeI CurNode
Definition: graph.h:940
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:259
TNodeI & operator++(int)
Increment iterator.
Definition: graph.h:900
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:515
TEdgeI GetRndEI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random edge in the graph.
Definition: graph.h:811
TIntV OutNIdV
Definition: graph.h:315
int GetOutDeg() const
Definition: graph.h:50
const TNode & GetNode(const int &NId) const
Definition: graph.h:414
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the graph without performing checks.
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:92
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: graph.cpp:577
void SortNIdV()
Definition: graph.h:334
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:1046
TNodeI EndNode
Definition: graph.h:940
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:360
void DelNode(const TNode &NodeI)
Deletes node of ID NodeI.GetId() from the graph.
Definition: graph.h:475
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:1008
int GetSrcNId() const
Returns the source node of the edge.
Definition: graph.h:403
int GetId() const
Returns ID of the current node.
Definition: graph.h:644
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:84
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:945
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:368
void PackNIdV()
Definition: graph.h:884
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: graph.h:111
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:51
bool IsOutEId(const int &EId) const
Tests whether the edge with ID EId is an out-edge of current node.
Definition: graph.h:680
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
int GetInNId(const int &NodeN) const
Definition: graph.h:326
int GetDeg() const
Definition: graph.h:323
int GetDeg() const
Definition: graph.h:48
bool operator<(const TEdgeI &EdgeI) const
Definition: graph.h:398
void ReserveNIdDeg(const int &NId, const int &Deg)
Reserves memory for node ID NId having Deg edges.
Definition: graph.h:265
bool IsOutNId(const int &NId) const
Definition: graph.h:330
int GetNbrNId(const int &NodeN) const
Definition: graph.h:328
int Len() const
Definition: hash.h:186
TNodeI(const TNodeI &NodeI)
Definition: graph.h:897
TInt MxEId
Definition: graph.h:716
int AddNode(const TNodeI &NodeI)
Adds a node of ID NodeI.GetId() to the graph.
Definition: graph.h:178
TNodeI(const THashIter &NodeHIter)
Definition: graph.h:70
TNodeI(const TNodeI &NodeI)
Definition: graph.h:634
bool IsInEId(const int &EId) const
Tests whether the edge with ID EId is an in-edge of current node.
Definition: graph.h:678
bool IsNbrNId(const int &NId) const
Definition: graph.h:331
bool operator==(const TEdgeI &EdgeI) const
Definition: graph.h:399
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd)
Returns an interator referring to a random node in the graph.
Definition: graph.h:1062
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:695
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:694
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:938
void Save(TSOut &SOut) const
Definition: graph.h:321
TNGraph & operator=(const TNGraph &Graph)
Definition: graph.h:434
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:626
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:405
bool IsInNId(const int &NId) const
Definition: graph.h:329
bool operator<(const TNodeI &NodeI) const
Definition: graph.h:80
int GetInDeg() const
Definition: graph.h:875
TUNGraph & operator=(const TUNGraph &Graph)
Definition: graph.h:164
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:372
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:646
bool Empty() const
Tests whether the graph is empty (has zero nodes).
Definition: graph.h:530
TEdgeI & operator++(int)
Increment iterator.
Definition: graph.h:127
THash< TInt, TEdge >::TIter THashIter
Definition: graph.h:688
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:648
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
Definition: graph.h:123
bool IsLeft() const
Tests whether the node is left hand side node.
Definition: graph.h:911
TUNGraph(const int &Nodes, const int &Edges)
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:148
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:758
TEdgeI(const TEdgeI &EdgeI)
Definition: graph.h:393
TEdgeI & operator=(const TEdgeI &EdgeI)
Definition: graph.h:125
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:97
void Save(TSOut &SOut) const
Saves the graph to a (binary) stream SOut.
Definition: graph.h:980
TBPGraph(TSIn &SIn)
Constructor for loading the graph from a (binary) stream SIn.
Definition: graph.h:978
TNodeI(const TNodeI &NodeI)
Definition: graph.h:346
void ReserveNIdOutDeg(const int &NId, const int &OutDeg)
Reserves memory for node ID NId having OutDeg out-edges.
Definition: graph.h:538
TNEGraph()
Definition: graph.h:720
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph without performing checks.
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:669
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:766
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:922
TIntV InEIdV
Definition: graph.h:593
int GetId() const
Definition: graph.h:620
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: graph.h:109
TNGraph TNet
Definition: graph.h:309