Exemplo n.º 1
0
 def sel_by_edge_criteria(self, edge_filter_dict):  ### returns a db_network object with the filtered properties 
     sub_network = nx.MultiGraph()
     filter_list = edge_filter_dict.keys()
     for node in self.nodes:
         sub_network.add_node(node)
     for edge, feat in self.graph.edges.items():
         bool_arr = []
         for fil in filter_list:
             bool_arr.append(edge_filter_dict[fil](feat[fil]))
         if np.all(bool_arr):
             sub_network.add_edge(edge[0],edge[1],**feat)
     sub_network.remove_nodes_from(list(isolates(sub_network)))
     return db_network(sub_network)
Exemplo n.º 2
0
def identify_orphaned_fuels_techs(package) -> Dict[str, str]:
    """Returns a list of fuels and technologies which are unconnected

    Returns
    -------
    dict

    """
    graph = create_graph(package)

    number_of_isolates = isolate.number_of_isolates(graph)
    logger.debug(
        "There are {} isolated nodes in the graph".format(number_of_isolates))

    isolated_nodes: Dict = defaultdict(list)

    for node_name in list(isolate.isolates(graph)):
        node_data = graph.nodes[node_name]
        isolated_nodes[node_data["type"]].append(node_name)

    return isolated_nodes
Exemplo n.º 3
0
def has_no_isolated_nodes(chain: Chain):
    graph, _ = as_nx_graph(chain)
    isolated = list(isolates(graph))
    if len(isolated) > 0:
        raise ValueError(f'{ERROR_PREFIX} Chain has isolated nodes')
    return True
Exemplo n.º 4
0
    def __call__(self, g, init_node=None):
        if not init_node:
            # current_node = sorted(g.degree, key=lambda x: x[1], reverse=True)[0][0]
            current_node = choice(list(g.nodes))
        else:
            current_node = init_node

        s = {current_node}

        while True:
            h_graph = self.remove_nodes_from_graph(g, s)
            if number_of_isolates(h_graph) == h_graph.number_of_nodes():
                return s

            is_continue = False
            desired_connected_components = [cc for cc in connected_components(h_graph) if len(cc) > 1]
            for connected_component_set in desired_connected_components:
                cc_graph = subgraph(h_graph, connected_component_set)
                if self.is_complete(cc_graph):
                    is_continue = True
                    cc_s = self.min_vertex_cover_complete(cc_graph)
                elif self.is_circle(cc_graph):
                    is_continue = True
                    cc_s = self.min_vertex_cover_circle(cc_graph)
                elif self.is_path(cc_graph):
                    is_continue = True
                    cc_s = self.min_vertex_cover_path(cc_graph)
                else:
                    cc_s = {}
                # cc_s = self(cc_graph)
                s = s.union(cc_s)

            if is_continue:
                continue

            nodes_with_degree_one = [node for node in h_graph.nodes if h_graph.degree[node] == 1]
            if nodes_with_degree_one:
                for node in nodes_with_degree_one:
                    adj_node = next(h_graph.neighbors(node))
                    # current_node = adj_node
                    s.add(adj_node)
                continue

            h_graph = self.remove_nodes_from_graph(g, s - {current_node})
            allowed_vertices = set(g.nodes) - s.union(isolates(h_graph))

            if current_node in isolates(h_graph):
                current_node = choice(list(allowed_vertices))
                s.add(current_node)
                continue

            shortest_distances = {}
            for destination_node in allowed_vertices:
                try:
                    shortest_distances[destination_node] = dijkstra_path_length(h_graph, current_node, destination_node)
                except NetworkXNoPath:
                    pass

            d = max(shortest_distances.values())

            if d > 1:
                t = [item[0] for item in shortest_distances.items() if item[1] == d - 1]

                if len(t) == 1:
                    current_node = t.pop()
                    s.add(current_node)
                    continue

                node_cc_increase = {}
                for node in t:
                    g_temp = self.remove_nodes_from_graph(h_graph, isolates(h_graph))
                    before_cc_count = number_connected_components(g_temp)

                    g_temp = self.remove_nodes_from_graph(h_graph, {node})
                    g_temp = self.remove_nodes_from_graph(g_temp, isolates(g_temp))
                    after_cc_count = number_connected_components(g_temp)
                    node_cc_increase[node] = after_cc_count - before_cc_count

                min_value = min(node_cc_increase.values())
                nodes_with_min_cc_increase = [node[0] for node in node_cc_increase.items() if node[1] == min_value]
                if len(nodes_with_min_cc_increase) == 1:
                    current_node = nodes_with_min_cc_increase.pop()

                else:
                    nodes_degree = {node: h_graph.degree[node] for node in nodes_with_min_cc_increase}
                    max_degree = max(nodes_degree.values())
                    max_degree_nodes_list = [item[0] for item in nodes_degree.items() if item[1] == max_degree]
                    current_node = min(node for node in max_degree_nodes_list)

                s.add(current_node)
                continue

            else:
                h_graph = self.remove_nodes_from_graph(g, s)
                is_min_cover = True
                for cc_graph in connected_components(h_graph):
                    cc_graph = h_graph.subgraph(cc_graph)
                    if self.is_complete(cc_graph) or self.is_path(cc_graph) or self.is_circle(cc_graph):
                        continue
                    else:
                        is_min_cover = False
                        break

                if is_min_cover:
                    return s

                nodes_degree = dict(h_graph.degree)
                max_degree = max(nodes_degree.values())
                max_degree_nodes_list = [item[0] for item in nodes_degree.items() if item[1] == max_degree]

                current_node = min(node for node in max_degree_nodes_list)
                s.add(current_node)
                continue
Exemplo n.º 5
0
    cycleEdges = []
    t = tuple()
    for e in cedges:
        # print(type(e[2]))
        if e[2] == "forward":
            t = e[0], e[1]
            cycleEdges.append(t)
        else:
            t = e[1], e[0]
            cycleEdges.append(t)
    CycleGraph.add_edges_from(cycleEdges)
    nx.draw(CycleGraph, with_labels=True)
    plt.show()
    print("----------发现环-----------")

    print("度为0的点为:", list(isolates(G)))

    degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
    print(degree_sequence)
    degreeCount = collections.Counter(degree_sequence)
    deg, cnt = zip(*degreeCount.items())

    fig, ax = plt.subplots()
    plt.bar(deg, cnt, width=0.80, color='b')

    plt.title("Degree Histogram")
    plt.ylabel("Count")
    plt.xlabel("Degree")
    ax.set_xticks([d + 0.4 for d in deg])
    ax.set_xticklabels(deg)
    plt.axes([0.4, 0.4, 0.5, 0.5])