How can I calculate the average path length of the graph efficiently in graph-tool?
<p>I am dealing with networks of the order of 10000 vertices and I am using <a href="https://graph-tool.skewed.de/" rel="nofollow">graph-tool</a> to analyze them. One of the things that I want to calculate for each of these graphs is the average path length which is defined as the average of the shortest distances over all the pairs of nodes in the graph. So I tried this:</p>

<pre><code>ave_path_length = 0
tot = 0

for v1 in G.vertices():
    print(v1)
    for v2 in G.vertices():
        if v1 != v2 :
            tot += 1
            ave_path_length += gt.shortest_distance(G, v1, v2)

ave_path_length /= tot
</code></pre>

<p>However, this is taking eternity. Is there a better method to achieve the task? Thanks in advance.</p>