Exemple #1
0
    def main(self, filename):

        # #拿到图
        initG = commons.get_networkByFile(filename)
        max_sub_graph = commons.judge_data(initG)
        print('是否是一棵树?', nx.is_tree(max_sub_graph))
        # source_list = product_sourceList(max_sub_graph, 2)
        source_list = self.produce_source(max_sub_graph, 1)
        # print('两个节点的距离', nx.shortest_path_length(max_sub_graph, source=source_list[0], target=source_list[1]))
        infectG, T = commons.propagation1(max_sub_graph, source_list)

        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        # 将在这里进行单源测试。
        '''   第一种,就是jarden center '''
        #
        object_single = single_Source_detection.Single_source()
        reverse_node = object_single.revsitionAlgorithm_singlueSource(
            subinfectG)
        result_node = self.single_source_bound_ture(subinfectG,
                                                    reverse_node[0],
                                                    source_list[0])
        print('真实源是', source_list[0])
        print('预测源是', result_node[0])
        distance = nx.shortest_path_length(subinfectG,
                                           source=source_list[0],
                                           target=result_node[0])
        print('结果是', distance)
        return distance
Exemple #2
0
    def main(self, filename):
        # #拿到图
        # subGraph=self.get_Graph('../Propagation_subgraph/many_methods/result/chouqu.txt')

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/3regular_tree1000.txt')
        # initG = commons.get_networkByFile('../../data/4_regular_graph_3000_data.txt')

        initG = commons.get_networkByFile(filename)
        # initG = commons.get_networkByFile('../../../data/4_regular_graph_3000_data.txt')

        # initG = commons.get_networkByFile('../../../data/email-Eu-core.txt')

        max_sub_graph = commons.judge_data(initG)
        # source_list = product_sourceList(max_sub_graph, 2)
        source_list = commons.product_sourceList(max_sub_graph, 2)
        # print('两个节点的距离', nx.shortest_path_length(max_sub_graph, source=source_list[0], target=source_list[1]))
        infectG = commons.propagation1(max_sub_graph, source_list)

        # subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        single_Source_detection_object = single_Source_detection.Single_source(
        )
        '''
        底下将是所有的步骤组合操作。目前是2源的。
        1  抽取子图操作
        2  分区
        3 分别多源定位
        '''

        # 1  抽取子图操作,共有3种抽取子图操作。我们选择那3种呢?
        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        '''''' '# 2 分区,分区的太多了,我们看看那种好。' ''

        result = self.findmultiplesource(infectG, subinfectG, sourceNumber=2)

        result_source_list = []
        for community in result:
            subsubinfectG = nx.Graph()
            for edge in list(subinfectG.edges()):
                if edge[0] in community and (edge[1] in community):
                    subsubinfectG.add_edge(edge[0], edge[1])
            # 看下图连通吗。
            maxsubsubinfectG = self.judge_data(subsubinfectG)
            # 开始单源定位了。
            '''jar center'''
            source_node = single_Source_detection_object.revsitionAlgorithm_singlueSource(
                maxsubsubinfectG)
            # source_node = single_Source_detection_object.single_source_bydistance_coverage(infectG, maxsubsubinfectG)
            result_source_list.append(source_node[0])

        distance = commons.cal_distance(max_sub_graph, source_list,
                                        result_source_list)

        return distance
Exemple #3
0
    def main(self, filename):
        # #拿到图
        # subGraph=self.get_Graph('../Propagation_subgraph/many_methods/result/chouqu.txt')

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/3regular_tree1000.txt')
        initG = commons.get_networkByFile(filename)

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/4_regular_graph_3000_data.txt')

        # initG = commons.get_networkByFile('../../../data/email-Eu-core.txt')

        max_sub_graph = commons.judge_data(initG)
        # source_list = product_sourceList(max_sub_graph, 2)
        source_list = commons.product_sourceList(max_sub_graph, 2)
        # print('两个节点的距离', nx.shortest_path_length(max_sub_graph, source=source_list[0], target=source_list[1]))
        infectG = commons.propagation1(max_sub_graph, source_list)

        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        '''
        思路:
        1  借助概率p= 0,5以及不同的时间t生成大量的集合。假设知道时间吧。
        2 然后用这些集合去覆盖感染区域,尽可能让blue中点多,而红色区域少。
            这种方法还可以用来确定种子节点的。
            
        
        '''

        # 1  抽取子图操作,共有3种抽取子图操作。我们选择那3种呢?
        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        '''''' '# 2 分区,分区的太多了,我们看看那种好。' ''

        self.product_many_region(0.5, 6, infectG, subinfectG, source_list)

        partion_graph_object = Partion_graph.Partion_graph()
        result = partion_graph_object.other_k_center(infectG,
                                                     subinfectG,
                                                     source_list,
                                                     source_number=2)

        single_Source_detection_object = single_Source_detection.Single_source(
        )
        print('result', result)
        # 对每一个感染的点建图,用真实的建成一个传播子图
        result_source_list = []
        '''

            3  针对2传回来的多个区域,开始定位源点。
         '''
        for community in result:
            subsubinfectG = nx.Graph()
            for edge in list(subinfectG.edges()):
                if edge[0] in community and (edge[1] in community):
                    subsubinfectG.add_edge(edge[0], edge[1])
            # 看下图连通吗。
            maxsubsubinfectG = self.judge_data(subsubinfectG)
            # 开始单源定位了。
            '''jar center'''
            # source_node = single_Source_detection_object.revsitionAlgorithm_singlueSource(maxsubsubinfectG)
            source_node = single_Source_detection_object.single_source_bydistance_coverage(
                infectG, maxsubsubinfectG)
            #
            # source_node = single_Source_detection_object.single_source_bydistance(maxsubsubinfectG)

            result_source_list.append(source_node[0])

        distance = commons.cal_distance(max_sub_graph, source_list,
                                        result_source_list)

        return distance
Exemple #4
0
    #             subsubinfectG.add_edge(edge[0], edge[1])
    #     # 看下图连通吗。
    #     # maxsubsubinfectG = self.judge_data(subsubinfectG)
    #     # 开始单源定位了。
    #     '''jar center'''
    #     # # source_node = single_Source_detection_object.revsitionAlgorithm_singlueSource(maxsubsubinfectG)
    #     source_node = single_Source_detection_object.single_source_bydistance_coverage(infectG, subsubinfectG,
    #                                                                                    source_list)
    #     #
    #     # source_node = single_Source_detection_object.single_source_bydistance(maxsubsubinfectG)
    #
    #     result_source_list.append(source_node[0])
    #
    #
    #
    #
    single_Source_detection_object = single_Source_detection.Single_source()
    source_node = single_Source_detection_object.revsitionAlgorithm_singlueSource(
        subinfectG)

    # source_node = single_Source_detection_object.single_source_bydistance_coverage(infectG,subinfectG,source_list)

    result_source_list = []
    result_source_list.append(source_node[0])

    #需要画3种点,一个是源点,一个是传播点。一个是未传播点。

    plot_G_node_color_PPT(initG, subinfectG, source_list, result_source_list)

    # plot_G(initG)
    # plot_G_node_color(initG,[1])
    def main(self):
        # #拿到图
        # subGraph=self.get_Graph('../Propagation_subgraph/many_methods/result/chouqu.txt')

        # initG = commons.get_networkByFile('../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../data/3regular_tree1000.txt')
        initG = commons.get_networkByFile(
            '../../data/4_regular_graph_3000_data.txt')

        # initG = commons.get_networkByFile('../../../data/CA-GrQc.txt')
        # initG = commons.get_networkByFile('../../../data/4_regular_graph_3000_data.txt')

        # initG = commons.get_networkByFile('../../../data/email-Eu-core.txt')

        max_sub_graph = commons.judge_data(initG)
        # source_list = product_sourceList(max_sub_graph, 2)
        source_list = commons.product_sourceList(max_sub_graph, 2)
        # print('两个节点的距离', nx.shortest_path_length(max_sub_graph, source=source_list[0], target=source_list[1]))
        infectG = commons.propagation1(max_sub_graph, source_list)

        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        '''
        底下将是所有的步骤组合操作。目前是2源的。
        1  抽取子图操作
        2  分区
        3 分别多源定位
        '''

        # 1  抽取子图操作,共有3种抽取子图操作。我们选择那3种呢?
        subinfectG = commons.get_subGraph_true(infectG)  # 只取感染点,为2表示,真实的感染图。
        '''''' '# 2 分区,分区的太多了,我们看看那种好。' ''
        '''2.1  Different'''

        result = self.Parttion_Different_Time(infectG, sourceNumber=2)
        print('result', len(result))
        single_Source_detection_object = single_Source_detection.Single_source(
        )
        print('result', result)
        # 对每一个感染的点建图,用真实的建成一个传播子图
        result_source_list = []
        '''

            3  针对2传回来的多个区域,开始定位源点。
         '''
        for community_node, community in result:
            subsubinfectG = nx.Graph()
            for edge in list(subinfectG.edges()):
                if edge[0] in community and (edge[1] in community):
                    subsubinfectG.add_edge(edge[0], edge[1])
            # 看下图连通吗。
            maxsubsubinfectG = self.judge_data(subsubinfectG)
            # 开始单源定位了。

            source_node = single_Source_detection_object.revsitionAlgorithm_singlueSource(
                maxsubsubinfectG)
            result_source_list.append(source_node[0])

        distance = commons.cal_distance(max_sub_graph, source_list,
                                        result_source_list)

        return distance
Exemple #6
0
    def coverage_best_kcenter(self,
                              infectG,
                              subinfectG,
                              source_list,
                              source_number=2):
        # 首先需要判断是否多源。不断找源点去对这个区域。
        tempGraph = nx.Graph()
        tempGraph = subinfectG
        tempGraphNodelist = []
        for node in list(tempGraph.nodes):
            tempGraphNodelist.append(node)
        print('这个传播子图的节点个数,也是我们用来做u的备选集合的' + str(len(set(tempGraphNodelist))))
        print('这个感染区域的传播图节点个数')
        print(tempGraph.number_of_nodes())
        Alternativenodeset = list(tempGraph.nodes())  # 备选集合。
        print('tempgraph的所有点数' + str(len(Alternativenodeset)))
        minCoverlist = []
        print('在源点在' + str(source_number) + '个数的情况下')
        # print('在h为' + str(h) + '的情况下')
        # 计算图的拉普拉斯
        k = 1
        sourceNum = 2
        # while 1:
        minCoverlist = []
        print('在源点在' + str(sourceNum) + '个数的情况下')
        # print('在h为' + str(h) + '的情况下')

        # 基于k-means的方法。
        if sourceNum == 2:
            partion_region = []
            # 选择距离最远的点。
            distance_iter = nx.shortest_path_length(subinfectG)
            everynode_distance = []
            for node, node_distance in distance_iter:
                # print(node_distance)
                sort_list = sorted(node_distance.items(),
                                   key=lambda x: x[1],
                                   reverse=True)
                # print('sort_list',sort_list)
                everynode_distance.append(
                    [node, sort_list[0][0], sort_list[0][1]])
            # print('everynode_idstance',everynode_distance)
            sort_every_distance = sorted(everynode_distance,
                                         key=lambda x: x[2],
                                         reverse=True)
            # print(sort_every_distance)
            node_twolist = [[], []]
            lengthA_dict = nx.single_source_bellman_ford_path_length(
                subinfectG, sort_every_distance[0][0], weight='weight')
            lengthB_dict = nx.single_source_bellman_ford_path_length(
                subinfectG, sort_every_distance[0][1], weight='weight')
            for node in list(subinfectG.nodes):
                if lengthA_dict[node] > lengthB_dict[node]:  # 这个点离b近一些。
                    node_twolist[1].append(node)
                elif lengthA_dict[node] < lengthB_dict[node]:
                    node_twolist[0].append(node)
                else:
                    node_twolist[0].append(node)
                    node_twolist[1].append(node)
            print('len(node_twolist[0]', len(node_twolist[0]))
            print('len(node_twolist[1]', len(node_twolist[1]))
            source_distance_list = []
            new_subinfectG = nx.Graph()
            new_node_list = []
            for community in node_twolist:
                #给定两个区域构建子图,找出距离中心,然后再找出最大半径。进行h求解。
                single_Source_detection_object = single_Source_detection.Single_source(
                )

                for edge in list(subinfectG.edges()):
                    if edge[0] in community and (edge[1] in community):
                        new_subinfectG.add_edge(edge[0], edge[1])
                    # 看下图连通吗。
                maxsubsubinfectG = self.judge_data(new_subinfectG)
                source_node = single_Source_detection_object.single_source_bydistance_coverage(
                    infectG, maxsubsubinfectG)
                source_distance_list.append(source_node)
                print('source_node', source_node)
                distance = nx.shortest_path_length(new_subinfectG,
                                                   source=source_node[0])
                print('distance', distance)
                sort_list_temp = sorted(distance.items(),
                                        key=lambda x: x[1],
                                        reverse=True)
                h = sort_list_temp[0][1] - 3
                print('最大的h是多少呢?', h)
                edges = nx.bfs_edges(subinfectG,
                                     source=source_node[0],
                                     depth_limit=h)
                nodes = [source_node[0]] + [v for u, v in edges]
                new_node_list.append(nodes)

            print('len(new_node_list)', len(new_node_list[0]))
            print('len(new_node_list)', len(new_node_list[1]))
            print('我们找出的共有元素是多少来着')
            print(len([x for x in new_node_list[0] if x in new_node_list[1]]))

            return new_node_list