def average_shortest_path_length(G, weight=None):
 n = len(G)
 if (n == 0):
  msg = 'the null graph has no paths, thus there is no averageshortest path length'
  raise nx.NetworkXPointlessConcept(msg)
 if (n == 1):
  return 0
 if (G.is_directed() and (not nx.is_weakly_connected(G))):
  raise nx.NetworkXError('Graph is not weakly connected.')
 if ((not G.is_directed()) and (not nx.is_connected(G))):
  raise nx.NetworkXError('Graph is not connected.')
 if (weight is None):
  path_length = (lambda v: nx.single_source_shortest_path_length(G, v))
 else:
  ssdpl = nx.single_source_dijkstra_path_length
  path_length = (lambda v: ssdpl(G, v, weight=weight))
 s = sum((l for u in G for (v, l) in path_length(u)))
 return (s / (n * (n - 1)))

def average_shortest_path_length ( G , weight = None ) : n = len ( G ) if ( n == 0 ) : msg = 'the null graph has no paths, thus there is no averageshortest path length' raise nx . NetworkXPointlessConcept ( msg ) if ( n == 1 ) : return 0 if ( G . is_directed ( ) and ( not nx . is_weakly_connected ( G ) ) ) : raise nx . NetworkXError ( 'Graph is not weakly connected.' ) if ( ( not G . is_directed ( ) ) and ( not nx . is_connected ( G ) ) ) : raise nx . NetworkXError ( 'Graph is not connected.' ) if ( weight is None ) : path_length = ( lambda v : nx . single_source_shortest_path_length ( G , v ) ) else : ssdpl = nx . single_source_dijkstra_path_length path_length = ( lambda v : ssdpl ( G , v , weight = weight ) ) s = sum ( ( l for u in G for ( v , l ) in path_length ( u ) ) ) return ( s / ( n * ( n - 1 ) ) )
return the average shortest path length .