From 6622aa3b1ec301ffae1a9fa66678c73cc09601cc Mon Sep 17 00:00:00 2001 From: "Shawn M. Emery" Date: Sat, 9 Dec 2023 23:12:40 -0700 Subject: [PATCH] SNAP should support finding the NId(s) with the minimum degree --- snap-core/alg.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/snap-core/alg.h b/snap-core/alg.h index 66c7b05d0..b0cb2d20c 100644 --- a/snap-core/alg.h +++ b/snap-core/alg.h @@ -21,6 +21,13 @@ template int GetMxInDegNId(const PGraph& Graph); /// Returns a randomly chosen node from all the nodes with the maximum out-degree. template int GetMxOutDegNId(const PGraph& Graph); +/// Returns a randomly chosen node from all the nodes with the minimum degree. +template int GetMnDegNId(const PGraph& Graph); +/// Returns a randomly chosen node from all the nodes with the minimum in-degree. +template int GetMnInDegNId(const PGraph& Graph); +/// Returns a randomly chosen node from all the nodes with the minimum out-degree. +template int GetMnOutDegNId(const PGraph& Graph); + // degree histograms /// Returns an in-degree histogram: a set of pairs (in-degree, number of nodes of such in-degree) template void GetInDegCnt(const PGraph& Graph, TIntPrV& DegToCntV); @@ -175,6 +182,42 @@ int GetMxOutDegNId(const PGraph& Graph) { return MxDegV[TInt::Rnd.GetUniDevInt(MxDegV.Len())]; } +template +int GetMnDegNId(const PGraph& Graph) { + TIntV MnDegV; + int MnDeg=INT_MAX; + for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { + if (MnDeg > NI.GetDeg()) { MnDegV.Clr(); MnDeg = NI.GetDeg(); } + if (MnDeg == NI.GetDeg()) { MnDegV.Add(NI.GetId()); } + } + EAssertR(! MnDegV.Empty(), "Input graph is empty!"); + return MnDegV[TInt::Rnd.GetUniDevInt(MnDegV.Len())]; +} + +template +int GetMnInDegNId(const PGraph& Graph) { + TIntV MnDegV; + int MnDeg=INT_MAX; + for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { + if (MnDeg > NI.GetInDeg()) { MnDegV.Clr(); MnDeg = NI.GetInDeg(); } + if (MnDeg == NI.GetInDeg()) { MnDegV.Add(NI.GetId()); } + } + EAssertR(! MnDegV.Empty(), "Input graph is empty!"); + return MnDegV[TInt::Rnd.GetUniDevInt(MnDegV.Len())]; +} + +template +int GetMnOutDegNId(const PGraph& Graph) { + TIntV MnDegV; + int MnDeg=INT_MAX; + for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { + if (MnDeg > NI.GetOutDeg()) { MnDegV.Clr(); MnDeg = NI.GetOutDeg(); } + if (MnDeg == NI.GetOutDeg()) { MnDegV.Add(NI.GetId()); } + } + EAssertR(! MnDegV.Empty(), "Input graph is empty!"); + return MnDegV[TInt::Rnd.GetUniDevInt(MnDegV.Len())]; +} + template void GetInDegCnt(const PGraph& Graph, TIntPrV& DegToCntV) { TIntH DegToCntH;