<p><code>Graph::sparsifyGraph()</code> is my function. </p>

<pre><code>void Graph::sparsifyGraph(){
    for(Node&amp; nod: nodes){
    std::vector&lt;Edge&gt; edges;
    double total =0;
    for(Neighbor&amp; nb:neighbors(nod.getNodeId())){
        if(nod.getNodeId() &gt; nb.nodeId) continue;
        Edge edge(nod, getNode(nb.nodeId), nb.algebraicDist);
        total+=nb.algebraicDist;
        edges.push_back(edge);
    }

    /*const double mean = total/edges.size();
    //sort the edges according to the algebraic distance
    sort(edges.begin(), edges.end(),SortByAlgebraicDistance());
    int p = edges.size() * 0.5;
    double median =0;
    if(p%2==0){
        median = (edges[p-1].weight + edges[p].weight) *0.5;
    }else{
        median = edges[p-1].weight;
    }
    std::cout&lt;&lt;mean&lt;&lt;" "&lt;&lt;median&lt;&lt;std::endl;*/

    //lets try k-means
    using namespace mlpack::kmeans;
    using namespace mlpack::metric;
    // The dataset we are clustering.
    arma::mat data(1, edges.size());
    int col =0;
    for(Edge&amp; e: edges){
        //std::cout&lt;&lt;e&lt;&lt;std::endl;
        data(0,col) = e.weight;
        col++;
    }

    // The number of clusters we are getting.
    size_t clusters = 5;
    // The centroids will be stored in this matrix.
    // The assignments will be stored in this vector.
    arma::Col&lt;size_t&gt; assignments;

    // Initialize with the default arguments.
    //KMeans&lt;SquaredEuclideanDistance,RefinedStart, MaxVarianceNewCluster&gt; k;
    KMeans&lt;&gt; k;
    //RefinedStart k;
    k.Cluster(data, clusters, assignments);

    for(int i =0;i&lt;edges.size();i++){
        std::cout&lt;&lt;edges[i].weight&lt;&lt;" "&lt;&lt;assignments(i)&lt;&lt;std::endl;
    }
    //std::cout&lt;&lt;edges.size()&lt;&lt;" "&lt;&lt;assignments.size()&lt;&lt;std::endl;
    //exit(0);
}
}
</code></pre>

<p>This is what my Makefile look like:</p>

<pre><code> # Warnings frequently signal eventual errors:
 CXXFLAGS=-std=c++11 -g -O3 #-W -Wall -Weffc++ -Wextra -pedantic -O3

 #Includes
 INCLUDES=-I/usr/include/libxml2/ -I/usr/local/include/mlpack -I/Users/emmanueljohn/libs/NetworKit/include
 LFLAGS=-L/Users/emmanueljohn/libs/NetworKit

#Linker flags for both OS X and Linux
LDFLAGS=-lexpat -fopenmp -lmlpack #-lNetworKit 

# Generates list of object files from all the
#   source files in directory
OBJS = $(addsuffix .o, $(basename $(shell find *.cpp)))
DEPS = $(OBJS:.o=.d)

# Set executable name
EXEC = run

# Declare the phony targets
.PHONY: echo clean r clang gcc setclang setgcc vg

# Phony targets to run dependencies in order
gcc: | setgcc $(EXEC)
clang: | setclang $(EXEC)

# For use with the clang static analyzer by
#  using the environment variable for CXX
sb: | $(clean) $(EXEC)

vg: $(EXEC)
    valgrind ./$(EXEC)

# Run the executable
r:
./$(EXEC)

clean:
rm -rf $(OBJS)
rm -rf $(DEPS)
rm -rf $(EXEC)

# Phony target to use clang for compile and linking
setclang:
@echo "Setting clang"
$(eval CXX = clang++)
$(eval CXX_LINK = clang++)

# Phony target to use g++ for compile and linking
setgcc:
@echo "Setting g++"
$(eval CXX = g++-4.8)
$(eval CXX_LINK = g++-4.8)

# $&lt; refers to the first dependency
# Uses static pattern rule to keep from compiling all
#   objects every time.
$(OBJS): %.o: %.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) $(LFLAGS) -c $&lt; -o $@ 

$(DEPS): %.d: %.cpp
@echo "Generating "$@
@set -e; rm -f $@; \
  g++-4.8 -MM $(CPPFLAGS) $&lt; &gt; $@.$$$$; \
  sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' &lt; $@.$$$$ &gt; $@; \
  rm -f $@.$$$$
#g++ -o LibDemo -std=c++11 -I/home/emmanuj/libs/networkit/include -    L/home/emmanuj/libs/networkit/ LibDemo.cpp -lNetworKit -fopenmp
# $@ refers to the target
$(EXEC): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(INCLUDES) $(LFLAGS) $(LDFLAGS)

include $(DEPS)
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br>Reply to this email directly or <a href="https://github.com/mlpack/mlpack/issues/441#issuecomment-109643580">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AJ4bFCWBY41SdTGlHhzbu_EU8HfoZ411ks5oQ0aAgaJpZM4E5QzN.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
  <div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
    <link itemprop="url" href="https://github.com/mlpack/mlpack/issues/441#issuecomment-109643580"></link>
    <meta itemprop="name" content="View Issue"></meta>
  </div>
  <meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>