示例#1
0
def maximize_disconnected_pairs(G, k):
    tmp = set(G)
    nodes = tmp.copy()

    node = next(iter(tmp))
    tmp.discard(node)

    subgraph = nx.subgraph_view(G, filter_node=lambda n: n in tmp)
    connectivity = comb(
        len(tmp), k) - connectivity_metrics.pairwise_connectivity(subgraph)

    vertices = [node]
    maximum = connectivity

    while tmp:
        node = next(iter(tmp))

        tmp.discard(node)
        nodes.discard(node)

        subgraph = nx.subgraph_view(G, filter_node=lambda n: n in nodes)
        connectivity = comb(
            len(tmp), k) - connectivity_metrics.pairwise_connectivity(subgraph)

        if connectivity > maximum:
            vertices = [node]
            maximum = connectivity
        elif connectivity == maximum:
            vertices.append(node)

        nodes.add(node)

    return vertices
示例#2
0
def minimize_pairwise_connectivity(G, S0):
    tmp = S0.copy()
    nodes = tmp.copy()

    node = next(iter(tmp))
    tmp.discard(node)

    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in tmp)
    connectivity = connectivity_metrics.pairwise_connectivity(subgraph)

    vertices = [node]
    minimum = connectivity

    while tmp:
        node = next(iter(tmp))

        tmp.discard(node)
        nodes.discard(node)

        subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in nodes)
        connectivity = connectivity_metrics.pairwise_connectivity(subgraph)

        if connectivity < minimum:
            vertices = [node]
            minimum = connectivity
        elif connectivity == minimum:
            vertices.append(node)

        nodes.add(node)

    return vertices
示例#3
0
    def interlayer(self, l1, l2=None):
        """return interlayer network for given layer (optional to other layer)"""
        if not isinstance(l1, Iterable) and len(self.aspects) == 2:
            l1 = (l1,)
        else:
            l1 = tuple(l1)

        if l2 is not None:
            if not isinstance(l2, Iterable) and len(self.aspects) == 2:
                l2 = (l2,)
            else:
                l2 = tuple(l2)
            interlayer_view = nx.subgraph_view(self, filter_node=lambda n: n[1:] == l1 or n[1:] == l2,
                                               filter_edge=lambda n1, n2: n1[1:] != n2[1:])
        else:
            interlayer_view = nx.subgraph_view(self, filter_edge=lambda n1, n2: n1[1:] == l1 and n1[1:] != n2[1:])

        interlayer_view.aspects = tuple({} for _ in self.aspects)
        for n in interlayer_view.nodes:
            interlayer_view.aspects[0][n[0]] = self.aspects[0][n[0]]
        if l2 is None:
            for av, a in zip(interlayer_view.aspects[1:], self.aspects[1:]):
                av.update(a)
        else:
            for i in range(1, len(self.aspects)):
                interlayer_view.aspects[i][l1[i-1]] = self.aspects[i][l1[i-1]]
                interlayer_view.aspects[i][l2[i-1]] = self.aspects[i][l2[i-1]]
        return interlayer_view
示例#4
0
 def partition(G, sc_weights):
     if len(G) <= sc_len:
         sc_weights += [G.size(weight='weight')]
         return list(G.nodes)
     else:
         a_nodes, b_nodes = kl(G,
                               partition=None,
                               weight='weight',
                               seed=42,
                               max_iter=256)
         a = nx.subgraph_view(G, filter_node=lambda x: x in a_nodes)
         b = nx.subgraph_view(G, filter_node=lambda x: x in b_nodes)
         return partition(a, sc_weights) + partition(b, sc_weights)
示例#5
0
def get_edge_filtered_subgraph_view(
        graph: nx.Graph, query_params: "GraphQueryParams") -> nx.Graph:
    """
    Return a subgraph view of input graph according to edge values specified in
    the query_dict. Nodes of degree 0 after filtering are removed.
    """
    def filter_edge_by_equality(node_start, node_stop):
        res = graph[node_start][node_stop].get(query_params.label) \
            == query_params.value
        return res

    def filter_edge_by_inclusion(node_start, node_stop):
        res = query_params.value in \
            graph[node_start][node_stop].get(query_params.label, [])
        return res

    if query_params.query_type == 'equality':
        filter_edge = filter_edge_by_equality

    elif query_params.query_type == 'inclusion':
        filter_edge = filter_edge_by_inclusion

    res = nx.subgraph_view(graph, filter_edge=filter_edge)

    # Keep only nodes of degree > 0 after edge filtering
    subgraph_node_names = []
    for node_name in res.nodes:
        if nx.degree(res, node_name) > 0:
            subgraph_node_names.append(node_name)
    res = nx.subgraph(res, subgraph_node_names)
    return res
def get_participants(network: nx.DiGraph) -> NodeDataView:
    def filter_participant(n):
        if isinstance(network.nodes[n]["item"], Participant):
            return True
        return False
    view = nx.subgraph_view(network, filter_node=filter_participant)
    return view.nodes(data="item")
示例#7
0
def graph_layout(edge_lists: List[List[Dict[str, int]]], height_px, width_px):
    # lay out connected components, in bounding boxes. then offset
    # noinspection PyTypeChecker

    g: nx.DiGraph = nx.DiGraph()
    for edge_list in edge_lists:
        for edge in edge_list:
            g.add_edge(edge["source"], edge["target"])

    # noinspection PyTypeChecker
    components_layouts = [
        connected_component_layout(
            nx.subgraph_view(
                g, filter_node=lambda vertex: vertex in component_vertices))
        for component_vertices in nx.weakly_connected_components(g)
    ]

    positions = dict()
    corner = np.array([-float(height_px) / 2, -float(width_px) / 2])
    running_y = 0.0
    for component_pos, geom in components_layouts:
        running_y = max(running_y, geom[1])
        for node in component_pos:
            positions[node] = component_pos[node] + corner
        corner += np.array([geom[0] + 1.0, 0])
        if corner[0] > 20.0:
            corner[0] = 0
            corner[1] += running_y
            running_y = 0.0
    return positions
示例#8
0
 def filter_view(self, filter_node=nx.classes.filters.no_filter, filter_edge=nx.classes.filters.no_filter):
     sg = nx.subgraph_view(self, filter_node=filter_node, filter_edge=filter_edge)
     sg.aspects = tuple({} for _ in self.aspects)
     for n in sg.nodes:
         for i, ni in enumerate(n):
             sg.aspects[i][ni] = self.aspects[i][ni]
     return sg
示例#9
0
def get_pairwise_connectivity(exclude=None):
    if exclude is None:
        exclude = {}

    S = set(exclude)
    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S)
    return connectivity_metric.pairwise_connectivity(subgraph)
def connected_components(exclude=None):
    if exclude is None:
        exclude = {}

    S = set(exclude)
    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S)
    return nx.number_connected_components(subgraph)
示例#11
0
def get_zero_weight_subgraph(graph, cost_weight):
    """
  Return view of graph with edges whose cost is zero
  """
    def filter_edge(n1, n2, k):
        return graph.edges[n1, n2, k][cost_weight] == 0

    return nx.subgraph_view(graph, filter_edge=filter_edge)
示例#12
0
def get_critical_nodes_greedy(G, k):
    S = cnp.greedy_cnp(G, k)
    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S)

    fitness = connectivity_metrics.pairwise_connectivity(subgraph)
    print(fitness)

    return fitness
示例#13
0
文件: util.py 项目: zebulon2/indra
def get_subgraph(g, edge_filter_func):
    """Get a subgraph of original graph filtered by a provided function."""
    logger.info('Getting subgraph with %s function' % edge_filter_func)
    view = nx.subgraph_view(g,
                            filter_edge=functools.partial(edge_filter_func, g))
    # Copying to get a graph object instead of view
    new_g = view.copy()
    return new_g
示例#14
0
def get_critical_nodes_ga(G, k):
    S, _ = cnp.genetic_algorithm(G, k)
    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S)

    fitness = connectivity_metrics.pairwise_connectivity(subgraph)
    print(fitness)

    return fitness
示例#15
0
def get_participants(network) -> Dict[int, Participant]:
    def filter_participant(n):
        if isinstance(network.nodes[n]["item"], Participant):
            return True
        return False

    view = nx.subgraph_view(network, filter_node=filter_participant)
    return view.nodes(data="item")
示例#16
0
def get_edges_by_type(network, edge_type_selection):
    def filter_by_type(n1, n2):
        if network.edges[(n1, n2)]["type"] == edge_type_selection:
            return True
        return False

    view = nx.subgraph_view(network, filter_edge=filter_by_type)
    return view.edges()
示例#17
0
    def find_substrate_path(self, copied_substrate, vnr, embedding_s_nodes):
        embedding_s_paths = {}
        directed_copied_substrate = copied_substrate.net.to_directed()

        # mapping the virtual nodes and substrate_net nodes
        for src_v_node, dst_v_node, edge_data in vnr.net.edges(data=True):
            v_link = (src_v_node, dst_v_node)
            src_s_node = embedding_s_nodes[src_v_node][0]
            dst_s_node = embedding_s_nodes[dst_v_node][0]
            v_bandwidth_demand = edge_data['bandwidth']

            if src_s_node == dst_s_node:
                s_links_in_path = []
                embedding_s_paths[v_link] = (s_links_in_path, v_bandwidth_demand)
            else:
                subnet = nx.subgraph_view(
                    copied_substrate.net,
                    filter_edge=lambda node_1_id, node_2_id: \
                        True if copied_substrate.net.edges[(node_1_id, node_2_id)][
                                    'bandwidth'] >= v_bandwidth_demand else False
                )

                # Just for assertion
                # for u, v, a in subnet.edges(data=True):
                #     assert a["bandwidth"] >= v_bandwidth_demand

                if len(subnet.edges) == 0 or not nx.has_path(subnet, source=src_s_node, target=dst_s_node):
                    self.num_link_embedding_fails += 1
                    msg = "VNR {0} REJECTED ({1}): 'no suitable LINK for bandwidth demand: {2} {3}".format(
                        vnr.id, self.num_link_embedding_fails, v_bandwidth_demand, vnr
                    )
                    self.logger.info("{0} {1}".format(utils.step_prefix(self.time_step), msg))
                    return None

                MAX_K = 1

                # shortest_s_path = utils.k_shortest_paths(subnet, source=src_s_node, target=dst_s_node, k=MAX_K)[0]
                # https://networkx.org/documentation/stable//reference/algorithms/generated/networkx.algorithms.flow.shortest_augmenting_path.html
                residual_network = shortest_augmenting_path(directed_copied_substrate, src_s_node, dst_s_node,
                                                            capacity='bandwidth',
                                                            cutoff=v_bandwidth_demand)
                s_links_in_path = []
                path = []
                for src_r_node, dst_r_node, r_edge_data in residual_network.edges(data=True):
                    if r_edge_data['flow'] > 0:
                        s_links_in_path.append((src_r_node, dst_r_node))

                # s_links_in_path = []
                # for node_idx in range(len(shortest_s_path) - 1):
                #     s_links_in_path.append((shortest_s_path[node_idx], shortest_s_path[node_idx + 1]))

                for s_link in s_links_in_path:
                    # assert copied_substrate.net.edges[s_link]['bandwidth'] >= v_bandwidth_demand
                    copied_substrate.net.edges[s_link]['bandwidth'] -= v_bandwidth_demand

                embedding_s_paths[v_link] = (s_links_in_path, v_bandwidth_demand)

        return embedding_s_paths
示例#18
0
文件: graph.py 项目: mbroadst/mongo
    def get_direct_nonprivate_graph(self):
        """Get a graph view of direct nonprivate edges."""

        def filter_direct_nonprivate_edges(n1, n2):
            return (self[n1][n2].get(EdgeProps.direct.name) and
                    (self[n1][n2].get(EdgeProps.visibility.name) == self.get_deptype('Public') or
                     self[n1][n2].get(EdgeProps.visibility.name) == self.get_deptype('Interface')))

        return networkx.subgraph_view(self, filter_edge=filter_direct_nonprivate_edges)
示例#19
0
    def collapsed_view(self) -> nx.DiGraph:
        outer_nodes = set()
        for terminal_uuid in self._parsed_artifact_uuids:
            outer_nodes |= self.get_outer_provenance_nodes(terminal_uuid)

        def n_filter(node):
            return node in outer_nodes

        return nx.subgraph_view(self.dag, filter_node=n_filter)
示例#20
0
def get_proposals(network, status: ProposalStatus = None):
    def filter_proposal(n):
        if isinstance(network.nodes[n]["item"], Proposal):
            if status:
                return network.nodes[n]["item"].status == status
            return True
        return False

    view = nx.subgraph_view(network, filter_node=filter_proposal)
    return view.nodes(data="item")
示例#21
0
文件: graph.py 项目: mbroadst/mongo
    def get_node_tree(self, node):
        """Get a tree with the passed node as the single root."""

        direct_nonprivate_graph = self.get_direct_nonprivate_graph()
        substree_set = networkx.descendants(direct_nonprivate_graph, node)

        def subtree(n1):
            return n1 in substree_set or n1 == node

        return networkx.subgraph_view(direct_nonprivate_graph, filter_node=subtree)
示例#22
0
def get_filtered_graph(graph, filter_node=None, filter_edge=None):
    if filter_node:
        return nx.subgraph_view(graph, filter_node=filter_node)
    elif filter_edge:
        return nx.restricted_view(graph, [],
                                  [(n1, n2)
                                   for (n1, n2, attr) in graph.edges(data=True)
                                   if not filter_edge((n1, n2, attr))])
    else:
        return graph
示例#23
0
def decompose_into_chains(problem_instance: dimod.BinaryQuadraticModel):
    problem_graph = problem_instance.to_networkx_graph()
    for log_qb in problem_graph:
        problem_graph.add_node(log_qb, endnode=False)
    problem_size = len(problem_graph)
    color_sets = find_edge_coloring(problem_graph)
    color_sets = sorted(color_sets,
                        key=lambda color_set: len(color_set),
                        reverse=True)
    for log_qb0, log_qb1 in problem_graph.edges():
        if frozenset((log_qb0, log_qb1)) in color_sets[0]:
            color0 = problem_graph[log_qb0][log_qb1]['color']
            break
    for log_qb0, log_qb1 in problem_graph.edges():
        if frozenset((log_qb0, log_qb1)) in color_sets[1]:
            color1 = problem_graph[log_qb0][log_qb1]['color']
            break

    def filter_edge(log_qb0, log_qb1) -> bool:
        if problem_graph[log_qb0][log_qb1]['color'] == color0:
            return True
        elif problem_graph[log_qb0][log_qb1]['color'] == color1:
            return True
        else:
            return False

    twocolor_graph = nx.subgraph_view(problem_graph, filter_edge=filter_edge)
    components = [
        twocolor_graph.subgraph(c).copy()
        for c in nx.connected_components(twocolor_graph)
    ]
    chains, loops = [], []
    for component in components:
        deg = 3
        for node in component:
            if component.degree(node) < deg:
                node0 = node
                deg = component.degree(node)
        for neighbor in component.neighbors(node0):
            node1 = neighbor
            break
        lst = [node0, node1]
        for _ in range(len(component) - 2):
            for neighbor in component.neighbors(node1):
                if neighbor is not node0:
                    node0 = node1
                    node1 = neighbor
                    lst.append(node1)
                    break
        if deg == 1:
            chains.append(lst)
        elif deg == 2:
            loops.append(lst)

    return chains, loops
示例#24
0
    def processes_subgraph_view(self) -> nx.DiGraph:
        """Returns the subgraph view of the :doc:`CBRGraph <cbrg>` including only its
        processes.

        :return: The processes' subgraph view from the :doc:`CBRGraph <cbrg>`
        :rtype: networkx.classes.digraph.DiGraph
        """
        def filter_process(node):
            return self.g.nodes[node]["type"] == "cbp"

        return nx.subgraph_view(self.g, filter_node=filter_process)
示例#25
0
def get_edges_by_type(network: nx.DiGraph,
                      edge_type_selection: str,
                      participant_idx: int = None):
    def filter_by_type(n1, n2):
        if (participant_idx is None or n1 == participant_idx
            ) and network.edges[(n1, n2)]["type"] == edge_type_selection:
            return True
        return False

    view = nx.subgraph_view(network, filter_edge=filter_by_type)
    return view.edges()
示例#26
0
文件: claim.py 项目: JoshC8C7/AFCProj
    def extract_subclaims(self):
        # Convert graphviz to networkx.
        G = nx.nx_pydot.from_pydot(graph_from_dot_data(self.graph.source)[0])
        claims_list = []
        subtrees = []

        # Find the subclaim roots - the verbs that the conjunction of implies the documents root.
        subtree_roots = list(p[0] for p in G.in_edges(
            nbunch=self.argID(self.doc[:])))  # Store the subclaim graph roots
        H = nx.subgraph_view(
            G, filter_node=(lambda n: n != self.argID(self.doc[:]))
        )  # Create a view without the overall text/main root.

        # After removing the root, should have 1+ connected components. If a cycle is detected when trying to find them,
        # remove the last edge that caused it (this rarely happens in practice).
        cycling = True
        while cycling:
            try:
                # Create the subclaim graphs - once detaching the whole-text root these are connected components
                subtrees = [
                    H.subgraph(c).copy()
                    for c in nx.weakly_connected_components(H)
                ]
            except nx.HasACycle:
                G.remove_edge(nx.find_cycle(G)[-1])
            else:
                cycling = False

        # Create a Claim object for each component.
        for subclaim in subtrees:

            # take all roots that form this subclaim (could be multiple if they're connected - this makes the 'tree' not
            # strictly a 'tree').
            rel_roots = list(filter(lambda x: x in subclaim, subtree_roots))
            removed_edges, removed_nodes = [], []

            # Outbound edges from roots are nearly always erroneous, so they are removed as soon as possible:
            # Also remove any cases of nodes that have entirely dashed input.
            for j in rel_roots:
                for i in subclaim.out_edges(j):
                    removed_edges.append((i[0], i[1]))

                for i2 in subclaim.nodes():
                    if len(subclaim.in_edges(i2)) and all(
                            x[2].get('style', '') == 'dotted'
                            for x in subclaim.in_edges(i2, data=True)):
                        removed_nodes.append(i2)
            subclaim.remove_nodes_from(removed_nodes)
            subclaim.remove_edges_from(removed_edges)
            claims_list.append(Claim(self, subclaim, rel_roots))

        return claims_list
示例#27
0
文件: state.py 项目: Jc2k/distribd
    def get_tags(self, repository):
        def _filter(node):
            n = self.graph.nodes[node]
            if n[ATTR_TYPE] != TYPE_TAG:
                return False
            if n[ATTR_REPOSITORY] != repository:
                return False
            return True

        tags = subgraph_view(self.graph, _filter)
        resolved_tags = [tags.nodes[tag][ATTR_TAG] for tag in tags.nodes]
        if len(resolved_tags) == 0:
            raise KeyError()
        return resolved_tags
示例#28
0
文件: cnp.py 项目: Eliezer-Beczi/CNDP
def _update(G, best_S, P, alpha):
    subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in best_S)
    metric = connectivity_metric.pairwise_connectivity(subgraph)
    avg = 0

    for S in P:
        avg += len(S.intersection(best_S))

    avg /= len(P)

    try:
        gamma = (alpha * metric) / avg
    except:
        gamma = 1

    return gamma
示例#29
0
def g(G, partition_list, args):
    result = 0

    partition_size = default_size
    if args.weighted_size:
        partition_size = weighted_size

    for p in partition_list:
        n_partitions = len(partition_list)
        p_view = nx.subgraph_view(G, filter_node=lambda n: n in p)
        p_view = p_view.to_undirected()

        cost = num_edges_in_partition(G, p) - \
            partition_cost(args, G, n_partitions, partition_size(args, G, p), partition_size(args, G, G))
        result += cost

    return result
示例#30
0
文件: utils.py 项目: link-kut/or
def find_all_s_paths_2(copied_substrate, embedding_s_nodes, vnr):
    all_s_paths = {}

    # 각 v_link 당 가능한 모든 s_path (set of s_link) 구성하여 all_s_paths에 저장
    for src_v_node, dst_v_node, edge_data in vnr.net.edges(data=True):
        v_link = (src_v_node, dst_v_node)
        src_s_node = embedding_s_nodes[src_v_node][0]
        dst_s_node = embedding_s_nodes[dst_v_node][0]
        v_bandwidth_demand = edge_data['bandwidth']

        if src_s_node == dst_s_node:
            all_s_paths[v_link][0] = ([], v_bandwidth_demand)
        else:
            subnet = nx.subgraph_view(
                copied_substrate.net,
                filter_edge=lambda node_1_id, node_2_id: \
                    True if copied_substrate.net.edges[(node_1_id, node_2_id)]['bandwidth'] >= v_bandwidth_demand else False
            )

            if len(subnet.edges) == 0 or not nx.has_path(
                    subnet, source=src_s_node, target=dst_s_node):
                return False, (v_link, v_bandwidth_demand)

            all_paths = nx.all_simple_paths(
                subnet,
                source=src_s_node,
                target=dst_s_node,
                cutoff=config.MAX_EMBEDDING_PATH_LENGTH)

            all_s_paths[v_link] = {}
            s_path_idx = 0
            for path in all_paths:
                s_links_in_path = []
                for node_idx in range(len(path) - 1):
                    s_links_in_path.append(
                        (path[node_idx], path[node_idx + 1]))

                all_s_paths[v_link][s_path_idx] = (s_links_in_path,
                                                   v_bandwidth_demand)
                s_path_idx += 1

            if s_path_idx == 0:
                return False, (v_link, None)

    return True, all_s_paths