コード例 #1
0
def girvan_newman(mat, K):
    num_vertices = mat.shape[0]

    # make a copy of matrix since we are going
    # to update it as we remove edges
    work_mat = mat.copy()

    components = get_components(mat)

    while len(components) < K:
        # compute edge betweenness (one component at a time)
        eb = np.zeros((num_vertices, num_vertices))
        for vertices in components:
            cur_mat = work_mat[vertices, :][:, vertices]
            cur_eb = edge_betweenness(cur_mat)
            #print(cur_eb)
            for i in range(len(vertices)):
                eb[vertices[i], vertices] = cur_eb[i, :]
        #print(eb)
        #print(work_mat)

        # remove edge and get components
        remove_vertices = np.where(eb == np.amax(eb))
        #print(remove_vertices)
        for item in remove_vertices:
            work_mat[item[0], item[1]] = 0
            #print(f'work_mat: {work_mat}')

        k_components = get_components(work_mat)
        #print(f'k_components: {k_components}')

        assign_list = components_to_assignment(k_components, num_vertices)

        #        print(assign_list)
        return assign_list
コード例 #2
0
def girvan_newman(mat, K):
    num_vertices = mat.shape[0]

    # make a copy of matrix since we are going
    # to update it as we remove edges
    work_mat = mat.copy()

    components = get_components(mat)

    while len(components) < K:
        # compute edge betweenness (one component at a time)
        eb = np.zeros((num_vertices, num_vertices))
        for vertices in components:
            cur_mat = work_mat[vertices, :][:, vertices]
            cur_eb = edge_betweenness(cur_mat)
            for i in range(len(vertices)):
                eb[vertices[i], vertices] = cur_eb[i, :]
        # remove edge and get components
        # YOU NEED TO FINISH THIS PART
        # Done
        max_eb = np.max(eb)
        edge = np.where(eb == max_eb)
        work_mat[edge] = 0
        components = get_components(work_mat)

        # These lines is for testing only, remove in your solution
        #components = []
        #vertices_per_component = math.ceil( num_vertices / K )
        #for i in range(K):
        #start = i * vertices_per_component
        #end = min(start+vertices_per_component, num_vertices-1)
        #components.append(np.arange(end, start, -1))

    return components_to_assignment(components, num_vertices)
コード例 #3
0
def girvan_newman(mat, K):
    num_vertices = mat.shape[0]

    # make a copy of matrix since we are going
    # to update it as we remove edges
    work_mat = mat.copy()

    components = get_components(mat)

    # if only 1 component, get_components() returns a 1D list with all vertices....convert this to 2D list for while loop to work
    if isinstance(components[0], list):
        pass
    else:
        components = [components]

    while len(components) < K:
        # print(components)
        # compute edge betweenness (one component at a time)
        eb = np.zeros((num_vertices, num_vertices))
        for vertices in components:
            cur_mat = work_mat[vertices, :][:, vertices]
            cur_eb = edge_betweenness(cur_mat)
            for i in range(len(vertices)):
                eb[vertices[i], vertices] = cur_eb[i, :]

        # find location of maximum betweeness
        edge = np.where(eb == eb.max())

        # remove that edge
        work_mat[edge[0][0], edge[0][1]] = 0
        work_mat[edge[0][1], edge[0][0]] = 0

        # get new components
        components = get_components(work_mat)

        if isinstance(components[0], list):
            pass
        else:
            components = [components]
        # print(components)

    return components_to_assignment(components, num_vertices)