Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNAP should support finding the NId(s) with the minimum degree #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions snap-core/alg.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ template <class PGraph> int GetMxInDegNId(const PGraph& Graph);
/// Returns a randomly chosen node from all the nodes with the maximum out-degree.
template <class PGraph> int GetMxOutDegNId(const PGraph& Graph);

/// Returns a randomly chosen node from all the nodes with the minimum degree.
template <class PGraph> int GetMnDegNId(const PGraph& Graph);
/// Returns a randomly chosen node from all the nodes with the minimum in-degree.
template <class PGraph> int GetMnInDegNId(const PGraph& Graph);
/// Returns a randomly chosen node from all the nodes with the minimum out-degree.
template <class PGraph> int GetMnOutDegNId(const PGraph& Graph);

// degree histograms
/// Returns an in-degree histogram: a set of pairs <code>(in-degree, number of nodes of such in-degree)</code>
template <class PGraph> void GetInDegCnt(const PGraph& Graph, TIntPrV& DegToCntV);
Expand Down Expand Up @@ -175,6 +182,42 @@ int GetMxOutDegNId(const PGraph& Graph) {
return MxDegV[TInt::Rnd.GetUniDevInt(MxDegV.Len())];
}

template <class PGraph>
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 <class PGraph>
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 <class PGraph>
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 <class PGraph>
void GetInDegCnt(const PGraph& Graph, TIntPrV& DegToCntV) {
TIntH DegToCntH;
Expand Down