9 template <
class PGraph>
PNGraph GetBfsTree(
const PGraph& Graph,
const int& StartNId,
const bool& FollowOut,
const bool& FollowIn);
11 template <
class PGraph>
int GetSubTreeSz(
const PGraph& Graph,
const int& StartNId,
const bool& FollowOut,
const bool& FollowIn,
int& TreeSzX,
int& TreeDepthX);
15 template <
class PGraph>
int GetNodesAtHop(
const PGraph& Graph,
const int& StartNId,
const int& Hop,
TIntV& NIdV,
const bool& IsDir=
false);
19 template <
class PGraph>
int GetNodesAtHops(
const PGraph& Graph,
const int& StartNId,
TIntPrV& HopCntV,
const bool& IsDir=
false);
26 template <
class PGraph>
int GetShortPath(
const PGraph& Graph,
const int& SrcNId,
const int& DstNId,
const bool& IsDir=
false);
32 template <
class PGraph>
int GetShortPath(
const PGraph& Graph,
const int& SrcNId,
TIntH& NIdToDistH,
const bool& IsDir=
false,
const int& MaxDist=
TInt::Mx);
40 template <
class PGraph>
int GetBfsFullDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir=
false);
44 template <
class PGraph>
double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir=
false);
48 template <
class PGraph>
double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir,
double& EffDiamX,
int& FullDiamX);
50 template <
class PGraph>
double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir,
double& EffDiamX,
int& FullDiamX,
double& AvgSPLX);
52 template <
class PGraph>
double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const TIntV& SubGraphNIdV,
const bool& IsDir,
double& EffDiamX,
int& FullDiamX);
73 template<
class PGraph>
81 TBreathFS(
const PGraph& GraphPt,
const bool& InitBigQ=
true) :
84 void SetGraph(
const PGraph& GraphPt);
86 int DoBfs(
const int& StartNode,
const bool& FollowOut,
const bool& FollowIn,
const int& TargetNId=-1,
const int& MxDist=
TInt::Mx);
93 int GetHops(
const int& SrcNId,
const int& DstNId)
const;
96 int GetRndPath(
const int& SrcNId,
const int& DstNId,
TIntV& PathNIdV)
const;
99 template<
class PGraph>
102 const int N=GraphPt->GetNodes();
103 if (Queue.Reserved() < N) { Queue.Gen(N); }
104 if (NIdDistH.GetReservedKeyIds() < N) { NIdDistH.Gen(N); }
107 template<
class PGraph>
108 int TBreathFS<PGraph>::DoBfs(
const int& StartNode,
const bool& FollowOut,
const bool& FollowIn,
const int& TargetNId,
const int& MxDist) {
109 StartNId = StartNode;
110 IAssert(Graph->IsNode(StartNId));
113 NIdDistH.Clr(
false); NIdDistH.AddDat(StartNId, 0);
114 Queue.Clr(
false); Queue.Push(StartNId);
116 while (! Queue.Empty()) {
117 const int NId = Queue.Top(); Queue.Pop();
118 const int Dist = NIdDistH.GetDat(NId);
119 if (Dist == MxDist) {
break; }
120 const typename PGraph::TObj::TNodeI NodeI = Graph->GetNI(NId);
122 for (v = 0; v < NodeI.GetOutDeg(); v++) {
123 const int DstNId = NodeI.GetOutNId(v);
124 if (! NIdDistH.IsKey(DstNId)) {
125 NIdDistH.AddDat(DstNId, Dist+1);
127 if (DstNId == TargetNId) {
return MaxDist; }
133 for (v = 0; v < NodeI.GetInDeg(); v++) {
134 const int DstNId = NodeI.GetInNId(v);
135 if (! NIdDistH.IsKey(DstNId)) {
136 NIdDistH.AddDat(DstNId, Dist+1);
138 if (DstNId == TargetNId) {
return MaxDist; }
147 template<
class PGraph>
150 if (SrcNId!=StartNId) {
return -1; }
151 if (! NIdDistH.IsKeyGetDat(DstNId, Dist)) {
return -1; }
155 template<
class PGraph>
158 if (SrcNId!=StartNId || ! NIdDistH.IsKey(DstNId)) {
return -1; }
159 PathNIdV.
Add(DstNId);
162 TInt CurDist, NextDist;
163 while (CurNId != SrcNId) {
164 typename PGraph::TObj::TNodeI NI = Graph->GetNI(CurNId);
165 IAssert(NIdDistH.IsKeyGetDat(CurNId, CurDist));
166 CloserNIdV.
Clr(
false);
167 for (
int e = 0; e < NI.GetDeg(); e++) {
168 const int Next = NI.GetNbrNId(e);
169 if (NIdDistH.IsKeyGetDat(Next, NextDist)) {
170 if (NextDist == CurDist-1) { CloserNIdV.
Add(Next); }
175 PathNIdV.
Add(CurNId);
178 return PathNIdV.
Len()-1;
185 template <
class PGraph>
186 PNGraph GetBfsTree(
const PGraph& Graph,
const int& StartNId,
const bool& FollowOut,
const bool& FollowIn) {
194 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NId);
199 for (
int e = 0; e < NI.GetInDeg(); e++) {
200 const int Prev = NI.GetInNId(e);
206 for (
int e = 0; e < NI.GetOutDeg(); e++) {
207 const int Prev = NI.GetOutNId(e);
216 template <
class PGraph>
217 int GetSubTreeSz(
const PGraph& Graph,
const int& StartNId,
const bool& FollowOut,
const bool& FollowIn,
int& TreeSz,
int& TreeDepth) {
228 template <
class PGraph>
229 int GetNodesAtHop(
const PGraph& Graph,
const int& StartNId,
const int& Hop,
TIntV& NIdV,
const bool& IsDir) {
231 BFS.
DoBfs(StartNId,
true, !IsDir, -1, Hop);
240 template <
class PGraph>
250 return HopCntV.
Len();
253 template <
class PGraph>
254 int GetShortPath(
const PGraph& Graph,
const int& SrcNId,
TIntH& NIdToDistH,
const bool& IsDir,
const int& MaxDist) {
256 BFS.
DoBfs(SrcNId,
true, ! IsDir, -1, MaxDist);
259 return NIdToDistH[NIdToDistH.
Len()-1];
262 template <
class PGraph>
263 int GetShortPath(
const PGraph& Graph,
const int& SrcNId,
const int& DstNId,
const bool& IsDir) {
266 return BFS.
GetHops(SrcNId, DstNId);
269 template <
class PGraph>
270 int GetBfsFullDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir) {
277 template <
class PGraph>
278 double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir) {
285 template <
class PGraph>
286 double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir,
double& EffDiam,
int& FullDiam) {
288 EffDiam = -1; FullDiam = -1;
289 return GetBfsEffDiam(Graph, NTestNodes, IsDir, EffDiam, FullDiam, AvgDiam);
292 template <
class PGraph>
293 double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const bool& IsDir,
double& EffDiam,
int& FullDiam,
double& AvgSPL) {
294 EffDiam = -1; FullDiam = -1; AvgSPL = -1;
300 for (
int tries = 0; tries <
TMath::Mn(NTestNodes, Graph->GetNodes()); tries++) {
301 const int NId = NodeIdV[tries];
307 double SumPathL=0, PathCnt=0;
308 for (
int i = 0; i < DistToCntH.
Len(); i++) {
310 SumPathL += DistToCntH.
GetKey(i) * DistToCntH[i];
311 PathCnt += DistToCntH[i];
315 FullDiam = DistNbrsPdfV.
Last().Key;
316 AvgSPL = SumPathL/PathCnt;
320 template <
class PGraph>
321 double GetBfsEffDiam(
const PGraph& Graph,
const int& NTestNodes,
const TIntV& SubGraphNIdV,
const bool& IsDir,
double& EffDiam,
int& FullDiam) {
330 for (
int tries = 0; tries <
TMath::Mn(NTestNodes, SubGraphNIdV.
Len()); tries++) {
331 const int NId = NodeIdV[tries];
333 for (
int i = 0; i < SubGraphNIdV.
Len(); i++) {
335 DistToCntH.
AddDat(Dist) += 1;
340 for (
int i = 0; i < DistToCntH.
Len(); i++) {
345 FullDiam = DistNbrsPdfV.
Last().Key;
static const T & Mn(const T &LVal, const T &RVal)
int GetHops(const int &SrcNId, const int &DstNId) const
int GetBfsFullDiam(const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
Returns the (approximation of the) Diameter (maximum shortest path length) of a graph (by performing ...
double GetBfsEffDiam(const PGraph &Graph, const int &NTestNodes, const bool &IsDir=false)
Returns the (approximation of the) Effective Diameter (90-th percentile of the distribution of shorte...
static const T & Mx(const T &LVal, const T &RVal)
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
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 Fo...
TSizeTy Len() const
Returns the number of elements in the vector.
TKeyDat< TInt, TFlt > TIntFltKd
TBreathFS(const PGraph &GraphPt, const bool &InitBigQ=true)
int GetNodesAtHops(const PGraph &Graph, const int &StartNId, TIntPrV &HopCntV, const bool &IsDir=false)
Returns the number of nodes at each hop distance from the starting node StartNId. ...
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
const TDat & GetDat(const TKey &Key) const
void SetGraph(const PGraph &GraphPt)
Sets the graph to be used by the BFS to GraphPt and resets the data structures.
PNGraph GetBfsTree(const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn)
Returns a directed Breadth-First-Search tree rooted at StartNId.
bool Empty() const
Tests whether the vector is empty.
int GetRndPath(const int &SrcNId, const int &DstNId, TIntV &PathNIdV) const
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
int GetNodesAtHop(const PGraph &Graph, const int &StartNId, const int &Hop, TIntV &NIdV, const bool &IsDir=false)
Finds IDs of all nodes that are at distance Hop from node StartNId.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
double CalcEffDiamPdf(const TIntFltKdV &DistNbrsPdfV, const double &Percentile)
Helper function for computing a given Percentile of a (unnormalized) probability distribution functio...
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const
const TVal & Last() const
Returns a reference to the last element of the vector.
int GetSubTreeSz(const PGraph &Graph, const int &StartNId, const bool &FollowOut, const bool &FollowIn, int &TreeSzX, int &TreeDepthX)
Returns the BFS tree size (number of nodes) and depth (number of levels) by following in-links (param...
void GetKeyV(TVec< TKey > &KeyV) const
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
int GetNVisited() const
Returns the number of nodes visited/reached by the BFS.
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
void GetVisitedNIdV(TIntV &NIdV) const
Returns the IDs of the nodes visited/reached by the BFS.
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
void Reverse()
Reverses the order of the elements in the vector.
int GetUniDevInt(const int &Range=0)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
int GetShortPath(const PGraph &Graph, const int &SrcNId, const int &DstNId, const bool &IsDir=false)
Returns the length of the shortest path from node SrcNId to node DstNId.
TDat & AddDat(const TKey &Key)
const TKey & GetKey(const int &KeyId) const
void SortByDat(const bool &Asc=true)