[graph-tool] ranking vertices by value of vertex property

Ioana K-Hulpus ioana.hulpus at gmail.com
Tue Jul 7 15:39:11 CEST 2020

I ran PageRank on my graph, and now I need to get the top-k highest scored.
Is there any faster / more straightforward way of achieving this than by
creating and then sorting a new dictionary, as in the following method?

def top_k_page_rank(g, pagerank_prop, k):
    pr_sorted_scores = sort_pagerank_scores(g, pagerank_prop)
    top_k = list(pr_sorted_scores.items())[0:k]
    return top_k

def sort_pagerank_scores(g, pagerank_prop):
    pr_scores = dict()
    for v in g.vertices():
        pr_scores[v] = g.vertex_properties[pagerank_prop][v]
    pr_sorted_scores = {k: v for k, v in sorted(pr_scores.items(),
key=lambda item: item[1], reverse=True)}
    return pr_sorted_scores


Sent from: https://nabble.skewed.de/

More information about the graph-tool mailing list