def inter_cluster_variance(clusters, graph):
    """
    The inter cluster variance, or the within-cluster sum of squares returns for all given clusters
    the sum of squared distances of each sample within that cluster to the clusters centroid.
    The sum of those squared distances is returned. For distance calculation 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 inter cluster variances for all given clusters
    """
    if len(clusters) > 0:
        result = 0
        for cluster in clusters:
            cluster_mean = graph.distance.mean(list(cluster))
            for node in cluster:
                result += graph.distance(cluster_mean, node)**2
        return result
    return float("inf")