def wiener_index(G, weight=None):
 is_directed = G.is_directed()
 if ((is_directed and (not is_strongly_connected(G))) or ((not is_directed) and (not is_connected(G)))):
  return float('inf')
 total = sum(chaini((p.values() for (v, p) in spl(G, weight=weight))))
 return (total if is_directed else (total / 2))

def wiener_index ( G , weight = None ) : is_directed = G . is_directed ( ) if ( ( is_directed and ( not is_strongly_connected ( G ) ) ) or ( ( not is_directed ) and ( not is_connected ( G ) ) ) ) : return float ( 'inf' ) total = sum ( chaini ( ( p . values ( ) for ( v , p ) in spl ( G , weight = weight ) ) ) ) return ( total if is_directed else ( total / 2 ) )
returns the wiener index of the given graph .