SNAP Library 6.0, Developer Reference
2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
Directed multigraph. More...
#include <graph.h>
Classes | |
class | TEdge |
class | TEdgeI |
Edge iterator. Only forward iteration (operator++) is supported. More... | |
class | TNode |
class | TNodeI |
Node iterator. Only forward iteration (operator++) is supported. More... | |
Public Types | |
typedef TNEGraph | TNet |
typedef TPt< TNEGraph > | PNet |
Public Member Functions | |
TNEGraph () | |
TNEGraph (const int &Nodes, const int &Edges) | |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. More... | |
TNEGraph (const TNEGraph &Graph) | |
TNEGraph (TSIn &SIn) | |
Constructor for loading the graph from a (binary) stream SIn. More... | |
void | Save (TSOut &SOut) const |
Saves the graph to a (binary) stream SOut. More... | |
bool | HasFlag (const TGraphFlag &Flag) const |
Allows for run-time checking the type of the graph (see the TGraphFlag for flags). More... | |
TNEGraph & | operator= (const TNEGraph &Graph) |
int | GetNodes () const |
Returns the number of nodes in the graph. More... | |
int | AddNode (int NId=-1) |
Adds a node of ID NId to the graph. More... | |
int | AddNode (const TNodeI &NodeId) |
Adds a node of ID NodeI.GetId() to the graph. More... | |
void | DelNode (const int &NId) |
Deletes node of ID NId from the graph. More... | |
void | DelNode (const TNode &NodeI) |
Deletes node of ID NodeI.GetId() from the graph. More... | |
bool | IsNode (const int &NId) const |
Tests whether ID NId is a node. More... | |
TNodeI | BegNI () const |
Returns an iterator referring to the first node in the graph. More... | |
TNodeI | EndNI () const |
Returns an iterator referring to the past-the-end node in the graph. More... | |
TNodeI | GetNI (const int &NId) const |
Returns an iterator referring to the node of ID NId in the graph. More... | |
int | GetMxNId () const |
Returns an ID that is larger than any node ID in the graph. More... | |
int | GetEdges () const |
Returns the number of edges in the graph. More... | |
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. More... | |
int | AddEdge (const TEdgeI &EdgeI) |
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph. More... | |
void | DelEdge (const int &EId) |
Deletes an edge with edge ID EId from the graph. More... | |
void | DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) |
Deletes all edges between node IDs SrcNId and DstNId from the graph. More... | |
bool | IsEdge (const int &EId) const |
Tests whether an edge with edge ID EId exists in the graph. More... | |
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. More... | |
bool | IsEdge (const int &SrcNId, const int &DstNId, int &EId, const bool &IsDir=true) const |
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. if an edge exists, return its edge ID in EId. More... | |
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. More... | |
TEdgeI | BegEI () const |
Returns an iterator referring to the first edge in the graph. More... | |
TEdgeI | EndEI () const |
Returns an iterator referring to the past-the-end edge in the graph. More... | |
TEdgeI | GetEI (const int &EId) const |
Returns an iterator referring to edge with edge ID EId. More... | |
TEdgeI | GetEI (const int &SrcNId, const int &DstNId) const |
Returns an iterator referring to edge (SrcNId, DstNId) in the graph. More... | |
int | GetRndNId (TRnd &Rnd=TInt::Rnd) |
Returns an ID of a random node in the graph. More... | |
TNodeI | GetRndNI (TRnd &Rnd=TInt::Rnd) |
Returns an interator referring to a random node in the graph. More... | |
int | GetRndEId (TRnd &Rnd=TInt::Rnd) |
Returns an ID of a random edge in the graph. More... | |
TEdgeI | GetRndEI (TRnd &Rnd=TInt::Rnd) |
Returns an interator referring to a random edge in the graph. More... | |
void | GetNIdV (TIntV &NIdV) const |
Gets a vector IDs of all nodes in the graph. More... | |
void | GetEIdV (TIntV &EIdV) const |
Gets a vector IDs of all edges in the graph. More... | |
bool | Empty () const |
Tests whether the graph is empty (has zero nodes). More... | |
void | Clr () |
Deletes all nodes and edges from the graph. More... | |
void | Reserve (const int &Nodes, const int &Edges) |
Reserves memory for a graph of Nodes nodes and Edges edges. More... | |
void | Defrag (const bool &OnlyNodeLinks=false) |
Defragments the graph. More... | |
bool | IsOk (const bool &ThrowExcept=true) const |
Checks the graph data structure for internal consistency. More... | |
void | Dump (FILE *OutF=stdout) const |
Print the graph in a human readable form to an output stream OutF. More... | |
Static Public Member Functions | |
static PNEGraph | New () |
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New(). More... | |
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 Edges edges. More... | |
static PNEGraph | Load (TSIn &SIn) |
Static constructor that loads the graph from a stream SIn and returns a pointer to it. More... | |
static PNEGraph | GetSmallGraph () |
Returns a small multigraph on 5 nodes and 6 edges. More... | |
Private Member Functions | |
TNode & | GetNode (const int &NId) |
const TNode & | GetNode (const int &NId) const |
TEdge & | GetEdge (const int &EId) |
const TEdge & | GetEdge (const int &EId) const |
Private Attributes | |
TCRef | CRef |
TInt | MxNId |
TInt | MxEId |
THash< TInt, TNode > | NodeH |
THash< TInt, TEdge > | EdgeH |
Friends | |
class | TPt< TNEGraph > |
Directed multigraph.
Node IDs can be arbitrary non-negative integers. Edges have IDs. Nodes and edges have no attributes/data associated with them. There can be more than one directed edge from one source node to a destination node. Self loops (one per node) are allowed as well as multiple (parallel) edges. The directed multigraph data structure is implemented using sorted adjacency lists. This means adding a node takes constant time, while adding an edge takes linear time (since adjacency list is kept sorted) in the node degree. Accessing arbitrary node takes constant time and accessing any edge takes logarithmic time in the node degree.
typedef TPt<TNEGraph> TNEGraph::PNet |
typedef TNEGraph TNEGraph::TNet |
|
inline |
|
inlineexplicit |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition at line 802 of file graph.h.
References Reserve().
|
inline |
int TNEGraph::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.
Returns the ID of the edge being added. If EId is -1, edge ID is automatically assigned. Aborts, if an edge with ID EId already exists. Aborts, if SrcNId or DstNId are not nodes in the graph.
Definition at line 516 of file graph.cpp.
References THash< TKey, TDat, THashFunc >::AddDat(), TVec< TVal, TSizeTy >::AddSorted(), EdgeH, TStr::Fmt(), GetNode(), IAssertR, TNEGraph::TNode::InEIdV, IsEdge(), IsNode(), TMath::Mx(), MxEId, and TNEGraph::TNode::OutEIdV.
|
inline |
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition at line 858 of file graph.h.
References AddEdge(), TNEGraph::TEdgeI::GetDstNId(), TNEGraph::TEdgeI::GetId(), and TNEGraph::TEdgeI::GetSrcNId().
Referenced by AddEdge().
int TNEGraph::AddNode | ( | int | NId = -1 | ) |
Adds a node of ID NId to the graph.
Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.
Definition at line 486 of file graph.cpp.
References THash< TKey, TDat, THashFunc >::AddDat(), TStr::Fmt(), IAssertR, IsNode(), TMath::Mx(), MxNId, and NodeH.
|
inline |
Adds a node of ID NodeI.GetId() to the graph.
Definition at line 830 of file graph.h.
References AddNode(), and TNEGraph::TNodeI::GetId().
Referenced by AddNode().
|
inline |
Returns an iterator referring to the first edge in the graph.
Definition at line 876 of file graph.h.
References THash< TKey, TDat, THashFunc >::BegI().
Referenced by Dump().
|
inline |
Returns an iterator referring to the first node in the graph.
Definition at line 840 of file graph.h.
References THash< TKey, TDat, THashFunc >::BegI().
Referenced by Dump().
|
inline |
Deletes all nodes and edges from the graph.
Definition at line 900 of file graph.h.
References THash< TKey, TDat, THashFunc >::Clr().
void TNEGraph::Defrag | ( | const bool & | OnlyNodeLinks = false | ) |
Defragments the graph.
After performing many node and edge insertions and deletions to a graph, the graph data structure will be fragmented in memory. This function compacts down the graph data structure and frees unneeded memory.
Definition at line 577 of file graph.cpp.
References THash< TKey, TDat, THashFunc >::Defrag(), EdgeH, THash< TKey, TDat, THashFunc >::FFirstKeyId(), THash< TKey, TDat, THashFunc >::FNextKeyId(), TNEGraph::TNode::InEIdV, THash< TKey, TDat, THashFunc >::IsKeyIdEqKeyN(), NodeH, TNEGraph::TNode::OutEIdV, and TVec< TVal, TSizeTy >::Pack().
void TNEGraph::DelEdge | ( | const int & | EId | ) |
Deletes an edge with edge ID EId from the graph.
Definition at line 527 of file graph.cpp.
References TVec< TVal, TSizeTy >::DelIfIn(), THash< TKey, TDat, THashFunc >::DelKey(), EdgeH, TNEGraph::TEdge::GetDstNId(), GetEdge(), GetNode(), TNEGraph::TEdge::GetSrcNId(), IAssert, TNEGraph::TNode::InEIdV, IsEdge(), and TNEGraph::TNode::OutEIdV.
void TNEGraph::DelEdge | ( | const int & | SrcNId, |
const int & | DstNId, | ||
const bool & | IsDir = true |
||
) |
Deletes all edges between node IDs SrcNId and DstNId from the graph.
If the edge (SrcNId, DstNId) does not exist in the graph function still completes. But the function aborts if SrcNId or DstNId are not nodes in the graph.
Definition at line 537 of file graph.cpp.
References TVec< TVal, TSizeTy >::DelIfIn(), THash< TKey, TDat, THashFunc >::DelKey(), EdgeH, GetNode(), IAssert, TNEGraph::TNode::InEIdV, IsEdge(), and TNEGraph::TNode::OutEIdV.
void TNEGraph::DelNode | ( | const int & | NId | ) |
Deletes node of ID NId from the graph.
If the node of ID NId does not exist the function aborts.
Definition at line 497 of file graph.cpp.
References THash< TKey, TDat, THashFunc >::DelKey(), EdgeH, TNEGraph::TEdge::GetDstNId(), GetEdge(), TNEGraph::TNode::GetInDeg(), TNEGraph::TNode::GetInEId(), GetNode(), TNEGraph::TNode::GetOutDeg(), TNEGraph::TNode::GetOutEId(), TNEGraph::TEdge::GetSrcNId(), IAssert, and NodeH.
|
inline |
Deletes node of ID NodeI.GetId() from the graph.
Definition at line 836 of file graph.h.
References DelNode(), and TNEGraph::TNode::GetId().
Referenced by DelNode().
void TNEGraph::Dump | ( | FILE * | OutF = stdout | ) | const |
Print the graph in a human readable form to an output stream OutF.
Definition at line 639 of file graph.cpp.
References BegEI(), BegNI(), edge, EndEI(), EndNI(), GetEdges(), and GetNodes().
|
inline |
Tests whether the graph is empty (has zero nodes).
Definition at line 898 of file graph.h.
References GetNodes().
|
inline |
Returns an iterator referring to the past-the-end edge in the graph.
Definition at line 878 of file graph.h.
References THash< TKey, TDat, THashFunc >::EndI().
Referenced by Dump().
|
inline |
Returns an iterator referring to the past-the-end node in the graph.
Definition at line 842 of file graph.h.
References THash< TKey, TDat, THashFunc >::EndI().
Referenced by Dump(), and TBPGraph::GetEI().
|
inlineprivate |
Definition at line 792 of file graph.h.
References EdgeH, and THash< TKey, TDat, THashFunc >::GetDat().
Referenced by DelEdge(), DelNode(), TNEGraph::TNodeI::GetInNId(), TNEGraph::TNodeI::GetNbrNId(), TNEGraph::TNodeI::GetOutNId(), IsEdge(), and TNEGraph::TNodeI::IsInNId().
|
inlineprivate |
Definition at line 793 of file graph.h.
References EdgeH, and THash< TKey, TDat, THashFunc >::GetDat().
|
inline |
Returns the number of edges in the graph.
Definition at line 849 of file graph.h.
References THash< TKey, TDat, THashFunc >::Len().
Referenced by Dump(), TBPGraph::Dump(), and GetEIdV().
|
inline |
Returns an iterator referring to edge with edge ID EId.
Definition at line 880 of file graph.h.
References THash< TKey, TDat, THashFunc >::GetI().
Referenced by GetRndEI().
|
inline |
Returns an iterator referring to edge (SrcNId, DstNId) in the graph.
Definition at line 882 of file graph.h.
References GetEI(), and GetEId().
Referenced by GetEI().
|
inline |
Returns an edge ID between node IDs SrcNId and DstNId, if such an edge exists. Otherwise, return -1.
Definition at line 874 of file graph.h.
References IsEdge().
Referenced by GetEI().
void TNEGraph::GetEIdV | ( | TIntV & | EIdV | ) | const |
Gets a vector IDs of all edges in the graph.
Definition at line 570 of file graph.cpp.
References TVec< TVal, TSizeTy >::Add(), EdgeH, THash< TKey, TDat, THashFunc >::FFirstKeyId(), THash< TKey, TDat, THashFunc >::FNextKeyId(), TVec< TVal, TSizeTy >::Gen(), GetEdges(), and THash< TKey, TDat, THashFunc >::GetKey().
|
inline |
|
inline |
Returns an iterator referring to the node of ID NId in the graph.
Definition at line 844 of file graph.h.
References THash< TKey, TDat, THashFunc >::GetI().
Referenced by TBPGraph::GetEI(), and GetRndNI().
void TNEGraph::GetNIdV | ( | TIntV & | NIdV | ) | const |
Gets a vector IDs of all nodes in the graph.
Definition at line 564 of file graph.cpp.
References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::FFirstKeyId(), THash< TKey, TDat, THashFunc >::FNextKeyId(), TVec< TVal, TSizeTy >::Gen(), THash< TKey, TDat, THashFunc >::GetKey(), GetNodes(), and NodeH.
|
inlineprivate |
|
inlineprivate |
Definition at line 791 of file graph.h.
References THash< TKey, TDat, THashFunc >::GetDat(), and NodeH.
|
inline |
Returns the number of nodes in the graph.
Definition at line 822 of file graph.h.
References THash< TKey, TDat, THashFunc >::Len().
Referenced by Dump(), TBPGraph::Dump(), Empty(), GetNIdV(), TBPGraph::GetNIdV(), and TBPGraph::GetRndNId().
Returns an interator referring to a random edge in the graph.
Definition at line 891 of file graph.h.
References GetEI(), and GetRndEId().
Returns an ID of a random edge in the graph.
Definition at line 889 of file graph.h.
References THash< TKey, TDat, THashFunc >::GetKey(), and THash< TKey, TDat, THashFunc >::GetRndKeyId().
Referenced by GetRndEI().
Returns an interator referring to a random node in the graph.
Definition at line 887 of file graph.h.
References GetNI(), and GetRndNId().
Returns an ID of a random node in the graph.
Definition at line 885 of file graph.h.
References THash< TKey, TDat, THashFunc >::GetKey(), and THash< TKey, TDat, THashFunc >::GetRndKeyId().
Referenced by GetRndNI().
|
static |
Returns a small multigraph on 5 nodes and 6 edges.
/// Edges: 0 -> 1, 0 -> 2, 0 -> 3, 0 -> 4, 1 -> 2, 1 -> 2 ///
Definition at line 660 of file graph.cpp.
References New().
bool TNEGraph::HasFlag | ( | const TGraphFlag & | Flag | ) | const |
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition at line 464 of file graph.cpp.
References HasGraphFlag.
|
inline |
|
inline |
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition at line 870 of file graph.h.
References IsEdge().
Referenced by IsEdge().
bool TNEGraph::IsEdge | ( | const int & | SrcNId, |
const int & | DstNId, | ||
int & | EId, | ||
const bool & | IsDir = true |
||
) | const |
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. if an edge exists, return its edge ID in EId.
Definition at line 547 of file graph.cpp.
References edge, TNEGraph::TEdge::GetDstNId(), GetEdge(), TNEGraph::TEdge::GetId(), TNEGraph::TNode::GetInDeg(), TNEGraph::TNode::GetInEId(), GetNode(), TNEGraph::TNode::GetOutDeg(), TNEGraph::TNode::GetOutEId(), and TNEGraph::TEdge::GetSrcNId().
|
inline |
Tests whether ID NId is a node.
Definition at line 838 of file graph.h.
References THash< TKey, TDat, THashFunc >::IsKey().
Referenced by AddEdge(), AddNode(), TBPGraph::DelNode(), TBPGraph::IsEdge(), and IsOk().
bool TNEGraph::IsOk | ( | const bool & | ThrowExcept = true | ) | const |
Checks the graph data structure for internal consistency.
For each node in the graph check that its neighbors are also nodes in the graph.
Definition at line 586 of file graph.cpp.
References TStr::CStr(), EAssertR, EdgeH, ErrNotify(), THash< TKey, TDat, THashFunc >::FFirstKeyId(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::FNextKeyId(), TNEGraph::TEdge::GetDstNId(), TNEGraph::TNode::GetId(), TNEGraph::TEdge::GetId(), TNEGraph::TNode::GetInDeg(), TNEGraph::TNode::GetInEId(), TNEGraph::TNode::GetOutDeg(), TNEGraph::TNode::GetOutEId(), TNEGraph::TEdge::GetSrcNId(), TNEGraph::TNode::InEIdV, IsEdge(), IsNode(), TVec< TVal, TSizeTy >::IsSorted(), NodeH, and TNEGraph::TNode::OutEIdV.
Static constructor that loads the graph from a stream SIn and returns a pointer to it.
Definition at line 815 of file graph.h.
References TNEGraph().
|
inlinestatic |
Static constructor that returns a pointer to the graph. Call: PNEGraph Graph = TNEGraph::New().
Definition at line 809 of file graph.h.
References TNEGraph().
Referenced by GetSmallGraph().
|
inlinestatic |
Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and Edges edges.
Call: PNEGraph Graph = TNEGraph::New(Nodes, Edges).
Definition at line 813 of file graph.h.
References TNEGraph().
|
inline |
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition at line 902 of file graph.h.
References THash< TKey, TDat, THashFunc >::Gen().
Referenced by TNEGraph().
|
inline |
Saves the graph to a (binary) stream SOut.
Definition at line 807 of file graph.h.
References THash< TKey, TDat, THashFunc >::Save(), and TInt::Save().
|
private |
Definition at line 796 of file graph.h.
Referenced by AddEdge(), and operator=().
|
private |
Definition at line 796 of file graph.h.
Referenced by AddNode(), TBPGraph::AddNode(), GetMxNId(), and operator=().