SNAP Library 2.2, User Reference  2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
graph.h
Go to the documentation of this file.
00001 //#//////////////////////////////////////////////
00003 
00004 class TUNGraph;
00005 class TBPGraph;
00006 //TODO:class TUNEGraph; -- undirected multigraph
00007 
00009 typedef TPt<TUNGraph> PUNGraph;
00011 typedef TPt<TBPGraph> PBPGraph;
00012 
00013 //#//////////////////////////////////////////////
00015 class TNGraph;
00016 class TNEGraph;
00017 
00019 typedef TPt<TNGraph> PNGraph;
00021 typedef TPt<TNEGraph> PNEGraph;
00022 
00023 //#//////////////////////////////////////////////
00025 
00032 class TUNGraph {
00033 public:
00034   typedef TUNGraph TNet;
00035   typedef TPt<TUNGraph> PNet;
00036 public:
00037   class TNode {
00038   private:
00039     TInt Id;
00040     TIntV NIdV;
00041   public:
00042     TNode() : Id(-1), NIdV() { }
00043     TNode(const int& NId) : Id(NId), NIdV() { }
00044     TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV) { }
00045     TNode(TSIn& SIn) : Id(SIn), NIdV(SIn) { }
00046     void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); }
00047     int GetId() const { return Id; }
00048     int GetDeg() const { return NIdV.Len(); }
00049     int GetInDeg() const { return GetDeg(); }
00050     int GetOutDeg() const { return GetDeg(); }
00051     int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
00052     int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
00053     int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
00054     bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
00055     bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
00056     bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
00057     void PackOutNIdV() { NIdV.Pack(); }
00058     void PackNIdV() { NIdV.Pack(); }
00059     friend class TUNGraph;
00060     friend class TUNGraphMtx;
00061   };
00063   class TNodeI {
00064   private:
00065     typedef THash<TInt, TNode>::TIter THashIter;
00066     THashIter NodeHI;
00067   public:
00068     TNodeI() : NodeHI() { }
00069     TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
00070     TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
00071     TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
00072 
00074     TNodeI& operator++ (int) { NodeHI++; return *this; }
00075 
00076     bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
00077     bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
00078 
00080     int GetId() const { return NodeHI.GetDat().GetId(); }
00082     int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
00084     int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
00086     int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
00088 
00091     int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
00093 
00096     int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
00098 
00101     int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
00103     bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
00105     bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
00107     bool IsNbrNId(const int& NId) const { return NodeHI.GetDat().IsNbrNId(NId); }
00108     friend class TUNGraph;
00109   };
00111   class TEdgeI {
00112   private:
00113     TNodeI CurNode, EndNode;
00114     int CurEdge;
00115   public:
00116     TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
00117     TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
00118     TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
00119     TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; } return *this; }
00121     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; }
00122     bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
00123     bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
00125     int GetId() const { return -1; }
00127     int GetSrcNId() const { return CurNode.GetId(); }
00129     int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
00130     friend class TUNGraph;
00131   };
00132 private:
00133   TCRef CRef;
00134   TInt MxNId, NEdges;
00135   THash<TInt, TNode> NodeH;
00136 private:
00137   TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
00138   const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
00139 public:
00140   TUNGraph() : CRef(), MxNId(0), NEdges(0), NodeH() { }
00142   explicit TUNGraph(const int& Nodes, const int& Edges) : MxNId(0), NEdges(0) { Reserve(Nodes, Edges); }
00143   TUNGraph(const TUNGraph& Graph) : MxNId(Graph.MxNId), NEdges(Graph.NEdges), NodeH(Graph.NodeH) { }
00145   TUNGraph(TSIn& SIn) : MxNId(SIn), NEdges(SIn), NodeH(SIn) { }
00147   void Save(TSOut& SOut) const { MxNId.Save(SOut); NEdges.Save(SOut); NodeH.Save(SOut); }
00149   static PUNGraph New() { return new TUNGraph(); }
00151 
00153   static PUNGraph New(const int& Nodes, const int& Edges) { return new TUNGraph(Nodes, Edges); }
00155   static PUNGraph Load(TSIn& SIn) { return PUNGraph(new TUNGraph(SIn)); }
00157   bool HasFlag(const TGraphFlag& Flag) const;
00158   TUNGraph& operator = (const TUNGraph& Graph) {
00159     if (this!=&Graph) { MxNId=Graph.MxNId; NEdges=Graph.NEdges; NodeH=Graph.NodeH; } return *this; }
00160   
00162   int GetNodes() const { return NodeH.Len(); }
00164 
00168   int AddNode(int NId = -1);
00170   int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId()); }
00172 
00181   int AddNode(const int& NId, const TIntV& NbrNIdV);
00183 
00191   int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId);
00193 
00195   void DelNode(const int& NId);
00197   void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
00199   bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
00201   TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
00203   TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
00205   TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
00207   int GetMxNId() const { return MxNId; }
00208 
00210   int GetEdges() const;
00212 
00216   int AddEdge(const int& SrcNId, const int& DstNId);
00218   int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
00220 
00223   void DelEdge(const int& SrcNId, const int& DstNId);
00225   bool IsEdge(const int& SrcNId, const int& DstNId) const;
00227   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; }
00229   TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
00231   TEdgeI GetEI(const int& EId) const;
00233 
00235   TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
00236 
00238   int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
00240   TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
00242   void GetNIdV(TIntV& NIdV) const;
00243 
00245   bool Empty() const { return GetNodes()==0; }
00247   void Clr() { MxNId=0; NEdges=0; NodeH.Clr(); }
00249   void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) NodeH.Gen(Nodes/2); }
00251   void ReserveNIdDeg(const int& NId, const int& Deg) { GetNode(NId).NIdV.Reserve(Deg); }
00253 
00257   void Defrag(const bool& OnlyNodeLinks=false);
00259 
00261   bool IsOk(const bool& ThrowExcept=true) const;
00263   void Dump(FILE *OutF=stdout) const;
00265 
00271   static PUNGraph GetSmallGraph();
00272 
00273   friend class TUNGraphMtx;
00274   friend class TPt<TUNGraph>;
00275 };
00276 
00277 //#//////////////////////////////////////////////
00279 
00293 class TNGraph {
00294 public:
00295   typedef TNGraph TNet;
00296   typedef TPt<TNGraph> PNet;
00297 public:
00298   class TNode {
00299   private:
00300     TInt Id;
00301     TIntV InNIdV, OutNIdV;
00302   public:
00303     TNode() : Id(-1), InNIdV(), OutNIdV() { }
00304     TNode(const int& NId) : Id(NId), InNIdV(), OutNIdV() { }
00305     TNode(const TNode& Node) : Id(Node.Id), InNIdV(Node.InNIdV), OutNIdV(Node.OutNIdV) { }
00306     TNode(TSIn& SIn) : Id(SIn), InNIdV(SIn), OutNIdV(SIn) { }
00307     void Save(TSOut& SOut) const { Id.Save(SOut); InNIdV.Save(SOut); OutNIdV.Save(SOut); }
00308     int GetId() const { return Id; }
00309     int GetDeg() const { return GetInDeg() + GetOutDeg(); }
00310     int GetInDeg() const { return InNIdV.Len(); }
00311     int GetOutDeg() const { return OutNIdV.Len(); }
00312     int GetInNId(const int& NodeN) const { return InNIdV[NodeN]; }
00313     int GetOutNId(const int& NodeN) const { return OutNIdV[NodeN]; }
00314     int GetNbrNId(const int& NodeN) const { return NodeN<GetOutDeg()?GetOutNId(NodeN):GetInNId(NodeN-GetOutDeg()); }
00315     bool IsInNId(const int& NId) const { return InNIdV.SearchBin(NId) != -1; }
00316     bool IsOutNId(const int& NId) const { return OutNIdV.SearchBin(NId) != -1; }
00317     bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
00318     void PackOutNIdV() { OutNIdV.Pack(); }
00319     void PackNIdV() { InNIdV.Pack(); }
00320     friend class TNGraph;
00321     friend class TNGraphMtx;
00322   };
00324   class TNodeI {
00325   private:
00326     typedef THash<TInt, TNode>::TIter THashIter;
00327     THashIter NodeHI;
00328   public:
00329     TNodeI() : NodeHI() { }
00330     TNodeI(const THashIter& NodeHIter) : NodeHI(NodeHIter) { }
00331     TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI) { }
00332     TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; return *this; }
00334     TNodeI& operator++ (int) { NodeHI++; return *this; }
00335     bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
00336     bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
00338     int GetId() const { return NodeHI.GetDat().GetId(); }
00340     int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
00342     int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
00344     int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
00346 
00348     int GetInNId(const int& NodeN) const { return NodeHI.GetDat().GetInNId(NodeN); }
00350 
00352     int GetOutNId(const int& NodeN) const { return NodeHI.GetDat().GetOutNId(NodeN); }
00354 
00356     int GetNbrNId(const int& NodeN) const { return NodeHI.GetDat().GetNbrNId(NodeN); }
00358     bool IsInNId(const int& NId) const { return NodeHI.GetDat().IsInNId(NId); }
00360     bool IsOutNId(const int& NId) const { return NodeHI.GetDat().IsOutNId(NId); }
00362     bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
00363     friend class TNGraph;
00364   };
00366   class TEdgeI {
00367   private:
00368     TNodeI CurNode, EndNode;
00369     int CurEdge;
00370   public:
00371     TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
00372     TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
00373     TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
00374     TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; }  return *this; }
00376     TEdgeI& operator++ (int) { CurEdge++; if (CurEdge >= CurNode.GetOutDeg()) { CurEdge=0; CurNode++;
00377       while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } }  return *this; }
00378     bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
00379     bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
00381     int GetId() const { return -1; }
00383     int GetSrcNId() const { return CurNode.GetId(); }
00385     int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
00386     friend class TNGraph;
00387   };
00388 private:
00389   TCRef CRef;
00390   TInt MxNId;
00391   THash<TInt, TNode> NodeH;
00392 private:
00393   TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
00394   const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
00395 public:
00396   TNGraph() : CRef(), MxNId(0), NodeH() { }
00398   explicit TNGraph(const int& Nodes, const int& Edges) : MxNId(0) { Reserve(Nodes, Edges); }
00399   TNGraph(const TNGraph& Graph) : MxNId(Graph.MxNId), NodeH(Graph.NodeH) { }
00401   TNGraph(TSIn& SIn) : MxNId(SIn), NodeH(SIn) { }
00403   void Save(TSOut& SOut) const { MxNId.Save(SOut); NodeH.Save(SOut); }
00405   static PNGraph New() { return new TNGraph(); }
00407 
00409   static PNGraph New(const int& Nodes, const int& Edges) { return new TNGraph(Nodes, Edges); }
00411   static PNGraph Load(TSIn& SIn) { return PNGraph(new TNGraph(SIn)); }
00413   bool HasFlag(const TGraphFlag& Flag) const;
00414   TNGraph& operator = (const TNGraph& Graph) {
00415     if (this!=&Graph) { MxNId=Graph.MxNId; NodeH=Graph.NodeH; }  return *this; }
00416   
00418   int GetNodes() const { return NodeH.Len(); }
00420 
00424   int AddNode(int NId = -1);
00426   int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
00428 
00437   int AddNode(const int& NId, const TIntV& InNIdV, const TIntV& OutNIdV);
00439 
00447   int AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& SrcVId, const int& DstVId);
00449 
00451   void DelNode(const int& NId);
00453   void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
00455   bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
00457   TNodeI BegNI() const { return TNodeI(NodeH.BegI()); }
00459   TNodeI EndNI() const { return TNodeI(NodeH.EndI()); }
00461   TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId)); }
00462   // GetNodeC() has been commented out. It was a quick shortcut, do not use.
00463   //const TNode& GetNodeC(const int& NId) const { return NodeH.GetDat(NId); }
00465   int GetMxNId() const { return MxNId; }
00466 
00468   int GetEdges() const;
00470 
00476   int AddEdge(const int& SrcNId, const int& DstNId);
00478   int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
00480 
00484   void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
00486   bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const;
00488   TEdgeI BegEI() const { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0){NI++;} return TEdgeI(NI, EndNI()); }
00490   TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
00492   TEdgeI GetEI(const int& EId) const; // not supported
00494   TEdgeI GetEI(const int& SrcNId, const int& DstNId) const;
00495 
00497   int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
00499   TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
00501   void GetNIdV(TIntV& NIdV) const;
00502 
00504   bool Empty() const { return GetNodes()==0; }
00506   void Clr() { MxNId=0; NodeH.Clr(); }
00508   void Reserve(const int& Nodes, const int& Edges) { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
00510   void ReserveNIdInDeg(const int& NId, const int& InDeg) { GetNode(NId).InNIdV.Reserve(InDeg); }
00512   void ReserveNIdOutDeg(const int& NId, const int& OutDeg) { GetNode(NId).OutNIdV.Reserve(OutDeg); }
00514 
00519   void Defrag(const bool& OnlyNodeLinks=false);
00521 
00524   bool IsOk(const bool& ThrowExcept=true) const;
00526   void Dump(FILE *OutF=stdout) const;
00528 
00532   static PNGraph GetSmallGraph();
00533   friend class TPt<TNGraph>;
00534   friend class TNGraphMtx;
00535 };
00536 
00537 // set flags
00538 namespace TSnap {
00539 template <> struct IsDirected<TNGraph> { enum { Val = 1 }; };
00540 }
00541 
00542 //#//////////////////////////////////////////////
00544 
00557 class TNEGraph {
00558 public:
00559   typedef TNEGraph TNet;
00560   typedef TPt<TNEGraph> PNet;
00561 public:
00562   class TNode {
00563   private:
00564     TInt Id;
00565     TIntV InEIdV, OutEIdV;
00566   public:
00567     TNode() : Id(-1), InEIdV(), OutEIdV() { }
00568     TNode(const int& NId) : Id(NId), InEIdV(), OutEIdV() { }
00569     TNode(const TNode& Node) : Id(Node.Id), InEIdV(Node.InEIdV), OutEIdV(Node.OutEIdV) { }
00570     TNode(TSIn& SIn) : Id(SIn), InEIdV(SIn), OutEIdV(SIn) { }
00571     void Save(TSOut& SOut) const { Id.Save(SOut); InEIdV.Save(SOut); OutEIdV.Save(SOut); }
00572     int GetId() const { return Id; }
00573     int GetDeg() const { return GetInDeg() + GetOutDeg(); }
00574     int GetInDeg() const { return InEIdV.Len(); }
00575     int GetOutDeg() const { return OutEIdV.Len(); }
00576     int GetInEId(const int& EdgeN) const { return InEIdV[EdgeN]; }
00577     int GetOutEId(const int& EdgeN) const { return OutEIdV[EdgeN]; }
00578     int GetNbrEId(const int& EdgeN) const { return EdgeN<GetOutDeg()?GetOutEId(EdgeN):GetInEId(EdgeN-GetOutDeg()); }
00579     bool IsInEId(const int& EId) const { return InEIdV.SearchBin(EId) != -1; }
00580     bool IsOutEId(const int& EId) const { return OutEIdV.SearchBin(EId) != -1; }
00581     friend class TNEGraph;
00582   };
00583   class TEdge {
00584   private:
00585     TInt Id, SrcNId, DstNId;
00586   public:
00587     TEdge() : Id(-1), SrcNId(-1), DstNId(-1) { }
00588     TEdge(const int& EId, const int& SourceNId, const int& DestNId) : Id(EId), SrcNId(SourceNId), DstNId(DestNId) { }
00589     TEdge(const TEdge& Edge) : Id(Edge.Id), SrcNId(Edge.SrcNId), DstNId(Edge.DstNId) { }
00590     TEdge(TSIn& SIn) : Id(SIn), SrcNId(SIn), DstNId(SIn) { }
00591     void Save(TSOut& SOut) const { Id.Save(SOut); SrcNId.Save(SOut); DstNId.Save(SOut); }
00592     int GetId() const { return Id; }
00593     int GetSrcNId() const { return SrcNId; }
00594     int GetDstNId() const { return DstNId; }
00595     friend class TNEGraph;
00596   };
00598   class TNodeI {
00599   private:
00600     typedef THash<TInt, TNode>::TIter THashIter;
00601     THashIter NodeHI;
00602     const TNEGraph *Graph;
00603   public:
00604     TNodeI() : NodeHI(), Graph(NULL) { }
00605     TNodeI(const THashIter& NodeHIter, const TNEGraph* GraphPt) : NodeHI(NodeHIter), Graph(GraphPt) { }
00606     TNodeI(const TNodeI& NodeI) : NodeHI(NodeI.NodeHI), Graph(NodeI.Graph) { }
00607     TNodeI& operator = (const TNodeI& NodeI) { NodeHI = NodeI.NodeHI; Graph=NodeI.Graph; return *this; }
00609     TNodeI& operator++ (int) { NodeHI++; return *this; }
00610     bool operator < (const TNodeI& NodeI) const { return NodeHI < NodeI.NodeHI; }
00611     bool operator == (const TNodeI& NodeI) const { return NodeHI == NodeI.NodeHI; }
00613     int GetId() const { return NodeHI.GetDat().GetId(); }
00615     int GetDeg() const { return NodeHI.GetDat().GetDeg(); }
00617     int GetInDeg() const { return NodeHI.GetDat().GetInDeg(); }
00619     int GetOutDeg() const { return NodeHI.GetDat().GetOutDeg(); }
00621 
00623     int GetInNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetInEId(EdgeN)).GetSrcNId(); }
00625 
00627     int GetOutNId(const int& EdgeN) const { return Graph->GetEdge(NodeHI.GetDat().GetOutEId(EdgeN)).GetDstNId(); }
00629 
00631     int GetNbrNId(const int& EdgeN) const { const TEdge& E = Graph->GetEdge(NodeHI.GetDat().GetNbrEId(EdgeN));
00632       return GetId()==E.GetSrcNId() ? E.GetDstNId():E.GetSrcNId(); }
00634     bool IsInNId(const int& NId) const;
00636     bool IsOutNId(const int& NId) const;
00638     bool IsNbrNId(const int& NId) const { return IsOutNId(NId) || IsInNId(NId); }
00639     // edges
00641     int GetInEId(const int& EdgeN) const { return NodeHI.GetDat().GetInEId(EdgeN); }
00643     int GetOutEId(const int& EdgeN) const { return NodeHI.GetDat().GetOutEId(EdgeN); }
00645     int GetNbrEId(const int& EdgeN) const { return NodeHI.GetDat().GetNbrEId(EdgeN); }
00647     bool IsInEId(const int& EId) const { return NodeHI.GetDat().IsInEId(EId); }
00649     bool IsOutEId(const int& EId) const { return NodeHI.GetDat().IsOutEId(EId); }
00651     bool IsNbrEId(const int& EId) const { return IsInEId(EId) || IsOutEId(EId); }
00652     friend class TNEGraph;
00653   };
00655   class TEdgeI {
00656   private:
00657     typedef THash<TInt, TEdge>::TIter THashIter;
00658     THashIter EdgeHI;
00659     const TNEGraph *Graph;
00660   public:
00661     TEdgeI() : EdgeHI(), Graph(NULL) { }
00662     TEdgeI(const THashIter& EdgeHIter, const TNEGraph *GraphPt) : EdgeHI(EdgeHIter), Graph(GraphPt) { }
00663     TEdgeI(const TEdgeI& EdgeI) : EdgeHI(EdgeI.EdgeHI), Graph(EdgeI.Graph) { }
00664     TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { EdgeHI=EdgeI.EdgeHI; Graph=EdgeI.Graph; }  return *this; }
00666     TEdgeI& operator++ (int) { EdgeHI++; return *this; }
00667     bool operator < (const TEdgeI& EdgeI) const { return EdgeHI < EdgeI.EdgeHI; }
00668     bool operator == (const TEdgeI& EdgeI) const { return EdgeHI == EdgeI.EdgeHI; }
00670     int GetId() const { return EdgeHI.GetDat().GetId(); }
00672     int GetSrcNId() const { return EdgeHI.GetDat().GetSrcNId(); }
00674     int GetDstNId() const { return EdgeHI.GetDat().GetDstNId(); }
00675     friend class TNEGraph;
00676   };
00677 
00678 private:
00679   TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
00680   const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
00681   TEdge& GetEdge(const int& EId) { return EdgeH.GetDat(EId); }
00682   const TEdge& GetEdge(const int& EId) const { return EdgeH.GetDat(EId); }
00683 private:
00684   TCRef CRef;
00685   TInt MxNId, MxEId;
00686   THash<TInt, TNode> NodeH;
00687   THash<TInt, TEdge> EdgeH;
00688 public:
00689   TNEGraph() : CRef(), MxNId(0), MxEId(0) { }
00691   explicit TNEGraph(const int& Nodes, const int& Edges) : CRef(), MxNId(0), MxEId(0) { Reserve(Nodes, Edges); }
00692   TNEGraph(const TNEGraph& Graph) : MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { }
00694   TNEGraph(TSIn& SIn) : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { }
00696   void Save(TSOut& SOut) const { MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut); }
00698   static PNEGraph New() { return PNEGraph(new TNEGraph()); }
00700 
00702   static PNEGraph New(const int& Nodes, const int& Edges) { return PNEGraph(new TNEGraph(Nodes, Edges)); }
00704   static PNEGraph Load(TSIn& SIn) { return PNEGraph(new TNEGraph(SIn)); }
00706   bool HasFlag(const TGraphFlag& Flag) const;
00707   TNEGraph& operator = (const TNEGraph& Graph) { if (this!=&Graph) {
00708     MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; }  return *this; }
00709 
00711   int GetNodes() const { return NodeH.Len(); }
00713 
00717   int AddNode(int NId = -1);
00719   int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); }
00721 
00723   void DelNode(const int& NId);
00725   void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
00727   bool IsNode(const int& NId) const { return NodeH.IsKey(NId); }
00729   TNodeI BegNI() const { return TNodeI(NodeH.BegI(), this); }
00731   TNodeI EndNI() const { return TNodeI(NodeH.EndI(), this); }
00733   TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId), this); }
00735   int GetMxNId() const { return MxNId; }
00736 
00738   int GetEdges() const { return EdgeH.Len(); }
00740 
00745   int AddEdge(const int& SrcNId, const int& DstNId, int EId  = -1);
00747   int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); }
00749   void DelEdge(const int& EId);
00751 
00755   void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true);
00757   bool IsEdge(const int& EId) const { return EdgeH.IsKey(EId); }
00759   bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const { int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); }
00761   bool IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir = true) const;
00763   int GetEId(const int& SrcNId, const int& DstNId) const { int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; }
00765   TEdgeI BegEI() const { return TEdgeI(EdgeH.BegI(), this); }
00767   TEdgeI EndEI() const { return TEdgeI(EdgeH.EndI(), this); }
00769   TEdgeI GetEI(const int& EId) const { return TEdgeI(EdgeH.GetI(EId), this); }
00771   TEdgeI GetEI(const int& SrcNId, const int& DstNId) const { return GetEI(GetEId(SrcNId, DstNId)); }
00772 
00774   int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
00776   TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
00778   int GetRndEId(TRnd& Rnd=TInt::Rnd) { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); }
00780   TEdgeI GetRndEI(TRnd& Rnd=TInt::Rnd) { return GetEI(GetRndEId(Rnd)); }
00782   void GetNIdV(TIntV& NIdV) const;
00784   void GetEIdV(TIntV& EIdV) const;
00785 
00787   bool Empty() const { return GetNodes()==0; }
00789   void Clr() { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); }
00791   void Reserve(const int& Nodes, const int& Edges) {
00792     if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } }
00794 
00799   void Defrag(const bool& OnlyNodeLinks=false);
00801 
00804   bool IsOk(const bool& ThrowExcept=true) const;
00806   void Dump(FILE *OutF=stdout) const;
00808 
00812   static PNEGraph GetSmallGraph();
00813   friend class TPt<TNEGraph>;
00814 };
00815 
00816 // set flags
00817 namespace TSnap {
00818 template <> struct IsMultiGraph<TNEGraph> { enum { Val = 1 }; };
00819 template <> struct IsDirected<TNEGraph> { enum { Val = 1 }; };
00820 }
00821 
00822 //#//////////////////////////////////////////////
00824 
00825 class TBPGraph {
00826 public:
00827   typedef TBPGraph TNet;
00828   typedef TPt<TBPGraph> PNet;
00829   typedef enum { bgsUndef, bgsLeft, bgsRight, bgsBoth } TNodeTy; // left or right hand side node
00830 public:
00831   class TNode {
00832   private:
00833     TInt Id;
00834     TIntV NIdV;
00835     TNodeTy NodeTy; // remove
00836   public:
00837     TNode() : Id(-1), NIdV(), NodeTy(bgsUndef) { }
00838     TNode(const int& NId) : Id(NId), NIdV(), NodeTy(true?bgsLeft:bgsRight) { }
00839     TNode(const TNode& Node) : Id(Node.Id), NIdV(Node.NIdV), NodeTy(Node.NodeTy) { }
00840     TNode(TSIn& SIn) : Id(SIn), NIdV(SIn), NodeTy(bgsUndef) { TInt Ty(SIn); NodeTy=(TNodeTy)Ty.Val; }
00841     void Save(TSOut& SOut) const { Id.Save(SOut); NIdV.Save(SOut); TInt(NodeTy).Save(SOut); }
00842     int GetId() const { return Id; }
00843     int GetDeg() const { return NIdV.Len(); }
00844     int GetInDeg() const { return GetDeg(); }
00845     int GetOutDeg() const { return GetDeg(); }
00846     int GetInNId(const int& NodeN) const { return GetNbrNId(NodeN); }
00847     int GetOutNId(const int& NodeN) const { return GetNbrNId(NodeN); }
00848     int GetNbrNId(const int& NodeN) const { return NIdV[NodeN]; }
00849     bool IsNbrNId(const int& NId) const { return NIdV.SearchBin(NId)!=-1; }
00850     bool IsInNId(const int& NId) const { return IsNbrNId(NId); }
00851     bool IsOutNId(const int& NId) const { return IsNbrNId(NId); }
00852     void PackOutNIdV() { NIdV.Pack(); }
00853     void PackNIdV() { NIdV.Pack(); }
00854     friend class TBPGraph;
00855   };
00857   class TNodeI {
00858   private:
00859     typedef THash<TInt, TNode>::TIter THashIter;
00860     THashIter LeftHI, RightHI; // iterator over left and right hand-side nodes
00861   private:
00862     inline THashIter HI() const { return ! LeftHI.IsEnd()?LeftHI:RightHI; }
00863   public:
00864     TNodeI() : LeftHI(), RightHI() { }
00865     TNodeI(const THashIter& LeftHIter, const THashIter& RightHIter) : LeftHI(LeftHIter), RightHI(RightHIter) { }
00866     TNodeI(const TNodeI& NodeI) : LeftHI(NodeI.LeftHI), RightHI(NodeI.RightHI) { }
00867     TNodeI& operator = (const TNodeI& NodeI) { LeftHI = NodeI.LeftHI; RightHI=NodeI.RightHI; return *this; }
00869     TNodeI& operator++ (int) { 
00870       if (! LeftHI.IsEnd()) { 
00871         LeftHI++; } 
00872       else if (! RightHI.IsEnd()) { 
00873         RightHI++; } 
00874       return *this; }
00875     bool operator < (const TNodeI& NodeI) const { return LeftHI < NodeI.LeftHI || (LeftHI==NodeI.LeftHI && RightHI < NodeI.RightHI); }
00876     bool operator == (const TNodeI& NodeI) const { return LeftHI==NodeI.LeftHI && RightHI==NodeI.RightHI; }
00878     int GetId() const { return HI().GetDat().GetId(); }
00880     bool IsLeft() const { return ! LeftHI.IsEnd(); }
00882     bool IsRight() const { return ! IsLeft(); }
00884     int GetDeg() const { return HI().GetDat().GetDeg(); }
00886     int GetInDeg() const { return HI().GetDat().GetInDeg(); }
00888     int GetOutDeg() const { return HI().GetDat().GetOutDeg(); }
00890 
00891     int GetInNId(const int& NodeN) const { return HI().GetDat().GetInNId(NodeN); }
00893 
00894     int GetOutNId(const int& NodeN) const { return HI().GetDat().GetOutNId(NodeN); }
00896 
00897     int GetNbrNId(const int& NodeN) const { return HI().GetDat().GetNbrNId(NodeN); }
00899     bool IsInNId(const int& NId) const { return HI().GetDat().IsInNId(NId); }
00901     bool IsOutNId(const int& NId) const { return HI().GetDat().IsOutNId(NId); }
00903     bool IsNbrNId(const int& NId) const { return HI().GetDat().IsNbrNId(NId); }
00904     friend class TBPGraph;
00905   };
00907   class TEdgeI {
00908   private:
00909     TNodeI CurNode, EndNode; // end node on the 'left'
00910     int CurEdge;
00911   public:
00912     TEdgeI() : CurNode(), EndNode(), CurEdge(0) { }
00913     TEdgeI(const TNodeI& NodeI, const TNodeI& EndNodeI, const int& EdgeN=0) : CurNode(NodeI), EndNode(EndNodeI), CurEdge(EdgeN) { }
00914     TEdgeI(const TEdgeI& EdgeI) : CurNode(EdgeI.CurNode), EndNode(EdgeI.EndNode), CurEdge(EdgeI.CurEdge) { }
00915     TEdgeI& operator = (const TEdgeI& EdgeI) { if (this!=&EdgeI) { CurNode=EdgeI.CurNode; EndNode=EdgeI.EndNode; CurEdge=EdgeI.CurEdge; }  return *this; }
00917     TEdgeI& operator++ (int) { CurEdge++; if (CurEdge >= CurNode.GetOutDeg()) { CurEdge=0; CurNode++;
00918       while (CurNode < EndNode && CurNode.GetOutDeg()==0) { CurNode++; } }  return *this; }
00919     bool operator < (const TEdgeI& EdgeI) const { return CurNode<EdgeI.CurNode || (CurNode==EdgeI.CurNode && CurEdge<EdgeI.CurEdge); }
00920     bool operator == (const TEdgeI& EdgeI) const { return CurNode == EdgeI.CurNode && CurEdge == EdgeI.CurEdge; }
00922     int GetId() const { return -1; }
00924     int GetSrcNId() const { return CurNode.GetId(); }
00926     int GetDstNId() const { return CurNode.GetOutNId(CurEdge); }
00928     int GetLNId() const { return GetSrcNId(); }
00930     int GetRNId() const { return GetDstNId(); }
00931     friend class TBPGraph;
00932   };
00933 private:
00934   TCRef CRef;
00935   TInt MxNId;                 // maximum node ID in the graph
00936   THash<TInt, TNode> LeftH;   // 'left' nodes
00937   THash<TInt, TNode> RightH;  // 'right' nodes
00938 private:
00939   //TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); }
00940   //const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); }
00941 public:
00942   TBPGraph() : CRef(), MxNId(0), LeftH(), RightH() { }
00944   explicit TBPGraph(const int& Nodes, const int& Edges) : MxNId(0) { } 
00945   TBPGraph(const TBPGraph& BPGraph) : MxNId(BPGraph.MxNId), LeftH(BPGraph.LeftH), RightH(BPGraph.RightH) { }
00947   TBPGraph(TSIn& SIn) : MxNId(SIn), LeftH(SIn), RightH(SIn) { }
00949   void Save(TSOut& SOut) const { MxNId.Save(SOut); LeftH.Save(SOut); RightH.Save(SOut); }
00951   static PBPGraph New() { return new TBPGraph(); }
00953 
00954   static PBPGraph New(const int& Nodes, const int& Edges) { return new TBPGraph(Nodes, Edges); }
00956   static PBPGraph Load(TSIn& SIn) { return PBPGraph(new TBPGraph(SIn)); }
00958   bool HasFlag(const TGraphFlag& Flag) const;
00959   TBPGraph& operator = (const TBPGraph& BPGraph) {
00960     if (this!=&BPGraph) { MxNId=BPGraph.MxNId; LeftH=BPGraph.LeftH; RightH=BPGraph.RightH; }  return *this; }
00961   
00963   int GetNodes() const { return GetLNodes() + GetRNodes(); }
00965   int GetLNodes() const { return LeftH.Len(); }
00967   int GetRNodes() const { return RightH.Len(); }
00969 
00970   int AddNode(int NId = -1, const bool& LeftNode=true);
00972   int AddNode(const TNodeI& NodeI) { return AddNode(NodeI.GetId(), NodeI.IsLeft()); }
00974 
00975   void DelNode(const int& NId);
00977   void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); }
00979   bool IsNode(const int& NId) const { return IsLNode(NId) || IsRNode(NId); }
00981   bool IsLNode(const int& NId) const { return LeftH.IsKey(NId); }
00983   bool IsRNode(const int& NId) const { return RightH.IsKey(NId); }
00985   int GetMxNId() const { return MxNId; }
00986     
00988   TNodeI BegNI() const { return TNodeI(LeftH.BegI(), RightH.BegI()); }
00990   TNodeI EndNI() const { return TNodeI(LeftH.EndI(), RightH.EndI()); }
00992   TNodeI GetNI(const int& NId) const { return IsLNode(NId) ? TNodeI(LeftH.GetI(NId), RightH.EndI()) : TNodeI(LeftH.EndI(), RightH.GetI(NId)); }
00994   TNodeI BegLNI() const { return TNodeI(LeftH.BegI(), RightH.EndI()); }
00996   TNodeI EndLNI() const { return EndNI(); }
00998   TNodeI BegRNI() const { return TNodeI(LeftH.EndI(), RightH.BegI()); }
01000   TNodeI EndRNI() const { return EndNI(); }
01001 
01003   int GetEdges() const;
01005 
01006   int AddEdge(const int& LeftNId, const int& RightNId);
01008   int AddEdge(const TEdgeI& EdgeI) { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId()); }
01010 
01011   void DelEdge(const int& LeftNId, const int& RightNId);
01013   bool IsEdge(const int& LeftNId, const int& RightNId) const;
01015   TEdgeI BegEI() const { TNodeI NI=BegLNI(); while (NI<EndLNI() && (NI.GetOutDeg()==0 || NI.GetId()>NI.GetOutNId(0))) { NI++; } return TEdgeI(NI, EndLNI()); }
01017   TEdgeI EndEI() const { return TEdgeI(EndNI(), EndNI()); }
01019   TEdgeI GetEI(const int& EId) const;
01021 
01022   TEdgeI GetEI(const int& LeftNId, const int& RightNId) const;
01023     
01025   int GetRndNId(TRnd& Rnd=TInt::Rnd);
01027   int GetRndLNId(TRnd& Rnd=TInt::Rnd);
01029   int GetRndRNId(TRnd& Rnd=TInt::Rnd);
01031   TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); }
01033   void GetNIdV(TIntV& NIdV) const;
01035   void GetLNIdV(TIntV& NIdV) const;
01037   void GetRNIdV(TIntV& NIdV) const;
01038 
01040   bool Empty() const { return GetNodes()==0; }
01042   void Clr() { MxNId=0; LeftH.Clr(); RightH.Clr(); }
01044   void Reserve(const int& Nodes, const int& Edges);
01046 
01047   void Defrag(const bool& OnlyNodeLinks=false);
01049 
01050   bool IsOk(const bool& ThrowExcept=true) const;
01052   void Dump(FILE *OutF=stdout) const;
01054 
01055   static PBPGraph GetSmallGraph();
01056 
01057   friend class TPt<TBPGraph>;
01058 };
01059 
01060 // set flags
01061 namespace TSnap {
01062 template <> struct IsBipart<TBPGraph> { enum { Val = 1 }; };
01063 }