Discussion:
[igraph] Sorting vertices by degree with igraph C library
Vincent Beugnet
2018-03-22 19:56:43 UTC
Permalink
_______________________________________________
igraph-help mailing list
igraph-***@nongnu.org
https://lists.nongnu.org/mailman/listinfo/igraph-help
Szabolcs Horvát
2018-03-22 21:36:17 UTC
Permalink
Hello Vincent,

Normally you would have to program this yourself. E.g. you can generate a
vector of increasing integers (vertex indices), write a custom comparison
function that compares v1 and v2 through degrees[v1] and degrees[v2], then
use this to sort the indices (e.g. through the qsort() C standard library
function, or much more easily with std::sort() in C++)

Luckily, this is already done in igraph, though not documented. The
function is igraph_vector_qsort_ind() You'll find its description in the
vector.pmt file.
Hello,
I'd like to sort all vertices from a graph (undirected) by decreasing
degree and print their ids with this order. But as the vector in
igraph_degree is not related with the graph,
The degree vector is related with the graph in that the degree of the nth
vertex is the nth entry in the vector.
I don't know how to get the ordered ids. Can someone help me ?
Thanks
P.S: I'm using the C library
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
Vincent Beugnet
2018-03-24 14:48:29 UTC
Permalink
Thank you very much !

Last question: is there a stable sort already implemented in igraph ?
Szabolcs Horvát
2018-03-24 16:26:07 UTC
Permalink
To give a concrete example, you could sort an igraph vector 'vec' using

std::stable_sort(vec.stor_begin, vec.stor_end);

To stick to the documented API (might be a good idea), you can use

std::stable_sort(VECTOR(vec), VECTOR(vec) + igraph_vector_size(&vec));

http://en.cppreference.com/w/cpp/algorithm/stable_sort

You don't need to use any other C++ features in your program if you don't
like C++. You can keep it as C. Compiling as C++ will only impose minor
restrictions.
I do not know. I believe that the documented ones in igraph are not
stable.
Personally, I would just use std::stable_sort() from the C++ standard
library. Is there any reason why you don't want to use that, and want
something from igraph instead?
Post by Vincent Beugnet
Thank you very much !
Last question: is there a stable sort already implemented in igraph ?
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
Loading...