Question: # TODO: Implement the function. The function is partially implemented below. def get _ average _ path _ length ( g , n _ samples

# TODO: Implement the function. The function is partially implemented below.
def get_average_path_length(g, n_samples=100):
"""
Get the average path length between two random nodes.
"""
# Pick two random distinct node pairs
n_nodes = g.vcount()
while True:
src = np.random.choice(n_nodes, n_samples)
trg = np.random.choice(n_nodes, n_samples)
# If all the two nodes are distinct
# (We do not want to have two nodes to be the same node, which results in the path length of 0, leading to the underestimation of the average path length)
if np.all(src != trg):
break
# Compute the average shortest path length between the sampled nodes
path_lengths =...
# Return the average path length
return np.mean(path_lengths)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!