SNAP Library 2.0, User Reference
2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <bfsdfs.h>
Public Member Functions | |
TBreathFS (const PGraph &GraphPt, const bool &InitBigQ=true) | |
void | SetGraph (const PGraph &GraphPt) |
Sets the graph to be used by the BFS to GraphPt and resets the data structures. | |
int | DoBfs (const int &StartNode, const bool &FollowOut, const bool &FollowIn, const int &TargetNId=-1, const int &MxDist=TInt::Mx) |
Performs BFS from node id StartNode for at maps MxDist steps by only following in-links (parameter FollowIn = true) and/or out-links (parameter FollowOut = true). | |
int | GetNVisited () const |
Returns the number of nodes visited/reached by the BFS. | |
void | GetVisitedNIdV (TIntV &NIdV) const |
Returns the IDs of the nodes visited/reached by the BFS. | |
int | GetHops (const int &SrcNId, const int &DstNId) const |
int | GetRndPath (const int &SrcNId, const int &DstNId, TIntV &PathNIdV) const |
Public Attributes | |
PGraph | Graph |
TSnapQueue< int > | Queue |
TInt | StartNId |
TIntH | NIdDistH |
Breath-First-Search class. The class is meant for executing many BFSs over a fixed graph. This means that the class can keep the hash tables and queues initialized between different calls of the DoBfs() function.
int TBreathFS< PGraph >::DoBfs | ( | const int & | StartNode, |
const bool & | FollowOut, | ||
const bool & | FollowIn, | ||
const int & | TargetNId = -1 , |
||
const int & | MxDist = TInt::Mx |
||
) |
Performs BFS from node id StartNode for at maps MxDist steps by only following in-links (parameter FollowIn = true) and/or out-links (parameter FollowOut = true).
Definition at line 108 of file bfsdfs.h.
{ StartNId = StartNode; IAssert(Graph->IsNode(StartNId)); // const typename PGraph::TObj::TNodeI StartNodeI = Graph->GetNI(StartNode); // IAssertR(StartNodeI.GetOutDeg() > 0, TStr::Fmt("No neighbors from start node %d.", StartNode)); NIdDistH.Clr(false); NIdDistH.AddDat(StartNId, 0); Queue.Clr(false); Queue.Push(StartNId); int v, MaxDist = 0; while (! Queue.Empty()) { const int NId = Queue.Top(); Queue.Pop(); const int Dist = NIdDistH.GetDat(NId); if (Dist == MxDist) { break; } // max distance limit reached const typename PGraph::TObj::TNodeI NodeI = Graph->GetNI(NId); if (FollowOut) { // out-links for (v = 0; v < NodeI.GetOutDeg(); v++) { // out-links const int DstNId = NodeI.GetOutNId(v); if (! NIdDistH.IsKey(DstNId)) { NIdDistH.AddDat(DstNId, Dist+1); MaxDist = TMath::Mx(MaxDist, Dist+1); if (DstNId == TargetNId) { return MaxDist; } Queue.Push(DstNId); } } } if (FollowIn) { // in-links for (v = 0; v < NodeI.GetInDeg(); v++) { const int DstNId = NodeI.GetInNId(v); if (! NIdDistH.IsKey(DstNId)) { NIdDistH.AddDat(DstNId, Dist+1); MaxDist = TMath::Mx(MaxDist, Dist+1); if (DstNId == TargetNId) { return MaxDist; } Queue.Push(DstNId); } } } } return MaxDist; }
int TBreathFS< PGraph >::GetHops | ( | const int & | SrcNId, |
const int & | DstNId | ||
) | const |
Returns the shortst path distance between SrcNId and DistNId. Note you have to first call DoBFs(). SrcNId must be equal to StartNode, otherwise return value is -1.
Definition at line 148 of file bfsdfs.h.
{ TInt Dist; if (SrcNId!=StartNId) { return -1; } if (! NIdDistH.IsKeyGetDat(DstNId, Dist)) { return -1; } return Dist.Val; }
int TBreathFS< PGraph >::GetNVisited | ( | ) | const [inline] |
int TBreathFS< PGraph >::GetRndPath | ( | const int & | SrcNId, |
const int & | DstNId, | ||
TIntV & | PathNIdV | ||
) | const |
Returns a random shortest path from SrcNId to DstNId. Note you have to first call DoBFs(). SrcNId must be equal to StartNode, otherwise return value is -1.
Definition at line 156 of file bfsdfs.h.
{ PathNIdV.Clr(false); if (SrcNId!=StartNId || ! NIdDistH.IsKey(DstNId)) { return -1; } PathNIdV.Add(DstNId); TIntV CloserNIdV; int CurNId = DstNId; TInt CurDist, NextDist; while (CurNId != SrcNId) { typename PGraph::TObj::TNodeI NI = Graph->GetNI(CurNId); IAssert(NIdDistH.IsKeyGetDat(CurNId, CurDist)); CloserNIdV.Clr(false); for (int e = 0; e < NI.GetDeg(); e++) { const int Next = NI.GetNbrNId(e); IAssert(NIdDistH.IsKeyGetDat(Next, NextDist)); if (NextDist == CurDist-1) { CloserNIdV.Add(Next); } } IAssert(! CloserNIdV.Empty()); CurNId = CloserNIdV[TInt::Rnd.GetUniDevInt(CloserNIdV.Len())]; PathNIdV.Add(CurNId); } PathNIdV.Reverse(); return PathNIdV.Len()-1; }
void TBreathFS< PGraph >::GetVisitedNIdV | ( | TIntV & | NIdV | ) | const [inline] |
TSnapQueue<int> TBreathFS< PGraph >::Queue |