9 template <
class PGraph>
void GetNodeWcc(
const PGraph& Graph,
const int& NId,
TIntV& CnCom);
11 template <
class PGraph>
bool IsConnected(
const PGraph& Graph);
13 template <
class PGraph>
bool IsWeaklyConn(
const PGraph& Graph);
21 template <
class PGraph>
void GetWccs(
const PGraph& Graph,
TCnComV& CnComV);
29 template <
class PGraph>
void GetSccs(
const PGraph& Graph,
TCnComV& CnComV);
31 template <
class PGraph>
double GetMxWccSz(
const PGraph& Graph);
33 template <
class PGraph>
double GetMxSccSz(
const PGraph& Graph);
39 template <
class PGraph> PGraph
GetMxWcc(
const PGraph& Graph);
44 template <
class PGraph> PGraph
GetMxScc(
const PGraph& Graph);
49 template <
class PGraph> PGraph
GetMxBiCon(
const PGraph& Graph);
117 template <
class PGraph,
class TVisitor>
118 static void GetDfsVisitor(
const PGraph& Graph, TVisitor& Visitor);
123 template <
class PGraph,
class TVisitor>
125 const int Nodes = Graph->GetNodes();
127 int edge=0, Deg=0, U=0;
129 typename PGraph::TObj::TNodeI NI, UI;
130 for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
132 if (! ColorH.
IsKey(U)) {
134 Visitor.DiscoverNode(U);
135 Stack.
Push(
TIntTr(U, 0, Graph->GetNI(U).GetOutDeg()));
136 while (! Stack.
Empty()) {
139 typename PGraph::TObj::TNodeI UI = Graph->GetNI(U);
141 while (edge != Deg) {
142 const int V = UI.GetOutNId(edge);
143 Visitor.ExamineEdge(U, V);
144 if (! ColorH.
IsKey(V)) {
145 Visitor.TreeEdge(U, V);
149 Visitor.DiscoverNode(U);
150 UI = Graph->GetNI(U);
151 edge = 0; Deg = UI.GetOutDeg();
153 else if (ColorH.
GetDat(V) == 1) {
154 Visitor.BackEdge(U, V);
157 Visitor.FwdEdge(U, V);
161 Visitor.FinishNode(U);
189 void FwdEdge(
const int& NId1,
const int& NId2) {
215 if (!
Stack.Empty()) {
219 CnComV.
Add(NIdV); } }
228 void FwdEdge(
const int& NId1,
const int& NId2) { }
233 template <
class PGraph,
bool OnlyCount = false>
249 typename PGraph::TObj::TNodeI NI =
Graph->GetNI(NId);
252 for (
int i = 0; i < NI.GetOutDeg(); i++) {
255 if (TmRtW.
Val1 < 0) {
257 if (TmRtN.
Val2 == NId) {
258 if (! OnlyCount) { CnComV.
Add(); }
260 if (OnlyCount) { Cnt++; }
else { CnComV.
Last().
Add(W); }
265 void TreeEdge(
const int& NId1,
const int& NId2) { }
266 void BackEdge(
const int& NId1,
const int& NId2) { }
267 void FwdEdge(
const int& NId1,
const int& NId2) { }
276 template <
class PGraph>
278 typename PGraph::TObj::TNodeI NI;
281 VisitedNId.AddKey(NId);
283 while (! NIdQ.Empty()) {
284 const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
286 for (
int e = 0; e < Node.GetInDeg(); e++) {
287 const int InNId = Node.GetInNId(e);
288 if (! VisitedNId.IsKey(InNId)) {
289 NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
292 for (
int e = 0; e < Node.GetOutDeg(); e++) {
293 const int OutNId = Node.GetOutNId(e);
294 if (! VisitedNId.IsKey(OutNId)) {
295 NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
298 CnCom.
Gen(VisitedNId.Len(), 0);
299 for (
int i = 0; i < VisitedNId.Len(); i++) {
300 CnCom.
Add(VisitedNId.GetKey(i));
304 template <
class PGraph>
309 template <
class PGraph>
311 if (Graph->Empty()) {
316 typename PGraph::TObj::TNodeI NI;
318 NIdQ.
Push(Graph->BegNI().GetId());
319 while (! NIdQ.Empty()) {
320 const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
322 for (
int e = 0; e < Node.GetInDeg(); e++) {
323 const int InNId = Node.GetInNId(e);
324 if (! VisitedNId.IsKey(InNId)) { NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
327 for (
int e = 0; e < Node.GetOutDeg(); e++) {
328 const int OutNId = Node.GetOutNId(e);
329 if (! VisitedNId.IsKey(OutNId)) { NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
332 if (VisitedNId.Len() < Graph->GetNodes()) {
return false; }
336 template <
class PGraph>
341 typename PGraph::TObj::TNodeI NI;
344 for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
345 if (NI.GetDeg() == 0) { Cnt++; VisitedNId.AddKey(NI.GetId()); }
347 if (Cnt > 0) SzToCntH.AddDat(1, Cnt);
349 for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
350 if (! VisitedNId.IsKey(NI.GetId())) {
351 VisitedNId.
AddKey(NI.GetId());
352 NIdQ.Clr(
false); NIdQ.Push(NI.GetId());
354 while (! NIdQ.Empty()) {
355 const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
357 for (
int e = 0; e < Node.GetInDeg(); e++) {
358 const int InNId = Node.GetInNId(e);
359 if (! VisitedNId.IsKey(InNId)) { NIdQ.Push(InNId); VisitedNId.AddKey(InNId); }
362 for (
int e = 0; e < Node.GetOutDeg(); e++) {
363 const int OutNId = Node.GetOutNId(e);
364 if (! VisitedNId.IsKey(OutNId)) { NIdQ.Push(OutNId); VisitedNId.AddKey(OutNId); }
368 SzToCntH.AddDat(Cnt) += 1;
371 SzToCntH.GetKeyDatPrV(WccSzCnt);
375 template <
class PGraph>
377 typename PGraph::TObj::TNodeI NI;
381 CnComV.
Clr(); CcNIdV.
Gen(1);
383 for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
384 if (NI.GetDeg() == 0) {
385 const int NId = NI.GetId();
386 VisitedNId.AddKey(NId);
387 CcNIdV[0] = NId; CnComV.
Add(CcNIdV);
391 for (NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
392 const int NId = NI.GetId();
393 if (! VisitedNId.IsKey(NId)) {
394 VisitedNId.AddKey(NId);
395 NIdQ.Clr(
false); NIdQ.Push(NId);
396 CcNIdV.
Clr(); CcNIdV.Add(NId);
397 while (! NIdQ.Empty()) {
398 const typename PGraph::TObj::TNodeI Node = Graph->GetNI(NIdQ.Top()); NIdQ.Pop();
400 for (
int e = 0; e < Node.GetInDeg(); e++) {
401 const int InNId = Node.GetInNId(e);
402 if (! VisitedNId.IsKey(InNId)) {
403 NIdQ.Push(InNId); VisitedNId.
AddKey(InNId); CcNIdV.Add(InNId); }
406 for (
int e = 0; e < Node.GetOutDeg(); e++) {
407 const int OutNId = Node.GetOutNId(e);
408 if (! VisitedNId.IsKey(OutNId)) {
409 NIdQ.Push(OutNId); VisitedNId.
AddKey(OutNId); CcNIdV.Add(OutNId); }
419 template <
class PGraph>
427 template <
class PGraph>
435 template <
class PGraph>
439 if (Graph->GetNodes() == 0) {
return 0; }
440 else {
return CnComV[0].
Len() / double(Graph->GetNodes()); }
443 template <
class PGraph>
447 if (Graph->GetNodes() == 0) {
return 0; }
448 else {
return CnComV[0].
Len() / double(Graph->GetNodes()); }
451 template <
class PGraph>
455 if (CnComV.
Empty()) {
return PGraph::TObj::New(); }
456 int CcId = 0, MxSz = 0;
457 for (
int i = 0; i < CnComV.
Len(); i++) {
458 if (MxSz < CnComV[i].Len()) {
459 MxSz=CnComV[i].
Len(); CcId=i; }
461 if (CnComV[CcId].Len()==Graph->GetNodes()) {
468 template <
class PGraph>
472 if (CnComV.
Empty()) {
return PGraph::TObj::New(); }
473 int CcId = 0, MxSz = 0;
474 for (
int i = 0; i < CnComV.
Len(); i++) {
475 if (MxSz < CnComV[i].Len()) {
476 MxSz=CnComV[i].
Len(); CcId=i; }
478 if (CnComV[CcId].Len()==Graph->GetNodes()) {
485 template <
class PGraph>
488 GetBiCon(TSnap::ConvertGraph<PUNGraph, PGraph>(Graph), CnComV);
489 if (CnComV.
Empty()) {
return PGraph::TObj::New(); }
490 int CcId = 0, MxSz = 0;
491 for (
int i = 0; i < CnComV.
Len(); i++) {
492 if (MxSz < CnComV[i].Len()) {
493 MxSz=CnComV[i].
Len(); CcId=i; }
495 if (CnComV[CcId].Len()==Graph->GetNodes()) {
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
static void Dump(const TCnComV &CnComV, const TStr &Desc=TStr())
static const T & Mn(const T &LVal, const T &RVal)
TPair< TInt, TInt > TIntPr
PUNGraph GetMxBiCon(const PUNGraph &Graph, const bool &RenumberNodes)
Returns a graph representing the largest bi-connected component on an undirected Graph.
void BackEdge(const int &NId1, const int &NId2)
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
void Add(const int &NodeId)
void FwdEdge(const int &NId1, const int &NId2)
void FinishNode(const int &NId)
TBiConVisitor(const int &Nodes)
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
void GetArtPoints(const PUNGraph &Graph, TIntV &ArtNIdV)
Returns articulation points of a Graph.
double GetMxWccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest weakly connected component of a Graph.
void GetNodeWcc(const PGraph &Graph, const int &NId, TIntV &CnCom)
Returns (via output parameter CnCom) all nodes that are in the same connected component as node NId...
TArtPointVisitor(const int &Nodes)
TSizeTy Len() const
Returns the number of elements in the vector.
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
const TInt & GetRndNId() const
void GetKeyV(TVec< TKey > &KeyV) const
void Gen(const int &ExpectVals)
void GetBiConSzCnt(const PUNGraph &Graph, TIntPrV &SzCntV)
Returns a distribution of bi-connected component sizes.
int GetMinDiscTm(const int &NId1, const int &NId2) const
void GetSccs(const PGraph &Graph, TCnComV &CnComV)
Returns all strongly connected components in a Graph.
THash< TInt, TInt > ParentH
void ExamineEdge(const int &NId1, const int &NId2)
const TDat & GetDat(const TKey &Key) const
void ExamineEdge(const int &NId1, const int &NId2)
void GetSccSzCnt(const PGraph &Graph, TIntPrV &SccSzCnt)
Returns a distribution of strongly connected component sizes.
void FwdEdge(const int &NId1, const int &NId2)
void DiscoverNode(int NId)
void FinishNode(const int &NId)
void Save(TSOut &SOut) const
bool Empty() const
Tests whether the vector is empty.
TCnCom & operator=(const TCnCom &CC)
void Sort(const bool &Asc=true)
void TreeEdge(const int &NId1, const int &NId2)
static void GetDfsVisitor(const PGraph &Graph, TVisitor &Visitor)
Biconnected componetns Depth-First-Search visitor class.
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void DiscoverNode(int NId)
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
bool operator<(const TCnCom &CC) const
double GetMxSccSz(const PGraph &Graph)
Returns the fraction of nodes in the largest strongly connected component of a Graph.
void BackEdge(const int &NId1, const int &NId2)
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
THash< TInt, TIntPr > VnLowH
bool IsNIdIn(const int &NId) const
void FinishNode(const int &NId)
int AddKey(const TKey &Key)
const TVal & Last() const
Returns a reference to the last element of the vector.
const TIntV & operator()() const
int GetPrimHashCd() const
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
void TreeEdge(const int &NId1, const int &NId2)
void Get1CnCom(const PUNGraph &Graph, TCnComV &Cn1ComV)
Returns 1-components: maximal connected components of that can be disconnected from the Graph by remo...
const TInt & GetVal(const int &NIdN) const
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
void Get1CnComSzCnt(const PUNGraph &Graph, TIntPrV &SzCntV)
Distribution of sizes of 1-components, maximal number of components that can be disconnected from the...
void GetBiCon(const PUNGraph &Graph, TCnComV &BiCnComV)
Returns all bi-connected components of a Graph.
THash< TInt, TIntPr > VnLowH
void GetEdgeBridges(const PUNGraph &Graph, TIntPrV &EdgeV)
Returns bridge edges of a Graph.
bool IsWeaklyConn(const PGraph &Graph)
Tests whether the Graph is weakly connected.
void ExamineEdge(const int &NId1, const int &NId2)
void BackEdge(const int &NId1, const int &NId2)
Strongly connected componetns Depht-First-Search visitor class.
bool IsConnected(const PGraph &Graph)
Tests whether the Graph is (weakly) connected.
TSccVisitor(const PGraph &_Graph)
bool operator==(const TCnCom &CC) const
void TreeEdge(const int &NId1, const int &NId2)
const TInt & operator[](const int &NIdN) const
void Push(const TVal &Val)
Adds an element at the end of the queue.
Articulation point Depth-First-Search visitor class.
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
void Save(TSOut &SOut) const
TTriple< TInt, TInt, TInt > TIntTr
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
int GetUniDevInt(const int &Range=0)
bool IsKey(const TKey &Key) const
TCnCom(const TIntV &NodeIdV)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
void FwdEdge(const int &NId1, const int &NId2)
void DiscoverNode(int NId)
TDat & AddDat(const TKey &Key)
void GetWccSzCnt(const PGraph &Graph, TIntPrV &WccSzCnt)
Returns a distribution of weakly connected component sizes.
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
static void SaveTxt(const TCnComV &CnComV, const TStr &FNm, const TStr &Desc=TStr())
THash< TInt, TInt > ParentH
PGraph GetMxScc(const PGraph &Graph)
Returns a graph representing the largest strongly connected component on an input Graph...
THash< TInt, TIntPr > TmRtH