python_data_scien/algorithmic_thinking2/project3/alg_project3_solution.py
def fast_closest_pair(cluster_list):
    """
    Helper function to compute the closest pair of clusters in a vertical strip
    
    Input: cluster_list is a list of clusters produced by fast_closest_pair
    horiz_center is the horizontal position of the strip's_list vertical center line
    half_width is the half the width of the strip (idx_i.e; the maximum horizontal distance
    that a cluster can lie from the center line)

    Output: tuple of the form (dist, idx1, idx2) where the centers of the clusters
    cluster_list[idx1] and cluster_list[idx2] lie in the strip and have minimum distance dist.       
    """
    len_n = len(cluster_list)
    if len_n <= 3:
        result_tuple = slow_closest_pair(cluster_list)
    else:
        idx_m = int(math.floor(len_n/2.0))
        cluster_list_l = cluster_list[:idx_m]
        cluster_list_r = cluster_list[idx_m:]
        distance_tuple_l = fast_closest_pair(cluster_list_l)
        distance_tuple_r = fast_closest_pair(cluster_list_r)
        if distance_tuple_l[0] < distance_tuple_r[0]:
            result_tuple = distance_tuple_l
        else:
            result_tuple = (distance_tuple_r[0],distance_tuple_r[1]+idx_m,distance_tuple_r[2]+idx_m)
        mid = (cluster_list[idx_m-1].horiz_center()+cluster_list[idx_m].horiz_center())/2.0
        cps_tuple = closest_pair_strip(cluster_list,mid,result_tuple[0])
        if cps_tuple[0] < result_tuple[0]:
            result_tuple = cps_tuple
    return result_tuple

    Helper function to compute the closest pair of clusters in a vertical strip
    
    Input: cluster_list is a list of clusters produced by fast_closest_pair
    horiz_center is the horizontal position of the strip's_list vertical center line
    half_width is the half the width of the strip (idx_i.e; the maximum horizontal distance
    that a cluster can lie from the center line)

    Output: tuple of the form (dist, idx1, idx2) where the centers of the clusters
    cluster_list[idx1] and cluster_list[idx2] lie in the strip and have minimum distance dist.       
    
Helper function to compute the closest pair of clusters in a vertical strip
Input: cluster_list is a list of clusters produced by fast_closest_pair
horiz_center is the horizontal position of the strip's_list vertical center line
half_width is the half the width of the strip (idx_i.e; the maximum horizontal distance
that a cluster can lie from the center line)

tuple of the form (dist, idx1, idx2) where the centers of the clusters
cluster_list[idx1] and cluster_list[idx2] lie in the strip and have minimum distance dist.
Helper function to compute the closest pair of clusters in a vertical strip Input : cluster_list is a list of clusters produced by fast_closest_pair horiz_center is the horizontal position of the strip ' s_list vertical center line half_width is the half the width of the strip ( idx_i . e ; the maximum horizontal distance that a cluster can lie from the center line ) tuple of the form ( dist idx1 idx2 ) where the centers of the clusters cluster_list [ idx1 ] and cluster_list [ idx2 ] lie in the strip and have minimum distance dist .
def fast_closest_pair(cluster_list):
    len_n = len(cluster_list)
    if len_n <= 3:
        result_tuple = slow_closest_pair(cluster_list)
    else:
        idx_m = int(math.floor(len_n/2.0))
        cluster_list_l = cluster_list[:idx_m]
        cluster_list_r = cluster_list[idx_m:]
        distance_tuple_l = fast_closest_pair(cluster_list_l)
        distance_tuple_r = fast_closest_pair(cluster_list_r)
        if distance_tuple_l[0] < distance_tuple_r[0]:
            result_tuple = distance_tuple_l
        else:
            result_tuple = (distance_tuple_r[0],distance_tuple_r[1]+idx_m,distance_tuple_r[2]+idx_m)
        mid = (cluster_list[idx_m-1].horiz_center()+cluster_list[idx_m].horiz_center())/2.0
        cps_tuple = closest_pair_strip(cluster_list,mid,result_tuple[0])
        if cps_tuple[0] < result_tuple[0]:
            result_tuple = cps_tuple
    return result_tuple
def fast_closest_pair ( cluster_list ) : len_n = len ( cluster_list ) if len_n <= 3 : result_tuple = slow_closest_pair ( cluster_list ) else : idx_m = int ( math . floor ( len_n / 2.0 ) ) cluster_list_l = cluster_list [ : idx_m ] cluster_list_r = cluster_list [ idx_m : ] distance_tuple_l = fast_closest_pair ( cluster_list_l ) distance_tuple_r = fast_closest_pair ( cluster_list_r ) if distance_tuple_l [ 0 ] < distance_tuple_r [ 0 ] : result_tuple = distance_tuple_l else : result_tuple = ( distance_tuple_r [ 0 ] , distance_tuple_r [ 1 ] + idx_m , distance_tuple_r [ 2 ] + idx_m ) mid = ( cluster_list [ idx_m - 1 ] . horiz_center ( ) + cluster_list [ idx_m ] . horiz_center ( ) ) / 2.0 cps_tuple = closest_pair_strip ( cluster_list , mid , result_tuple [ 0 ] ) if cps_tuple [ 0 ] < result_tuple [ 0 ] : result_tuple = cps_tuple return result_tuple
Helper function to compute the closest pair of clusters in a vertical strip
Input: cluster_list is a list of clusters produced by fast_closest_pair
horiz_center is the horizontal position of the strip's_list vertical center line
half_width is the half the width of the strip (idx_i.e; the maximum horizontal distance
that a cluster can lie from the center line)
Helper function to compute the closest pair of clusters in a vertical strip Input : cluster_list is a list of clusters produced by fast_closest_pair horiz_center is the horizontal position of the strip ' s_list vertical center line half_width is the half the width of the strip ( idx_i . e ; the maximum horizontal distance that a cluster can lie from the center line )
"""
    Helper function to compute the closest pair of clusters in a vertical strip
    
    Input: cluster_list is a list of clusters produced by fast_closest_pair
    horiz_center is the horizontal position of the strip's_list vertical center line
    half_width is the half the width of the strip (idx_i.e; the maximum horizontal distance
    that a cluster can lie from the center line)

    Output: tuple of the form (dist, idx1, idx2) where the centers of the clusters
    cluster_list[idx1] and cluster_list[idx2] lie in the strip and have minimum distance dist.       
    """
cluster_list