SNAP Library 4.0, User Reference
2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
Undirected graph. More...
#include <graph.h>
Classes | |
class | TEdgeI |
Edge iterator. Only forward iteration (operator++) is supported. More... | |
class | TLoadTNodeInitializer |
class | TNode |
class | TNodeI |
Node iterator. Only forward iteration (operator++) is supported. More... | |
Public Types | |
typedef TUNGraph | TNet |
typedef TPt< TUNGraph > | PNet |
Public Member Functions | |
TUNGraph () | |
TUNGraph (const int &Nodes, const int &Edges) | |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. More... | |
TUNGraph (const TUNGraph &Graph) | |
TUNGraph (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... | |
TUNGraph & | operator= (const TUNGraph &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 | AddNodeUnchecked (int NId=-1) |
Adds a node of ID NId to the network, noop if the node already exists. More... | |
int | AddNode (const TNodeI &NodeI) |
Adds a node of ID NodeI.GetId() to the graph. More... | |
int | AddNode (const int &NId, const TIntV &NbrNIdV) |
Adds a node of ID NId to the graph and create edges to all nodes in vector NbrNIdV. More... | |
int | AddNode (const int &NId, const TVecPool< TInt > &Pool, const int &NIdVId) |
Adds a node of ID NId to the graph and create edges to all nodes in vector NIdVId in 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 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) |
Adds an edge between node IDs SrcNId and DstNId to the graph. More... | |
int | AddEdgeUnchecked (const int &SrcNId, const int &DstNId) |
Adds an edge between node IDs SrcNId and DstNId to the graph. More... | |
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. More... | |
int | AddEdge (const TEdgeI &EdgeI) |
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph. More... | |
void | DelEdge (const int &SrcNId, const int &DstNId) |
Deletes an edge between node IDs SrcNId and DstNId from the graph. More... | |
bool | IsEdge (const int &SrcNId, const int &DstNId) const |
Tests whether an edge between node IDs SrcNId and 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 &EId) const |
Not supported/implemented! 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 | SortNodeAdjV () |
Sorts the adjacency lists of each node. More... | |
void | Reserve (const int &Nodes, const int &Edges) |
Reserves memory for a graph of Nodes nodes and Edges edges. More... | |
void | ReserveNIdDeg (const int &NId, const int &Deg) |
Reserves memory for node ID NId having Deg 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 PUNGraph | New () |
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New(). More... | |
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 Edges edges. More... | |
static PUNGraph | Load (TSIn &SIn) |
Static constructor that loads the graph from a stream SIn and returns a pointer to it. More... | |
static PUNGraph | LoadShM (TShMIn &ShMIn) |
Static constructor that loads the graph from shared memory. More... | |
static PUNGraph | GetSmallGraph () |
Returns a small graph on 5 nodes and 5 edges. More... | |
Private Member Functions | |
TNode & | GetNode (const int &NId) |
const TNode & | GetNode (const int &NId) const |
void | LoadGraphShM (TShMIn &ShMIn) |
Private Attributes | |
TCRef | CRef |
TInt | MxNId |
TInt | NEdges |
THash< TInt, TNode > | NodeH |
Friends | |
class | TUNGraphMtx |
class | TPt< TUNGraph > |
Undirected graph.
Node IDs can be arbitrary non-negative integers. Nodes and edges have no attributes/data associated with them. There is at most one undirected edge between a pair of nodes. Self loops (one per node) are allowed but multiple (parallel) edges are not. The undirected 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<TUNGraph> TUNGraph::PNet |
typedef TUNGraph TUNGraph::TNet |
|
inlineexplicit |
Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges.
Definition at line 164 of file graph.h.
|
inline |
int TUNGraph::AddEdge | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Adds an edge between node IDs SrcNId and 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 TUNGraph have no IDs we return -1. The function aborts if SrcNId or DstNId are not nodes in the graph.
Definition at line 92 of file graph.cpp.
|
inline |
Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph.
Definition at line 265 of file graph.h.
int TUNGraph::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 at line 112 of file graph.cpp.
int TUNGraph::AddEdgeUnchecked | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Adds an edge between node IDs SrcNId and DstNId to the graph.
Returns -1. The method assumes that SrcNId and DstNId are existing nodes in the graph and adds new neighbor values at the end of their adjacency vectors. No checks are performed to verify these assumptions. Adjacency vectors must be sorted and have duplicates removed after a sequence of these operations to assure consistency of data structures.
Definition at line 103 of file graph.cpp.
int TUNGraph::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 8 of file graph.cpp.
|
inline |
int TUNGraph::AddNode | ( | const int & | NId, |
const TIntV & | NbrNIdV | ||
) |
Adds a node of ID NId to the graph and create edges to all nodes in vector NbrNIdV.
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 NbrNIdV vector do not exist. Use TUNGraph::IsOk to check that the resulting graph is consistent after the operation.
Definition at line 28 of file graph.cpp.
Adds a node of ID NId to the graph and create edges to all nodes in vector NIdVId in 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 TUNGraph::IsOk to check that the resulting graph is consistent.
Definition at line 49 of file graph.cpp.
int TUNGraph::AddNodeUnchecked | ( | int | NId = -1 | ) |
Adds a node of ID NId to the network, noop if the node already exists.
Returns -1, if the node NId already exists. Otherwise, it returns the ID of the node being added. If NId is -1, node ID is automatically assigned.
Definition at line 20 of file graph.cpp.
|
inline |
Returns an iterator referring to the first edge in the graph.
Definition at line 274 of file graph.h.
|
inline |
|
inline |
Deletes all nodes and edges from the graph.
Definition at line 294 of file graph.h.
void TUNGraph::Defrag | ( | const bool & | OnlyNodeLinks = false | ) |
void TUNGraph::DelEdge | ( | const int & | SrcNId, |
const int & | DstNId | ||
) |
Deletes an edge 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 124 of file graph.cpp.
void TUNGraph::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 67 of file graph.cpp.
|
inline |
Deletes node of ID NodeI.GetId() from the graph.
Definition at line 233 of file graph.h.
void TUNGraph::Dump | ( | FILE * | OutF = stdout | ) | const |
Print the graph in a human readable form to an output stream OutF.
Definition at line 207 of file graph.cpp.
|
inline |
Tests whether the graph is empty (has zero nodes).
Definition at line 292 of file graph.h.
|
inline |
Returns an iterator referring to the past-the-end edge in the graph.
Definition at line 276 of file graph.h.
|
inline |
int TUNGraph::GetEdges | ( | ) | const |
TEdgeI TUNGraph::GetEI | ( | const int & | EId | ) | const |
Not supported/implemented!
TUNGraph::TEdgeI TUNGraph::GetEI | ( | const int & | SrcNId, |
const int & | DstNId | ||
) | const |
Returns an iterator referring to edge (SrcNId, DstNId) in the graph.
Note that since this is an undirected graph GetEI(SrcNId, DstNId) has the same effect as GetEI(DstNId, SrcNId).
Definition at line 143 of file graph.cpp.
|
inline |
|
inline |
void TUNGraph::GetNIdV | ( | TIntV & | NIdV | ) | const |
Gets a vector IDs of all nodes in the graph.
Definition at line 153 of file graph.cpp.
|
inlineprivate |
|
inline |
Returns an interator referring to a random node in the graph.
Definition at line 287 of file graph.h.
Returns an ID of a random node in the graph.
Definition at line 285 of file graph.h.
|
static |
Returns a small graph on 5 nodes and 5 edges.
/// Graph: 3--0--4 /// /| /// 1-2 ///
Definition at line 221 of file graph.cpp.
bool TUNGraph::HasFlag | ( | const TGraphFlag & | Flag | ) | const |
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition at line 3 of file graph.cpp.
bool TUNGraph::IsEdge | ( | const int & | SrcNId, |
const int & | DstNId | ||
) | const |
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition at line 137 of file graph.cpp.
|
inline |
bool TUNGraph::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 170 of file graph.cpp.
|
inlineprivate |
Definition at line 155 of file graph.h.
|
inlinestatic |
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition at line 172 of file graph.h.
|
inlinestatic |
|
inline |
|
inline |
|
inline |
|
inline |
Sorts the adjacency lists of each node.
Definition at line 296 of file graph.h.
|
friend |