def test_cuttree0(self):
     G = graph()
     nations_of_the_world(G)
     
     ct = cut_tree( G )
     
     import pdb
     pdb.set_trace()
    def test_cut_tree(self):
        #set up the graph (see example on wikipedia page for Gomory-Hu tree)
        gr = graph()
        gr.add_nodes([0,1,2,3,4,5])
        gr.add_edge((0,1), wt=1)
        gr.add_edge((0,2), wt=7)
        gr.add_edge((1,3), wt=3)
        gr.add_edge((1,2), wt=1)
        gr.add_edge((1,4), wt=2)
        gr.add_edge((2,4), wt=4)
        gr.add_edge((3,4), wt=1)
        gr.add_edge((3,5), wt=6)
        gr.add_edge((4,5), wt=2)

        ct = cut_tree(gr)

        #check ct
        assert ct[(2,0)] == 8
        assert ct[(4,2)] == 6
        assert ct[(1,4)] == 7
        assert ct[(3,1)] == 6
        assert ct[(5,3)] == 8
    def test_cut_tree(self):
        #set up the graph (see example on wikipedia page for Gomory-Hu tree)
        gr = graph()
        gr.add_nodes([0, 1, 2, 3, 4, 5])
        gr.add_edge((0, 1), wt=1)
        gr.add_edge((0, 2), wt=7)
        gr.add_edge((1, 3), wt=3)
        gr.add_edge((1, 2), wt=1)
        gr.add_edge((1, 4), wt=2)
        gr.add_edge((2, 4), wt=4)
        gr.add_edge((3, 4), wt=1)
        gr.add_edge((3, 5), wt=6)
        gr.add_edge((4, 5), wt=2)

        ct = cut_tree(gr)

        #check ct
        assert ct[(2, 0)] == 8
        assert ct[(4, 2)] == 6
        assert ct[(1, 4)] == 7
        assert ct[(3, 1)] == 6
        assert ct[(5, 3)] == 8
 def test_cut_tree_with_random_graph(self):
     gr = testlib.new_graph()
     ct = cut_tree(gr)
 def test_cut_tree_with_empty_graph(self):
     gr = graph()
     ct = cut_tree(gr)
     assert ct == {}
Beispiel #6
0
    def graph_features(self):
        """Generating topological features including vertice orders for future use."""
        gr_nodes = self.gr.nodes()
        gr_edges = self.gr.edges()
        node_count = len(gr_nodes)
        edge_count = len(gr_edges) / 2.0
        average_order = 0.0
        clustering_coefficient = 0.0
        max_order = 0
        for each_node in gr_nodes:
            #for orders
            current_node_order = self.gr.node_order(each_node)
            average_order += current_node_order
            max_order = max(max_order, current_node_order)
            #now for clustering coefficient
            direct_neighbors = self.gr.neighbors(each_node)
            tmp_v_edge_count = 0.0
            tmp_r_edge_count = 0.0
            for virtual_edge in itertools.product(direct_neighbors,
                                                  direct_neighbors):
                if virtual_edge[0] != virtual_edge[1]:
                    tmp_v_edge_count += 1.0
                    if self.gr.has_edge(tuple(virtual_edge)):
                        tmp_r_edge_count += 1.0
            if tmp_v_edge_count == 0:
                clustering_coefficient += 0.0
            else:
                clustering_coefficient += (tmp_r_edge_count / tmp_v_edge_count)
        clustering_coefficient /= float(node_count)
        average_order /= float(node_count)
        #for kernel order
        cut_dict = cut_tree(self.gr)
        cut_places = set(cut_dict.values())
        how_many_kernel_orders = list(range(5))
        kernel_orders = []
        bloods = 0.0
        for kernel_tick in how_many_kernel_orders:
            if kernel_tick in cut_places:
                bloods += 1.0
            kernel_orders.append(bloods)
        #for redundant edges and missing edges
        redundant_edges = 0.0
        missing_edges = 0.0
        for each_edge in gr_edges:
            node0 = each_edge[0]
            node1 = each_edge[1]
            #find common set of nodes' neighbors
            common_set = set(self.gr.neighbors(node0)).intersection(
                set(self.gr.neighbors(node1)))
            if len(common_set) == 0:
                missing_edges += 1.0
            elif len(common_set) > 1:
                in_cell_edges = list(
                    itertools.combinations(list(common_set), 2))
                cell_judge = True
                for cell_edge in in_cell_edges:
                    if self.gr.has_edge(cell_edge):
                        cell_judge = False
                if cell_judge == False:
                    redundant_edges += 1.0
        if edge_count != 0.0:
            redundant_edges /= float(edge_count)
            missing_edges /= float(edge_count)

        #average edge lenghth
        total_length = 0.0
        for each_edge in gr_edges:
            node0 = each_edge[0]
            node1 = each_edge[1]
            total_length += sqrt(
                sum((self.nodes[node0] - self.nodes[node1])**2))
        if len(gr_edges) == 0:
            average_length = 0.0
        else:
            average_length = total_length / float(len(gr_edges))

        return [
            average_length, node_count, edge_count, average_order, max_order,
            redundant_edges, missing_edges
        ] + kernel_orders
Beispiel #7
0
def group(setN, gr, write_mark, minimum_cluster, alpha):
    ##    print gr
    ##    print write_mark
    N = len(setN)
    ##    print N
    density = zeros(N)
    for i in range(N):
        distances = 0.0
        neighbor_set = gr.neighbors(i)
        for each_node in neighbor_set:
            tmp_d = setN[i] - setN[each_node]
            distances += sqrt(inner(tmp_d, tmp_d))
        if distances == 0.0 or len(neighbor_set) == 0:
            ##            print i,N,'in the pool'
            ##            raw_input('ISB..')
            distances = 0.0
        else:
            ##            print distances,float(len(neighbor_set))
            distances = distances / float(len(neighbor_set))
        density[i] = 1.0 / pow(1 + distances, 2.0)
    density_copy = deepcopy(density)
    ##    print density
    #* 1 *
    remain_set = set(range(N))
    clusters = {}
    for i in range(N):
        if is_max(i, gr, density):
            clusters[i] = i
            remain_set.remove(i)
            density_copy[i] = -1
##    print 'remaining set',len(remain_set)
#* 2 *
    while len(remain_set) > 0:
        unlabeled_max = argmax(density_copy)
        density_copy[unlabeled_max] = -1
        #* 3 *
        neighbor_set = gr.neighbors(unlabeled_max)
        tmp_density = density[neighbor_set]
        label_index = neighbor_set[argmax(tmp_density)]
        clusters[unlabeled_max] = clusters[label_index]
        remain_set.remove(unlabeled_max)
    #algorithm 2
    #find boundary edges
    cluster_centers = clusters.values()
    ##    print len(cluster_centers),cluster_centers
    borders = []
    for each_edge in gr.edges():
        if clusters[each_edge[0]] != clusters[each_edge[1]]:
            if each_edge[0] < each_edge[1]:
                borders.append(each_edge)
    #find big clusters
    connected_groups = connected_components(gr)
    group_count = len(set(connected_groups.values()))
    big_cluster_head = []
    tmp_set_appeared = set([])
    for head, group_index in connected_groups.items():
        if group_index not in tmp_set_appeared:
            big_cluster_head.append(head)
            tmp_set_appeared.add(group_index)

    #construct edge set
    heads_tails = accessibility(gr)
    head_and_tail = {}
    for head, tail in heads_tails.items():
        if head in big_cluster_head:
            head_and_tail[connected_groups[head]] = tail

    #tresholds of the super clusters
    Gc = {}
    for head, tail in head_and_tail.items():
        tmp_tresh = 0.0
        count = 0.0
        for each_edge in itertools.combinations(tail, 2):
            if gr.has_edge(each_edge):
                count += 1.0
                tmp_tresh += abs(density[each_edge[0]] - density[each_edge[1]])
        if count == 0.0:
            ##            print('sigularity')
            Gc[head] = 0.0
        else:
            Gc[head] = alpha * tmp_tresh / count
    #* 2 *
##    print 'group,tresh',borders,Gc
    while len(borders) > 0:
        ##        print borders,'borders'
        current_border = borders.pop()
        Dab = max(density[current_border[0]], density[current_border[1]])
        Dca = density[clusters[current_border[0]]]
        Dcb = density[clusters[current_border[1]]]
        Gtresh = Gc[connected_groups[current_border[0]]]
        ##        print Dca - Dab,Dcb - Dab,Gtresh,'x'
        if connected_groups[current_border[0]] != connected_groups[
                current_border[1]]:
            raw_input('there be a problem')
        if ((Dca - Dab < Gtresh) | (Dcb - Dab < Gtresh)) == False:
            clusterA = clusters[current_border[0]]
            clusterB = clusters[current_border[1]]
            ##            print gr,'sb'
            gr.del_edge(current_border)
            tmp_borders = deepcopy(borders)
            for each_edge in tmp_borders:
                if (clusters[each_edge[0]] == clusterA
                        and clusters[each_edge[1]] == clusterB) or (
                            clusters[each_edge[1]] == clusterA
                            and clusters[each_edge[0]] == clusterB):
                    ##                    raw_input('del')
                    gr.del_edge(each_edge)
                    borders.remove(each_edge)


##    out_cast = []
##    for i in range(len(setN)):
##        if gr.neighbors(i) == 0:
##            out_cast.append(i)
##    for isolation in out_cast:
##        gr.del_node(isolation)
##        setN.remove(isolation)

##    print len(setN)
##    print write_mark
    if write_mark:
        connected_groups = connected_components(gr)
        group_count = len(set(connected_groups.values()))
        if minimum_cluster > group_count:

            grade = minimum_cluster - group_count

            from pygraph.algorithms.minmax import cut_tree
            yourcut = cut_tree(gr)
            ##            print 'cut tree',yourcut.values(),grade
            yourset = yourcut.values()
            for i in range(grade):
                print min(yourset)
                yourset.remove(min(yourset))
            max_degree = min(yourset)
            ##            print max_degree,yourset

            for edge_name, cut_degree in yourcut.items():
                if (cut_degree < max_degree + 1) and (gr.has_edge(edge_name)):
                    ##                    print edge_name
                    gr.del_edge(edge_name)
                    print 'cluster break x 1', edge_name
 def test_cut_tree_with_random_graph(self):
     gr = testlib.new_graph()
     ct = cut_tree(gr)
 def test_cut_tree_with_empty_graph(self):
     gr = graph()
     ct = cut_tree(gr)
     assert ct == {}