def intra_cluster_variance(clusters, graph):
    """
    The intra cluster variance, or the between-cluster sum of squares returns for all given clusters
    the sum of squared distances from their centroid to the overall centroid of the given graph.
    For distance calculation and determination of centroids the distance function provided by the
    given graph is used.

    :param clusters: The clusters to determine the squared sum for
    :param graph: The underlying graph that offers a distance function
    :return: Sum of intra cluster variances for all given clusters
    """
    mean = graph.distance.mean([node for cluster in clusters for node in cluster])
    result = 0
    for cluster in clusters:
        result += len(cluster) * graph.distance(graph.distance.mean(list(cluster)), mean)**2
    return result