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 graph for multi-threaded operations. More...
#include <graphmp.h>
Classes | |
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 TNGraphMP | TNet |
typedef TPt< TNGraphMP > | PNet |
Public Member Functions | |
TNGraphMP () | |
TNGraphMP (const int &Nodes, const int &Edges) | |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. More... | |
TNGraphMP (const TNGraphMP &Graph) | |
TNGraphMP (TSIn &SIn) | |
Constructor that loads 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... | |
TNGraphMP & | operator= (const TNGraphMP &Graph) |
int | GetNodes () const |
Returns the number of nodes in the graph. More... | |
void | SetNodes (const int &Length) |
Sets the number of nodes in the graph. More... | |
int | AddNode (int NId=-1) |
Adds a node of ID NId to the graph. More... | |
int | AddNodeUnchecked (int NId=-1) |
Adds a node of ID NId to the graph without performing checks. More... | |
int | AddNode (const TNodeI &NodeId) |
Adds a node of ID NodeI.GetId() to the graph. More... | |
int | AddNode (const int &NId, const TIntV &InNIdV, const TIntV &OutNIdV) |
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV. More... | |
int | AddNode (const int &NId, const TVecPool< TInt > &Pool, const int &SrcVId, const int &DstVId) |
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV in the vector pool Pool, creates edges from the node to all nodes in vector OutNIdVin the vector pool Pool . 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 the maximum id of a any node in the graph. More... | |
int | Reserved () const |
int | GetEdges () const |
Returns the number of edges in the graph. More... | |
int | AddEdge (const int &SrcNId, const int &DstNId) |
Adds an edge from node IDs SrcNId to node DstNId to the graph. More... | |
int | AddEdgeUnchecked (const int &SrcNId, const int &DstNId) |
Adds an edge from node IDs SrcNId to node DstNId to the graph without performing checks. More... | |
int | AddEdge (const TEdgeI &EdgeI) |
Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph. More... | |
int | AddOutEdge1 (int &SrcIdx, const int &SrcNId, const int &DstNId) |
int | AddInEdge1 (int &DstIdx, const int &SrcNId, const int &DstNId) |
void | AddOutEdge2 (const int &SrcNId, const int &DstNId) |
void | AddInEdge2 (const int &SrcNId, const int &DstNId) |
void | AddNodeWithEdges (const TInt &NId, TIntV &InNIdV, TIntV &OutNIdV) |
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV. More... | |
void | DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) |
Deletes an edge from node IDs SrcNId to DstNId from the graph. More... | |
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. 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 &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... | |
void | GetNIdV (TIntV &NIdV) const |
Gets a vector IDs of all nodes 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 | ReserveNodeDegs (const int &Idx, const int &InDeg, const int &OutDeg) |
Reserves memory for node Idx having InDeg in-edges and OutDeg out-edges. More... | |
void | ReserveNIdInDeg (const int &NId, const int &InDeg) |
Reserves memory for node ID NId having InDeg in-edges. More... | |
void | ReserveNIdOutDeg (const int &NId, const int &OutDeg) |
Reserves memory for node ID NId having OutDeg out-edges. More... | |
void | SortEdges (const int &Idx, const int &InDeg, const int &OutDeg) |
Sorts in-edges and out-edges. More... | |
void | SortNodeAdjV () |
Sorts the adjacency lists of each node. 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 PNGraphMP | New () |
Static constructor that returns a pointer to the graph. Call: PNGraphMP Graph = TNGraphMP::New(). More... | |
static PNGraphMP | 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 PNGraphMP | Load (TSIn &SIn) |
Static constructor that loads the graph from a stream SIn and returns a pointer to it. More... | |
static PNGraphMP | GetSmallGraph () |
Returns a small graph on 5 nodes and 6 edges. More... | |
Private Member Functions | |
TNode & | GetNode (const int &NId) |
const TNode & | GetNode (const int &NId) const |
Private Attributes | |
TCRef | CRef |
TInt | MxNId |
THashMP< TInt, TNode > | NodeH |
Friends | |
class | TPt< TNGraphMP > |
class | TNGraphMPMtx |
Directed graph for multi-threaded operations.
Node IDs can be arbitrary non-negative integers. Nodes and edges have no attributes/data associated with them. There is at most one directed edge from one source node to a destination node. There can be an edge between the same pair of nodes in the opposite direction. Self loops (one per node) are allowed but multiple (parallel) edges are not. The directed graph 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<TNGraphMP> TNGraphMP::PNet |
typedef TNGraphMP TNGraphMP::TNet |
|
inline |
|
inlineexplicit |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition at line 134 of file graphmp.h.
References Reserve().
|
inline |
int TNGraphMP::AddEdge | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Adds an edge from node IDs SrcNId to node DstNId to the graph.
If the edge already exists return -2. If the edge was successfully added return -1. Normally the function should return an ID of the edge added but since edges in TNGraphMP have no IDs we return -1. Function aborts if SrcNId or DstNId are not nodes in the graph.
Definition at line 97 of file graphmp.cpp.
References TVec< TVal, TSizeTy >::AddSorted(), TStr::Fmt(), GetNode(), IAssertR, TNGraphMP::TNode::InNIdV, IsEdge(), IsNode(), and TNGraphMP::TNode::OutNIdV.
|
inline |
Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() to the graph.
Definition at line 221 of file graphmp.h.
References AddEdge(), TNGraphMP::TEdgeI::GetDstNId(), and TNGraphMP::TEdgeI::GetSrcNId().
Referenced by AddEdge().
int TNGraphMP::AddEdgeUnchecked | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Adds an edge from node IDs SrcNId to node DstNId to the graph without performing checks.
Definition at line 106 of file graphmp.cpp.
References TVec< TVal, TSizeTy >::Add(), GetNode(), TNGraphMP::TNode::InNIdV, and TNGraphMP::TNode::OutNIdV.
int TNGraphMP::AddInEdge1 | ( | int & | DstIdx, |
const int & | SrcNId, | ||
const int & | DstNId | ||
) |
Definition at line 253 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddKey12(), and NodeH.
void TNGraphMP::AddInEdge2 | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Definition at line 276 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::GetKeyId(), and NodeH.
int TNGraphMP::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 10 of file graphmp.cpp.
References THashMP< 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 166 of file graphmp.h.
References AddNode(), and TNGraphMP::TNodeI::GetId().
Referenced by AddNode().
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV.
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.
The operation can create inconsistent graphs when the neighboring nodes in vectors InNIdV and OutNIdV do not exist. Use TNGraphMP::IsOk to check that the resulting graph is consistent after the operation.
Definition at line 30 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddDat(), TStr::Fmt(), IAssertR, TNGraphMP::TNode::Id, TNGraphMP::TNode::InNIdV, IsNode(), TMath::Mx(), MxNId, NodeH, TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::Sort().
int TNGraphMP::AddNode | ( | const int & | NId, |
const TVecPool< TInt > & | Pool, | ||
const int & | SrcVId, | ||
const int & | DstVId | ||
) |
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV in the vector pool Pool, creates edges from the node to all nodes in vector OutNIdVin the vector pool Pool .
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.
The operation can create inconsistent graphs when the neighboring nodes stored in the Pool vector are not explicitly added to the graph. Use TNGraphMP::IsOk to check that the resulting graph is consistent.
Definition at line 50 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddDat(), TStr::Fmt(), TVec< TVal, TSizeTy >::GenExt(), TVecPool< TVal, TSizeTy >::GetValVPt(), TVecPool< TVal, TSizeTy >::GetVLen(), IAssertR, TNGraphMP::TNode::Id, TNGraphMP::TNode::InNIdV, IsNode(), TMath::Mx(), MxNId, NodeH, TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::Sort().
int TNGraphMP::AddNodeUnchecked | ( | int | NId = -1 | ) |
Adds a node of ID NId to the graph without performing checks.
Definition at line 21 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddDat(), IsNode(), TMath::Mx(), MxNId, and NodeH.
Adds a node of ID NId to the graph, creates edges to the node from all nodes in vector InNIdV, creates edges from the node to all nodes in vector OutNIdV.
Definition at line 282 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddKey13(), TInt::GetPrimHashCd(), NodeH, and Reserved().
int TNGraphMP::AddOutEdge1 | ( | int & | SrcIdx, |
const int & | SrcNId, | ||
const int & | DstNId | ||
) |
Definition at line 234 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::AddKey12(), and NodeH.
void TNGraphMP::AddOutEdge2 | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Definition at line 272 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::GetKeyId(), and NodeH.
|
inline |
Returns an iterator referring to the first edge in the graph.
Definition at line 239 of file graphmp.h.
References BegNI(), EndNI(), and TNGraphMP::TNodeI::GetOutDeg().
|
inline |
Returns an iterator referring to the first node in the graph.
Definition at line 197 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::BegI().
Referenced by BegEI(), and SortNodeAdjV().
|
inline |
Deletes all nodes and edges from the graph.
Definition at line 257 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::Clr().
void TNGraphMP::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 149 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::FFirstKeyId(), THashMP< TKey, TDat, THashFunc >::FNextKeyId(), TNGraphMP::TNode::InNIdV, NodeH, TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::Pack().
void TNGraphMP::DelEdge | ( | const int & | SrcNId, |
const int & | DstNId, | ||
const bool & | IsDir = true |
||
) |
Deletes an edge from node IDs SrcNId to 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 112 of file graphmp.cpp.
References TVec< TVal, TSizeTy >::Del(), TStr::Fmt(), GetNode(), IAssertR, TNGraphMP::TNode::InNIdV, IsNode(), TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::SearchBin().
void TNGraphMP::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 68 of file graphmp.cpp.
References TVec< TVal, TSizeTy >::Del(), TNGraphMP::TNode::GetInDeg(), TNGraphMP::TNode::GetInNId(), GetNode(), TNGraphMP::TNode::GetOutDeg(), TNGraphMP::TNode::GetOutNId(), TNGraphMP::TNode::InNIdV, NodeH, TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::SearchBin().
|
inline |
Deletes node of ID NodeI.GetId() from the graph.
Definition at line 193 of file graphmp.h.
References DelNode(), and TNGraphMP::TNode::GetId().
Referenced by DelNode().
void TNGraphMP::Dump | ( | FILE * | OutF = stdout | ) | const |
Print the graph in a human readable form to an output stream OutF.
Definition at line 206 of file graphmp.cpp.
References edge, THashMP< TKey, TDat, THashFunc >::FFirstKeyId(), THashMP< TKey, TDat, THashFunc >::FNextKeyId(), GetEdges(), TNGraphMP::TNode::GetId(), TNGraphMP::TNode::GetInDeg(), TNGraphMP::TNode::GetInNId(), GetNodes(), TNGraphMP::TNode::GetOutDeg(), TNGraphMP::TNode::GetOutNId(), and NodeH.
|
inline |
Tests whether the graph is empty (has zero nodes).
Definition at line 255 of file graphmp.h.
References GetNodes().
|
inline |
Returns an iterator referring to the past-the-end edge in the graph.
Definition at line 241 of file graphmp.h.
References EndNI().
|
inline |
Returns an iterator referring to the past-the-end node in the graph.
Definition at line 199 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::EndI().
Referenced by BegEI(), EndEI(), GetEI(), and SortNodeAdjV().
int TNGraphMP::GetEdges | ( | ) | const |
Returns the number of edges in the graph.
Definition at line 89 of file graphmp.cpp.
References THashMP< TKey, TDat, THashFunc >::FFirstKeyId(), THashMP< TKey, TDat, THashFunc >::FNextKeyId(), and NodeH.
Referenced by Dump().
TNGraphMP::TEdgeI TNGraphMP::GetEI | ( | const int & | SrcNId, |
const int & | DstNId | ||
) | const |
Returns an iterator referring to edge (SrcNId, DstNId) in the graph.
Definition at line 136 of file graphmp.cpp.
References EndNI(), GetNI(), IAssert, and TNGraphMP::TNodeI::NodeHI.
|
inline |
|
inline |
Returns an iterator referring to the node of ID NId in the graph.
Definition at line 201 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::GetI().
Referenced by GetEI(), and GetRndNI().
void TNGraphMP::GetNIdV | ( | TIntV & | NIdV | ) | const |
Gets a vector IDs of all nodes in the graph.
Definition at line 143 of file graphmp.cpp.
References TVec< TVal, TSizeTy >::Add(), THashMP< TKey, TDat, THashFunc >::FFirstKeyId(), THashMP< TKey, TDat, THashFunc >::FNextKeyId(), TVec< TVal, TSizeTy >::Gen(), THashMP< TKey, TDat, THashFunc >::GetKey(), GetNodes(), and NodeH.
|
inlineprivate |
Definition at line 129 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::GetDat().
Referenced by AddEdge(), AddEdgeUnchecked(), DelEdge(), DelNode(), IsEdge(), ReserveNIdInDeg(), and ReserveNIdOutDeg().
|
inlineprivate |
Definition at line 130 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::GetDat().
|
inline |
Returns an interator referring to a random node in the graph.
Definition at line 250 of file graphmp.h.
References GetNI(), and GetRndNId().
Returns an ID of a random node in the graph.
Definition at line 248 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::GetKey(), and THashMP< TKey, TDat, THashFunc >::GetRndKeyId().
Referenced by GetRndNI().
|
static |
Returns a small graph on 5 nodes and 6 edges.
/// Edges: 0 -> 1, 1 -> 2, 0 -> 2, 1 -> 3, 3 -> 4, 2 -> 3 ///
Definition at line 223 of file graphmp.cpp.
References New().
bool TNGraphMP::HasFlag | ( | const TGraphFlag & | Flag | ) | const |
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition at line 6 of file graphmp.cpp.
References HasGraphFlag.
bool TNGraphMP::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 at line 130 of file graphmp.cpp.
References GetNode(), IsNode(), and TNGraphMP::TNode::IsOutNId().
Referenced by AddEdge().
|
inline |
Tests whether ID NId is a node.
Definition at line 195 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::IsKey().
Referenced by AddEdge(), AddNode(), AddNodeUnchecked(), DelEdge(), IsEdge(), and IsOk().
bool TNGraphMP::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 160 of file graphmp.cpp.
References TStr::CStr(), EAssertR, ErrNotify(), THashMP< TKey, TDat, THashFunc >::FFirstKeyId(), TStr::Fmt(), THashMP< TKey, TDat, THashFunc >::FNextKeyId(), TNGraphMP::TNode::GetId(), TNGraphMP::TNode::GetInDeg(), TNGraphMP::TNode::GetInNId(), TNGraphMP::TNode::GetOutDeg(), TNGraphMP::TNode::GetOutNId(), TNGraphMP::TNode::InNIdV, IsNode(), TVec< TVal, TSizeTy >::IsSorted(), NodeH, and TNGraphMP::TNode::OutNIdV.
Static constructor that loads the graph from a stream SIn and returns a pointer to it.
Definition at line 147 of file graphmp.h.
References TNGraphMP().
|
inlinestatic |
Static constructor that returns a pointer to the graph. Call: PNGraphMP Graph = TNGraphMP::New().
Definition at line 141 of file graphmp.h.
References TNGraphMP().
Referenced by GetSmallGraph(), TSnap::ToGraphMP(), and TSnap::ToGraphMP3().
|
inlinestatic |
Static constructor that returns a pointer to the graph and reserves enough memory for Nodes nodes and Edges edges.
Call: PNGraphMP Graph = TNGraphMP::New(Nodes, Edges).
Definition at line 145 of file graphmp.h.
References TNGraphMP().
|
inline |
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition at line 259 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::Gen().
Referenced by TNGraphMP().
|
inline |
Definition at line 206 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::GetReservedKeyIds().
Referenced by AddNodeWithEdges().
|
inline |
Reserves memory for node ID NId having InDeg in-edges.
Definition at line 263 of file graphmp.h.
References GetNode(), TNGraphMP::TNode::InNIdV, and TVec< TVal, TSizeTy >::Reserve().
|
inline |
Reserves memory for node ID NId having OutDeg out-edges.
Definition at line 265 of file graphmp.h.
References GetNode(), TNGraphMP::TNode::OutNIdV, and TVec< TVal, TSizeTy >::Reserve().
|
inline |
|
inline |
Saves the graph to a (binary) stream SOut.
Definition at line 139 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::Save(), and TInt::Save().
|
inline |
Sets the number of nodes in the graph.
Definition at line 156 of file graphmp.h.
References THashMP< TKey, TDat, THashFunc >::SetLen().
|
inline |
|
inline |
Sorts the adjacency lists of each node.
Definition at line 269 of file graphmp.h.
References BegNI(), and EndNI().
|
private |
Definition at line 126 of file graphmp.h.
Referenced by AddNode(), AddNodeUnchecked(), GetMxNId(), and operator=().
Definition at line 127 of file graphmp.h.
Referenced by AddInEdge1(), AddInEdge2(), AddNode(), AddNodeUnchecked(), AddNodeWithEdges(), AddOutEdge1(), AddOutEdge2(), Defrag(), DelNode(), Dump(), GetEdges(), GetNIdV(), IsOk(), and operator=().